Unified · Split
| @@ -75,6 +75,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 74 unmodified lines | |||
| 75 | var event: EventHelper? { get { return appHub.event } } | 75 | var event: EventHelper? { get { return appHub.event } } |
| 76 | var input: InputHelper { get { return appHub.input } } | 76 | var input: InputHelper { get { return appHub.input } } |
| 77 | var configs: [NSTouchBarItem.Identifier:Config] = [:] | 77 | var configs: [NSTouchBarItem.Identifier:Config] = [:] |
| 78 | var observers: [NSKeyValueObservation] = [] | ||
| 78 | var isPaused: Bool = false { didSet { updatePlayButton() } } | 79 | var isPaused: Bool = false { didSet { updatePlayButton() } } |
| 79 | var position: Double = 0 { didSet { updateTouchBarTimeItems() } } | 80 | var position: Double = 0 { didSet { updateTouchBarTimeItems() } } |
| 80 | var duration: Double = 0 { didSet { updateTouchBarTimeItems() } } | 81 | var duration: Double = 0 { didSet { updateTouchBarTimeItems() } } |
| @@ -138,7 +139,7 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 57 unmodified lines | |||
| 138 | defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar] | 139 | defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar] |
| 139 | customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem, | 140 | customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem, |
| 140 | .previousChapter, .nextChapter, .cycleAudio, .cycleSubtitle, .currentPosition, .timeLeft] | 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 | event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE)) | 144 | event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE)) |
| 144 | event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE)) | 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 | item.view = config.handler(config) | 159 | item.view = config.handler(config) |
| 159 | item.customizationLabel = config.name | 160 | item.customizationLabel = config.name |
| 160 | configs[identifier]?.item = item | 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 | return item | 163 | return item |
| 163 | } | 164 | } |
| 164 | 165 | ||
| @@ -179,14 +180,8 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber { · 14 unmodified lines | |||
| 179 | return slider | 180 | return slider |
| 180 | } | 181 | } |
| 181 | 182 | ||
| 182 | override func observeValue( | 183 | func changed(visibility: Bool?) { |
| 183 | forKeyPath keyPath: String?, | 184 | if let visible = visibility, visible { |
| 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 { | ||
| 190 | updateTouchBarTimeItems() | 185 | updateTouchBarTimeItems() |
| 191 | updatePlayButton() | 186 | updatePlayButton() |
| 192 | } | 187 | } |