2022-06-18 12:39:49 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ControlsOverlay: View {
|
2022-06-24 23:39:29 +00:00
|
|
|
@EnvironmentObject<NetworkStateModel> private var networkState
|
2022-06-18 12:39:49 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
@EnvironmentObject<PlayerControlsModel> private var model
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
@State private var contentSize: CGSize = .zero
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
|
2022-08-14 17:06:22 +00:00
|
|
|
@Default(.qualityProfiles) private var qualityProfiles
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
enum Field: Hashable {
|
|
|
|
case qualityProfile
|
|
|
|
case stream
|
|
|
|
case increaseRate
|
|
|
|
case decreaseRate
|
|
|
|
case captions
|
|
|
|
}
|
|
|
|
|
|
|
|
@FocusState private var focusedField: Field?
|
2022-08-14 22:17:00 +00:00
|
|
|
@State private var presentingButtonHintAlert = false
|
2022-08-14 17:06:22 +00:00
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2022-07-05 17:20:25 +00:00
|
|
|
ScrollView {
|
2022-08-14 17:06:22 +00:00
|
|
|
VStack {
|
|
|
|
Section(header: controlsHeader("Rate & Captions")) {
|
|
|
|
HStack(spacing: rateButtonsSpacing) {
|
|
|
|
decreaseRateButton
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .decreaseRate)
|
|
|
|
#endif
|
|
|
|
rateButton
|
|
|
|
increaseRateButton
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .increaseRate)
|
|
|
|
#endif
|
|
|
|
}
|
2022-07-11 16:30:12 +00:00
|
|
|
|
|
|
|
captionsButton
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .captions)
|
|
|
|
#endif
|
|
|
|
.disabled(player.activeBackend != .mpv)
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
.foregroundColor(.white)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Section(header: controlsHeader("Quality Profile")) {
|
|
|
|
qualityProfileButton
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .qualityProfile)
|
|
|
|
#endif
|
2022-07-11 16:30:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 21:05:40 +00:00
|
|
|
Section(header: controlsHeader("Stream & Player")) {
|
2022-08-14 17:06:22 +00:00
|
|
|
qualityButton
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .stream)
|
|
|
|
#endif
|
|
|
|
|
2022-08-20 21:05:40 +00:00
|
|
|
HStack(spacing: 8) {
|
|
|
|
backendButtons
|
|
|
|
}
|
2022-07-05 17:20:25 +00:00
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2022-07-05 17:20:25 +00:00
|
|
|
if player.activeBackend == .mpv,
|
|
|
|
showMPVPlaybackStats
|
|
|
|
{
|
2022-08-14 17:06:22 +00:00
|
|
|
Section(header: controlsHeader("Statistics")) {
|
|
|
|
mpvPlaybackStats
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.frame(width: 400)
|
|
|
|
#else
|
|
|
|
.frame(width: 240)
|
|
|
|
#endif
|
2022-07-05 17:20:25 +00:00
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.overlay(
|
|
|
|
GeometryReader { geometry in
|
|
|
|
Color.clear.onAppear {
|
|
|
|
contentSize = geometry.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.horizontal, 40)
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(maxHeight: overlayHeight)
|
|
|
|
#if os(tvOS)
|
2022-08-14 22:17:00 +00:00
|
|
|
.alert(isPresented: $presentingButtonHintAlert) {
|
|
|
|
Alert(title: Text("Press and hold to open this menu"))
|
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.onAppear {
|
|
|
|
focusedField = .qualityProfile
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var overlayHeight: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
contentSize.height + 50.0
|
|
|
|
#else
|
|
|
|
contentSize.height
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private func controlsHeader(_ text: String) -> some View {
|
|
|
|
Text(text)
|
|
|
|
.font(.system(.caption))
|
|
|
|
.foregroundColor(.secondary)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var backendButtons: some View {
|
|
|
|
ForEach(PlayerBackendType.allCases, id: \.self) { backend in
|
|
|
|
backendButton(backend)
|
2022-08-20 21:05:40 +00:00
|
|
|
#if !os(tvOS)
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(height: 40)
|
2022-08-20 21:05:40 +00:00
|
|
|
#endif
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(iOS)
|
2022-08-20 21:05:40 +00:00
|
|
|
.frame(maxWidth: 115)
|
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 4))
|
2022-08-14 17:06:22 +00:00
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func backendButton(_ backend: PlayerBackendType) -> some View {
|
|
|
|
Button {
|
|
|
|
player.saveTime {
|
|
|
|
player.changeActiveBackend(from: player.activeBackend, to: backend)
|
|
|
|
model.resetTimer()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Text(backend.label)
|
|
|
|
.foregroundColor(player.activeBackend == backend ? .accentColor : .secondary)
|
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private var rateButton: some View {
|
|
|
|
#if os(macOS)
|
|
|
|
ratePicker
|
|
|
|
.labelsHidden()
|
|
|
|
.frame(maxWidth: 100)
|
|
|
|
#elseif os(iOS)
|
|
|
|
Menu {
|
|
|
|
ratePicker
|
|
|
|
} label: {
|
|
|
|
Text(player.rateLabel(player.currentRate))
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
.frame(width: 123)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
.frame(width: 123, height: 40)
|
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 3))
|
|
|
|
#else
|
|
|
|
Text(player.rateLabel(player.currentRate))
|
|
|
|
.frame(minWidth: 120)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var ratePicker: some View {
|
|
|
|
Picker("Rate", selection: $player.currentRate) {
|
|
|
|
ForEach(PlayerModel.availableRates, id: \.self) { rate in
|
|
|
|
Text(player.rateLabel(rate)).tag(rate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var increaseRateButton: some View {
|
|
|
|
let increasedRate = PlayerModel.availableRates.first { $0 > player.currentRate }
|
|
|
|
return Button {
|
|
|
|
if let rate = increasedRate {
|
|
|
|
player.currentRate = rate
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Increase rate", systemImage: "plus")
|
2022-08-06 14:22:41 +00:00
|
|
|
.foregroundColor(.primary)
|
2022-06-18 12:39:49 +00:00
|
|
|
.labelStyle(.iconOnly)
|
2022-06-25 17:05:08 +00:00
|
|
|
.padding(8)
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 50, height: 40)
|
2022-06-18 12:39:49 +00:00
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.buttonStyle(.bordered)
|
2022-08-14 17:06:22 +00:00
|
|
|
#elseif os(iOS)
|
2022-06-18 12:39:49 +00:00
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 4))
|
|
|
|
#endif
|
|
|
|
.disabled(increasedRate.isNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var decreaseRateButton: some View {
|
|
|
|
let decreasedRate = PlayerModel.availableRates.last { $0 < player.currentRate }
|
|
|
|
|
|
|
|
return Button {
|
|
|
|
if let rate = decreasedRate {
|
|
|
|
player.currentRate = rate
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Decrease rate", systemImage: "minus")
|
2022-08-06 14:22:41 +00:00
|
|
|
.foregroundColor(.primary)
|
2022-06-18 12:39:49 +00:00
|
|
|
.labelStyle(.iconOnly)
|
2022-06-25 17:05:08 +00:00
|
|
|
.padding(8)
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 50, height: 40)
|
2022-06-18 12:39:49 +00:00
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.buttonStyle(.bordered)
|
2022-08-14 17:06:22 +00:00
|
|
|
#elseif os(iOS)
|
2022-06-18 12:39:49 +00:00
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 4))
|
|
|
|
#endif
|
|
|
|
.disabled(decreasedRate.isNil)
|
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
private var rateButtonsSpacing: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
10
|
|
|
|
#else
|
|
|
|
8
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private var qualityProfileButton: some View {
|
|
|
|
#if os(macOS)
|
|
|
|
qualityProfilePicker
|
|
|
|
.labelsHidden()
|
|
|
|
.frame(maxWidth: 300)
|
|
|
|
#elseif os(iOS)
|
|
|
|
Menu {
|
|
|
|
qualityProfilePicker
|
|
|
|
} label: {
|
2022-08-21 14:28:30 +00:00
|
|
|
Text(player.qualityProfileSelection?.description ?? "Automatic")
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(maxWidth: 240)
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
.frame(maxWidth: 240)
|
|
|
|
.frame(height: 40)
|
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 3))
|
|
|
|
#else
|
2022-08-14 22:17:00 +00:00
|
|
|
Button {
|
|
|
|
presentingButtonHintAlert = true
|
|
|
|
} label: {
|
2022-08-21 14:28:30 +00:00
|
|
|
Text(player.qualityProfileSelection?.description ?? "Automatic")
|
2022-08-14 17:06:22 +00:00
|
|
|
.lineLimit(1)
|
|
|
|
.frame(maxWidth: 320)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
2022-08-14 22:17:00 +00:00
|
|
|
Button("Automatic") { player.qualityProfileSelection = nil }
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
ForEach(qualityProfiles) { qualityProfile in
|
|
|
|
Button {
|
|
|
|
player.qualityProfileSelection = qualityProfile
|
|
|
|
} label: {
|
|
|
|
Text(qualityProfile.description)
|
|
|
|
}
|
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var qualityProfilePicker: some View {
|
|
|
|
Picker("Quality Profile", selection: $player.qualityProfileSelection) {
|
|
|
|
Text("Automatic").tag(QualityProfile?.none)
|
|
|
|
ForEach(qualityProfiles) { qualityProfile in
|
|
|
|
Text(qualityProfile.description).tag(qualityProfile as QualityProfile?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@ViewBuilder private var qualityButton: some View {
|
|
|
|
#if os(macOS)
|
|
|
|
StreamControl()
|
|
|
|
.labelsHidden()
|
|
|
|
.frame(maxWidth: 300)
|
|
|
|
#elseif os(iOS)
|
|
|
|
Menu {
|
|
|
|
StreamControl()
|
|
|
|
} label: {
|
2022-08-21 14:28:30 +00:00
|
|
|
Text(player.streamSelection?.resolutionAndFormat ?? "loading")
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 140, height: 40)
|
2022-06-18 12:39:49 +00:00
|
|
|
.foregroundColor(.primary)
|
|
|
|
}
|
2022-06-25 17:05:08 +00:00
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundColor(.primary)
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 240, height: 40)
|
2022-08-06 13:26:59 +00:00
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 3))
|
2022-08-14 17:06:22 +00:00
|
|
|
#else
|
2022-08-14 22:17:00 +00:00
|
|
|
StreamControl(presentingButtonHintAlert: $presentingButtonHintAlert)
|
2022-06-18 12:39:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:20:25 +00:00
|
|
|
@ViewBuilder private var captionsButton: some View {
|
|
|
|
#if os(macOS)
|
|
|
|
captionsPicker
|
|
|
|
.labelsHidden()
|
|
|
|
.frame(maxWidth: 300)
|
2022-07-05 17:26:03 +00:00
|
|
|
#elseif os(iOS)
|
2022-07-05 17:20:25 +00:00
|
|
|
Menu {
|
|
|
|
captionsPicker
|
|
|
|
} label: {
|
|
|
|
HStack(spacing: 4) {
|
|
|
|
Image(systemName: "text.bubble")
|
|
|
|
if let captions = captionsBinding.wrappedValue {
|
|
|
|
Text(captions.code)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 240)
|
|
|
|
.frame(height: 40)
|
2022-07-05 17:20:25 +00:00
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundColor(.primary)
|
2022-08-14 17:06:22 +00:00
|
|
|
.frame(width: 240)
|
2022-07-05 17:20:25 +00:00
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 3))
|
2022-08-14 17:06:22 +00:00
|
|
|
#else
|
2022-08-14 22:17:00 +00:00
|
|
|
Button {
|
|
|
|
presentingButtonHintAlert = true
|
|
|
|
} label: {
|
2022-08-14 17:06:22 +00:00
|
|
|
HStack(spacing: 8) {
|
|
|
|
Image(systemName: "text.bubble")
|
|
|
|
if let captions = captionsBinding.wrappedValue {
|
|
|
|
Text(captions.code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 320)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(player.currentVideo?.captions ?? []) { caption in
|
|
|
|
Button(caption.description) { captionsBinding.wrappedValue = caption }
|
|
|
|
}
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:20:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private var captionsPicker: some View {
|
|
|
|
let captions = player.currentVideo?.captions ?? []
|
|
|
|
Picker("Captions", selection: captionsBinding) {
|
|
|
|
if captions.isEmpty {
|
|
|
|
Text("Not available")
|
|
|
|
} else {
|
|
|
|
Text("Disabled").tag(Captions?.none)
|
|
|
|
}
|
|
|
|
ForEach(captions) { caption in
|
|
|
|
Text(caption.description).tag(Optional(caption))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(captions.isEmpty)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var captionsBinding: Binding<Captions?> {
|
|
|
|
.init(
|
|
|
|
get: { player.mpvBackend.captions },
|
|
|
|
set: {
|
|
|
|
player.mpvBackend.captions = $0
|
|
|
|
Defaults[.captionsLanguageCode] = $0?.code
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
var mpvPlaybackStats: some View {
|
|
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
|
|
mpvPlaybackStatRow("Hardware decoder", player.mpvBackend.hwDecoder)
|
|
|
|
mpvPlaybackStatRow("Dropped frames", String(player.mpvBackend.frameDropCount))
|
|
|
|
mpvPlaybackStatRow("Stream FPS", String(format: "%.2ffps", player.mpvBackend.outputFps))
|
|
|
|
mpvPlaybackStatRow("Cached time", String(format: "%.2fs", player.mpvBackend.cacheDuration))
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.padding(.top, 2)
|
|
|
|
#if os(tvOS)
|
|
|
|
.font(.system(size: 20))
|
|
|
|
#else
|
|
|
|
.font(.system(size: 11))
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
func mpvPlaybackStatRow(_ label: String, _ value: String) -> some View {
|
|
|
|
HStack {
|
|
|
|
Text(label)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
Spacer()
|
|
|
|
Text(value)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ControlsOverlay_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
ControlsOverlay()
|
2022-07-05 17:20:25 +00:00
|
|
|
.environmentObject(NetworkStateModel())
|
2022-06-18 12:39:49 +00:00
|
|
|
.environmentObject(PlayerModel())
|
|
|
|
.environmentObject(PlayerControlsModel())
|
|
|
|
}
|
|
|
|
}
|