From a2e2702ca1d3af5b24a53c3865bd01d60aa04563 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 7 Aug 2022 13:15:27 +0200 Subject: [PATCH] Add setting "Close player when closing video" --- Model/Player/PlayerModel.swift | 12 ++++++++++++ Shared/Defaults.swift | 1 + Shared/Player/Controls/PlayerControls.swift | 17 +++++------------ Shared/Player/VideoPlayerView.swift | 2 +- Shared/Settings/PlayerSettings.swift | 8 +++++++- Shared/Views/ControlsBar.swift | 1 - 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index cd3ac275..d65a70a8 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -150,6 +150,7 @@ final class PlayerModel: ObservableObject { #if !os(macOS) @Default(.closePiPAndOpenPlayerOnEnteringForeground) var closePiPAndOpenPlayerOnEnteringForeground + @Default(.closePlayerOnItemClose) private var closePlayerOnItemClose #endif private var currentArtwork: MPMediaItemArtwork? @@ -524,12 +525,23 @@ final class PlayerModel: ObservableObject { } func closeCurrentItem(finished: Bool = false) { + pause() + prepareCurrentItemForHistory(finished: finished) currentItem = nil backend.closeItem() aspectRatio = VideoPlayerView.defaultAspectRatio resetAutoplay() + + closePiP() + exitFullScreen() + + #if !os(macOS) + if closePlayerOnItemClose { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in self?.hide() } + } + #endif } func closePiP() { diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index 5d538cb8..9bcb6601 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -63,6 +63,7 @@ extension Defaults.Keys { static let pauseOnEnteringBackground = Key("pauseOnEnteringBackground", default: true) #endif static let closeLastItemOnPlaybackEnd = Key("closeLastItemOnPlaybackEnd", default: false) + static let closePlayerOnItemClose = Key("closePlayerOnItemClose", default: false) static let closePiPOnNavigation = Key("closePiPOnNavigation", default: false) static let closePiPOnOpeningPlayer = Key("closePiPOnOpeningPlayer", default: false) diff --git a/Shared/Player/Controls/PlayerControls.swift b/Shared/Player/Controls/PlayerControls.swift index bbb6df88..77aebffc 100644 --- a/Shared/Player/Controls/PlayerControls.swift +++ b/Shared/Player/Controls/PlayerControls.swift @@ -23,6 +23,10 @@ struct PlayerControls: View { @FocusState private var focusedField: Field? #endif + #if !os(macOS) + @Default(.closePlayerOnItemClose) private var closePlayerOnItemClose + #endif + init(player: PlayerModel, thumbnails: ThumbnailsModel) { self.player = player self.thumbnails = thumbnails @@ -242,18 +246,7 @@ struct PlayerControls: View { private var closeVideoButton: some View { button("Close", systemImage: "xmark") { - player.pause() - - player.hide() - player.closePiP() - - var delay = 0.2 - #if os(macOS) - delay = 0.0 - #endif - DispatchQueue.main.asyncAfter(deadline: .now() + delay) { - player.closeCurrentItem() - } + player.closeCurrentItem() } } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 688a8838..eaa23708 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -299,7 +299,7 @@ struct VideoPlayerView: View { @ViewBuilder var playerPlaceholder: some View { if player.currentItem.isNil { - ZStack(alignment: .topLeading) { + ZStack(alignment: .topTrailing) { HStack { Spacer() VStack { diff --git a/Shared/Settings/PlayerSettings.swift b/Shared/Settings/PlayerSettings.swift index 58596bab..0ff0ab6d 100644 --- a/Shared/Settings/PlayerSettings.swift +++ b/Shared/Settings/PlayerSettings.swift @@ -18,6 +18,7 @@ struct PlayerSettings: View { @Default(.closePiPOnNavigation) private var closePiPOnNavigation @Default(.closePiPOnOpeningPlayer) private var closePiPOnOpeningPlayer #if !os(macOS) + @Default(.closePlayerOnItemClose) private var closePlayerOnItemClose @Default(.pauseOnEnteringBackground) private var pauseOnEnteringBackground @Default(.closePiPAndOpenPlayerOnEnteringForeground) private var closePiPAndOpenPlayerOnEnteringForeground #endif @@ -61,6 +62,7 @@ struct PlayerSettings: View { pauseOnHidingPlayerToggle #if !os(macOS) pauseOnEnteringBackgroundToogle + closePlayerOnItemCloseToggle #endif closeLastItemOnPlaybackEndToggle systemControlsCommandsPicker @@ -175,6 +177,10 @@ struct PlayerSettings: View { private var pauseOnEnteringBackgroundToogle: some View { Toggle("Pause when entering background", isOn: $pauseOnEnteringBackground) } + + private var closePlayerOnItemCloseToggle: some View { + Toggle("Close player when closing video", isOn: $closePlayerOnItemClose) + } #endif private var closeLastItemOnPlaybackEndToggle: some View { @@ -207,7 +213,7 @@ struct PlayerSettings: View { #endif } -struct PlaybackSettings_Previews: PreviewProvider { +struct PlayerSettings_Previews: PreviewProvider { static var previews: some View { VStack(alignment: .leading) { PlayerSettings() diff --git a/Shared/Views/ControlsBar.swift b/Shared/Views/ControlsBar.swift index 3cf75527..bfdcf0d2 100644 --- a/Shared/Views/ControlsBar.swift +++ b/Shared/Views/ControlsBar.swift @@ -113,7 +113,6 @@ struct ControlsBar: View { Button { model.closeCurrentItem() - model.closePiP() } label: { Label("Close Video", systemImage: "xmark") .padding(.vertical, 10)