summaryrefslogtreecommitdiffstats
path: root/osdep/mac/touch_bar.swift
blob: dcebb2497051187b02993307187c000f764c1c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * This file is part of mpv.
 *
 * mpv is free software) you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation) either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY) without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

extension NSTouchBar.CustomizationIdentifier {
    public static let customId: NSTouchBar.CustomizationIdentifier = "io.mpv.touchbar"
}

extension NSTouchBarItem.Identifier {
    public static let seekBar = NSTouchBarItem.Identifier(custom: ".seekbar")
    public static let play = NSTouchBarItem.Identifier(custom: ".play")
    public static let nextItem = NSTouchBarItem.Identifier(custom: ".nextItem")
    public static let previousItem = NSTouchBarItem.Identifier(custom: ".previousItem")
    public static let nextChapter = NSTouchBarItem.Identifier(custom: ".nextChapter")
    public static let previousChapter = NSTouchBarItem.Identifier(custom: ".previousChapter")
    public static let cycleAudio = NSTouchBarItem.Identifier(custom: ".cycleAudio")
    public static let cycleSubtitle = NSTouchBarItem.Identifier(custom: ".cycleSubtitle")
    public static let currentPosition = NSTouchBarItem.Identifier(custom: ".currentPosition")
    public static let timeLeft = NSTouchBarItem.Identifier(custom: ".timeLeft")

    init(custom: String) {
        self.init(NSTouchBar.CustomizationIdentifier.customId + custom)
    }
}

extension TouchBar {
    enum `Type` {
        case button
        case text
        case slider
    }

    struct Config {
        let name: String
        let type: Type
        let command: String
        var view: NSView?
        var item: NSCustomTouchBarItem?
        var constraint: NSLayoutConstraint?
        let image: NSImage
        let imageAlt: NSImage

        init(
            name: String = "",
            type: Type = .button,
            command: String = "",
            view: NSView? = nil,
            item: NSCustomTouchBarItem? = nil,
            constraint: NSLayoutConstraint? = nil,
            image: NSImage? = nil,
            imageAlt: NSImage? = nil
        ) {
            self.name = name
            self.type = type
            self.command = command
            self.view = view
            self.item = item
            self.constraint = constraint
            self.image = image ?? NSImage(size: NSSize(width: 1, height: 1))
            self.imageAlt = imageAlt ?? NSImage(size: NSSize(width: 1, height: 1))
        }
    }
}

class TouchBar: NSTouchBar, NSTouchBarDelegate {
    var configs: [NSTouchBarItem.Identifier:Config] = [:]
    var isPaused: Bool = false
    var position: Double = 0
    var duration: Double = 0

    override init() {
        super.init()

        configs = [
            .seekBar: Config(name: "Seek Bar", type: .slider, command: "seek %f absolute-percent"),
            .currentPosition: Config(name: "Current Position", type: .text),
            .timeLeft: Config(name: "Time Left", type: .text),
            .play: Config(
                name: "Play Button",
                type: .button,
                command: "cycle pause",
                image: .init(named: NSImage.touchBarPauseTemplateName),
                imageAlt: .init(named: NSImage.touchBarPlayTemplateName)
            ),
            .previousItem: Config(
                name: "Previous Playlist Item",
                type: .button,
                command: "playlist-prev",
                image: .init(named: NSImage.touchBarGoBackTemplateName)
            ),
            .nextItem: Config(
                name: "Next Playlist Item",
                type: .button,
                command: "playlist-next",
                image: .init(named: NSImage.touchBarGoForwardTemplateName)
            ),
            .previousChapter: Config(
                name: "Previous Chapter",
                type: .button,
                command: "add chapter -1",
                image: .init(named: NSImage.touchBarSkipBackTemplateName)
            ),
            .nextChapter: Config(
                name: "Next Chapter",
                type: .button,
                command: "add chapter 1",
                image: .init(named: NSImage.touchBarSkipAheadTemplateName)
            ),
            .cycleAudio: Config(
                name: "Cycle Audio",
                type: .button,
                command: "cycle audio",
                image: .init(named: NSImage.touchBarAudioInputTemplateName)
                ),
            .cycleSubtitle: Config(
                name: "Cycle Subtitle",
                type: .button,
                command: "cycle sub",
                image: .init(named: NSImage.touchBarComposeTemplateName)
            )
        ]

        delegate = self
        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)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
        guard let config = configs[identifier] else { return nil }

        switch config.type {
        case .button:
            let item = NSCustomTouchBarItem(identifier: identifier)
            let image = config.image
            let button = NSButton(image: image, target: self, action: #selector(buttonAction(_:)))
            item.view = button;
            item.customizationLabel = config.name
            configs[identifier]?.view = button
            configs[identifier]?.item = item
            item.addObserver(self, forKeyPath: "visible", options: [.new], context: nil)
            return item
        case .text:
            let item = NSCustomTouchBarItem(identifier: identifier)
            let text = NSTextField(labelWithString: "0:00")
            text.alignment = .center
            item.view = text;
            item.customizationLabel = config.name
            configs[identifier]?.view = text
            configs[identifier]?.item = item
            item.addObserver(self, forKeyPath: "visible", options: [.new], context: nil)
            return item
        case .slider:
            let item = NSCustomTouchBarItem(identifier: identifier)
            let slider = NSSlider(target: self, action: #selector(seekbarChanged(_:)))
            slider.minValue = 0
            slider.maxValue = 100
            item.view = slider;
            item.customizationLabel = config.name
            configs[identifier]?.view = slider
            configs[identifier]?.item = item
            item.addObserver(self, forKeyPath: "visible", options: [.new], context: nil)
            return item
        }
    }

    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 {
            updateTouchBarTimeItems()
            updatePlayButton()
        }
    }

    func updateTouchBarTimeItems() {
        if !isVisible { return }
        updateSlider()
        updateTimeLeft()
        updateCurrentPosition()
    }

    func updateSlider() {
        guard let config = configs[.seekBar], let slider = config.view as? NSSlider else { return }
        if !(config.item?.isVisible ?? false) { return }

        if duration <= 0 {
            slider.isEnabled = false
            slider.doubleValue = 0
        } else {
            slider.isEnabled = true
            if (!slider.isHighlighted) {
                slider.doubleValue = (position / duration) * 100
            }
        }
    }

    func updateTimeLeft() {
        guard let config = configs[.timeLeft], let text = config.view as? NSTextField else { return }
        if !(config.item?.isVisible ?? false) { return }

        removeConstraintFor(identifier: .timeLeft)
        if duration <= 0 {
            text.stringValue = ""
        } else {
            let left = Int(floor(duration) - floor(position))
            let leftFormat = format(time: left)
            let durationFormat = format(time: Int(duration))
            text.stringValue = "-\(leftFormat)"
            applyConstraintFrom(string: "-\(durationFormat)", identifier: .timeLeft)
        }
    }

    func updateCurrentPosition() {
        guard let config = configs[.currentPosition], let text = config.view as? NSTextField else { return }
        if !(config.item?.isVisible ?? false) { return }

        text.stringValue = format(time: Int(floor(position)))
        removeConstraintFor(identifier: .currentPosition)
        if duration <= 0 {
            applyConstraintFrom(string: format(time: Int(position)), identifier: .currentPosition)
        } else {
            applyConstraintFrom(string: format(time: Int(duration)), identifier: .currentPosition)
        }
    }

    func updatePlayButton() {
        guard let config = configs[.play], let button = config.view as? NSButton else { return }
        if !isVisible || !(config.item?.isVisible ?? false) { return }

        if isPaused {
            button.image = configs[.play]?.imageAlt
        } else {
            button.image = configs[.play]?.image
        }
    }

    @objc func buttonAction(_ button: NSButton) {
        guard let identifier = getIdentifierFrom(view: button), let command = configs[identifier]?.command else { return }
        EventsResponder.sharedInstance().inputHelper.command(command)
    }

    @objc func seekbarChanged(_ slider: NSSlider) {
        guard let identifier = getIdentifierFrom(view: slider), let command = configs[identifier]?.command else { return }
        EventsResponder.sharedInstance().inputHelper.command(String(format: command, slider.doubleValue))
    }

    func format(time: Int) -> String {
        let seconds = time % 60
        let minutes = (time / 60) % 60
        let hours = time / (60 * 60)

        var stime = hours > 0 ? "\(hours):" : ""
        stime = (stime.count > 0 || minutes > 9) ? String(format: "%@%02d:", stime, minutes) : "\(minutes):"
        stime = String(format: "%@%02d", stime, seconds)

        return stime
    }

    func removeConstraintFor(identifier: NSTouchBarItem.Identifier) {
        guard let text = configs[identifier]?.view as? NSTextField,
              let constraint = configs[identifier]?.constraint as? NSLayoutConstraint else { return }
        text.removeConstraint(constraint)
    }

    func applyConstraintFrom(string: String, identifier: NSTouchBarItem.Identifier) {
        guard let text = configs[identifier]?.view as? NSTextField else { return }
        let fString = string.components(separatedBy: .decimalDigits).joined(separator: "0")
        let textField = NSTextField(labelWithString: fString)
        let size = textField.frame.size

        let con = NSLayoutConstraint(item: text, attribute: .width, relatedBy: .equal, toItem: nil,
            attribute: .notAnAttribute, multiplier: 1.0, constant: ceil(size.width * 1.1))
        text.addConstraint(con)
        configs[identifier]?.constraint = con
    }

    func getIdentifierFrom(view: NSView) -> NSTouchBarItem.Identifier? {
        for (identifier, config) in configs {
            if config.view == view {
                return identifier
            }
        }
        return nil
    }

    @objc func processEvent(_ event: UnsafeMutablePointer<mpv_event>) {
        switch event.pointee.event_id {
        case MPV_EVENT_END_FILE:
            position = 0
            duration = 0
        case MPV_EVENT_PROPERTY_CHANGE:
            handlePropertyChange(event)
        default:
            break
        }
    }

    func handlePropertyChange(_ event: UnsafeMutablePointer<mpv_event>) {
        let pData = OpaquePointer(event.pointee.data)
        guard let property = UnsafePointer<mpv_event_property>(pData)?.pointee else { return }

        switch String(cString: property.name) {
        case "time-pos" where property.format == MPV_FORMAT_DOUBLE:
            let newPosition = max(LibmpvHelper.mpvDoubleToDouble(property.data) ?? 0, 0)
            if Int(floor(newPosition) - floor(position)) != 0 {
                position = newPosition
                updateTouchBarTimeItems()
            }
        case "duration" where property.format == MPV_FORMAT_DOUBLE:
            duration = LibmpvHelper.mpvDoubleToDouble(property.data) ?? 0
            updateTouchBarTimeItems()
        case "pause" where property.format == MPV_FORMAT_FLAG:
            isPaused = LibmpvHelper.mpvFlagToBool(property.data) ?? false
            updatePlayButton()
        default:
            break
        }
    }
}