yattee/Shared/Player/PlayerDragGesture.swift

119 lines
4.0 KiB
Swift
Raw Normal View History

import Defaults
import SwiftUI
extension VideoPlayerView {
var playerDragGesture: some Gesture {
DragGesture(minimumDistance: 30, coordinateSpace: .global)
#if os(iOS)
.updating($dragGestureOffset) { value, state, _ in
guard isVerticalDrag else { return }
var translation = value.translation
2024-08-31 23:54:24 +00:00
translation.height = max(-translation.height, translation.height)
state = translation
}
#endif
.updating($dragGestureState) { _, state, _ in
state = true
}
.onChanged { value in
guard player.presentingPlayer,
!controlsOverlayModel.presenting,
2024-08-31 23:54:24 +00:00
dragGestureState,
!disableToggleGesture else { return }
if player.controls.presentingControls, !player.musicMode {
player.controls.presentingControls = false
}
if player.musicMode {
player.backend.stopControlsUpdates()
}
2022-08-29 11:55:23 +00:00
let verticalDrag = value.translation.height
let horizontalDrag = value.translation.width
#if os(iOS)
if viewDragOffset > 0, !isVerticalDrag {
isVerticalDrag = true
}
#endif
if !isVerticalDrag,
horizontalPlayerGestureEnabled,
abs(horizontalDrag) > seekGestureSensitivity,
!isHorizontalDrag,
player.activeBackend == .mpv || !avPlayerUsesSystemControls
{
isHorizontalDrag = true
player.seek.onSeekGestureStart()
viewDragOffset = 0
}
if horizontalPlayerGestureEnabled, isHorizontalDrag {
player.seek.updateCurrentTime {
let time = player.backend.playerItemDuration?.seconds ?? 0
if player.seek.gestureStart.isNil {
player.seek.gestureStart = time
}
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
player.seek.gestureSeek = timeSeek
}
return
}
2024-08-31 23:54:24 +00:00
// Toggle fullscreen on upward drag only when not disabled
if verticalDrag < -50 {
player.toggleFullScreenAction()
2024-08-31 23:54:24 +00:00
disableGestureTemporarily()
return
}
2024-08-31 23:54:24 +00:00
// Ignore downward swipes when in fullscreen
guard verticalDrag > 0 && !player.playingFullScreen else {
return
}
2024-08-31 23:54:24 +00:00
viewDragOffset = verticalDrag
}
.onEnded { _ in
onPlayerDragGestureEnded()
}
}
2022-08-29 15:58:40 +00:00
func onPlayerDragGestureEnded() {
if horizontalPlayerGestureEnabled, isHorizontalDrag {
isHorizontalDrag = false
2022-08-29 11:55:23 +00:00
player.seek.onSeekGestureEnd()
}
isVerticalDrag = false
guard player.presentingPlayer,
2022-09-01 23:05:31 +00:00
!controlsOverlayModel.presenting else { return }
if viewDragOffset > 100 {
withAnimation(Constants.overlayAnimation) {
viewDragOffset = Self.hiddenOffset
}
} else {
withAnimation(Constants.overlayAnimation) {
viewDragOffset = 0
}
player.backend.setNeedsDrawing(true)
player.show()
if player.musicMode {
player.backend.startControlsUpdates()
}
}
}
2024-08-31 23:54:24 +00:00
// 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
}
}
}