|
6 | 6 | #include "../../window.h" |
7 | 7 | #include "../../window_manager.h" |
8 | 8 |
|
| 9 | +// Forward declaration for the delegate |
| 10 | +@class NativeAPIWindowManagerDelegate; |
| 11 | + |
| 12 | +namespace nativeapi { |
| 13 | + |
| 14 | +// Private implementation to hide Objective-C details |
| 15 | +class WindowManager::WindowManagerImpl { |
| 16 | +public: |
| 17 | + WindowManagerImpl(WindowManager* manager); |
| 18 | + ~WindowManagerImpl(); |
| 19 | + |
| 20 | + void SetupEventMonitoring(); |
| 21 | + void CleanupEventMonitoring(); |
| 22 | + void OnWindowEvent(NSWindow* window, const std::string& event_type); |
| 23 | + |
| 24 | +private: |
| 25 | + WindowManager* manager_; |
| 26 | + NativeAPIWindowManagerDelegate* delegate_; |
| 27 | +}; |
| 28 | + |
| 29 | +} // namespace nativeapi |
| 30 | + |
| 31 | +// Objective-C delegate class to handle NSWindow notifications |
| 32 | +@interface NativeAPIWindowManagerDelegate : NSObject |
| 33 | +@property (nonatomic, assign) nativeapi::WindowManager::WindowManagerImpl* impl; |
| 34 | +- (instancetype)initWithImpl:(nativeapi::WindowManager::WindowManagerImpl*)impl; |
| 35 | +@end |
| 36 | + |
| 37 | +@implementation NativeAPIWindowManagerDelegate |
| 38 | + |
| 39 | +- (instancetype)initWithImpl:(nativeapi::WindowManager::WindowManagerImpl*)impl { |
| 40 | + if (self = [super init]) { |
| 41 | + _impl = impl; |
| 42 | + } |
| 43 | + return self; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)windowDidBecomeKey:(NSNotification*)notification { |
| 47 | + NSWindow* window = [notification object]; |
| 48 | + if (_impl) { |
| 49 | + _impl->OnWindowEvent(window, "focused"); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +- (void)windowDidResignKey:(NSNotification*)notification { |
| 54 | + NSWindow* window = [notification object]; |
| 55 | + if (_impl) { |
| 56 | + _impl->OnWindowEvent(window, "blurred"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +- (void)windowDidMiniaturize:(NSNotification*)notification { |
| 61 | + NSWindow* window = [notification object]; |
| 62 | + if (_impl) { |
| 63 | + _impl->OnWindowEvent(window, "minimized"); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +- (void)windowDidDeminiaturize:(NSNotification*)notification { |
| 68 | + NSWindow* window = [notification object]; |
| 69 | + if (_impl) { |
| 70 | + _impl->OnWindowEvent(window, "restored"); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +- (void)windowDidResize:(NSNotification*)notification { |
| 75 | + NSWindow* window = [notification object]; |
| 76 | + if (_impl) { |
| 77 | + _impl->OnWindowEvent(window, "resized"); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +- (void)windowDidMove:(NSNotification*)notification { |
| 82 | + NSWindow* window = [notification object]; |
| 83 | + if (_impl) { |
| 84 | + _impl->OnWindowEvent(window, "moved"); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +- (void)windowWillClose:(NSNotification*)notification { |
| 89 | + NSWindow* window = [notification object]; |
| 90 | + if (_impl) { |
| 91 | + _impl->OnWindowEvent(window, "closing"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@end |
| 96 | + |
9 | 97 | namespace nativeapi { |
10 | 98 |
|
11 | | -WindowManager::WindowManager() {} |
| 99 | +WindowManager::WindowManagerImpl::WindowManagerImpl(WindowManager* manager) |
| 100 | + : manager_(manager), delegate_(nullptr) { |
| 101 | +} |
| 102 | + |
| 103 | +WindowManager::WindowManagerImpl::~WindowManagerImpl() { |
| 104 | + CleanupEventMonitoring(); |
| 105 | +} |
| 106 | + |
| 107 | +void WindowManager::WindowManagerImpl::SetupEventMonitoring() { |
| 108 | + if (!delegate_) { |
| 109 | + delegate_ = [[NativeAPIWindowManagerDelegate alloc] initWithImpl:this]; |
| 110 | + |
| 111 | + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 112 | + [center addObserver:delegate_ |
| 113 | + selector:@selector(windowDidBecomeKey:) |
| 114 | + name:NSWindowDidBecomeKeyNotification |
| 115 | + object:nil]; |
| 116 | + [center addObserver:delegate_ |
| 117 | + selector:@selector(windowDidResignKey:) |
| 118 | + name:NSWindowDidResignKeyNotification |
| 119 | + object:nil]; |
| 120 | + [center addObserver:delegate_ |
| 121 | + selector:@selector(windowDidMiniaturize:) |
| 122 | + name:NSWindowDidMiniaturizeNotification |
| 123 | + object:nil]; |
| 124 | + [center addObserver:delegate_ |
| 125 | + selector:@selector(windowDidDeminiaturize:) |
| 126 | + name:NSWindowDidDeminiaturizeNotification |
| 127 | + object:nil]; |
| 128 | + [center addObserver:delegate_ |
| 129 | + selector:@selector(windowDidResize:) |
| 130 | + name:NSWindowDidResizeNotification |
| 131 | + object:nil]; |
| 132 | + [center addObserver:delegate_ |
| 133 | + selector:@selector(windowDidMove:) |
| 134 | + name:NSWindowDidMoveNotification |
| 135 | + object:nil]; |
| 136 | + [center addObserver:delegate_ |
| 137 | + selector:@selector(windowWillClose:) |
| 138 | + name:NSWindowWillCloseNotification |
| 139 | + object:nil]; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +void WindowManager::WindowManagerImpl::CleanupEventMonitoring() { |
| 144 | + if (delegate_) { |
| 145 | + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 146 | + [center removeObserver:delegate_]; |
| 147 | + delegate_ = nil; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +void WindowManager::WindowManagerImpl::OnWindowEvent(NSWindow* window, const std::string& event_type) { |
| 152 | + WindowID window_id = [window windowNumber]; |
| 153 | + |
| 154 | + if (event_type == "focused") { |
| 155 | + WindowFocusedEvent event(window_id); |
| 156 | + manager_->DispatchWindowEvent(event); |
| 157 | + } else if (event_type == "blurred") { |
| 158 | + WindowBlurredEvent event(window_id); |
| 159 | + manager_->DispatchWindowEvent(event); |
| 160 | + } else if (event_type == "minimized") { |
| 161 | + WindowMinimizedEvent event(window_id); |
| 162 | + manager_->DispatchWindowEvent(event); |
| 163 | + } else if (event_type == "restored") { |
| 164 | + WindowRestoredEvent event(window_id); |
| 165 | + manager_->DispatchWindowEvent(event); |
| 166 | + } else if (event_type == "resized") { |
| 167 | + NSRect frame = [window frame]; |
| 168 | + Size new_size = {frame.size.width, frame.size.height}; |
| 169 | + WindowResizedEvent event(window_id, new_size); |
| 170 | + manager_->DispatchWindowEvent(event); |
| 171 | + } else if (event_type == "moved") { |
| 172 | + NSRect frame = [window frame]; |
| 173 | + Point new_position = {frame.origin.x, frame.origin.y}; |
| 174 | + WindowMovedEvent event(window_id, new_position); |
| 175 | + manager_->DispatchWindowEvent(event); |
| 176 | + } else if (event_type == "closing") { |
| 177 | + WindowClosedEvent event(window_id); |
| 178 | + manager_->DispatchWindowEvent(event); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +WindowManager::WindowManager() : impl_(std::make_unique<WindowManagerImpl>(this)) { |
| 183 | + SetupEventMonitoring(); |
| 184 | +} |
| 185 | + |
| 186 | +WindowManager::~WindowManager() { |
| 187 | + CleanupEventMonitoring(); |
| 188 | +} |
| 189 | + |
| 190 | +void WindowManager::SetupEventMonitoring() { |
| 191 | + impl_->SetupEventMonitoring(); |
| 192 | +} |
12 | 193 |
|
13 | | -WindowManager::~WindowManager() {} |
| 194 | +void WindowManager::CleanupEventMonitoring() { |
| 195 | + impl_->CleanupEventMonitoring(); |
| 196 | +} |
| 197 | + |
| 198 | +void WindowManager::DispatchWindowEvent(const Event& event) { |
| 199 | + event_dispatcher_.DispatchSync(event); |
| 200 | +} |
14 | 201 |
|
15 | 202 | // Create a new window with the given options. |
16 | 203 | std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) { |
|
28 | 215 | WindowID window_id = [ns_window windowNumber]; |
29 | 216 | auto window = std::make_shared<Window>((__bridge void*)ns_window); |
30 | 217 | windows_[window_id] = window; |
| 218 | + |
| 219 | + // Dispatch window created event |
| 220 | + WindowCreatedEvent created_event(window_id); |
| 221 | + DispatchWindowEvent(created_event); |
| 222 | + |
31 | 223 | return window; |
32 | 224 | } |
33 | 225 |
|
| 226 | +// Destroy a window by its ID. Returns true if window was destroyed. |
| 227 | +bool WindowManager::Destroy(WindowID id) { |
| 228 | + auto it = windows_.find(id); |
| 229 | + if (it != windows_.end()) { |
| 230 | + // Get the NSWindow to close it |
| 231 | + NSArray* ns_windows = [[NSApplication sharedApplication] windows]; |
| 232 | + for (NSWindow* ns_window in ns_windows) { |
| 233 | + if ([ns_window windowNumber] == id) { |
| 234 | + [ns_window close]; |
| 235 | + windows_.erase(it); |
| 236 | + return true; |
| 237 | + } |
| 238 | + } |
| 239 | + // Remove from our map even if we couldn't find the NSWindow |
| 240 | + windows_.erase(it); |
| 241 | + } |
| 242 | + return false; |
| 243 | +} |
| 244 | + |
34 | 245 | std::shared_ptr<Window> WindowManager::Get(WindowID id) { |
35 | 246 | auto it = windows_.find(id); |
36 | 247 | if (it != windows_.end()) { |
|
0 commit comments