Skip to content

Commit 033cfb1

Browse files
committed
Defer window show hook to next runloop on macOS
The original makeKeyAndOrderFront implementation is now deferred to the next runloop turn to ensure the Dart hook runs first. An associated flag prevents multiple schedules for the same call, improving hook reliability.
1 parent 568b378 commit 033cfb1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/platform/macos/window_manager_macos.mm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ @implementation NSWindow (NativeAPISwizzle)
4848
- (void)na_swizzled_makeKeyAndOrderFront:(id)sender {
4949
// Invoke hook before showing
5050
nativeapi::WindowManager::GetInstance().InvokeWillShowHook([self windowNumber]);
51-
// Call original implementation (swapped)
52-
[self na_swizzled_makeKeyAndOrderFront:sender];
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+
}
5366
}
5467

5568
- (void)na_swizzled_orderOut:(id)sender {

0 commit comments

Comments
 (0)