summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorder richter <der.richter@gmx.de>2024-02-06 22:46:58 +0100
committerder richter <der.richter@gmx.de>2024-02-15 00:11:55 +0100
commitde36f11fa8b25dd85f40cfc306540b586fe1899d (patch)
treeba0c2e3d8621ef0872f6cc2eb5d8a1cb96b54d06
parent05d0252a30a5f6193197bc3605cf3f84004b393f (diff)
downloadmpv-de36f11fa8b25dd85f40cfc306540b586fe1899d.tar.bz2
mpv-de36f11fa8b25dd85f40cfc306540b586fe1899d.tar.xz
mac/remote: replace command handler with generic handler function config
preparation for the upcoming changes for new functionality not related to key handling.
-rw-r--r--osdep/macos/remote_command_center.swift32
1 files changed, 17 insertions, 15 deletions
diff --git a/osdep/macos/remote_command_center.swift b/osdep/macos/remote_command_center.swift
index 0bd97aecd8..874ae88d84 100644
--- a/osdep/macos/remote_command_center.swift
+++ b/osdep/macos/remote_command_center.swift
@@ -18,6 +18,8 @@
import MediaPlayer
extension RemoteCommandCenter {
+ typealias ConfigHandler = (MPRemoteCommandEvent) -> (MPRemoteCommandHandlerStatus)
+
enum KeyType {
case normal
case repeatable
@@ -27,10 +29,12 @@ extension RemoteCommandCenter {
let key: Int32
let type: KeyType
var state: UInt32 = 0
+ let handler: ConfigHandler
- init(key: Int32, type: KeyType = .normal) {
+ init(key: Int32, type: KeyType = .normal, handler: @escaping ConfigHandler = { event in return .commandFailed }) {
self.key = key
self.type = type
+ self.handler = handler
}
}
}
@@ -58,14 +62,14 @@ class RemoteCommandCenter: NSObject {
]
configs = [
- commandCenter.pauseCommand: Config(key: MP_KEY_PAUSEONLY),
- commandCenter.playCommand: Config(key: MP_KEY_PLAYONLY),
- commandCenter.stopCommand: Config(key: MP_KEY_STOP),
- commandCenter.nextTrackCommand: Config(key: MP_KEY_NEXT),
- commandCenter.previousTrackCommand: Config(key: MP_KEY_PREV),
- commandCenter.togglePlayPauseCommand: Config(key: MP_KEY_PLAY),
- commandCenter.seekForwardCommand: Config(key: MP_KEY_FORWARD, type: .repeatable),
- commandCenter.seekBackwardCommand: Config(key: MP_KEY_REWIND, type: .repeatable)
+ commandCenter.pauseCommand: Config(key: MP_KEY_PAUSEONLY, handler: keyHandler),
+ commandCenter.playCommand: Config(key: MP_KEY_PLAYONLY, handler: keyHandler),
+ commandCenter.stopCommand: Config(key: MP_KEY_STOP, handler: keyHandler),
+ commandCenter.nextTrackCommand: Config(key: MP_KEY_NEXT, handler: keyHandler),
+ commandCenter.previousTrackCommand: Config(key: MP_KEY_PREV, handler: keyHandler),
+ commandCenter.togglePlayPauseCommand: Config(key: MP_KEY_PLAY, handler: keyHandler),
+ commandCenter.seekForwardCommand: Config(key: MP_KEY_FORWARD, type: .repeatable, handler: keyHandler),
+ commandCenter.seekBackwardCommand: Config(key: MP_KEY_REWIND, type: .repeatable, handler: keyHandler)
]
disabledCommands = [
@@ -96,11 +100,9 @@ class RemoteCommandCenter: NSObject {
}
@objc func start() {
- for (cmd, _) in configs {
+ for (cmd, config) in configs {
cmd.isEnabled = true
- cmd.addTarget { [unowned self] event in
- return self.cmdHandler(event)
- }
+ cmd.addTarget(handler: config.handler)
}
infoCenter.nowPlayingInfo = nowPlayingInfo
@@ -134,8 +136,8 @@ class RemoteCommandCenter: NSObject {
infoCenter.playbackState = isPaused ? .paused : .playing
}
- func cmdHandler(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
- guard let config = configs[event.command] else {
+ lazy var keyHandler: ConfigHandler = { event in
+ guard let config = self.configs[event.command] else {
return .commandFailed
}