From 241fe855ccabe410f2019ad75365ab24e0c345c3 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 12 Dec 2023 17:22:18 +0100 Subject: [PATCH] macOS large title bar: hide NSToolbar of window title bar when window becomes full screen --- .../src/main/objcpp/MacWindow.mm | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-macos/src/main/objcpp/MacWindow.mm b/flatlaf-natives/flatlaf-natives-macos/src/main/objcpp/MacWindow.mm index f15264e3..62c41c7a 100644 --- a/flatlaf-natives/flatlaf-natives-macos/src/main/objcpp/MacWindow.mm +++ b/flatlaf-natives/flatlaf-natives-macos/src/main/objcpp/MacWindow.mm @@ -15,6 +15,7 @@ */ #import +#import #import #import "JNIUtils.h" #import "JNFRunLoop.h" @@ -98,9 +99,13 @@ JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindo if( nsWindow == NULL ) return; + if( hasToolbar == (nsWindow.toolbar != NULL) ) + return; + [FlatJNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){ NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] ); + // add/remove toolbar NSToolbar* toolbar = NULL; if( hasToolbar ) { toolbar = [NSToolbar new]; @@ -108,9 +113,42 @@ JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindo } nsWindow.toolbar = toolbar; - // TODO handle fullscreen - NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] ); + + // when window becomes full screen, it is necessary to hide the toolbar + // because it otherwise is shown non-transparent and hides Swing components + static char enterObserverKey; + static char exitObserverKey; + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; + if( hasToolbar ) { + NSLog( @"add observers %@", nsWindow ); + id enterObserver = [center addObserverForName:NSWindowWillEnterFullScreenNotification + object:nsWindow queue:nil usingBlock:^(NSNotification *note) { + NSLog( @"enter full screen %@", nsWindow ); + if( nsWindow.toolbar != NULL ) + nsWindow.toolbar.visible = NO; + }]; + id exitObserver = [center addObserverForName:NSWindowDidExitFullScreenNotification + object:nsWindow queue:nil usingBlock:^(NSNotification *note) { + NSLog( @"exit full screen %@", nsWindow ); + if( nsWindow.toolbar != NULL ) + nsWindow.toolbar.visible = YES; + }]; + objc_setAssociatedObject( nsWindow, &enterObserverKey, enterObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); + objc_setAssociatedObject( nsWindow, &exitObserverKey, exitObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); + } else { + NSLog( @"remove observers %@", nsWindow ); + id enterObserver = objc_getAssociatedObject( nsWindow, &enterObserverKey ); + id exitObserver = objc_getAssociatedObject( nsWindow, &exitObserverKey ); + if( enterObserver != NULL ) { + [center removeObserver:enterObserver]; + objc_setAssociatedObject( nsWindow, &enterObserverKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); + } + if( exitObserver != NULL ) { + [center removeObserver:exitObserver]; + objc_setAssociatedObject( nsWindow, &exitObserverKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); + } + } }]; JNI_COCOA_EXIT()