From d0a2661c5b4adc4706b61a5cc86e73448fa936ea Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 7 Nov 2020 14:26:14 +0100 Subject: [PATCH] mac: make focus property observable i missed the VO_EVENT_FOCUS event and the possibility to observe this property and didn't include it in my initial focus commit for that matter. --- video/out/mac/common.swift | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/video/out/mac/common.swift b/video/out/mac/common.swift index 7b3c0fa10d..cb1b74b196 100644 --- a/video/out/mac/common.swift +++ b/video/out/mac/common.swift @@ -38,6 +38,8 @@ class Common: NSObject { var displaySleepAssertion: IOPMAssertionID = IOPMAssertionID(0) + var appNotificationObservers: [NSObjectProtocol] = [] + var cursorVisibilityWanted: Bool = true var title: String = "mpv" { @@ -57,6 +59,7 @@ class Common: NSObject { startDisplayLink(vo) initLightSensor() addDisplayReconfigureObserver() + addAppNotifications() mpv.setMacOptionCallback(macOptsWakeupCallback, context: self) } @@ -152,6 +155,7 @@ class Common: NSObject { stopDisplaylink() uninitLightSensor() removeDisplayReconfigureObserver() + removeAppNotifications() enableDisplaySleep() window?.orderOut(nil) @@ -347,6 +351,34 @@ class Common: NSObject { CGDisplayRemoveReconfigurationCallback(reconfigureCallback, MPVHelper.bridge(obj: self)) } + func addAppNotifications() { + appNotificationObservers.append(NotificationCenter.default.addObserver( + forName: NSApplication.didBecomeActiveNotification, + object: nil, + queue: .main, + using: { [weak self] (_) in self?.appDidBecomeActive() } + )) + appNotificationObservers.append(NotificationCenter.default.addObserver( + forName: NSApplication.didResignActiveNotification, + object: nil, + queue: .main, + using: { [weak self] (_) in self?.appDidResignActive() } + )) + } + + func removeAppNotifications() { + appNotificationObservers.forEach { NotificationCenter.default.removeObserver($0) } + appNotificationObservers.removeAll() + } + + func appDidBecomeActive() { + flagEvents(VO_EVENT_FOCUS) + } + + func appDidResignActive() { + flagEvents(VO_EVENT_FOCUS) + } + func setAppIcon() { if let app = NSApp as? Application, ProcessInfo.processInfo.environment["MPVBUNDLE"] != "true"