From ae9d7fc76ae8c523272fa586e11bdc034af94809 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Sat, 11 Nov 2023 07:35:57 +0100 Subject: [PATCH] macOS: only unhide manu in non-native FS if focus lost to Ghostty This fixes #786 by adding a check to the callback that unhides the menu bar to only unhide the menu bar if focus was lost to another Ghostty window. That's the desired behaviour: when focus is lost to another app's window, we want the non-native-fullscreen window to stay unchanged in background, but when changing focus to another Ghostty window, we want to unhide the menu bar. Why did this problem even show up? It started to show up with the introduction of the Xib-file based approach, in 3018377. Before that, in 27ddc90, for example, the app would receive the same notifications, but the `.autoHideMenuBar` didn't have an effect on the window. Only after adding the Xib-file did it start to have an effect. So I figured there's two ways we could fix it: 1. Figure out why the `.autoHideMenuBar` now works with Xib-files and suppress it, or 2. Encode in the code the behaviour that we actually want: we only want to show the menu bar when focus shifts to another one of Ghostty's windows, but we want to leave it untouched when focus is lost to another app's window. I went with (2) but I think (1) is also valid and happy to close PR if that's what we want to do. --- macos/Sources/Helpers/FullScreenHandler.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/macos/Sources/Helpers/FullScreenHandler.swift b/macos/Sources/Helpers/FullScreenHandler.swift index a1caf8069..2bb716c00 100644 --- a/macos/Sources/Helpers/FullScreenHandler.swift +++ b/macos/Sources/Helpers/FullScreenHandler.swift @@ -83,7 +83,7 @@ class FullScreenHandler { object: window) NotificationCenter.default.addObserver( self, - selector: #selector(FullScreenHandler.unHideMenu), + selector: #selector(FullScreenHandler.onDidResignMain), name: NSWindow.didResignMainNotification, object: window) } @@ -105,8 +105,16 @@ class FullScreenHandler { NSApp.presentationOptions.insert(.autoHideMenuBar) } - @objc func unHideMenu() { - NSApp.presentationOptions.remove(.autoHideMenuBar) + @objc func onDidResignMain(_ notification: Notification) { + guard let resigningWindow = notification.object as? NSWindow else { return } + guard let mainWindow = NSApplication.shared.mainWindow else { return } + + // We're only unhiding the menu bar, if the focus shifted within our application. + // In that case, `mainWindow` is the window of our application the focus shifted + // to. + if !resigningWindow.isEqual(mainWindow) { + NSApp.presentationOptions.remove(.autoHideMenuBar) + } } @objc func hideDock() {