History

mac/touchbar: use KVO block API instead of old obj-c instance method

der richter committed 2 years ago

Commit 6df06e51 parent 69d70141 file · +5 −10Browse files

Unified · Split

osdep/mac/touch_bar.swift+5−10
@@ -75,6 +75,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 74 unmodified lines
7575 var event: EventHelper? { get { return appHub.event } }
7676 var input: InputHelper { get { return appHub.input } }
7777 var configs: [NSTouchBarItem.Identifier:Config] = [:]
78+ var observers: [NSKeyValueObservation] = []
7879 var isPaused: Bool = false { didSet { updatePlayButton() } }
7980 var position: Double = 0 { didSet { updateTouchBarTimeItems() } }
8081 var duration: Double = 0 { didSet { updateTouchBarTimeItems() } }
@@ -138,7 +139,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 57 unmodified lines
138139 defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar]
139140 customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem,
140141 .previousChapter, .nextChapter, .cycleAudio, .cycleSubtitle, .currentPosition, .timeLeft]
141− addObserver(self, forKeyPath: "visible", options: [.new], context: nil)
142+ observers += [observe(\.isVisible, options: [.new]) { object, change in self.changed(visibility: change.newValue) }]
142143
143144 event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE))
144145 event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE))
@@ -158,7 +159,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 13 unmodified lines
158159 item.view = config.handler(config)
159160 item.customizationLabel = config.name
160161 configs[identifier]?.item = item
161− item.addObserver(self, forKeyPath: "visible", options: [.new], context: nil)
162+ observers += [item.observe(\.isVisible, options: [.new]) { object, change in self.changed(visibility: change.newValue) }]
162163 return item
163164 }
164165
@@ -179,14 +180,8 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 14 unmodified lines
179180 return slider
180181 }
181182
182− override func observeValue(
183− forKeyPath keyPath: String?,
184− of object: Any?,
185− change: [NSKeyValueChangeKey:Any]?,
186− context: UnsafeMutableRawPointer?
187− ) {
188− guard let visible = change?[.newKey] as? Bool else { return }
189− if keyPath == "isVisible" && visible {
183+ func changed(visibility: Bool?) {
184+ if let visible = visibility, visible {
190185 updateTouchBarTimeItems()
191186 updatePlayButton()
192187 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t