Mischa
Mischa

Reputation: 17241

How to get notified when an NSWindow's titler bar becomes visible in fullscreen mode?

When maximizing a macOS app to fullscreen mode, the menu bar is hidden by default:

Hidden Title Bar

However, when the user moves the cursor to the screen's top, the menu bar slides in again. In addition to that, the colored buttons for resizing and closing the window appear in the window's title bar:

Visible Title Bar

In my case (where the window's titleVisibility is set to hidden), this causes the toolbar buttons to shift. In order to prevent that (and let them keep their position), I would need to update the toolbar layout during the slide-in animation.

Unfortunately, I couldn't find any notification or delegate method that informs the window controller when the menu bar and the buttons are about to slide in (and with which animation).

Is there a way to detect this?

Upvotes: 0

Views: 364

Answers (1)

Marc T.
Marc T.

Reputation: 5320

This is the best solution I found and how I use it in my apps. Add this to your NSWindowController but you may need to tweak it that it fits exactly your needs.

UPDATE: I just see it only works without toolbar.

var observer:NSObjectProtocol?

func windowDidEnterFullScreen(_ notification: Notification) {

    observer = notificationCenter.addObserver(forName: NSWindow.didChangeOcclusionStateNotification, object: nil, queue: OperationQueue.main) { (notification) in

        if let window = notification.object as? NSWindow{

            if window.occlusionState == NSWindow.OcclusionState.init(rawValue: 8194){
                print("window title visible")
            } else {
                print("window title hidden")
            }
        }
    }
}

func windowDidExitFullScreen(_ notification: Notification) {

    notificationCenter.removeObserver(observer as Any)

}

Upvotes: 1

Related Questions