mac/touchbar: simplify update items logic

This commit is contained in:
der richter 2024-03-17 22:58:26 +01:00
parent 647bf5d0ba
commit be49f4fe20
1 changed files with 8 additions and 27 deletions

View File

@ -208,14 +208,9 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate {
guard let config = configs[.seekBar], let slider = config.view as? NSSlider else { return } guard let config = configs[.seekBar], let slider = config.view as? NSSlider else { return }
if !(config.item?.isVisible ?? false) { return } if !(config.item?.isVisible ?? false) { return }
if duration <= 0 { slider.isEnabled = duration > 0
slider.isEnabled = false if !slider.isHighlighted {
slider.doubleValue = 0 slider.doubleValue = slider.isEnabled ? (position / duration) * 100 : 0
} else {
slider.isEnabled = true
if (!slider.isHighlighted) {
slider.doubleValue = (position / duration) * 100
}
} }
} }
@ -224,14 +219,9 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate {
if !(config.item?.isVisible ?? false) { return } if !(config.item?.isVisible ?? false) { return }
removeConstraintFor(identifier: .timeLeft) removeConstraintFor(identifier: .timeLeft)
if duration <= 0 { text.stringValue = duration > 0 ? "-" + format(time: Int(floor(duration) - floor(position))) : ""
text.stringValue = "" if !text.stringValue.isEmpty {
} else { applyConstraintFrom(string: "-" + format(time: Int(duration)), identifier: .timeLeft)
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)
} }
} }
@ -241,22 +231,13 @@ class TouchBar: NSTouchBar, NSTouchBarDelegate {
text.stringValue = format(time: Int(floor(position))) text.stringValue = format(time: Int(floor(position)))
removeConstraintFor(identifier: .currentPosition) removeConstraintFor(identifier: .currentPosition)
if duration <= 0 { applyConstraintFrom(string: format(time: Int(duration > 0 ? duration : position)), identifier: .currentPosition)
applyConstraintFrom(string: format(time: Int(position)), identifier: .currentPosition)
} else {
applyConstraintFrom(string: format(time: Int(duration)), identifier: .currentPosition)
}
} }
func updatePlayButton() { func updatePlayButton() {
guard let config = configs[.play], let button = config.view as? NSButton else { return } guard let config = configs[.play], let button = config.view as? NSButton else { return }
if !isVisible || !(config.item?.isVisible ?? false) { return } if !isVisible || !(config.item?.isVisible ?? false) { return }
button.image = isPaused ? configs[.play]?.imageAlt : configs[.play]?.image
if isPaused {
button.image = configs[.play]?.imageAlt
} else {
button.image = configs[.play]?.image
}
} }
@objc func buttonAction(_ button: NSButton) { @objc func buttonAction(_ button: NSButton) {