@@ -46,28 +46,27 @@ - (void)na_swizzled_orderOut:(id)sender;
4646@implementation NSWindow (NativeAPISwizzle)
4747
4848- (void )na_swizzled_makeKeyAndOrderFront : (id )sender {
49- // Invoke hook before showing
50- nativeapi::WindowManager::GetInstance ().InvokeWillShowHook ([self windowNumber ]);
51-
52- // Defer calling original to the next runloop turn so Dart hook can run first
53- // Guard with associated flag to avoid multiple schedules for the same call
54- static const void * kNAWillShowScheduledKey = &kNAWillShowScheduledKey ;
55- id scheduled = objc_getAssociatedObject (self, kNAWillShowScheduledKey );
56- if (!scheduled) {
57- objc_setAssociatedObject (self, kNAWillShowScheduledKey , @YES ,
58- OBJC_ASSOCIATION_RETAIN_NONATOMIC );
59- dispatch_async (dispatch_get_main_queue (), ^{
60- // Clear flag
61- objc_setAssociatedObject (self, kNAWillShowScheduledKey , nil , OBJC_ASSOCIATION_ASSIGN );
62- // Call original implementation (swapped)
63- [self na_swizzled_makeKeyAndOrderFront: sender];
64- });
65- }
49+ // Ensure registry is in sync and then invoke hook with correct WindowId
50+ auto windows = nativeapi::WindowManager::GetInstance ().GetAll ();
51+ for (const auto & window : windows) {
52+ if (window->GetNativeObject () == (__bridge void *)self) {
53+ nativeapi::WindowManager::GetInstance ().InvokeWillShowHook (window->GetId ());
54+ break ;
55+ }
56+ }
57+ // First call original implementation so properties like title are up-to-date
58+ [self na_swizzled_makeKeyAndOrderFront: sender];
6659}
6760
6861- (void )na_swizzled_orderOut : (id )sender {
69- // Invoke hook before hiding
70- nativeapi::WindowManager::GetInstance ().InvokeWillHideHook ([self windowNumber ]);
62+ // Invoke hook before hiding, resolving id via registry without using windowNumber
63+ auto windows = nativeapi::WindowManager::GetInstance ().GetAll ();
64+ for (const auto & window : windows) {
65+ if (window->GetNativeObject () == (__bridge void *)self) {
66+ nativeapi::WindowManager::GetInstance ().InvokeWillHideHook (window->GetId ());
67+ break ;
68+ }
69+ }
7170 // Call original implementation (swapped)
7271 [self na_swizzled_orderOut: sender];
7372}
@@ -277,48 +276,54 @@ - (void)windowWillClose:(NSNotification*)notification {
277276}
278277
279278std::shared_ptr<Window> WindowManager::Get (WindowId id ) {
280- auto cached = WindowRegistry::GetInstance ().Get (id );
281- if (cached) {
282- return cached;
283- }
284- NSArray * ns_windows = [[NSApplication sharedApplication ] windows ];
285- for (NSWindow * ns_window in ns_windows) {
286- if ([ns_window windowNumber ] == id ) {
287- auto window = std::make_shared<Window>((__bridge void *)ns_window);
288- WindowRegistry::GetInstance ().Add (id , window);
289- return window;
290- }
279+ // First check if it's already in the registry
280+ auto window = WindowRegistry::GetInstance ().Get (id );
281+ if (window) {
282+ return window;
291283 }
292- return nullptr ;
284+
285+ // If not found, ensure all NSWindows are registered and try again
286+ GetAll ();
287+ return WindowRegistry::GetInstance ().Get (id );
293288}
294289
295290std::vector<std::shared_ptr<Window>> WindowManager::GetAll () {
296- std::vector<std::shared_ptr<Window>> windows;
297291 NSArray * ns_windows = [[NSApplication sharedApplication ] windows ];
292+
293+ // First, ensure all NSWindows are registered
298294 for (NSWindow * ns_window in ns_windows) {
299- WindowId window_id = [ns_window windowNumber ];
295+ // Create or get Window wrapper - this will handle ID assignment via associated object
296+ auto window = std::make_shared<Window>((__bridge void *)ns_window);
297+ WindowId window_id = window->GetId ();
298+
299+ // Add to registry if not already present
300300 if (!WindowRegistry::GetInstance ().Get (window_id)) {
301- auto window = std::make_shared<Window>((__bridge void *)ns_window);
302301 WindowRegistry::GetInstance ().Add (window_id, window);
303302 }
304303 }
305- // Merge cached windows from registry to ensure consistency
306- auto cached = WindowRegistry::GetInstance ().GetAll ();
307- for (const auto & w : cached) {
308- windows.push_back (w);
309- }
310- return windows;
304+
305+ // Then return all windows from registry (which now includes all NSWindows)
306+ return WindowRegistry::GetInstance ().GetAll ();
311307}
312308
313309std::shared_ptr<Window> WindowManager::GetCurrent () {
314310 NSApplication * app = [NSApplication sharedApplication ];
315311 NSArray * ns_windows = [[NSApplication sharedApplication ] windows ];
316312 NSWindow * ns_window = [app mainWindow ];
317- if (ns_window == nil ) {
313+ if (ns_window == nil && [ns_windows count ] > 0 ) {
318314 ns_window = [ns_windows objectAtIndex: 0 ];
319315 }
320316 if (ns_window != nil ) {
321- return Get ([ns_window windowNumber ]);
317+ // Create or get Window wrapper - this will handle ID retrieval via associated object
318+ auto window = std::make_shared<Window>((__bridge void *)ns_window);
319+ WindowId window_id = window->GetId ();
320+
321+ // Ensure it's in the registry
322+ if (!WindowRegistry::GetInstance ().Get (window_id)) {
323+ WindowRegistry::GetInstance ().Add (window_id, window);
324+ }
325+
326+ return window;
322327 }
323328 return nullptr ;
324329}
0 commit comments