macos: fix non-native-fullscreen menu & dock visibility bugs

This commit is contained in:
Will Pragnell
2023-09-01 21:28:34 -07:00
parent 86122624e0
commit f6e2b50732

View File

@ -53,10 +53,38 @@ class FullScreenHandler {
// Furthermore, it's much easier to figure out which screen the dock is on if the menubar // Furthermore, it's much easier to figure out which screen the dock is on if the menubar
// has not yet been hidden, so the order matters here! // has not yet been hidden, so the order matters here!
if (shouldHideDock(screen: screen)) { if (shouldHideDock(screen: screen)) {
NSApp.presentationOptions.insert(.autoHideDock) self.hideDock()
// Ensure that we always hide the dock bar for this window, but not for non fullscreen ones
NotificationCenter.default.addObserver(
self,
selector: #selector(FullScreenHandler.hideDock),
name: NSWindow.didBecomeMainNotification,
object: window)
NotificationCenter.default.addObserver(
self,
selector: #selector(FullScreenHandler.unHideDock),
name: NSWindow.didResignMainNotification,
object: window)
} }
if (hideMenu) { if (hideMenu) {
NSApp.presentationOptions.insert(.autoHideMenuBar) self.hideMenu()
// Ensure that we always hide the menu bar for this window, but not for non fullscreen ones
// This is not the best way to do this, not least because it causes the menu to stay visible
// for a brief moment before being hidden in some cases (e.g. when switching spaces).
// If we end up adding a NSWindowDelegate to PrimaryWindow, then we may be better off
// handling this there.
NotificationCenter.default.addObserver(
self,
selector: #selector(FullScreenHandler.hideMenu),
name: NSWindow.didBecomeMainNotification,
object: window)
NotificationCenter.default.addObserver(
self,
selector: #selector(FullScreenHandler.unHideMenu),
name: NSWindow.didResignMainNotification,
object: window)
} }
// This is important: it gives us the full screen, including the // This is important: it gives us the full screen, including the
@ -71,6 +99,22 @@ class FullScreenHandler {
window.makeKeyAndOrderFront(nil) window.makeKeyAndOrderFront(nil)
} }
@objc func hideMenu() {
NSApp.presentationOptions.insert(.autoHideMenuBar)
}
@objc func unHideMenu() {
NSApp.presentationOptions.remove(.autoHideMenuBar)
}
@objc func hideDock() {
NSApp.presentationOptions.insert(.autoHideDock)
}
@objc func unHideDock() {
NSApp.presentationOptions.remove(.autoHideDock)
}
func calculateFullscreenFrame(screenFrame: NSRect, subtractMenu: Bool)->NSRect { func calculateFullscreenFrame(screenFrame: NSRect, subtractMenu: Bool)->NSRect {
if (subtractMenu) { if (subtractMenu) {
let menuHeight = NSApp.mainMenu?.menuBarHeight ?? 0 let menuHeight = NSApp.mainMenu?.menuBarHeight ?? 0
@ -88,6 +132,11 @@ class FullScreenHandler {
// Restore previous presentation options // Restore previous presentation options
NSApp.presentationOptions = [] NSApp.presentationOptions = []
// Stop handling any window focus notifications
// that we use to manage menu bar visibility
NotificationCenter.default.removeObserver(self, name: NSWindow.didBecomeMainNotification, object: window)
NotificationCenter.default.removeObserver(self, name: NSWindow.didResignMainNotification, object: window)
// Restore frame // Restore frame
window.setFrame(window.frameRect(forContentRect: previousFrame), display: true) window.setFrame(window.frameRect(forContentRect: previousFrame), display: true)