summaryrefslogtreecommitdiffstats
path: root/video/out/mac/window.swift
diff options
context:
space:
mode:
Diffstat (limited to 'video/out/mac/window.swift')
-rw-r--r--video/out/mac/window.swift151
1 files changed, 96 insertions, 55 deletions
diff --git a/video/out/mac/window.swift b/video/out/mac/window.swift
index d692d0db91..c5a711ed86 100644
--- a/video/out/mac/window.swift
+++ b/video/out/mac/window.swift
@@ -19,21 +19,24 @@ import Cocoa
class Window: NSWindow, NSWindowDelegate {
weak var common: Common! = nil
- var mpv: MPVHelper? { get { return common.mpv } }
+ var option: OptionHelper { get { return common.option } }
+ var input: InputHelper? { get { return common.input } }
var targetScreen: NSScreen?
var previousScreen: NSScreen?
var currentScreen: NSScreen?
var unfScreen: NSScreen?
- var unfsContentFrame: NSRect?
+ var unfsContentFrame: NSRect = NSRect(x: 0, y: 0, width: 160, height: 90)
var isInFullscreen: Bool = false
- var isAnimating: Bool = false
var isMoving: Bool = false
- var forceTargetScreen: Bool = false
+ var previousStyleMask: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
+
+ var isAnimating: Bool = false
+ let animationLock: NSCondition = NSCondition()
- var unfsContentFramePixel: NSRect { get { return convertToBacking(unfsContentFrame ?? NSRect(x: 0, y: 0, width: 160, height: 90)) } }
- var framePixel: NSRect { get { return convertToBacking(frame) } }
+ var unfsContentFramePixel: NSRect { get { return convertToBacking(unfsContentFrame) } }
+ @objc var framePixel: NSRect { get { return convertToBacking(frame) } }
var keepAspect: Bool = true {
didSet {
@@ -42,7 +45,7 @@ class Window: NSWindow, NSWindowDelegate {
}
if keepAspect {
- contentAspectRatio = unfsContentFrame?.size ?? contentAspectRatio
+ contentAspectRatio = unfsContentFrame.size
} else {
resizeIncrements = NSSize(width: 1.0, height: 1.0)
}
@@ -61,6 +64,7 @@ class Window: NSWindow, NSWindowDelegate {
set {
let responder = firstResponder
let windowTitle = title
+ previousStyleMask = super.styleMask
super.styleMask = newValue
makeFirstResponder(responder)
title = windowTitle
@@ -88,7 +92,9 @@ class Window: NSWindow, NSWindowDelegate {
title = com.title
minSize = NSMakeSize(160, 90)
collectionBehavior = .fullScreenPrimary
+ ignoresMouseEvents = option.vo.cursor_passthrough
delegate = self
+ unfsContentFrame = contentRect
if let cView = contentView {
cView.addSubview(view)
@@ -100,13 +106,11 @@ class Window: NSWindow, NSWindowDelegate {
currentScreen = screen
unfScreen = screen
- if let app = NSApp as? Application {
- app.menuBar.register(#selector(setHalfWindowSize), for: MPM_H_SIZE)
- app.menuBar.register(#selector(setNormalWindowSize), for: MPM_N_SIZE)
- app.menuBar.register(#selector(setDoubleWindowSize), for: MPM_D_SIZE)
- app.menuBar.register(#selector(performMiniaturize(_:)), for: MPM_MINIMIZE)
- app.menuBar.register(#selector(performZoom(_:)), for: MPM_ZOOM)
- }
+ AppHub.shared.menu?.register(#selector(setHalfWindowSize), key: .itemHalfSize)
+ AppHub.shared.menu?.register(#selector(setNormalWindowSize), key: .itemNormalSize)
+ AppHub.shared.menu?.register(#selector(setDoubleWindowSize), key: .itemDoubleSize)
+ AppHub.shared.menu?.register(#selector(performMiniaturize(_:)), key: .itemMinimize)
+ AppHub.shared.menu?.register(#selector(performZoom(_:)), key: .itemZoom)
}
override func toggleFullScreen(_ sender: Any?) {
@@ -114,7 +118,9 @@ class Window: NSWindow, NSWindowDelegate {
return
}
+ animationLock.lock()
isAnimating = true
+ animationLock.unlock()
targetScreen = common.getTargetScreen(forFullscreen: !isInFullscreen)
if targetScreen == nil && previousScreen == nil {
@@ -136,7 +142,7 @@ class Window: NSWindow, NSWindowDelegate {
setFrame(frame, display: true)
}
- if Bool(mpv?.opts.native_fs ?? 1) {
+ if Bool(option.vo.native_fs) {
super.toggleFullScreen(sender)
} else {
if !isInFullscreen {
@@ -163,27 +169,31 @@ class Window: NSWindow, NSWindowDelegate {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = getFsAnimationDuration(duration - 0.05)
window.animator().setFrame(tScreen.frame, display: true)
- }, completionHandler: { })
+ }, completionHandler: nil)
}
func window(_ window: NSWindow, startCustomAnimationToExitFullScreenWithDuration duration: TimeInterval) {
guard let tScreen = targetScreen, let currentScreen = screen else { return }
let newFrame = calculateWindowPosition(for: tScreen, withoutBounds: tScreen == screen)
let intermediateFrame = aspectFit(rect: newFrame, in: currentScreen.frame)
- common.view?.layerContentsPlacement = .scaleProportionallyToFill
- common.titleBar?.hide()
- styleMask.remove(.fullScreen)
- setFrame(intermediateFrame, display: true)
+ common.titleBar?.hide(0.0)
NSAnimationContext.runAnimationGroup({ (context) -> Void in
- context.duration = getFsAnimationDuration(duration - 0.05)
- window.animator().setFrame(newFrame, display: true)
- }, completionHandler: { })
+ context.duration = 0.0
+ common.view?.layerContentsPlacement = .scaleProportionallyToFill
+ window.animator().setFrame(intermediateFrame, display: true)
+ }, completionHandler: {
+ NSAnimationContext.runAnimationGroup({ (context) -> Void in
+ context.duration = self.getFsAnimationDuration(duration - 0.05)
+ self.styleMask.remove(.fullScreen)
+ window.animator().setFrame(newFrame, display: true)
+ }, completionHandler: nil)
+ })
}
func windowDidEnterFullScreen(_ notification: Notification) {
isInFullscreen = true
- mpv?.setOption(fullscreen: isInFullscreen)
+ option.setOption(fullscreen: isInFullscreen)
common.updateCursorVisibility()
endAnimation(frame)
common.titleBar?.show()
@@ -192,7 +202,7 @@ class Window: NSWindow, NSWindowDelegate {
func windowDidExitFullScreen(_ notification: Notification) {
guard let tScreen = targetScreen else { return }
isInFullscreen = false
- mpv?.setOption(fullscreen: isInFullscreen)
+ option.setOption(fullscreen: isInFullscreen)
endAnimation(calculateWindowPosition(for: tScreen, withoutBounds: targetScreen == screen))
common.view?.layerContentsPlacement = .scaleProportionallyToFit
}
@@ -219,35 +229,60 @@ class Window: NSWindow, NSWindowDelegate {
}, completionHandler: nil )
}
+ animationLock.lock()
isAnimating = false
+ animationLock.signal()
+ animationLock.unlock()
common.windowDidEndAnimation()
}
func setToFullScreen() {
guard let targetFrame = targetScreen?.frame else { return }
- styleMask.insert(.fullScreen)
+
+ if #available(macOS 11.0, *) {
+ styleMask = .borderless
+ common.titleBar?.hide(0.0)
+ } else {
+ styleMask.insert(.fullScreen)
+ }
+
NSApp.presentationOptions = [.autoHideMenuBar, .autoHideDock]
setFrame(targetFrame, display: true)
endAnimation()
isInFullscreen = true
- mpv?.setOption(fullscreen: isInFullscreen)
+ option.setOption(fullscreen: isInFullscreen)
common.windowSetToFullScreen()
}
func setToWindow() {
guard let tScreen = targetScreen else { return }
+
+ if #available(macOS 11.0, *) {
+ styleMask = previousStyleMask
+ common.titleBar?.hide(0.0)
+ } else {
+ styleMask.remove(.fullScreen)
+ }
+
let newFrame = calculateWindowPosition(for: tScreen, withoutBounds: targetScreen == screen)
NSApp.presentationOptions = []
setFrame(newFrame, display: true)
- styleMask.remove(.fullScreen)
endAnimation()
isInFullscreen = false
- mpv?.setOption(fullscreen: isInFullscreen)
+ option.setOption(fullscreen: isInFullscreen)
common.windowSetToWindow()
}
+ func waitForAnimation() {
+ animationLock.lock()
+ while(isAnimating){
+ animationLock.wait()
+ }
+ animationLock.unlock()
+ }
+
func getFsAnimationDuration(_ def: Double) -> Double {
- let duration = mpv?.macOpts.macos_fs_animation_duration ?? -1
+ let duration = option.mac.macos_fs_animation_duration
if duration < 0 {
return def
} else {
@@ -274,6 +309,14 @@ class Window: NSWindow, NSWindowDelegate {
}
}
+ func setOnAllWorkspaces(_ state: Bool) {
+ if state {
+ collectionBehavior.insert(.canJoinAllSpaces)
+ } else {
+ collectionBehavior.remove(.canJoinAllSpaces)
+ }
+ }
+
func setMinimized(_ stateWanted: Bool) {
if isMiniaturized == stateWanted { return }
@@ -292,7 +335,7 @@ class Window: NSWindow, NSWindowDelegate {
func updateMovableBackground(_ pos: NSPoint) {
if !isInFullscreen {
- isMovableByWindowBackground = mpv?.canBeDraggedAt(pos) ?? true
+ isMovableByWindowBackground = input?.draggable(at: pos) ?? true
} else {
isMovableByWindowBackground = false
}
@@ -300,35 +343,31 @@ class Window: NSWindow, NSWindowDelegate {
func updateFrame(_ rect: NSRect) {
if rect != frame {
- let cRect = frameRect(forContentRect: rect)
unfsContentFrame = rect
- setFrame(cRect, display: true)
- common.windowDidUpdateFrame()
+ if !isInFullscreen {
+ let cRect = frameRect(forContentRect: rect)
+ setFrame(cRect, display: true)
+ common.windowDidUpdateFrame()
+ }
}
}
func updateSize(_ size: NSSize) {
if let currentSize = contentView?.frame.size, size != currentSize {
let newContentFrame = centeredContentSize(for: frame, size: size)
- if !isInFullscreen {
- updateFrame(newContentFrame)
- } else {
- unfsContentFrame = newContentFrame
- }
+ updateFrame(newContentFrame)
}
}
override func setFrame(_ frameRect: NSRect, display flag: Bool) {
if frameRect.width < minSize.width || frameRect.height < minSize.height {
- common.log.sendVerbose("tried to set too small window size: \(frameRect.size)")
+ common.log.verbose("tried to set too small window size: \(frameRect.size)")
return
}
super.setFrame(frameRect, display: flag)
- if let size = unfsContentFrame?.size, keepAspect {
- contentAspectRatio = size
- }
+ if keepAspect { contentAspectRatio = unfsContentFrame.size }
}
func centeredContentSize(for rect: NSRect, size sz: NSSize) -> NSRect {
@@ -349,10 +388,9 @@ class Window: NSWindow, NSWindowDelegate {
}
func calculateWindowPosition(for tScreen: NSScreen, withoutBounds: Bool) -> NSRect {
- guard let contentFrame = unfsContentFrame, let screen = unfScreen else {
- return frame
- }
- var newFrame = frameRect(forContentRect: contentFrame)
+ guard let screen = unfScreen else { return frame }
+
+ var newFrame = frameRect(forContentRect: unfsContentFrame)
let targetFrame = tScreen.frame
let targetVisibleFrame = tScreen.visibleFrame
let unfsScreenFrame = screen.frame
@@ -462,12 +500,12 @@ class Window: NSWindow, NSWindowDelegate {
@objc func setDoubleWindowSize() { setWindowScale(2.0) }
func setWindowScale(_ scale: Double) {
- mpv?.command("set window-scale \(scale)")
+ input?.command("set window-scale \(scale)")
}
func addWindowScale(_ scale: Double) {
if !isInFullscreen {
- mpv?.command("add window-scale \(scale)")
+ input?.command("add current-window-scale \(scale)")
}
}
@@ -500,7 +538,7 @@ class Window: NSWindow, NSWindowDelegate {
func windowDidEndLiveResize(_ notification: Notification) {
common.windowDidEndLiveResize()
- mpv?.setOption(maximized: isZoomed)
+ option.setOption(maximized: isZoomed)
if let contentViewFrame = contentView?.frame,
!isAnimating && !isInFullscreen
@@ -510,24 +548,27 @@ class Window: NSWindow, NSWindowDelegate {
}
func windowDidResize(_ notification: Notification) {
+ if let contentViewFrame = contentView?.frame, !isAnimating && !isInFullscreen && !inLiveResize {
+ unfsContentFrame = convertToScreen(contentViewFrame)
+ }
common.windowDidResize()
}
func windowShouldClose(_ sender: NSWindow) -> Bool {
- cocoa_put_key(MP_KEY_CLOSE_WIN)
+ input?.put(key: MP_KEY_CLOSE_WIN)
return false
}
func windowDidMiniaturize(_ notification: Notification) {
- mpv?.setOption(minimized: true)
+ option.setOption(minimized: true)
}
func windowDidDeminiaturize(_ notification: Notification) {
- mpv?.setOption(minimized: false)
+ option.setOption(minimized: false)
}
func windowDidResignKey(_ notification: Notification) {
- common.setCursorVisiblility(true)
+ common.setCursorVisibility(true)
}
func windowDidBecomeKey(_ notification: Notification) {
@@ -546,6 +587,6 @@ class Window: NSWindow, NSWindowDelegate {
}
func windowDidMove(_ notification: Notification) {
- mpv?.setOption(maximized: isZoomed)
+ option.setOption(maximized: isZoomed)
}
}