diff options
Diffstat (limited to 'osdep/mac/touch_bar.swift')
-rw-r--r-- | osdep/mac/touch_bar.swift | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/osdep/mac/touch_bar.swift b/osdep/mac/touch_bar.swift index 8e64c51b22..6c4ad57169 100644 --- a/osdep/mac/touch_bar.swift +++ b/osdep/mac/touch_bar.swift @@ -72,9 +72,10 @@ extension TouchBar { class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { unowned let appHub: AppHub - var event: EventHelper? { get { return appHub.event } } - var input: InputHelper { get { return appHub.input } } - var configs: [NSTouchBarItem.Identifier:Config] = [:] + var event: EventHelper? { return appHub.event } + var input: InputHelper { return appHub.input } + var configs: [NSTouchBarItem.Identifier: Config] = [:] + var observers: [NSKeyValueObservation] = [] var isPaused: Bool = false { didSet { updatePlayButton() } } var position: Double = 0 { didSet { updateTouchBarTimeItems() } } var duration: Double = 0 { didSet { updateTouchBarTimeItems() } } @@ -134,11 +135,11 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { ] delegate = self - customizationIdentifier = .customId; + customizationIdentifier = .customId defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar] customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem, .previousChapter, .nextChapter, .cycleAudio, .cycleSubtitle, .currentPosition, .timeLeft] - addObserver(self, forKeyPath: "visible", options: [.new], context: nil) + observers += [observe(\.isVisible, options: [.new]) { _, change in self.changed(visibility: change.newValue) }] event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE)) event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE)) @@ -158,7 +159,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { item.view = config.handler(config) item.customizationLabel = config.name configs[identifier]?.item = item - item.addObserver(self, forKeyPath: "visible", options: [.new], context: nil) + observers += [item.observe(\.isVisible, options: [.new]) { _, change in self.changed(visibility: change.newValue) }] return item } @@ -166,27 +167,21 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { return NSButton(image: config.image, target: self, action: #selector(Self.buttonAction(_:))) } - lazy var createText: ViewHandler = { config in + lazy var createText: ViewHandler = { _ in let text = NSTextField(labelWithString: "0:00") text.alignment = .center return text } - lazy var createSlider: ViewHandler = { config in + lazy var createSlider: ViewHandler = { _ in let slider = NSSlider(target: self, action: #selector(Self.seekbarChanged(_:))) slider.minValue = 0 slider.maxValue = 100 return slider } - override func observeValue( - forKeyPath keyPath: String?, - of object: Any?, - change: [NSKeyValueChangeKey:Any]?, - context: UnsafeMutableRawPointer? - ) { - guard let visible = change?[.newKey] as? Bool else { return } - if keyPath == "isVisible" && visible { + func changed(visibility: Bool?) { + if let visible = visibility, visible { updateTouchBarTimeItems() updatePlayButton() } @@ -270,10 +265,8 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { } func getIdentifierFrom(view: NSView) -> NSTouchBarItem.Identifier? { - for (identifier, config) in configs { - if config.item?.view == view { - return identifier - } + for (identifier, config) in configs where config.item?.view == view { + return identifier } return nil } |