2024-03-17 16:12:09 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-27 22:22:56 +00:00
|
|
|
import Cocoa
|
|
|
|
|
2024-03-17 16:12:09 +00:00
|
|
|
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 {
|
2024-03-18 00:39:03 +00:00
|
|
|
typealias ViewHandler = (Config) -> (NSView)
|
2024-03-17 16:12:09 +00:00
|
|
|
|
|
|
|
struct Config {
|
|
|
|
let name: String
|
|
|
|
let command: String
|
|
|
|
var item: NSCustomTouchBarItem?
|
|
|
|
var constraint: NSLayoutConstraint?
|
|
|
|
let image: NSImage
|
|
|
|
let imageAlt: NSImage
|
2024-03-18 00:39:03 +00:00
|
|
|
let handler: ViewHandler
|
2024-03-17 16:12:09 +00:00
|
|
|
|
|
|
|
init(
|
|
|
|
name: String = "",
|
|
|
|
command: String = "",
|
|
|
|
item: NSCustomTouchBarItem? = nil,
|
|
|
|
constraint: NSLayoutConstraint? = nil,
|
|
|
|
image: NSImage? = nil,
|
2024-03-18 00:39:03 +00:00
|
|
|
imageAlt: NSImage? = nil,
|
|
|
|
handler: @escaping ViewHandler = { _ in return NSButton(title: "", target: nil, action: nil) }
|
2024-03-17 16:12:09 +00:00
|
|
|
) {
|
|
|
|
self.name = name
|
|
|
|
self.command = command
|
|
|
|
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))
|
2024-03-18 00:39:03 +00:00
|
|
|
self.handler = handler
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 23:50:41 +00:00
|
|
|
class TouchBar: NSTouchBar, NSTouchBarDelegate, EventSubscriber {
|
|
|
|
unowned let appHub: AppHub
|
|
|
|
var event: EventHelper? { get { return appHub.event } }
|
2024-03-24 01:02:29 +00:00
|
|
|
var input: InputHelper { get { return appHub.input } }
|
2024-03-17 16:12:09 +00:00
|
|
|
var configs: [NSTouchBarItem.Identifier:Config] = [:]
|
2024-04-27 15:47:19 +00:00
|
|
|
var observers: [NSKeyValueObservation] = []
|
2024-03-17 23:23:12 +00:00
|
|
|
var isPaused: Bool = false { didSet { updatePlayButton() } }
|
|
|
|
var position: Double = 0 { didSet { updateTouchBarTimeItems() } }
|
|
|
|
var duration: Double = 0 { didSet { updateTouchBarTimeItems() } }
|
2024-03-23 22:57:17 +00:00
|
|
|
var rate: Double = 1
|
2024-03-17 16:12:09 +00:00
|
|
|
|
2024-03-23 23:50:41 +00:00
|
|
|
init(_ appHub: AppHub) {
|
|
|
|
self.appHub = appHub
|
2024-03-17 16:12:09 +00:00
|
|
|
super.init()
|
|
|
|
|
|
|
|
configs = [
|
2024-03-18 00:39:03 +00:00
|
|
|
.seekBar: Config(name: "Seek Bar", command: "seek %f absolute-percent", handler: createSlider),
|
|
|
|
.currentPosition: Config(name: "Current Position", handler: createText),
|
|
|
|
.timeLeft: Config(name: "Time Left", handler: createText),
|
2024-03-17 16:12:09 +00:00
|
|
|
.play: Config(
|
|
|
|
name: "Play Button",
|
|
|
|
command: "cycle pause",
|
|
|
|
image: .init(named: NSImage.touchBarPauseTemplateName),
|
2024-03-18 00:39:03 +00:00
|
|
|
imageAlt: .init(named: NSImage.touchBarPlayTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
),
|
|
|
|
.previousItem: Config(
|
|
|
|
name: "Previous Playlist Item",
|
|
|
|
command: "playlist-prev",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarGoBackTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
),
|
|
|
|
.nextItem: Config(
|
|
|
|
name: "Next Playlist Item",
|
|
|
|
command: "playlist-next",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarGoForwardTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
),
|
|
|
|
.previousChapter: Config(
|
|
|
|
name: "Previous Chapter",
|
|
|
|
command: "add chapter -1",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarSkipBackTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
),
|
|
|
|
.nextChapter: Config(
|
|
|
|
name: "Next Chapter",
|
|
|
|
command: "add chapter 1",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarSkipAheadTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
),
|
|
|
|
.cycleAudio: Config(
|
|
|
|
name: "Cycle Audio",
|
|
|
|
command: "cycle audio",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarAudioInputTemplateName),
|
|
|
|
handler: createButton
|
|
|
|
),
|
2024-03-17 16:12:09 +00:00
|
|
|
.cycleSubtitle: Config(
|
|
|
|
name: "Cycle Subtitle",
|
|
|
|
command: "cycle sub",
|
2024-03-18 00:39:03 +00:00
|
|
|
image: .init(named: NSImage.touchBarComposeTemplateName),
|
|
|
|
handler: createButton
|
2024-03-17 16:12:09 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
delegate = self
|
|
|
|
customizationIdentifier = .customId;
|
|
|
|
defaultItemIdentifiers = [.play, .previousItem, .nextItem, .seekBar]
|
|
|
|
customizationAllowedItemIdentifiers = [.play, .seekBar, .previousItem, .nextItem,
|
|
|
|
.previousChapter, .nextChapter, .cycleAudio, .cycleSubtitle, .currentPosition, .timeLeft]
|
2024-04-27 15:47:19 +00:00
|
|
|
observers += [observe(\.isVisible, options: [.new]) { object, change in self.changed(visibility: change.newValue) }]
|
2024-03-23 23:50:41 +00:00
|
|
|
|
|
|
|
event?.subscribe(self, event: .init(name: "duration", format: MPV_FORMAT_DOUBLE))
|
|
|
|
event?.subscribe(self, event: .init(name: "time-pos", format: MPV_FORMAT_DOUBLE))
|
|
|
|
event?.subscribe(self, event: .init(name: "speed", format: MPV_FORMAT_DOUBLE))
|
|
|
|
event?.subscribe(self, event: .init(name: "pause", format: MPV_FORMAT_FLAG))
|
|
|
|
event?.subscribe(self, event: .init(name: "MPV_EVENT_END_FILE"))
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
2024-03-23 23:50:41 +00:00
|
|
|
fatalError("init(coder:) has not been implemented")
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
|
|
|
guard let config = configs[identifier] else { return nil }
|
|
|
|
|
2024-03-18 00:39:03 +00:00
|
|
|
let item = NSCustomTouchBarItem(identifier: identifier)
|
|
|
|
item.view = config.handler(config)
|
|
|
|
item.customizationLabel = config.name
|
|
|
|
configs[identifier]?.item = item
|
2024-04-27 15:47:19 +00:00
|
|
|
observers += [item.observe(\.isVisible, options: [.new]) { object, change in self.changed(visibility: change.newValue) }]
|
2024-03-18 00:39:03 +00:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy var createButton: ViewHandler = { config in
|
|
|
|
return NSButton(image: config.image, target: self, action: #selector(Self.buttonAction(_:)))
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy var createText: ViewHandler = { config in
|
|
|
|
let text = NSTextField(labelWithString: "0:00")
|
|
|
|
text.alignment = .center
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy var createSlider: ViewHandler = { config in
|
|
|
|
let slider = NSSlider(target: self, action: #selector(Self.seekbarChanged(_:)))
|
|
|
|
slider.minValue = 0
|
|
|
|
slider.maxValue = 100
|
|
|
|
return slider
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 15:47:19 +00:00
|
|
|
func changed(visibility: Bool?) {
|
|
|
|
if let visible = visibility, visible {
|
2024-03-17 16:12:09 +00:00
|
|
|
updateTouchBarTimeItems()
|
|
|
|
updatePlayButton()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTouchBarTimeItems() {
|
|
|
|
if !isVisible { return }
|
|
|
|
updateSlider()
|
|
|
|
updateTimeLeft()
|
|
|
|
updateCurrentPosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSlider() {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let config = configs[.seekBar], let slider = config.item?.view as? NSSlider else { return }
|
2024-03-17 16:12:09 +00:00
|
|
|
if !(config.item?.isVisible ?? false) { return }
|
|
|
|
|
2024-03-17 21:58:26 +00:00
|
|
|
slider.isEnabled = duration > 0
|
|
|
|
if !slider.isHighlighted {
|
|
|
|
slider.doubleValue = slider.isEnabled ? (position / duration) * 100 : 0
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTimeLeft() {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let config = configs[.timeLeft], let text = config.item?.view as? NSTextField else { return }
|
2024-03-17 16:12:09 +00:00
|
|
|
if !(config.item?.isVisible ?? false) { return }
|
|
|
|
|
|
|
|
removeConstraintFor(identifier: .timeLeft)
|
2024-03-17 21:58:26 +00:00
|
|
|
text.stringValue = duration > 0 ? "-" + format(time: Int(floor(duration) - floor(position))) : ""
|
|
|
|
if !text.stringValue.isEmpty {
|
|
|
|
applyConstraintFrom(string: "-" + format(time: Int(duration)), identifier: .timeLeft)
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateCurrentPosition() {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let config = configs[.currentPosition], let text = config.item?.view as? NSTextField else { return }
|
2024-03-17 16:12:09 +00:00
|
|
|
if !(config.item?.isVisible ?? false) { return }
|
|
|
|
|
|
|
|
text.stringValue = format(time: Int(floor(position)))
|
|
|
|
removeConstraintFor(identifier: .currentPosition)
|
2024-03-17 21:58:26 +00:00
|
|
|
applyConstraintFrom(string: format(time: Int(duration > 0 ? duration : position)), identifier: .currentPosition)
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updatePlayButton() {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let config = configs[.play], let button = config.item?.view as? NSButton else { return }
|
2024-03-17 16:12:09 +00:00
|
|
|
if !isVisible || !(config.item?.isVisible ?? false) { return }
|
2024-03-17 21:58:26 +00:00
|
|
|
button.image = isPaused ? configs[.play]?.imageAlt : configs[.play]?.image
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func buttonAction(_ button: NSButton) {
|
|
|
|
guard let identifier = getIdentifierFrom(view: button), let command = configs[identifier]?.command else { return }
|
2024-03-24 01:02:29 +00:00
|
|
|
input.command(command)
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func seekbarChanged(_ slider: NSSlider) {
|
|
|
|
guard let identifier = getIdentifierFrom(view: slider), let command = configs[identifier]?.command else { return }
|
2024-03-24 01:02:29 +00:00
|
|
|
input.command(String(format: command, slider.doubleValue))
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func format(time: Int) -> String {
|
2024-03-17 22:47:30 +00:00
|
|
|
let formatter = DateComponentsFormatter()
|
|
|
|
formatter.unitsStyle = .positional
|
|
|
|
formatter.zeroFormattingBehavior = time >= (60 * 60) ? [.dropLeading] : []
|
|
|
|
formatter.allowedUnits = time >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
|
|
|
|
return formatter.string(from: TimeInterval(time)) ?? "0:00"
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeConstraintFor(identifier: NSTouchBarItem.Identifier) {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let text = configs[identifier]?.item?.view as? NSTextField,
|
2024-03-17 16:12:09 +00:00
|
|
|
let constraint = configs[identifier]?.constraint as? NSLayoutConstraint else { return }
|
|
|
|
text.removeConstraint(constraint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyConstraintFrom(string: String, identifier: NSTouchBarItem.Identifier) {
|
2024-03-18 00:39:03 +00:00
|
|
|
guard let text = configs[identifier]?.item?.view as? NSTextField else { return }
|
2024-03-17 22:59:03 +00:00
|
|
|
let fullString = string.components(separatedBy: .decimalDigits).joined(separator: "0")
|
|
|
|
let textField = NSTextField(labelWithString: fullString)
|
2024-03-17 16:12:09 +00:00
|
|
|
let con = NSLayoutConstraint(item: text, attribute: .width, relatedBy: .equal, toItem: nil,
|
2024-03-17 22:59:03 +00:00
|
|
|
attribute: .notAnAttribute, multiplier: 1.1, constant: ceil(textField.frame.size.width))
|
2024-03-17 16:12:09 +00:00
|
|
|
text.addConstraint(con)
|
|
|
|
configs[identifier]?.constraint = con
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIdentifierFrom(view: NSView) -> NSTouchBarItem.Identifier? {
|
|
|
|
for (identifier, config) in configs {
|
2024-03-18 00:39:03 +00:00
|
|
|
if config.item?.view == view {
|
2024-03-17 16:12:09 +00:00
|
|
|
return identifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-23 23:50:41 +00:00
|
|
|
func handle(event: EventHelper.Event) {
|
|
|
|
switch event.name {
|
|
|
|
case "MPV_EVENT_END_FILE":
|
2024-03-17 16:12:09 +00:00
|
|
|
position = 0
|
|
|
|
duration = 0
|
2024-03-23 23:50:41 +00:00
|
|
|
case "time-pos":
|
|
|
|
let newPosition = max(event.double ?? 0, 0)
|
2024-03-17 16:51:05 +00:00
|
|
|
if Int((floor(newPosition) - floor(position)) / rate) != 0 {
|
2024-03-17 16:12:09 +00:00
|
|
|
position = newPosition
|
|
|
|
}
|
2024-03-23 23:50:41 +00:00
|
|
|
case "pause": isPaused = event.bool ?? false
|
|
|
|
case "duration": duration = event.double ?? 0
|
|
|
|
case "speed": rate = event.double ?? 1
|
|
|
|
default: break
|
2024-03-17 16:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|