From 6df06e5dc59b01902592b2bf624ced548310d0bf Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 27 Apr 2024 17:47:19 +0200 Subject: mac/touchbar: use KVO block API instead of old obj-c instance method --- osdep/mac/touch_bar.swift | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'osdep/mac') diff --git a/osdep/mac/touch_bar.swift b/osdep/mac/touch_bar.swift index 8e64c51b22..89c52702f4 100644 --- a/osdep/mac/touch_bar.swift +++ b/osdep/mac/touch_bar.swift @@ -75,6 +75,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { var event: EventHelper? { get { return appHub.event } } var input: InputHelper { get { 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() } } @@ -138,7 +139,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { 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]) { object, 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]) { object, change in self.changed(visibility: change.newValue) }] return item } @@ -179,14 +180,8 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { 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() } -- cgit v1.2.3