From 2c7e4f59353705702224429f6a9b247f3ca6949e Mon Sep 17 00:00:00 2001 From: der richter Date: Sun, 31 Mar 2024 00:25:42 +0100 Subject: [PATCH] mac/app: use new open url App event to simplify open file event handling this event has several advantages, it unifies the mpv:// url handling, the dropping of files on the App icon and opening via finder into one event, and it also lets us remove the file open workaround. we had to keep track of opened files via the command line because the event was also triggered by passed files on the command line, leading to redundant load events. the new event doesn't trigger from files passed via the command line anymore. --- osdep/mac/application.swift | 50 +++++++++---------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/osdep/mac/application.swift b/osdep/mac/application.swift index 6a1af89cec..ee5ee8eea2 100644 --- a/osdep/mac/application.swift +++ b/osdep/mac/application.swift @@ -20,7 +20,6 @@ import Cocoa class Application: NSApplication, NSApplicationDelegate { var appHub: AppHub { get { return AppHub.shared } } let MPV_PROTOCOL: String = "mpv://" - @objc var openCount: Int = 0 var playbackThreadId: mp_thread! var argc: Int32? @@ -28,14 +27,6 @@ class Application: NSApplication, NSApplicationDelegate { override init() { super.init() - - let eventManager = NSAppleEventManager.shared() - eventManager.setEventHandler( - self, - andSelector: #selector(self.getUrl(event:replyEvent:)), - forEventClass: AEEventClass(kInternetEventClass), - andEventID: AEEventID(kAEGetURL) - ) } required init?(coder: NSCoder) { @@ -44,7 +35,6 @@ class Application: NSApplication, NSApplicationDelegate { deinit { let eventManager = NSAppleEventManager.shared() - eventManager.removeEventHandler(forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) eventManager.removeEventHandler(forEventClass: AEEventClass(kCoreEventClass), andEventID: kAEQuitApplication) } @@ -84,6 +74,18 @@ class Application: NSApplication, NSApplicationDelegate { } #endif + func application(_ application: NSApplication, open urls: [URL]) { + let files = urls.map { + if $0.isFileURL { return $0.path } + var path = $0.absoluteString + if path.hasPrefix(MPV_PROTOCOL) { path.removeFirst(MPV_PROTOCOL.count) } + return path.removingPercentEncoding ?? path + }.sorted { (strL: String, strR: String) -> Bool in + return strL.localizedStandardCompare(strR) == .orderedAscending + } + appHub.input.open(files: files) + } + func applicationWillFinishLaunching(_ notification: Notification) { let eventManager = NSAppleEventManager.shared() eventManager.setEventHandler( @@ -101,29 +103,6 @@ class Application: NSApplication, NSApplicationDelegate { } } - @objc func getUrl(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) { - guard var url: String = event?.paramDescriptor(forKeyword: keyDirectObject)?.stringValue else { return } - - if url.hasPrefix(MPV_PROTOCOL) { - url.removeFirst(MPV_PROTOCOL.count) - } - - url = url.removingPercentEncoding ?? url - appHub.input.open(files: [url]) - } - - func application(_ sender: NSApplication, openFiles: [String]) { - if openCount > 0 { - openCount -= openFiles.count - return - } - - let files = openFiles.sorted { (strL: String, strR: String) -> Bool in - return strL.localizedStandardCompare(strR) == .orderedAscending - } - appHub.input.open(files: files) - } - func bundleStartedFromFinder() -> Bool { return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true" } @@ -158,11 +137,6 @@ class Application: NSApplication, NSApplicationDelegate { setupBundle() initApplication(true) } else { - for argument in CommandLine.arguments.dropFirst() { - if !argument.hasPrefix("-") { - openCount += 1 - } - } initApplication(false) }