yattee/Shared/Modifiers/PlayerOverlayModifier.swift

55 lines
1.7 KiB
Swift
Raw Normal View History

import Defaults
2022-12-10 21:37:14 +00:00
import Foundation
import SwiftUI
struct PlayerOverlayModifier: ViewModifier {
@ObservedObject private var player = PlayerModel.shared
@State private var expansionState = ControlsBar.ExpansionState.mini
@Environment(\.navigationStyle) private var navigationStyle
@Default(.playerButtonShowsControlButtonsWhenMinimized) private var controlsWhenMinimized
2022-12-19 00:37:09 +00:00
@Default(.playerButtonIsExpanded) private var playerButtonIsExpanded
@Default(.playerBarMaxWidth) private var playerBarMaxWidth
2022-12-10 21:37:14 +00:00
func body(content: Content) -> some View {
content
#if !os(tvOS)
.overlay(overlay, alignment: .bottomTrailing)
#endif
2022-12-10 21:37:14 +00:00
}
@ViewBuilder var overlay: some View {
Group {
2022-12-19 12:45:41 +00:00
ControlsBar(fullScreen: .constant(false), expansionState: $expansionState, playerBar: true)
.offset(x: expansionState == .mini && !controlsWhenMinimized ? 10 : 0, y: 0)
.frame(maxWidth: maxWidth, alignment: .trailing)
.onAppear {
if playerButtonIsExpanded {
expansionState = .full
2022-12-19 00:37:09 +00:00
}
2022-12-19 12:45:41 +00:00
}
.animation(.easeIn, value: player.videoForDisplay)
2023-04-24 10:57:31 +00:00
.opacity(opacity)
}
}
2022-12-19 00:37:09 +00:00
2023-04-24 10:57:31 +00:00
var opacity: Double {
guard !player.closing else { return 0 }
return player.videoForDisplay == nil ? 0 : 1
}
2022-12-19 00:37:09 +00:00
var maxWidth: Double {
playerBarMaxWidth == "0" ? .infinity : (Double(playerBarMaxWidth) ?? 600)
}
}
struct PlayerOverlayModifier_Previews: PreviewProvider {
static var previews: some View {
HStack {}
.frame(maxWidth: .infinity, maxHeight: 100)
.modifier(PlayerOverlayModifier())
}
2022-12-10 21:37:14 +00:00
}