2022-06-17 10:27:01 +00:00
|
|
|
import Defaults
|
2022-02-16 20:23:11 +00:00
|
|
|
import Foundation
|
2022-06-07 21:27:48 +00:00
|
|
|
import SDWebImageSwiftUI
|
2022-02-16 20:23:11 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PlayerControls: View {
|
2022-02-21 20:57:12 +00:00
|
|
|
static let animation = Animation.easeInOut(duration: 0.2)
|
2022-02-16 20:23:11 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
private var player: PlayerModel { .shared }
|
|
|
|
private var thumbnails: ThumbnailsModel { .shared }
|
2022-02-16 21:51:37 +00:00
|
|
|
|
2022-09-01 23:05:31 +00:00
|
|
|
@ObservedObject private var model = PlayerControlsModel.shared
|
2022-02-16 20:23:11 +00:00
|
|
|
|
2022-02-27 20:31:17 +00:00
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
2022-03-27 19:22:13 +00:00
|
|
|
#elseif os(tvOS)
|
|
|
|
enum Field: Hashable {
|
2022-08-28 17:18:49 +00:00
|
|
|
case seekOSD
|
2022-03-27 19:22:13 +00:00
|
|
|
case play
|
|
|
|
case backward
|
|
|
|
case forward
|
2022-08-14 17:06:22 +00:00
|
|
|
case settings
|
|
|
|
case close
|
2022-03-27 19:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@FocusState private var focusedField: Field?
|
2022-02-27 20:31:17 +00:00
|
|
|
#endif
|
2022-02-16 20:23:11 +00:00
|
|
|
|
2022-08-07 11:15:27 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
@Default(.closePlayerOnItemClose) private var closePlayerOnItemClose
|
|
|
|
#endif
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
@Default(.playerControlsLayout) private var regularPlayerControlsLayout
|
|
|
|
@Default(.fullScreenPlayerControlsLayout) private var fullScreenPlayerControlsLayout
|
|
|
|
|
2022-09-01 23:05:31 +00:00
|
|
|
private let controlsOverlayModel = ControlOverlaysModel.shared
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
var playerControlsLayout: PlayerControlsLayout {
|
2022-09-01 23:05:31 +00:00
|
|
|
player.playingFullScreen ? fullScreenPlayerControlsLayout : regularPlayerControlsLayout
|
2022-08-28 17:18:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
var body: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
ZStack(alignment: .topLeading) {
|
|
|
|
Seek()
|
|
|
|
.zIndex(4)
|
|
|
|
.transition(.opacity)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .topLeading)
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .seekOSD)
|
2022-08-29 11:55:23 +00:00
|
|
|
.onChange(of: player.seek.lastSeekTime) { _ in
|
2022-08-28 17:18:49 +00:00
|
|
|
if !model.presentingControls {
|
|
|
|
focusedField = .seekOSD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
.offset(y: 2)
|
|
|
|
#endif
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
VStack {
|
2022-11-25 18:31:48 +00:00
|
|
|
ZStack {
|
2022-08-28 17:18:49 +00:00
|
|
|
VStack(spacing: 0) {
|
|
|
|
ZStack {
|
|
|
|
OpeningStream()
|
|
|
|
NetworkState()
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.offset(y: playerControlsLayout.osdVerticalOffset + 5)
|
|
|
|
|
2022-08-31 19:24:46 +00:00
|
|
|
Section {
|
2022-08-28 17:18:49 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
HStack {
|
|
|
|
seekBackwardButton
|
|
|
|
Spacer()
|
|
|
|
togglePlayButton
|
|
|
|
Spacer()
|
|
|
|
seekForwardButton
|
|
|
|
}
|
|
|
|
.font(.system(size: playerControlsLayout.bigButtonFontSize))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ZStack(alignment: .bottom) {
|
|
|
|
VStack(spacing: 4) {
|
|
|
|
#if !os(tvOS)
|
|
|
|
buttonsBar
|
|
|
|
|
|
|
|
HStack {
|
2022-09-01 23:05:31 +00:00
|
|
|
if !player.currentVideo.isNil, player.playingFullScreen {
|
2022-08-28 17:18:49 +00:00
|
|
|
Button {
|
|
|
|
withAnimation(Self.animation) {
|
|
|
|
model.presentingDetailsOverlay = true
|
|
|
|
}
|
|
|
|
} label: {
|
2022-12-17 18:35:07 +00:00
|
|
|
ControlsBar(fullScreen: $model.presentingDetailsOverlay, expansionState: .constant(.full), presentingControls: false, detailsTogglePlayer: false, detailsToggleFullScreen: false)
|
2022-08-28 17:18:49 +00:00
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 4))
|
|
|
|
.frame(maxWidth: 300, alignment: .leading)
|
2022-08-14 17:06:22 +00:00
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
.buttonStyle(.plain)
|
2022-07-10 17:51:46 +00:00
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
Spacer()
|
2022-07-10 17:51:46 +00:00
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Spacer()
|
2022-05-27 22:59:35 +00:00
|
|
|
|
2022-08-28 22:21:12 +00:00
|
|
|
if playerControlsLayout.displaysTitleLine {
|
|
|
|
VStack(alignment: .leading) {
|
2022-11-11 18:19:48 +00:00
|
|
|
Text(player.currentVideo?.displayTitle ?? "Not Playing")
|
2022-08-28 22:21:12 +00:00
|
|
|
.shadow(radius: 10)
|
|
|
|
.font(.system(size: playerControlsLayout.titleLineFontSize).bold())
|
|
|
|
.lineLimit(1)
|
|
|
|
|
2022-11-11 18:19:48 +00:00
|
|
|
Text(player.currentVideo?.displayAuthor ?? "")
|
2022-08-28 22:21:12 +00:00
|
|
|
.fontWeight(.semibold)
|
|
|
|
.shadow(radius: 10)
|
2022-12-14 11:56:47 +00:00
|
|
|
.foregroundColor(.init(white: 0.8))
|
2022-08-28 22:21:12 +00:00
|
|
|
.font(.system(size: playerControlsLayout.authorLineFontSize))
|
|
|
|
.lineLimit(1)
|
|
|
|
}
|
2022-12-14 11:56:47 +00:00
|
|
|
.foregroundColor(.white)
|
2022-08-28 22:21:12 +00:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
.offset(y: -40)
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
timeline
|
|
|
|
.padding(.bottom, 2)
|
|
|
|
}
|
|
|
|
.zIndex(1)
|
|
|
|
.padding(.top, 2)
|
|
|
|
.transition(.opacity)
|
|
|
|
|
|
|
|
HStack(spacing: playerControlsLayout.buttonsSpacing) {
|
|
|
|
#if os(tvOS)
|
|
|
|
togglePlayButton
|
|
|
|
seekBackwardButton
|
|
|
|
seekForwardButton
|
|
|
|
#endif
|
|
|
|
restartVideoButton
|
|
|
|
advanceToNextItemButton
|
|
|
|
Spacer()
|
|
|
|
#if os(tvOS)
|
|
|
|
settingsButton
|
|
|
|
#endif
|
|
|
|
playbackModeButton
|
|
|
|
#if os(tvOS)
|
|
|
|
closeVideoButton
|
|
|
|
#else
|
|
|
|
musicModeButton
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
.zIndex(0)
|
|
|
|
#if os(tvOS)
|
|
|
|
.offset(y: -playerControlsLayout.timelineHeight - 30)
|
|
|
|
#else
|
|
|
|
.offset(y: -playerControlsLayout.timelineHeight - 5)
|
|
|
|
#endif
|
2022-05-27 23:23:50 +00:00
|
|
|
}
|
2022-09-01 23:05:31 +00:00
|
|
|
}
|
|
|
|
.opacity(model.presentingControls ? 1 : 0)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
.frame(maxWidth: .infinity)
|
2022-06-18 12:39:49 +00:00
|
|
|
#if os(tvOS)
|
2022-08-20 21:05:40 +00:00
|
|
|
.onChange(of: model.presentingControls) { newValue in
|
|
|
|
if newValue { focusedField = .play }
|
|
|
|
}
|
|
|
|
.onChange(of: focusedField) { _ in model.resetTimer() }
|
2022-06-18 12:39:49 +00:00
|
|
|
#else
|
2022-08-20 21:05:40 +00:00
|
|
|
.background(PlayerGestures())
|
|
|
|
.background(controlsBackground)
|
2022-06-18 12:39:49 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-08 17:31:13 +00:00
|
|
|
if model.presentingDetailsOverlay {
|
2022-08-21 22:57:01 +00:00
|
|
|
Section {
|
|
|
|
VideoDetailsOverlay()
|
|
|
|
.frame(maxWidth: detailsWidth, maxHeight: detailsHeight)
|
|
|
|
.transition(.opacity)
|
|
|
|
}
|
|
|
|
.frame(maxHeight: .infinity, alignment: .top)
|
2022-08-08 17:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
.onChange(of: model.presentingOverlays) { newValue in
|
2022-08-08 17:31:13 +00:00
|
|
|
if newValue {
|
2022-08-23 21:14:13 +00:00
|
|
|
model.hide()
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-03-27 19:22:13 +00:00
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(tvOS)
|
2022-08-14 22:14:28 +00:00
|
|
|
.onReceive(model.reporter) { value in
|
2022-08-28 17:18:49 +00:00
|
|
|
guard player.presentingPlayer else { return }
|
2022-08-14 22:14:28 +00:00
|
|
|
if value == "swipe down", !model.presentingControls, !model.presentingOverlays {
|
|
|
|
withAnimation(Self.animation) {
|
2022-11-17 21:47:45 +00:00
|
|
|
controlsOverlayModel.hide()
|
2022-08-14 22:14:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
model.show()
|
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
model.resetTimer()
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-10 17:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var detailsWidth: Double {
|
2022-11-24 20:36:05 +00:00
|
|
|
guard player.playerSize.width.isFinite else { return 200 }
|
2022-07-10 17:51:46 +00:00
|
|
|
return [player.playerSize.width, 600].min()!
|
2022-07-05 17:20:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 16:43:23 +00:00
|
|
|
var detailsHeight: Double {
|
2022-11-24 20:36:05 +00:00
|
|
|
guard player.playerSize.height.isFinite else { return 200 }
|
2022-11-13 17:52:15 +00:00
|
|
|
var inset = 0.0
|
|
|
|
#if os(iOS)
|
|
|
|
inset = SafeArea.insets.bottom
|
|
|
|
#endif
|
|
|
|
return [player.playerSize.height - inset, 500].min()!
|
2022-07-11 16:43:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 21:27:48 +00:00
|
|
|
@ViewBuilder var controlsBackground: some View {
|
|
|
|
if player.musicMode,
|
|
|
|
let item = self.player.currentItem,
|
2022-06-18 12:39:49 +00:00
|
|
|
let video = item.video,
|
|
|
|
let url = thumbnails.best(video)
|
2022-06-07 21:27:48 +00:00
|
|
|
{
|
2022-11-10 18:11:19 +00:00
|
|
|
WebImage(url: url, options: [.lowPriority])
|
2022-09-11 19:33:08 +00:00
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
Rectangle().fill(Color("PlaceholderColor"))
|
2022-06-07 21:27:48 +00:00
|
|
|
}
|
2022-09-11 19:33:08 +00:00
|
|
|
.retryOnAppear(true)
|
|
|
|
.indicator(.activity)
|
|
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
2022-06-07 21:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
var timeline: some View {
|
2022-06-18 12:39:49 +00:00
|
|
|
TimelineView(context: .player).foregroundColor(.primary)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var hidePlayerButton: some View {
|
2022-05-27 22:59:35 +00:00
|
|
|
button("Hide", systemImage: "chevron.down") {
|
2022-02-16 20:23:11 +00:00
|
|
|
player.hide()
|
|
|
|
}
|
2022-05-27 22:59:35 +00:00
|
|
|
|
2022-03-27 19:22:13 +00:00
|
|
|
#if !os(tvOS)
|
2022-02-16 23:01:48 +00:00
|
|
|
.keyboardShortcut(.cancelAction)
|
2022-03-27 19:22:13 +00:00
|
|
|
#endif
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var playbackStatus: String {
|
|
|
|
if player.live {
|
|
|
|
return "LIVE"
|
|
|
|
}
|
|
|
|
|
|
|
|
guard !player.isLoadingVideo else {
|
|
|
|
return "loading..."
|
|
|
|
}
|
|
|
|
|
|
|
|
let videoLengthAtRate = (player.currentVideo?.length ?? 0) / Double(player.currentRate)
|
|
|
|
let remainingSeconds = videoLengthAtRate - (player.time?.seconds ?? 0)
|
|
|
|
|
|
|
|
if remainingSeconds < 60 {
|
|
|
|
return "less than a minute"
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeFinishAt = Date().addingTimeInterval(remainingSeconds)
|
|
|
|
|
|
|
|
return "ends at \(formattedTimeFinishAt(timeFinishAt))"
|
|
|
|
}
|
|
|
|
|
|
|
|
private func formattedTimeFinishAt(_ date: Date) -> String {
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
|
|
|
dateFormatter.dateStyle = .none
|
|
|
|
dateFormatter.timeStyle = .short
|
|
|
|
|
|
|
|
return dateFormatter.string(from: date)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttonsBar: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
HStack(spacing: playerControlsLayout.buttonsSpacing) {
|
2022-08-14 17:06:22 +00:00
|
|
|
fullscreenButton
|
2022-04-16 20:50:37 +00:00
|
|
|
|
2022-08-18 22:40:46 +00:00
|
|
|
pipButton
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(iOS)
|
|
|
|
lockOrientationButton
|
|
|
|
#endif
|
2022-06-07 21:27:48 +00:00
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
Spacer()
|
2022-06-07 22:05:16 +00:00
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
settingsButton
|
|
|
|
closeVideoButton
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var fullscreenButton: some View {
|
|
|
|
button(
|
|
|
|
"Fullscreen",
|
2022-09-01 23:05:31 +00:00
|
|
|
systemImage: player.playingFullScreen ? "arrow.down.right.and.arrow.up.left" : "arrow.up.left.and.arrow.down.right"
|
2022-02-16 20:23:11 +00:00
|
|
|
) {
|
2022-09-01 23:05:31 +00:00
|
|
|
player.toggleFullscreen(player.playingFullScreen)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
2022-03-27 19:22:13 +00:00
|
|
|
#if !os(tvOS)
|
2022-09-01 23:05:31 +00:00
|
|
|
.keyboardShortcut(player.playingFullScreen ? .cancelAction : .defaultAction)
|
2022-03-27 19:22:13 +00:00
|
|
|
#endif
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
private var settingsButton: some View {
|
2022-08-31 22:41:31 +00:00
|
|
|
button("settings", systemImage: "gearshape") {
|
2022-08-14 17:06:22 +00:00
|
|
|
withAnimation(Self.animation) {
|
2022-09-01 23:05:31 +00:00
|
|
|
controlsOverlayModel.toggle()
|
2022-08-14 17:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .settings)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:59:35 +00:00
|
|
|
private var closeVideoButton: some View {
|
|
|
|
button("Close", systemImage: "xmark") {
|
2022-08-07 11:15:27 +00:00
|
|
|
player.closeCurrentItem()
|
2022-05-27 22:59:35 +00:00
|
|
|
}
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .close)
|
|
|
|
#endif
|
2022-05-27 22:59:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 22:05:16 +00:00
|
|
|
private var musicModeButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
button("Music Mode", systemImage: "music.note", active: player.musicMode, action: player.toggleMusicMode)
|
2022-06-07 22:05:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:23:14 +00:00
|
|
|
private var pipButton: some View {
|
2022-08-26 20:17:21 +00:00
|
|
|
let image = player.transitioningToPiP ? "pip.fill" : player.pipController?.isPictureInPictureActive ?? false ? "pip.exit" : "pip.enter"
|
|
|
|
return button("PiP", systemImage: image) {
|
|
|
|
(player.pipController?.isPictureInPictureActive ?? false) ? player.closePiP() : player.startPiP()
|
2022-05-20 21:23:14 +00:00
|
|
|
}
|
2022-08-26 20:17:21 +00:00
|
|
|
.disabled(!player.pipPossible)
|
2022-05-20 21:23:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 23:26:35 +00:00
|
|
|
#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
|
2022-11-11 13:25:56 +00:00
|
|
|
let orientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
|
|
Orientation.lockOrientation(orientationMask, andRotateTo: .landscapeLeft)
|
|
|
|
// iOS 16 workaround
|
|
|
|
Orientation.lockOrientation(orientationMask, andRotateTo: orientation)
|
2022-07-10 23:26:35 +00:00
|
|
|
} else {
|
|
|
|
player.lockedOrientation = nil
|
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: OrientationTracker.shared.currentInterfaceOrientation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
var playbackModeButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
button("Playback Mode", systemImage: player.playbackMode.systemImage) {
|
2022-07-10 22:24:56 +00:00
|
|
|
player.playbackMode = player.playbackMode.next()
|
2022-08-14 17:58:37 +00:00
|
|
|
model.objectWillChange.send()
|
2022-07-10 22:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
var seekBackwardButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
var foregroundColor: Color?
|
|
|
|
var fontSize: Double?
|
|
|
|
var size: Double?
|
|
|
|
#if !os(tvOS)
|
|
|
|
foregroundColor = .white
|
|
|
|
fontSize = playerControlsLayout.bigButtonFontSize
|
|
|
|
size = playerControlsLayout.bigButtonSize
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return button("Seek Backward", systemImage: "gobackward.10", fontSize: fontSize, size: size, cornerRadius: 5, background: false, foregroundColor: foregroundColor) {
|
|
|
|
player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-07-21 22:44:21 +00:00
|
|
|
.disabled(player.liveStreamInAVPlayer)
|
2022-06-18 12:39:49 +00:00
|
|
|
#if os(tvOS)
|
2022-07-21 22:44:21 +00:00
|
|
|
.focused($focusedField, equals: .backward)
|
2022-06-18 12:39:49 +00:00
|
|
|
#else
|
2022-07-21 22:44:21 +00:00
|
|
|
.keyboardShortcut("k", modifiers: [])
|
|
|
|
.keyboardShortcut(KeyEquivalent.leftArrow, modifiers: [])
|
2022-06-18 12:39:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var seekForwardButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
var foregroundColor: Color?
|
|
|
|
var fontSize: Double?
|
|
|
|
var size: Double?
|
|
|
|
#if !os(tvOS)
|
|
|
|
foregroundColor = .white
|
|
|
|
fontSize = playerControlsLayout.bigButtonFontSize
|
|
|
|
size = playerControlsLayout.bigButtonSize
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return button("Seek Forward", systemImage: "goforward.10", fontSize: fontSize, size: size, cornerRadius: 5, background: false, foregroundColor: foregroundColor) {
|
|
|
|
player.backend.seek(relative: .secondsInDefaultTimescale(10), seekType: .userInteracted)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-07-21 22:44:21 +00:00
|
|
|
.disabled(player.liveStreamInAVPlayer)
|
2022-06-18 12:39:49 +00:00
|
|
|
#if os(tvOS)
|
2022-07-21 22:44:21 +00:00
|
|
|
.focused($focusedField, equals: .forward)
|
2022-06-18 12:39:49 +00:00
|
|
|
#else
|
2022-07-21 22:44:21 +00:00
|
|
|
.keyboardShortcut("l", modifiers: [])
|
|
|
|
.keyboardShortcut(KeyEquivalent.rightArrow, modifiers: [])
|
2022-06-18 12:39:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-05-29 15:50:54 +00:00
|
|
|
private var restartVideoButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
button("Restart video", systemImage: "backward.end.fill", cornerRadius: 5) {
|
|
|
|
player.backend.seek(to: 0.0, seekType: .userInteracted)
|
2022-05-29 15:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
private var togglePlayButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
var foregroundColor: Color?
|
|
|
|
var fontSize: Double?
|
|
|
|
var size: Double?
|
|
|
|
#if !os(tvOS)
|
|
|
|
foregroundColor = .white
|
|
|
|
fontSize = playerControlsLayout.bigButtonFontSize
|
|
|
|
size = playerControlsLayout.bigButtonSize
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return button(
|
2022-06-18 12:39:49 +00:00
|
|
|
model.isPlaying ? "Pause" : "Play",
|
|
|
|
systemImage: model.isPlaying ? "pause.fill" : "play.fill",
|
2022-08-28 17:18:49 +00:00
|
|
|
fontSize: fontSize,
|
|
|
|
size: size,
|
|
|
|
background: false, foregroundColor: foregroundColor
|
2022-06-18 12:39:49 +00:00
|
|
|
) {
|
|
|
|
player.backend.togglePlay()
|
2022-05-29 15:50:54 +00:00
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.focused($focusedField, equals: .play)
|
|
|
|
#else
|
|
|
|
.keyboardShortcut("p")
|
|
|
|
.keyboardShortcut(.space)
|
|
|
|
#endif
|
|
|
|
.disabled(model.isLoadingVideo)
|
2022-05-29 15:50:54 +00:00
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
private var advanceToNextItemButton: some View {
|
2022-08-28 17:18:49 +00:00
|
|
|
button("Next", systemImage: "forward.fill", cornerRadius: 5) {
|
2022-06-18 12:39:49 +00:00
|
|
|
player.advanceToNextItem()
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
2022-07-10 22:24:56 +00:00
|
|
|
.disabled(!player.isAdvanceToNextItemAvailable)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func button(
|
|
|
|
_ label: String,
|
2022-06-14 22:41:49 +00:00
|
|
|
systemImage: String? = nil,
|
2022-08-28 17:18:49 +00:00
|
|
|
fontSize: Double? = nil,
|
|
|
|
size: Double? = nil,
|
|
|
|
width _: Double? = nil,
|
|
|
|
height _: Double? = nil,
|
2022-02-16 20:23:11 +00:00
|
|
|
cornerRadius: Double = 3,
|
2022-12-03 14:33:32 +00:00
|
|
|
background: Bool = false,
|
2022-08-28 17:18:49 +00:00
|
|
|
foregroundColor: Color? = nil,
|
2022-06-07 21:27:48 +00:00
|
|
|
active: Bool = false,
|
2022-02-16 20:23:11 +00:00
|
|
|
action: @escaping () -> Void = {}
|
|
|
|
) -> some View {
|
2022-08-14 17:06:22 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
let useBackground = false
|
|
|
|
#else
|
|
|
|
let useBackground = background
|
|
|
|
#endif
|
|
|
|
return Button {
|
2022-02-16 20:23:11 +00:00
|
|
|
action()
|
|
|
|
model.resetTimer()
|
|
|
|
} label: {
|
2022-06-14 22:41:49 +00:00
|
|
|
Group {
|
|
|
|
if let image = systemImage {
|
|
|
|
Label(label, systemImage: image)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
} else {
|
|
|
|
Label(label, systemImage: "")
|
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
.contentShape(Rectangle())
|
2022-08-28 17:18:49 +00:00
|
|
|
.shadow(radius: (foregroundColor == .white || !useBackground) ? 3 : 0)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
.font(.system(size: fontSize ?? playerControlsLayout.buttonFontSize))
|
2022-02-27 20:31:17 +00:00
|
|
|
.buttonStyle(.plain)
|
2022-08-28 17:18:49 +00:00
|
|
|
.foregroundColor(foregroundColor.isNil ? (active ? Color("AppRedColor") : .primary) : foregroundColor)
|
|
|
|
.frame(width: size ?? playerControlsLayout.buttonSize, height: size ?? playerControlsLayout.buttonSize)
|
2022-08-14 17:06:22 +00:00
|
|
|
.modifier(ControlBackgroundModifier(enabled: useBackground))
|
2022-06-18 12:39:49 +00:00
|
|
|
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
2022-12-03 14:33:32 +00:00
|
|
|
.environment(\.colorScheme, .dark)
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PlayerControls_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-06-18 12:39:49 +00:00
|
|
|
ZStack {
|
2022-05-27 23:23:50 +00:00
|
|
|
Color.gray
|
2022-05-27 22:59:35 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
PlayerControls()
|
2022-05-27 22:59:35 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
}
|