From 938ad6ebc037ebb32b41619a31b15f8ade712867 Mon Sep 17 00:00:00 2001 From: Akemi Date: Fri, 16 Feb 2018 13:07:15 +0100 Subject: cocoa-cb: change border and borderless window styling the title bar is now within the window bounds instead of outside. same as QuickTime Player. it supports several standard styles, two dark and two light ones. additionally we have properly rounded corners now and the borderless window also has the proper window shadow. Also make the earliest supported macOS version 10.10. Fixes #4789, #3944 --- video/out/cocoa-cb/window.swift | 118 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 12 deletions(-) (limited to 'video/out/cocoa-cb/window.swift') diff --git a/video/out/cocoa-cb/window.swift b/video/out/cocoa-cb/window.swift index 869bfb8be6..bd94330dcb 100644 --- a/video/out/cocoa-cb/window.swift +++ b/video/out/cocoa-cb/window.swift @@ -49,6 +49,21 @@ class Window: NSWindow, NSWindowDelegate { } } + var border: Bool = true { + didSet { if !border { hideTitleBar() } } + } + + var titleBarEffect: NSVisualEffectView? + var titleBar: NSView { + get { return (standardWindowButton(.closeButton)?.superview)! } + } + var titleBarHeight: CGFloat { + get { return NSWindow.frameRect(forContentRect: CGRect.zero, styleMask: .titled).size.height } + } + var titleButtons: [NSButton] { + get { return ([.closeButton, .miniaturizeButton, .zoomButton] as [NSWindow.ButtonType]).flatMap { standardWindowButton($0) } } + } + override var canBecomeKey: Bool { return true } override var canBecomeMain: Bool { return true } @@ -95,6 +110,94 @@ class Window: NSWindow, NSWindowDelegate { } } + func initTitleBar() { + var f = contentView!.bounds + f.origin.y = f.size.height - titleBarHeight + f.size.height = titleBarHeight + + styleMask.insert(.fullSizeContentView) + titleBar.alphaValue = 0 + titlebarAppearsTransparent = true + titleBarEffect = NSVisualEffectView(frame: f) + titleBarEffect!.alphaValue = 0 + titleBarEffect!.blendingMode = .withinWindow + titleBarEffect!.autoresizingMask = [.viewWidthSizable, .viewMinYMargin] + + setTitleBarStyle(mpv.getStringProperty("macos-title-bar-style") ?? "dark") + contentView!.addSubview(titleBarEffect!, positioned: .above, relativeTo: nil) + + border = mpv.getBoolProperty("border") + } + + func setTitleBarStyle(_ style: String) { + var effect = style + if effect == "auto" { + let systemStyle = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") + effect = systemStyle == nil ? "mediumlight" : "ultradark" + } + + switch effect { + case "mediumlight": + appearance = NSAppearance(named: NSAppearanceNameVibrantLight) + titleBarEffect!.material = .titlebar + titleBarEffect!.state = .followsWindowActiveState + case "light": + appearance = NSAppearance(named: NSAppearanceNameVibrantLight) + titleBarEffect!.material = .light + titleBarEffect!.state = .active + case "ultradark": + appearance = NSAppearance(named: NSAppearanceNameVibrantDark) + titleBarEffect!.material = .titlebar + titleBarEffect!.state = .followsWindowActiveState + case "dark": fallthrough + default: + appearance = NSAppearance(named: NSAppearanceNameVibrantDark) + titleBarEffect!.material = .dark + titleBarEffect!.state = .active + } + } + + func showTitleBar() { + if !border && !isInFullscreen { return } + let loc = cocoaCB.view.convert(mouseLocationOutsideOfEventStream, from: nil) + + titleButtons.forEach { $0.isHidden = false } + NSAnimationContext.runAnimationGroup({ (context) -> Void in + context.duration = 0.20 + titleBar.animator().alphaValue = 1 + if !isInFullscreen && !isAnimating { + titleBarEffect!.animator().alphaValue = 1 + } + }, completionHandler: nil ) + + if loc.y > titleBarHeight { + hideTitleBarDelayed() + } else { + NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideTitleBar), object: nil) + } + } + + func hideTitleBar() { + if isInFullscreen && !isAnimating { + titleBarEffect!.alphaValue = 0 + return + } + NSAnimationContext.runAnimationGroup({ (context) -> Void in + context.duration = 0.20 + titleBar.animator().alphaValue = 0 + titleBarEffect!.animator().alphaValue = 0 + }, completionHandler: { + self.titleButtons.forEach { $0.isHidden = true } + }) + } + + func hideTitleBarDelayed() { + NSObject.cancelPreviousPerformRequests(withTarget: self, + selector: #selector(hideTitleBar), + object: nil) + perform(#selector(hideTitleBar), with: nil, afterDelay: 0.5) + } + override func toggleFullScreen(_ sender: Any?) { if isAnimating { return @@ -146,6 +249,7 @@ class Window: NSWindow, NSWindowDelegate { let cRect = contentRect(forFrameRect: frame) var intermediateFrame = aspectFit(rect: cRect, in: targetScreen!.frame) intermediateFrame = frameRect(forContentRect: intermediateFrame) + hideTitleBar() NSAnimationContext.runAnimationGroup({ (context) -> Void in context.duration = duration - 0.05 @@ -156,6 +260,7 @@ class Window: NSWindow, NSWindowDelegate { func window(_ window: NSWindow, startCustomAnimationToExitFullScreenWithDuration duration: TimeInterval) { let newFrame = calculateWindowPosition(for: targetScreen!, withoutBounds: targetScreen == screen) let intermediateFrame = aspectFit(rect: newFrame, in: screen!.frame) + hideTitleBar() setFrame(intermediateFrame, display: true) NSAnimationContext.runAnimationGroup({ (context) -> Void in @@ -169,6 +274,7 @@ class Window: NSWindow, NSWindowDelegate { cocoaCB.flagEvents(VO_EVENT_FULLSCREEN_STATE) cocoaCB.updateCusorVisibility() endAnimation(frame) + showTitleBar() } func windowDidExitFullScreen(_ notification: Notification) { @@ -222,18 +328,6 @@ class Window: NSWindow, NSWindowDelegate { cocoaCB.layer.neededFlips += 1 } - func setBorder(_ state: Bool) { - if styleMask.contains(.titled) != state { - if state { - styleMask.remove(.borderless) - styleMask.insert(.titled) - } else { - styleMask.remove(.titled) - styleMask.insert(.borderless) - } - } - } - func setOnTop(_ state: Bool) { if state { let ontopLevel = mpv.getStringProperty("ontop-level") ?? "window" -- cgit v1.2.3