Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSWindow Animate show / hide titlebar

I am able to show and hide my NSWindows titlebar in one of two ways via an action method containing :

window.titlebarAppearsTransparent = true
window.movableByWindowBackground  = true

or preferably :

window.styleMask = NSBorderlessWindowMask
window.movableByWindowBackground  = true
window.canBecomeKeyWindow

Am I able to animate this with a fade effect in any way..? Or would that involve a far more convoluted method of showing and hiding the windows titlebar..?

like image 887
Gary Simpson Avatar asked Feb 06 '15 17:02

Gary Simpson


2 Answers

Ok I have found a solution to showing and hiding the NSWindow titlebar with an animated effect.

You could implement this in any method, I have the titlebar fading out on a text changed event method, and fading back in with any movement.

Fade out the titlebar :

    if window.standardWindowButton(NSWindowButton.ZoomButton)?.superview?.alphaValue > 0.1 {
        window.standardWindowButton(NSWindowButton.ZoomButton)?.superview?.animator().alphaValue = 0
    }

Fade in the titlebar :

    if window.standardWindowButton(NSWindowButton.ZoomButton)?.superview?.alphaValue < 1 {
        window.standardWindowButton(NSWindowButton.ZoomButton)?.superview?.animator().alphaValue = 1
    }

So we are getting the titlebar view by the fact that it is the superview of the standardWindowButtons. And simply animating the alpha value of the titlebar view.

like image 197
Gary Simpson Avatar answered Nov 05 '22 16:11

Gary Simpson


Gary Simpson's answer refactored.

extension NSWindow {

    func setTitleBarHidden(hidden: Bool, animated: Bool = true) {

    let buttonSuperView = standardWindowButtonSuperView()
    if buttonSuperView == nil {
        return
    }
    let view = buttonSuperView!
    if hidden {
        if view.alphaValue > 0.1 {
            if !animated {
                view.alphaValue = 0
                return
            }
            view.animator().alphaValue = 0
        }
        return
    }
    if view.alphaValue < 1 {
        if !animated {
            view.alphaValue = 1
            return
        }
        view.animator().alphaValue = 1
    }
}

func standardWindowButtonSuperView() -> NSView? {
    //http://stackoverflow.com/a/28381918
    return standardWindowButton(NSWindowButton.ZoomButton)?.superview
}

}
like image 21
8suhas Avatar answered Nov 05 '22 16:11

8suhas