From e08f40ff6f8062880960e0a2dc600b3cc6a49008 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Fri, 20 Nov 2026 08:23:25 +0800 Subject: [PATCH 1/2] Fix macOS fullscreen hide, always-on-top fullscreen level, and SetFullScreenable --- src/platform/macos/window_macos.mm | 189 ++++++++++++++++++++++++-- tests/CMakeLists.txt | 4 + tests/window_lifecycle_macos_test.mm | 193 +++++++++++++++++++++++++++ 3 files changed, 375 insertions(+), 11 deletions(-) create mode 100644 tests/window_lifecycle_macos_test.mm diff --git a/src/platform/macos/window_macos.mm b/src/platform/macos/window_macos.mm index f5fe444..2ed517f 100644 --- a/src/platform/macos/window_macos.mm +++ b/src/platform/macos/window_macos.mm @@ -50,6 +50,8 @@ static const void* kWindowVisualEffectViewKey = &kWindowVisualEffectViewKey; static const void* kWindowAspectRatioKey = &kWindowAspectRatioKey; static const void* kWindowContentBackgroundKey = &kWindowContentBackgroundKey; +static const void* kWindowAlwaysOnTopKey = &kWindowAlwaysOnTopKey; +static const void* kWindowFullScreenCoordinatorKey = &kWindowFullScreenCoordinatorKey; // Also called by window_manager_macos.mm, for windows someone else shows. void NativeApiAttachPendingParentWindow(NSWindow* window) { @@ -79,6 +81,101 @@ static void NativeApiDetachFromParentWindow(NSWindow* window, bool keep_pending) OBJC_ASSOCIATION_RETAIN_NONATOMIC); } +static BOOL NativeApiWindowIsAlwaysOnTop(NSWindow* window) { + if (!window) return NO; + NSNumber* value = objc_getAssociatedObject(window, kWindowAlwaysOnTopKey); + if (value) return [value boolValue]; + return [window level] == NSFloatingWindowLevel; +} + +// Coordinates hiding a fullscreen window (exiting fullscreen first so macOS does +// not leave the user on a black fullscreen Space) and suspending NSFloatingWindowLevel +// while in native fullscreen (so macOS hides the menu bar and Dock properly). +@interface NativeApiFullScreenCoordinator : NSObject { + @public + NSWindow* __unsafe_unretained window; + BOOL pending_hide; + BOOL restore_always_on_top; +} +@end + +@implementation NativeApiFullScreenCoordinator +- (instancetype)initWithWindow:(NSWindow*)target { + if ((self = [super init])) { + window = target; + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; + [center addObserver:self + selector:@selector(willEnterFullScreen:) + name:NSWindowWillEnterFullScreenNotification + object:target]; + [center addObserver:self + selector:@selector(didExitFullScreen:) + name:NSWindowDidExitFullScreenNotification + object:target]; + [center addObserver:self + selector:@selector(willClose:) + name:NSWindowWillCloseNotification + object:target]; + } + return self; +} + +- (void)willEnterFullScreen:(NSNotification*)note { + (void)note; + if (!window) return; + if (NativeApiWindowIsAlwaysOnTop(window)) { + restore_always_on_top = YES; + [window setLevel:NSNormalWindowLevel]; + } +} + +- (void)didExitFullScreen:(NSNotification*)note { + (void)note; + if (!window) return; + if (restore_always_on_top) { + restore_always_on_top = NO; + if (NativeApiWindowIsAlwaysOnTop(window)) { + [window setLevel:NSFloatingWindowLevel]; + } + } + if (pending_hide) { + pending_hide = NO; + NativeApiDetachFromParentWindow(window, true); + [window setIsVisible:NO]; + [window orderOut:nil]; + } +} + +- (void)willClose:(NSNotification*)note { + (void)note; + [[NSNotificationCenter defaultCenter] removeObserver:self]; + window = nil; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +#if !__has_feature(objc_arc) + [super dealloc]; +#endif +} +@end + +static NativeApiFullScreenCoordinator* NativeApiGetFullScreenCoordinator(NSWindow* window, + bool create) { + if (!window) return nil; + NativeApiFullScreenCoordinator* coordinator = + objc_getAssociatedObject(window, kWindowFullScreenCoordinatorKey); + if (!coordinator && create) { + coordinator = [[NativeApiFullScreenCoordinator alloc] initWithWindow:window]; + objc_setAssociatedObject(window, kWindowFullScreenCoordinatorKey, coordinator, + OBJC_ASSOCIATION_RETAIN_NONATOMIC); +#if !__has_feature(objc_arc) + [coordinator release]; +#endif + } + return coordinator; +} + // The content may paint a backing of its own over the window's background. A view // controller that has a background colour is the case that matters: a // FlutterViewController is opaque black by default. @@ -521,6 +618,9 @@ static void NativeApiObserveWindowProperties(NSWindow* window) { } void Window::Show() { + if (auto* coordinator = NativeApiGetFullScreenCoordinator(pimpl_->ns_window_, false)) { + coordinator->pending_hide = NO; + } if (IsFocusable()) NativeApiRememberFocusReturnTarget(pimpl_->ns_window_); [pimpl_->ns_window_ setIsVisible:YES]; // Panels receive key focus when shown but should not activate the app. @@ -532,15 +632,29 @@ static void NativeApiObserveWindowProperties(NSWindow* window) { } void Window::ShowInactive() { + if (auto* coordinator = NativeApiGetFullScreenCoordinator(pimpl_->ns_window_, false)) { + coordinator->pending_hide = NO; + } [pimpl_->ns_window_ setIsVisible:YES]; [pimpl_->ns_window_ orderFrontRegardless]; NativeApiAttachPendingParentWindow(pimpl_->ns_window_); } void Window::Hide() { - NativeApiDetachFromParentWindow(pimpl_->ns_window_, true); - [pimpl_->ns_window_ setIsVisible:NO]; - [pimpl_->ns_window_ orderOut:nil]; + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + if (IsFullScreen()) { + auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); + coordinator->pending_hide = YES; + [window toggleFullScreen:nil]; + return; + } + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { + coordinator->pending_hide = NO; + } + NativeApiDetachFromParentWindow(window, true); + [window setIsVisible:NO]; + [window orderOut:nil]; } bool Window::IsVisible() const { @@ -597,13 +711,20 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { } void Window::SetFullScreen(bool is_full_screen) { + NSWindow* window = pimpl_->ns_window_; + if (!window) return; if (is_full_screen) { if (!IsFullScreen()) { - [pimpl_->ns_window_ toggleFullScreen:nil]; + if (NativeApiWindowIsAlwaysOnTop(window)) { + auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); + coordinator->restore_always_on_top = YES; + [window setLevel:NSNormalWindowLevel]; + } + [window toggleFullScreen:nil]; } } else { if (IsFullScreen()) { - [pimpl_->ns_window_ toggleFullScreen:nil]; + [window toggleFullScreen:nil]; } } } @@ -771,11 +892,30 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { void Window::SetFullScreenable(bool is_full_screenable) { detail::WindowPropertyScope property_scope(*this); - // TODO: Implement this + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + auto behavior = window.collectionBehavior; + if (is_full_screenable) { + behavior &= ~NSWindowCollectionBehaviorFullScreenNone; + behavior |= NSWindowCollectionBehaviorFullScreenPrimary; + } else { + behavior &= ~(NSWindowCollectionBehaviorFullScreenPrimary | + NSWindowCollectionBehaviorFullScreenAuxiliary); + behavior |= NSWindowCollectionBehaviorFullScreenNone; + } + @try { + window.collectionBehavior = behavior; + } @catch (NSException*) { + } } bool Window::IsFullScreenable() const { - return [pimpl_->ns_window_ styleMask] & NSWindowStyleMaskFullScreen; + NSWindow* window = pimpl_->ns_window_; + if (!window) return false; + const auto behavior = [window collectionBehavior]; + if (behavior & NSWindowCollectionBehaviorFullScreenNone) return false; + if (behavior & NSWindowCollectionBehaviorFullScreenPrimary) return true; + return ([window styleMask] & NSWindowStyleMaskResizable) != 0; } void Window::SetClosable(bool is_closable) { @@ -812,11 +952,30 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { void Window::SetAlwaysOnTop(bool is_always_on_top) { detail::WindowPropertyScope property_scope(*this); - [pimpl_->ns_window_ setLevel:is_always_on_top ? NSFloatingWindowLevel : NSNormalWindowLevel]; + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + objc_setAssociatedObject(window, kWindowAlwaysOnTopKey, @(is_always_on_top), + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + if (is_always_on_top) { + auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); + if (IsFullScreen()) { + coordinator->restore_always_on_top = YES; + [window setLevel:NSNormalWindowLevel]; + } else { + [window setLevel:NSFloatingWindowLevel]; + } + } else { + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { + coordinator->restore_always_on_top = NO; + } + if ([window level] == NSFloatingWindowLevel) { + [window setLevel:NSNormalWindowLevel]; + } + } } bool Window::IsAlwaysOnTop() const { - return [pimpl_->ns_window_ level] == NSFloatingWindowLevel; + return NativeApiWindowIsAlwaysOnTop(pimpl_->ns_window_); } // One level below NSNormalWindowLevel: beneath every ordinary window, above the desktop. @@ -824,8 +983,16 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { detail::WindowPropertyScope property_scope(*this); - [pimpl_->ns_window_ - setLevel:is_always_on_bottom ? kAlwaysOnBottomWindowLevel : NSNormalWindowLevel]; + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + if (is_always_on_bottom) { + objc_setAssociatedObject(window, kWindowAlwaysOnTopKey, @NO, + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { + coordinator->restore_always_on_top = NO; + } + } + [window setLevel:is_always_on_bottom ? kAlwaysOnBottomWindowLevel : NSNormalWindowLevel]; } bool Window::IsAlwaysOnBottom() const { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5ae2b60..9813993 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -194,6 +194,10 @@ if(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS") add_executable(window_title_bar_double_click_macos_test window_title_bar_double_click_macos_test.mm) target_link_libraries(window_title_bar_double_click_macos_test PRIVATE nativeapi) add_test(NAME window_title_bar_double_click_macos_test COMMAND window_title_bar_double_click_macos_test) + add_executable(window_lifecycle_macos_test window_lifecycle_macos_test.mm) + target_link_libraries(window_lifecycle_macos_test PRIVATE nativeapi) + add_test(NAME window_lifecycle_macos_test COMMAND window_lifecycle_macos_test) + set_tests_properties(window_lifecycle_macos_test PROPERTIES TIMEOUT 10) add_executable(window_mouse_forwarding_macos_test window_mouse_forwarding_macos_test.mm) target_link_libraries(window_mouse_forwarding_macos_test PRIVATE nativeapi) add_test(NAME window_mouse_forwarding_macos_test COMMAND window_mouse_forwarding_macos_test) diff --git a/tests/window_lifecycle_macos_test.mm b/tests/window_lifecycle_macos_test.mm new file mode 100644 index 0000000..db50abc --- /dev/null +++ b/tests/window_lifecycle_macos_test.mm @@ -0,0 +1,193 @@ +// Verifies macOS window lifecycle behaviors carried over in issue #80: +// - Hide() while in fullscreen exits fullscreen before ordering out (#379) +// - Always-on-top suspends NSFloatingWindowLevel across native fullscreen (#351) +// - SetFullScreenable() / IsFullScreenable() toggle collectionBehavior +// - SetResizable() round-trips cleanly (#462) +// - Hidden title bar leaves top-edge content buttons hit-testable (#565) +#include "nativeapi.h" + +#import +#include + +static int failures = 0; + +static void Check(bool ok, const char* label) { + std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl; + failures += !ok; +} + +@interface FakeFullScreenWindow : NSWindow { + @public + BOOL fake_full_screen; + int toggle_calls; + int order_out_calls; + int set_visible_false_calls; +} +@end + +@implementation FakeFullScreenWindow +- (NSWindowStyleMask)styleMask { + NSWindowStyleMask base = [super styleMask]; + if (fake_full_screen) return base | NSWindowStyleMaskFullScreen; + return base & ~NSWindowStyleMaskFullScreen; +} + +- (void)toggleFullScreen:(id)sender { + (void)sender; + ++toggle_calls; +} + +- (void)orderOut:(id)sender { + ++order_out_calls; + [super orderOut:sender]; +} + +- (void)setIsVisible:(BOOL)flag { + if (!flag) ++set_visible_false_calls; + [super setIsVisible:flag]; +} +@end + +int main() { + @autoreleasepool { + [NSApplication sharedApplication]; + + // 1. Hide() while in fullscreen exits fullscreen first, then orders out on exit (#379). + { + auto* host = [[FakeFullScreenWindow alloc] + initWithContentRect:NSMakeRect(100, 100, 400, 300) + styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | + NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable + backing:NSBackingStoreBuffered + defer:NO]; + host.releasedWhenClosed = NO; + nativeapi::Window window((__bridge void*)host); + + host->fake_full_screen = YES; + window.Hide(); + Check(host->toggle_calls == 1, "Hide() in fullscreen calls toggleFullScreen"); + Check(host->order_out_calls == 0 && host->set_visible_false_calls == 0, + "Hide() in fullscreen defers orderOut until exit notification"); + + host->fake_full_screen = NO; + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidExitFullScreenNotification + object:host]; + Check(host->order_out_calls == 1 && host->set_visible_false_calls == 1, + "DidExitFullScreen completes deferred hide"); + + // Show() before exit cancels pending hide. + host->fake_full_screen = YES; + host->toggle_calls = 0; + host->order_out_calls = 0; + host->set_visible_false_calls = 0; + window.Hide(); + Check(host->toggle_calls == 1, "second Hide() in fullscreen calls toggleFullScreen"); + window.ShowInactive(); + host->fake_full_screen = NO; + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidExitFullScreenNotification + object:host]; + Check(host->order_out_calls == 0 && host->set_visible_false_calls == 0, + "ShowInactive() cancels pending deferred hide"); + [host close]; + } + + // 2. Always-on-top + fullscreen drops window level during fullscreen and restores after (#351). + { + auto* host = [[FakeFullScreenWindow alloc] + initWithContentRect:NSMakeRect(100, 100, 400, 300) + styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | + NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable + backing:NSBackingStoreBuffered + defer:NO]; + host.releasedWhenClosed = NO; + nativeapi::Window window((__bridge void*)host); + + window.SetAlwaysOnTop(true); + Check(window.IsAlwaysOnTop() && host.level == NSFloatingWindowLevel, + "SetAlwaysOnTop(true) raises window level outside fullscreen"); + + window.SetFullScreen(true); + Check(host->toggle_calls == 1, "SetFullScreen(true) calls toggleFullScreen"); + Check(host.level == NSNormalWindowLevel, + "SetFullScreen(true) suspends floating level so menu bar/Dock hide"); + Check(window.IsAlwaysOnTop(), + "IsAlwaysOnTop() stays true while floating level is suspended for fullscreen"); + + host->fake_full_screen = YES; + host->fake_full_screen = NO; + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidExitFullScreenNotification + object:host]; + Check(host.level == NSFloatingWindowLevel, + "DidExitFullScreen restores floating window level"); + + // Calling SetAlwaysOnTop(true) while already in fullscreen also defers level raise. + window.SetAlwaysOnTop(false); + Check(!window.IsAlwaysOnTop() && host.level == NSNormalWindowLevel, + "SetAlwaysOnTop(false) resets level"); + host->fake_full_screen = YES; + window.SetAlwaysOnTop(true); + Check(window.IsAlwaysOnTop() && host.level == NSNormalWindowLevel, + "SetAlwaysOnTop(true) during fullscreen keeps NSNormalWindowLevel"); + host->fake_full_screen = NO; + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidExitFullScreenNotification + object:host]; + Check(host.level == NSFloatingWindowLevel, + "DidExitFullScreen applies always-on-top requested during fullscreen"); + + // User-initiated fullscreen entry (green button) also suspends floating level. + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowWillEnterFullScreenNotification + object:host]; + Check(host.level == NSNormalWindowLevel, + "WillEnterFullScreen drops floating level for user-initiated fullscreen"); + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidExitFullScreenNotification + object:host]; + Check(host.level == NSFloatingWindowLevel, + "DidExitFullScreen restores floating level after user-initiated fullscreen"); + [host close]; + } + + // 3. SetFullScreenable() / IsFullScreenable() and SetResizable() (#462). + { + auto* host = [[NSWindow alloc] + initWithContentRect:NSMakeRect(100, 100, 400, 300) + styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | + NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable + backing:NSBackingStoreBuffered + defer:NO]; + host.releasedWhenClosed = NO; + nativeapi::Window window((__bridge void*)host); + + Check(window.IsFullScreenable(), "resizable window is full-screenable by default"); + window.SetFullScreenable(false); + Check(!window.IsFullScreenable(), "SetFullScreenable(false) clears full-screenable"); + window.SetFullScreenable(true); + Check(window.IsFullScreenable(), "SetFullScreenable(true) sets full-screenable"); + + window.SetTitleBarStyle(nativeapi::TitleBarStyle::Hidden); + window.SetResizable(false); + Check(!window.IsResizable(), "SetResizable(false) removes resizable mask"); + window.SetResizable(true); + Check(window.IsResizable(), "SetResizable(true) restores resizable mask"); + + // 4. Hidden title bar: button at the very top of contentView is hit-testable (#565). + NSView* content = host.contentView; + auto* top_button = [[NSButton alloc] + initWithFrame:NSMakeRect(10, NSHeight(content.bounds) - 30, 80, 24)]; + [content addSubview:top_button]; + NSView* frame_view = content.superview; + NSPoint button_center = [frame_view convertPoint:NSMakePoint(40, 12) fromView:top_button]; + NSView* hit = [frame_view hitTest:button_center]; + std::cout << "INFO top-button hitTest class: " + << (hit ? NSStringFromClass([hit class]).UTF8String : "(nil)") << std::endl; + Check(hit == top_button, "top-edge button is hit-testable with hidden title bar"); + [host close]; + } + } + return failures == 0 ? 0 : 1; +} From b5c67ee4a99261611598d9918b253be30e387f1a Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sat, 21 Nov 2026 09:41:37 +0800 Subject: [PATCH 2/2] Read native NSWindow level directly when always-on-top is not suspended Drop the cached kWindowAlwaysOnTopKey associated object so direct native NSWindow.level changes after SetAlwaysOnTop(false) are still reflected by IsAlwaysOnTop() and reported by WindowPropertyChangedEvent. Only treat restore_always_on_top as overriding the window level while suspended for native fullscreen, and guard repeated Hide() calls in fullscreen from toggling twice. --- src/platform/macos/window_macos.mm | 35 ++++++++++++++-------------- tests/window_lifecycle_macos_test.mm | 10 +++++++- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/platform/macos/window_macos.mm b/src/platform/macos/window_macos.mm index 2ed517f..1d0011e 100644 --- a/src/platform/macos/window_macos.mm +++ b/src/platform/macos/window_macos.mm @@ -50,7 +50,6 @@ static const void* kWindowVisualEffectViewKey = &kWindowVisualEffectViewKey; static const void* kWindowAspectRatioKey = &kWindowAspectRatioKey; static const void* kWindowContentBackgroundKey = &kWindowContentBackgroundKey; -static const void* kWindowAlwaysOnTopKey = &kWindowAlwaysOnTopKey; static const void* kWindowFullScreenCoordinatorKey = &kWindowFullScreenCoordinatorKey; // Also called by window_manager_macos.mm, for windows someone else shows. @@ -81,13 +80,6 @@ static void NativeApiDetachFromParentWindow(NSWindow* window, bool keep_pending) OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -static BOOL NativeApiWindowIsAlwaysOnTop(NSWindow* window) { - if (!window) return NO; - NSNumber* value = objc_getAssociatedObject(window, kWindowAlwaysOnTopKey); - if (value) return [value boolValue]; - return [window level] == NSFloatingWindowLevel; -} - // Coordinates hiding a fullscreen window (exiting fullscreen first so macOS does // not leave the user on a black fullscreen Space) and suspending NSFloatingWindowLevel // while in native fullscreen (so macOS hides the menu bar and Dock properly). @@ -123,7 +115,7 @@ - (instancetype)initWithWindow:(NSWindow*)target { - (void)willEnterFullScreen:(NSNotification*)note { (void)note; if (!window) return; - if (NativeApiWindowIsAlwaysOnTop(window)) { + if ([window level] == NSFloatingWindowLevel) { restore_always_on_top = YES; [window setLevel:NSNormalWindowLevel]; } @@ -133,10 +125,10 @@ - (void)didExitFullScreen:(NSNotification*)note { (void)note; if (!window) return; if (restore_always_on_top) { + // Raise level before clearing restore_always_on_top so IsAlwaysOnTop() + // does not transiently dip to NO while KVO observes the level change. + [window setLevel:NSFloatingWindowLevel]; restore_always_on_top = NO; - if (NativeApiWindowIsAlwaysOnTop(window)) { - [window setLevel:NSFloatingWindowLevel]; - } } if (pending_hide) { pending_hide = NO; @@ -176,6 +168,14 @@ - (void)dealloc { return coordinator; } +static BOOL NativeApiWindowIsAlwaysOnTop(NSWindow* window) { + if (!window) return NO; + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { + if (coordinator->restore_always_on_top) return YES; + } + return [window level] == NSFloatingWindowLevel; +} + // The content may paint a backing of its own over the window's background. A view // controller that has a background colour is the case that matters: a // FlutterViewController is opaque black by default. @@ -645,8 +645,10 @@ static void NativeApiObserveWindowProperties(NSWindow* window) { if (!window) return; if (IsFullScreen()) { auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); - coordinator->pending_hide = YES; - [window toggleFullScreen:nil]; + if (!coordinator->pending_hide) { + coordinator->pending_hide = YES; + [window toggleFullScreen:nil]; + } return; } if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { @@ -954,14 +956,13 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { detail::WindowPropertyScope property_scope(*this); NSWindow* window = pimpl_->ns_window_; if (!window) return; - objc_setAssociatedObject(window, kWindowAlwaysOnTopKey, @(is_always_on_top), - OBJC_ASSOCIATION_RETAIN_NONATOMIC); if (is_always_on_top) { auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); if (IsFullScreen()) { coordinator->restore_always_on_top = YES; [window setLevel:NSNormalWindowLevel]; } else { + coordinator->restore_always_on_top = NO; [window setLevel:NSFloatingWindowLevel]; } } else { @@ -986,8 +987,6 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { NSWindow* window = pimpl_->ns_window_; if (!window) return; if (is_always_on_bottom) { - objc_setAssociatedObject(window, kWindowAlwaysOnTopKey, @NO, - OBJC_ASSOCIATION_RETAIN_NONATOMIC); if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { coordinator->restore_always_on_top = NO; } diff --git a/tests/window_lifecycle_macos_test.mm b/tests/window_lifecycle_macos_test.mm index db50abc..7a0434a 100644 --- a/tests/window_lifecycle_macos_test.mm +++ b/tests/window_lifecycle_macos_test.mm @@ -65,7 +65,9 @@ int main() { host->fake_full_screen = YES; window.Hide(); - Check(host->toggle_calls == 1, "Hide() in fullscreen calls toggleFullScreen"); + window.Hide(); + Check(host->toggle_calls == 1, + "Hide() in fullscreen calls toggleFullScreen once even if repeated"); Check(host->order_out_calls == 0 && host->set_visible_false_calls == 0, "Hide() in fullscreen defers orderOut until exit notification"); @@ -149,6 +151,12 @@ int main() { object:host]; Check(host.level == NSFloatingWindowLevel, "DidExitFullScreen restores floating level after user-initiated fullscreen"); + + // Direct native level changes remain reflected after SetAlwaysOnTop(false). + window.SetAlwaysOnTop(false); + host.level = NSFloatingWindowLevel; + Check(window.IsAlwaysOnTop(), + "direct native level raise is reflected by IsAlwaysOnTop()"); [host close]; }