1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Player/PlayerBackendView.swift

108 lines
3.8 KiB
Swift
Raw Normal View History

2023-05-21 02:19:10 +05:30
import Defaults
2022-08-06 18:57:34 +05:30
import SwiftUI
struct PlayerBackendView: View {
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
2023-05-21 03:48:10 +05:30
@ObservedObject private var safeAreaModel = SafeAreaModel.shared
2022-08-06 18:57:34 +05:30
#endif
@ObservedObject private var player = PlayerModel.shared
2022-08-06 18:57:34 +05:30
2023-05-21 02:19:10 +05:30
@Default(.avPlayerUsesSystemControls) private var avPlayerUsesSystemControls
2022-08-06 18:57:34 +05:30
var body: some View {
ZStack(alignment: .top) {
Group {
2022-12-21 03:51:12 +05:30
ZStack {
Group {
switch player.activeBackend {
case .mpv:
player.mpvPlayerView
case .appleAVPlayer:
2023-05-21 02:19:10 +05:30
#if os(tvOS)
AppleAVPlayerView()
#else
if avPlayerUsesSystemControls,
!player.playingInPictureInPicture,
!player.avPlayerBackend.isStartingPiP
{
AppleAVPlayerView()
} else if !avPlayerUsesSystemControls ||
2023-05-23 02:18:11 +05:30
player.playingInPictureInPicture ||
2023-05-21 02:19:10 +05:30
player.avPlayerBackend.isStartingPiP
{
AppleAVPlayerLayerView()
}
#endif
2022-12-21 03:51:12 +05:30
}
}
.zIndex(0)
2022-08-06 18:57:34 +05:30
}
}
.overlay(GeometryReader { proxy in
Color.clear
2022-08-13 20:16:45 +05:30
.onAppear { player.playerSize = proxy.size }
2022-08-06 18:57:34 +05:30
.onChange(of: proxy.size) { _ in player.playerSize = proxy.size }
.onChange(of: player.controls.presentingOverlays) { _ in player.playerSize = proxy.size }
})
#if !os(tvOS)
2023-05-21 02:19:10 +05:30
if player.activeBackend == .mpv || !avPlayerUsesSystemControls {
PlayerGestures()
}
PlayerControls()
#if os(iOS)
.padding(.top, controlsTopPadding)
.padding(.bottom, controlsBottomPadding)
#endif
#else
hiddenControlsButton
2022-08-06 18:57:34 +05:30
#endif
}
#if os(iOS)
2022-09-02 04:35:31 +05:30
.statusBarHidden(player.playingFullScreen)
2022-08-06 18:57:34 +05:30
#endif
}
#if os(iOS)
var controlsTopPadding: Double {
2022-09-02 04:35:31 +05:30
guard player.playingFullScreen else { return 0 }
2022-08-06 18:57:34 +05:30
if UIDevice.current.userInterfaceIdiom != .pad {
return verticalSizeClass == .compact ? safeAreaModel.safeArea.top : 0
2022-08-06 18:57:34 +05:30
}
2023-06-17 17:39:51 +05:30
return safeAreaModel.safeArea.top.isZero ? safeAreaModel.safeArea.bottom : safeAreaModel.safeArea.top
2022-08-06 18:57:34 +05:30
}
var controlsBottomPadding: Double {
if UIDevice.current.userInterfaceIdiom != .pad {
return player.playingFullScreen || verticalSizeClass == .compact ? safeAreaModel.safeArea.bottom : 0
2022-08-06 18:57:34 +05:30
}
2023-06-17 17:39:51 +05:30
return player.playingFullScreen ? safeAreaModel.safeArea.bottom : 0
2022-08-06 18:57:34 +05:30
}
#endif
#if os(tvOS)
private var hiddenControlsButton: some View {
VStack {
Button {
player.controls.show()
} label: {
EmptyView()
}
.offset(y: -100)
.buttonStyle(.plain)
.background(Color.clear)
.foregroundColor(.clear)
}
}
#endif
2022-08-06 18:57:34 +05:30
}
struct PlayerBackendView_Previews: PreviewProvider {
static var previews: some View {
PlayerBackendView()
.injectFixtureEnvironmentObjects()
}
}