1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Model/Player/PlayerTVMenu.swift

100 lines
3.4 KiB
Swift
Raw Normal View History

2021-10-23 22:19:45 +05:30
import Foundation
#if !os(macOS)
import UIKit
#endif
extension PlayerModel {
#if os(tvOS)
var streamsMenu: UIMenu {
UIMenu(
title: "Streams",
image: UIImage(systemName: "antenna.radiowaves.left.and.right"),
children: streamsMenuActions
)
}
var streamsMenuActions: [UIAction] {
guard !availableStreams.isEmpty else {
return [ // swiftlint:disable:this implicit_return
UIAction(title: "Empty", attributes: .disabled) { _ in }
]
}
2022-08-21 02:35:40 +05:30
return availableStreamsSorted.filter { backend.canPlay($0) }.map { stream in
2021-10-23 22:19:45 +05:30
let state = stream == streamSelection ? UIAction.State.on : .off
return UIAction(title: stream.description, state: state) { _ in
self.streamSelection = stream
self.upgradeToStream(stream)
}
}
}
var restoreLastSkippedSegmentAction: UIAction? {
guard let segment = lastSkipped else {
return nil // swiftlint:disable:this implicit_return
}
return UIAction(
2021-10-24 18:01:10 +05:30
title: "Restore \(segment.title())",
2021-10-23 22:19:45 +05:30
image: UIImage(systemName: "arrow.uturn.left.circle")
) { _ in
self.restoreLastSkippedSegment()
}
}
2021-11-02 22:54:59 +05:30
2022-08-21 02:35:40 +05:30
var switchToMPVAction: UIAction? {
UIAction(title: "Switch to MPV", image: UIImage(systemName: "m.circle")) { _ in
self.avPlayerBackend.controller?.dismiss(animated: false)
self.changeActiveBackend(from: .appleAVPlayer, to: .mpv)
}
}
2021-11-02 22:54:59 +05:30
private var rateMenu: UIMenu {
UIMenu(title: "Playback rate", image: UIImage(systemName: rateMenuSystemImage), children: rateMenuActions)
}
private var rateMenuSystemImage: String {
[0.0, 1.0].contains(currentRate) ? "speedometer" : (currentRate < 1.0 ? "tortoise.fill" : "hare.fill")
}
private var rateMenuActions: [UIAction] {
2022-11-11 03:49:34 +05:30
PlayerModel.shared.backend.suggestedPlaybackRates.map { rate in
let image = currentRate == rate ? UIImage(systemName: "checkmark") : nil
2021-11-02 22:54:59 +05:30
return UIAction(title: rateLabel(rate), image: image) { _ in
DispatchQueue.main.async {
self.currentRate = rate
}
}
}
}
private var playbackModeMenu: UIMenu {
UIMenu(title: "Playback Mode", image: UIImage(systemName: playbackMode.systemImage), children: playbackModeMenuActions)
}
private var playbackModeMenuActions: [UIAction] {
PlaybackMode.allCases.map { mode in
UIAction(title: mode.description, image: UIImage(systemName: mode.systemImage)) { _ in
DispatchQueue.main.async {
self.playbackMode = mode
}
}
}
}
2021-10-23 22:19:45 +05:30
#endif
func rebuildTVMenu() {
#if os(tvOS)
2022-03-27 17:12:20 +05:30
avPlayerBackend.controller?.playerView.transportBarCustomMenuItems = [
2021-10-23 22:19:45 +05:30
restoreLastSkippedSegmentAction,
2021-11-02 22:54:59 +05:30
rateMenu,
2022-08-21 02:35:40 +05:30
streamsMenu,
playbackModeMenu,
2022-08-21 02:35:40 +05:30
switchToMPVAction
2021-10-23 22:19:45 +05:30
].compactMap { $0 }
#endif
}
}