From fc175be76bd24ccdcea297571c6366261270aad9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Mon, 11 Jul 2022 01:26:35 +0200 Subject: [PATCH] Lock orientation button --- Model/Player/PlayerModel.swift | 6 ++++- Shared/Player/Controls/PlayerControls.swift | 16 ++++++++++++ Shared/Player/VideoPlayerView.swift | 29 ++++++++++++++------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 23285de3..bf037fb5 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -93,6 +93,10 @@ final class PlayerModel: ObservableObject { backend.setNeedsNetworkStateUpdates(true) }} + #if os(iOS) + @Published var lockedOrientation: UIInterfaceOrientationMask? + #endif + var accounts: AccountsModel var comments: CommentsModel var controls: PlayerControlsModel { didSet { @@ -717,7 +721,7 @@ final class PlayerModel: ObservableObject { #if os(iOS) if !playingFullScreen { - Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait) + Orientation.lockOrientation(.allButUpsideDown) } #endif } diff --git a/Shared/Player/Controls/PlayerControls.swift b/Shared/Player/Controls/PlayerControls.swift index a9e9ab69..7f99fce0 100644 --- a/Shared/Player/Controls/PlayerControls.swift +++ b/Shared/Player/Controls/PlayerControls.swift @@ -207,6 +207,7 @@ struct PlayerControls: View { #if os(iOS) pipButton + lockOrientationButton #endif Spacer() @@ -262,6 +263,21 @@ struct PlayerControls: View { } } + #if os(iOS) + private var lockOrientationButton: some View { + button("Lock Rotation", systemImage: player.lockedOrientation.isNil ? "lock.rotation.open" : "lock.rotation", active: !player.lockedOrientation.isNil) { + if player.lockedOrientation.isNil { + let orientationMask = OrientationTracker.shared.currentInterfaceOrientationMask + player.lockedOrientation = orientationMask + Orientation.lockOrientation(orientationMask) + } else { + player.lockedOrientation = nil + Orientation.lockOrientation(.allButUpsideDown, andRotateTo: OrientationTracker.shared.currentInterfaceOrientation) + } + } + } + #endif + var floatingControls: some View { HStack { HStack(spacing: 20) { diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 13d57577..51415cca 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -112,6 +112,13 @@ struct VideoPlayerView: View { player?.onPresentPlayer?() player?.onPresentPlayer = nil } + + if let orientationMask = player.lockedOrientation { + Orientation.lockOrientation( + orientationMask, + andRotateTo: orientationMask == .landscapeLeft ? .landscapeLeft : orientationMask == .landscapeRight ? .landscapeRight : .portrait + ) + } } else { if Defaults[.lockPortraitWhenBrowsing] { Orientation.lockOrientation(.portrait, andRotateTo: .portrait) @@ -333,8 +340,12 @@ struct VideoPlayerView: View { guard drag > 0 else { return } - if drag > 60, player.playingFullScreen { + if drag > 60, + player.playingFullScreen, + !OrientationTracker.shared.currentInterfaceOrientation.isLandscape + { player.exitFullScreen() + player.lockedOrientation = nil } viewVerticalOffset = drag @@ -343,13 +354,9 @@ struct VideoPlayerView: View { guard player.presentingPlayer, !playerControls.presentingControlsOverlay else { return } if viewVerticalOffset > 100 { - if player.playingFullScreen { - viewVerticalOffset = 0 - player.exitFullScreen() - } else { - player.backend.setNeedsDrawing(false) - player.hide() - } + player.backend.setNeedsDrawing(false) + player.hide() + player.exitFullScreen() } else { viewVerticalOffset = 0 player.backend.setNeedsDrawing(true) @@ -467,7 +474,11 @@ struct VideoPlayerView: View { object: nil, queue: .main ) { _ in - guard !Defaults[.honorSystemOrientationLock], player.presentingPlayer, !player.playingInPictureInPicture else { + guard !Defaults[.honorSystemOrientationLock], + player.presentingPlayer, + !player.playingInPictureInPicture, + player.lockedOrientation.isNil + else { return }