From 562df2d9ba5fec48ac9b5a1ee002df944375f0bf Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 26 May 2023 22:49:38 +0200 Subject: [PATCH] Add advanced setting "Show video context menu options to force selected backend" --- Model/Player/PlayerModel.swift | 8 +++++++- Shared/Defaults.swift | 1 + Shared/Settings/AdvancedSettings.swift | 9 +++++++++ Shared/Views/VideoContextMenuView.swift | 23 +++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index aef8468a..f12641bb 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -55,6 +55,7 @@ final class PlayerModel: ObservableObject { @Published var presentingPlayer = false { didSet { handlePresentationChange() } } @Published var activeBackend = PlayerBackendType.mpv + @Published var forceBackendOnPlay: PlayerBackendType? var avPlayerBackend = AVPlayerBackend() var mpvBackend = MPVBackend() @@ -416,6 +417,10 @@ final class PlayerModel: ObservableObject { ) } + DispatchQueue.main.async { + self.forceBackendOnPlay = nil + } + if !upgrading { DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 0.5) { self.updateCurrentArtwork() @@ -452,7 +457,7 @@ final class PlayerModel: ObservableObject { return } - if let backend = (live && forceAVPlayerForLiveStreams) ? PlayerBackendType.appleAVPlayer : qualityProfile?.backend, + if let backend = forceBackendOnPlay ?? ((live && forceAVPlayerForLiveStreams) ? PlayerBackendType.appleAVPlayer : qualityProfile?.backend), backend != activeBackend, backend == .appleAVPlayer || !(avPlayerBackend.startPictureInPictureOnPlay || playingInPictureInPicture) { @@ -621,6 +626,7 @@ final class PlayerModel: ObservableObject { func closeCurrentItem(finished: Bool = false) { pause() videoBeingOpened = nil + forceBackendOnPlay = nil closing = true controls.presentingControls = false diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index 248af22c..27052dae 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -198,6 +198,7 @@ extension Defaults.Keys { #endif static let showMPVPlaybackStats = Key("showMPVPlaybackStats", default: false) + static let showPlayNowInBackendContextMenu = Key("showPlayNowInBackendContextMenu", default: false) #if os(macOS) static let playerDetailsPageButtonLabelStyleDefault = ButtonLabelStyle.iconAndText diff --git a/Shared/Settings/AdvancedSettings.swift b/Shared/Settings/AdvancedSettings.swift index 1f50c4f9..555e061d 100644 --- a/Shared/Settings/AdvancedSettings.swift +++ b/Shared/Settings/AdvancedSettings.swift @@ -8,6 +8,7 @@ struct AdvancedSettings: View { @Default(.mpvEnableLogging) private var mpvEnableLogging @Default(.showCacheStatus) private var showCacheStatus @Default(.feedCacheSize) private var feedCacheSize + @Default(.showPlayNowInBackendContextMenu) private var showPlayNowInBackendContextMenu @State private var filesToShare = [MPVClient.logFile] @State private var presentingShareSheet = false @@ -56,6 +57,10 @@ struct AdvancedSettings: View { } @ViewBuilder var advancedSettings: some View { + Section(header: SettingsHeader(text: "Advanced")) { + showPlayNowInBackendButtonsToggle + } + Section(header: SettingsHeader(text: "MPV"), footer: mpvFooter) { showMPVPlaybackStatsToggle #if !os(tvOS) @@ -115,6 +120,10 @@ struct AdvancedSettings: View { .foregroundColor(.secondary) } + var showPlayNowInBackendButtonsToggle: some View { + Toggle("Show video context menu options to force selected backend", isOn: $showPlayNowInBackendContextMenu) + } + var showMPVPlaybackStatsToggle: some View { Toggle("Show playback statistics", isOn: $showMPVPlaybackStats) } diff --git a/Shared/Views/VideoContextMenuView.swift b/Shared/Views/VideoContextMenuView.swift index c65efedc..5d3efa50 100644 --- a/Shared/Views/VideoContextMenuView.swift +++ b/Shared/Views/VideoContextMenuView.swift @@ -21,6 +21,7 @@ struct VideoContextMenuView: View { @FetchRequest private var watchRequest: FetchedResults @Default(.saveHistory) private var saveHistory + @Default(.showPlayNowInBackendContextMenu) private var showPlayNowInBackendContextMenu private var backgroundContext = PersistenceController.shared.container.newBackgroundContext() @@ -71,6 +72,14 @@ struct VideoContextMenuView: View { #endif } + if showPlayNowInBackendContextMenu { + Section { + ForEach(PlayerBackendType.allCases, id: \.self) { backend in + playNowInBackendButton(backend) + } + } + } + Section { playNextButton addToQueueButton @@ -187,6 +196,20 @@ struct VideoContextMenuView: View { } } + private func playNowInBackendButton(_ backend: PlayerBackendType) -> some View { + Button { + if player.musicMode { + player.toggleMusicMode() + } + + player.forceBackendOnPlay = backend + + player.play(video) + } label: { + Label("Play Now in \(backend.label)", systemImage: "play") + } + } + private var playNowInPictureInPictureButton: some View { Button { player.avPlayerBackend.startPictureInPictureOnPlay = true