mirror of https://github.com/mpv-player/mpv
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.
This commit is contained in:
parent
fc978eb9f2
commit
2c7e4f5935
|
@ -20,7 +20,6 @@ import Cocoa
|
||||||
class Application: NSApplication, NSApplicationDelegate {
|
class Application: NSApplication, NSApplicationDelegate {
|
||||||
var appHub: AppHub { get { return AppHub.shared } }
|
var appHub: AppHub { get { return AppHub.shared } }
|
||||||
let MPV_PROTOCOL: String = "mpv://"
|
let MPV_PROTOCOL: String = "mpv://"
|
||||||
@objc var openCount: Int = 0
|
|
||||||
|
|
||||||
var playbackThreadId: mp_thread!
|
var playbackThreadId: mp_thread!
|
||||||
var argc: Int32?
|
var argc: Int32?
|
||||||
|
@ -28,14 +27,6 @@ class Application: NSApplication, NSApplicationDelegate {
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
super.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) {
|
required init?(coder: NSCoder) {
|
||||||
|
@ -44,7 +35,6 @@ class Application: NSApplication, NSApplicationDelegate {
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
let eventManager = NSAppleEventManager.shared()
|
let eventManager = NSAppleEventManager.shared()
|
||||||
eventManager.removeEventHandler(forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
|
|
||||||
eventManager.removeEventHandler(forEventClass: AEEventClass(kCoreEventClass), andEventID: kAEQuitApplication)
|
eventManager.removeEventHandler(forEventClass: AEEventClass(kCoreEventClass), andEventID: kAEQuitApplication)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +74,18 @@ class Application: NSApplication, NSApplicationDelegate {
|
||||||
}
|
}
|
||||||
#endif
|
#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) {
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
||||||
let eventManager = NSAppleEventManager.shared()
|
let eventManager = NSAppleEventManager.shared()
|
||||||
eventManager.setEventHandler(
|
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 {
|
func bundleStartedFromFinder() -> Bool {
|
||||||
return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true"
|
return ProcessInfo.processInfo.environment["MPVBUNDLE"] == "true"
|
||||||
}
|
}
|
||||||
|
@ -158,11 +137,6 @@ class Application: NSApplication, NSApplicationDelegate {
|
||||||
setupBundle()
|
setupBundle()
|
||||||
initApplication(true)
|
initApplication(true)
|
||||||
} else {
|
} else {
|
||||||
for argument in CommandLine.arguments.dropFirst() {
|
|
||||||
if !argument.hasPrefix("-") {
|
|
||||||
openCount += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
initApplication(false)
|
initApplication(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue