Unified · Split
| @@ -75,6 +75,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 74 unmodified lines | ||
| 75 | 75 | var event: EventHelper? { get { return appHub.event } } |
| 76 | 76 | var input: InputHelper { get { return appHub.input } } |
| 77 | 77 | var configs: [NSTouchBarItem.Identifier:Config] = [:] |
| 78 | + var observers: [NSKeyValueObservation] = [] | |
| 78 | 79 | var isPaused: Bool = false { didSet { updatePlayButton() } } |
| 79 | 80 | var position: Double = 0 { didSet { updateTouchBarTimeItems() } } |
| 80 | 81 | var duration: Double = 0 { didSet { updateTouchBarTimeItems() } } |
| @@ -138,7 +139,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 57 unmodified lines | ||
| 138 | 139 | defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar] |
| 139 | 140 | customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem, |
| 140 | 141 | .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) }] | |
| 142 | 143 | |
| 143 | 144 | event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE)) |
| 144 | 145 | event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE)) |
| @@ -158,7 +159,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 13 unmodified lines | ||
| 158 | 159 | item.view = config.handler(config) |
| 159 | 160 | item.customizationLabel = config.name |
| 160 | 161 | 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) }] | |
| 162 | 163 | return item |
| 163 | 164 | } |
| 164 | 165 | |
| @@ -179,14 +180,8 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 14 unmodified lines | ||
| 179 | 180 | return slider |
| 180 | 181 | } |
| 181 | 182 | |
| 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 { | |
| 190 | 185 | updateTouchBarTimeItems() |
| 191 | 186 | updatePlayButton() |
| 192 | 187 | } |