From 119c663436a58e2488572aff4ca8935df38fdc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Sun, 1 Sep 2024 01:54:24 +0200 Subject: [PATCH] Gestures: swipe up toggles fullscreen --- Shared/Player/PlayerDragGesture.swift | 50 ++++++++++++++------------- Shared/Player/VideoPlayerView.swift | 13 +++++-- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Shared/Player/PlayerDragGesture.swift b/Shared/Player/PlayerDragGesture.swift index 39e098e7..64975eb4 100644 --- a/Shared/Player/PlayerDragGesture.swift +++ b/Shared/Player/PlayerDragGesture.swift @@ -8,7 +8,7 @@ extension VideoPlayerView { .updating($dragGestureOffset) { value, state, _ in guard isVerticalDrag else { return } var translation = value.translation - translation.height = max(0, translation.height) + translation.height = max(-translation.height, translation.height) state = translation } #endif @@ -18,7 +18,8 @@ extension VideoPlayerView { .onChanged { value in guard player.presentingPlayer, !controlsOverlayModel.presenting, - dragGestureState else { return } + dragGestureState, + !disableToggleGesture else { return } if player.controls.presentingControls, !player.musicMode { player.controls.presentingControls = false @@ -61,19 +62,22 @@ extension VideoPlayerView { return } - guard verticalDrag > 0 else { return } - viewDragOffset = verticalDrag - - if verticalDrag > 60, - player.playingFullScreen - { - player.exitFullScreen(showControls: false) - #if os(iOS) - if Constants.isIPhone { - Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait) - } - #endif + // Toggle fullscreen on upward drag only when not disabled + if verticalDrag < -50 { + if player.playingFullScreen { + player.exitFullScreen(showControls: false) + } else { + player.enterFullScreen() + } + disableGestureTemporarily() + return } + + // Ignore downward swipes when in fullscreen + guard verticalDrag > 0 && !player.playingFullScreen else { + return + } + viewDragOffset = verticalDrag } .onEnded { _ in onPlayerDragGestureEnded() @@ -86,16 +90,6 @@ extension VideoPlayerView { player.seek.onSeekGestureEnd() } - if viewDragOffset > 60, - player.playingFullScreen - { - #if os(iOS) - player.lockedOrientation = nil - #endif - player.exitFullScreen(showControls: false) - viewDragOffset = 0 - return - } isVerticalDrag = false guard player.presentingPlayer, @@ -117,4 +111,12 @@ extension VideoPlayerView { } } } + + // Function to temporarily disable the toggle gesture after a fullscreen change + private func disableGestureTemporarily() { + disableToggleGesture = true + DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { + disableToggleGesture = false + } + } } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 437317cf..6127a076 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -47,11 +47,18 @@ struct VideoPlayerView: View { #if !os(tvOS) @GestureState var dragGestureState = false @GestureState var dragGestureOffset = CGSize.zero - @State var isHorizontalDrag = false // swiftlint:disable:this swiftui_state_private - @State var isVerticalDrag = false // swiftlint:disable:this swiftui_state_private - @State var viewDragOffset = Self.hiddenOffset // swiftlint:disable:this swiftui_state_private + // swiftlint:disable private_swiftui_state + @State var isHorizontalDrag = false + @State var isVerticalDrag = false + @State var viewDragOffset = Self.hiddenOffset + // swiftlint:enable private_swiftui_state + #endif + // swiftlint:disable private_swiftui_state + @State var disableToggleGesture = false + // swiftlint:enable private_swiftui_state + @ObservedObject var player = PlayerModel.shared // swiftlint:disable:this swiftui_state_private #if os(macOS)