diff options
Diffstat (limited to 'video/out/cocoa-cb/window.swift')
-rw-r--r-- | video/out/cocoa-cb/window.swift | 112 |
1 files changed, 84 insertions, 28 deletions
diff --git a/video/out/cocoa-cb/window.swift b/video/out/cocoa-cb/window.swift index b3db7aeb8f..d11706f38b 100644 --- a/video/out/cocoa-cb/window.swift +++ b/video/out/cocoa-cb/window.swift @@ -17,6 +17,31 @@ import Cocoa +class CustomTtitleBar: NSVisualEffectView { + + // catch these events so they are not propagated to the underlying view + override func mouseDown(with event: NSEvent) { } + + override func mouseUp(with event: NSEvent) { + if event.clickCount > 1 { + let def = UserDefaults.standard + var action = def.string(forKey: "AppleActionOnDoubleClick") + + // macOS 10.10 and earlier + if action == nil { + action = def.bool(forKey: "AppleMiniaturizeOnDoubleClick") == true ? + "Minimize" : "Maximize" + } + + if action == "Minimize" { + window!.miniaturize(self) + } else if action == "Maximize" { + window!.zoom(self) + } + } + } +} + class Window: NSWindow, NSWindowDelegate { weak var cocoaCB: CocoaCB! = nil @@ -78,28 +103,23 @@ class Window: NSWindow, NSWindowDelegate { } } - convenience init(cocoaCB ccb: CocoaCB) { - self.init(contentRect: NSMakeRect(0, 0, 960, 480), - styleMask: [.titled, .closable, .miniaturizable, .resizable], - backing: .buffered, defer: false, screen: NSScreen.main()) - cocoaCB = ccb - title = "mpv" - } - - convenience init(contentRect: NSRect, styleMask style: NSWindowStyleMask, - screen: NSScreen?, cocoaCB ccb: CocoaCB) - { - self.init(contentRect: contentRect, styleMask: style, + convenience init(contentRect: NSRect, screen: NSScreen?, view: NSView, cocoaCB ccb: CocoaCB) { + self.init(contentRect: contentRect, + styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false, screen: screen) cocoaCB = ccb + title = cocoaCB.title minSize = NSMakeSize(160, 90) collectionBehavior = .fullScreenPrimary delegate = self + contentView!.addSubview(view) + view.frame = contentView!.frame unfsContentFrame = convertToScreen(contentView!.frame) targetScreen = screen! currentScreen = screen! unfScreen = screen! + initTitleBar() if let app = NSApp as? Application { app.menuBar.register(#selector(setHalfWindowSize), for: MPM_H_SIZE) @@ -118,19 +138,36 @@ class Window: NSWindow, NSWindowDelegate { styleMask.insert(.fullSizeContentView) titleBar.alphaValue = 0 titlebarAppearsTransparent = true - titleBarEffect = NSVisualEffectView(frame: f) + titleBarEffect = CustomTtitleBar(frame: f) titleBarEffect!.alphaValue = 0 titleBarEffect!.blendingMode = .withinWindow titleBarEffect!.autoresizingMask = [.viewWidthSizable, .viewMinYMargin] - setTitleBarStyle(mpv.getStringProperty("macos-title-bar-style") ?? "dark") + setTitleBarStyle(Int(mpv.macOpts!.macos_title_bar_style)) contentView!.addSubview(titleBarEffect!, positioned: .above, relativeTo: nil) - - border = mpv.getBoolProperty("border") } - func setTitleBarStyle(_ style: String) { - var effect = style + func setTitleBarStyle(_ style: Any) { + var effect: String + + if style is Int { + switch style as! Int { + case 4: + effect = "auto" + case 3: + effect = "mediumlight" + case 2: + effect = "light" + case 1: + effect = "ultradark" + case 0: fallthrough + default: + effect = "dark" + } + } else { + effect = style as! String + } + if effect == "auto" { let systemStyle = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") effect = systemStyle == nil ? "mediumlight" : "ultradark" @@ -167,6 +204,7 @@ class Window: NSWindow, NSWindowDelegate { titleBar.animator().alphaValue = 1 if !isInFullscreen && !isAnimating { titleBarEffect!.animator().alphaValue = 1 + titleBarEffect!.isHidden = false } }, completionHandler: nil ) @@ -181,6 +219,7 @@ class Window: NSWindow, NSWindowDelegate { if titleBarEffect == nil { return } if isInFullscreen && !isAnimating { titleBarEffect!.alphaValue = 0 + titleBarEffect!.isHidden = true return } NSAnimationContext.runAnimationGroup({ (context) -> Void in @@ -189,6 +228,7 @@ class Window: NSWindow, NSWindowDelegate { titleBarEffect!.animator().alphaValue = 0 }, completionHandler: { self.titleButtons.forEach { $0.isHidden = true } + self.titleBarEffect!.isHidden = true }) } @@ -297,7 +337,7 @@ class Window: NSWindow, NSWindowDelegate { } func endAnimation(_ newFrame: NSRect = NSZeroRect) { - if !NSEqualRects(newFrame, NSZeroRect) { + if !NSEqualRects(newFrame, NSZeroRect) && isAnimating { NSAnimationContext.runAnimationGroup({ (context) -> Void in context.duration = 0.01 self.animator().setFrame(newFrame, display: true) @@ -339,16 +379,26 @@ class Window: NSWindow, NSWindowDelegate { } } - func setOnTop(_ state: Bool) { + func setOnTop(_ state: Bool, _ ontopLevel: Any) { if state { - let ontopLevel = mpv.getStringProperty("ontop-level") ?? "window" - switch ontopLevel { - case "window": - level = Int(CGWindowLevelForKey(.floatingWindow)) - case "system": - level = Int(CGWindowLevelForKey(.statusWindow))+1 - default: - level = Int(ontopLevel)! + if ontopLevel is Int { + switch ontopLevel as! Int { + case -1: + level = Int(CGWindowLevelForKey(.floatingWindow)) + case -2: + level = Int(CGWindowLevelForKey(.statusWindow))+1 + default: + level = ontopLevel as! Int + } + } else { + switch ontopLevel as! String { + case "window": + level = Int(CGWindowLevelForKey(.floatingWindow)) + case "system": + level = Int(CGWindowLevelForKey(.statusWindow))+1 + default: + level = Int(ontopLevel as! String)! + } } collectionBehavior.remove(.transient) collectionBehavior.insert(.managed) @@ -563,6 +613,12 @@ class Window: NSWindow, NSWindowDelegate { cocoaCB.updateCusorVisibility() } + func windowDidChangeOcclusionState(_ notification: Notification) { + if occlusionState.contains(.visible) { + cocoaCB.layer.update(force: true) + } + } + func windowWillMove(_ notification: Notification) { isMoving = true } |