From 8b724fb457636b33ae68d79e8c1ed34627e513e5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 29 May 2022 14:29:43 +0200 Subject: [PATCH] Minor improvements --- Model/Player/Backends/MPVBackend.swift | 2 + Shared/Navigation/ContentView.swift | 2 +- Shared/Player/VideoPlayerView.swift | 59 ++++++++++++++------------ 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 486d6ac4..1dca3342 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -251,9 +251,11 @@ final class MPVBackend: PlayerBackend { func enterFullScreen() { model.toggleFullscreen(controls?.playingFullscreen ?? false) + #if os(iOS) if Defaults[.lockLandscapeWhenEnteringFullscreen] { Orientation.lockOrientation(.landscape, andRotateTo: UIDevice.current.orientation.isLandscape ? nil : .landscapeRight) } + #endif } func exitFullScreen() {} diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 2847681d..035b6fce 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -57,7 +57,7 @@ struct ContentView: View { .environmentObject(subscriptions) .environmentObject(thumbnailsModel) - #if !os(macOS) + #if os(iOS) .overlay(videoPlayer) #endif diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 39fc970a..fe331b31 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -7,6 +7,12 @@ import Siesta import SwiftUI struct VideoPlayerView: View { + #if os(iOS) + static let hiddenPlayerOffset = UIScreen.main.bounds.height + 100 + #else + static let hiddenPlayerOffset = 0.0 + #endif + static let defaultAspectRatio = 16 / 9.0 static var defaultMinimumHeightLeft: Double { #if os(macOS) @@ -38,7 +44,7 @@ struct VideoPlayerView: View { #endif #if !os(macOS) - @State private var playerOffset = UIScreen.main.bounds.height + @State private var playerOffset = Self.hiddenPlayerOffset #endif @EnvironmentObject private var accounts @@ -71,6 +77,27 @@ struct VideoPlayerView: View { handleOrientationDidChangeNotification() } #endif + .onChange(of: player.presentingPlayer) { newValue in + if newValue { + playerOffset = 0 + #if os(iOS) + configureOrientationUpdatesBasedOnAccelerometer() + #endif + } else { + #if os(iOS) + if Defaults[.lockPortraitWhenBrowsing] { + Orientation.lockOrientation(.portrait, andRotateTo: .portrait) + } else { + Orientation.lockOrientation(.allButUpsideDown) + } + + motionManager?.stopAccelerometerUpdates() + motionManager = nil + #else + playerOffset = Self.hiddenPlayerOffset + #endif + } + } } .offset(y: playerOffset) .animation(.easeIn(duration: 0.2), value: playerOffset) @@ -126,30 +153,7 @@ struct VideoPlayerView: View { hoveringPlayer = hovering hovering ? playerControls.show() : playerControls.hide() } - #if !os(tvOS) - .onChange(of: player.presentingPlayer) { newValue in - if newValue { - playerOffset = 0 - #if os(iOS) - configureOrientationUpdatesBasedOnAccelerometer() - #endif - } else { - #if !os(macOS) - playerOffset = UIScreen.main.bounds.height - - #if os(iOS) - if Defaults[.lockPortraitWhenBrowsing] { - Orientation.lockOrientation(.portrait, andRotateTo: .portrait) - } else { - Orientation.lockOrientation(.allButUpsideDown) - } - - motionManager?.stopAccelerometerUpdates() - motionManager = nil - #endif - #endif - } - } + #if !os(macOS) .gesture( DragGesture(minimumDistance: 0) .onChanged { value in @@ -178,8 +182,7 @@ struct VideoPlayerView: View { } } ) - #endif - #if os(macOS) + #else .onAppear(perform: { NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { if hoveringPlayer { @@ -463,7 +466,7 @@ struct VideoPlayerView: View { } private func handleOrientationDidChangeNotification() { - playerOffset = playerOffset == 0 ? 0 : UIScreen.main.bounds.height + playerOffset = playerOffset == 0 ? 0 : Self.hiddenPlayerOffset let newOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation if newOrientation?.isLandscape ?? false, player.presentingPlayer,