2022-08-28 22:48:49 +05:30
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
extension VideoPlayerView {
|
|
|
|
var playerDragGesture: some Gesture {
|
2024-05-16 22:29:54 +05:30
|
|
|
DragGesture(minimumDistance: 30, coordinateSpace: .global)
|
|
|
|
#if os(iOS)
|
2022-08-28 22:48:49 +05:30
|
|
|
.updating($dragGestureOffset) { value, state, _ in
|
|
|
|
guard isVerticalDrag else { return }
|
|
|
|
var translation = value.translation
|
2024-09-01 05:24:24 +05:30
|
|
|
translation.height = max(-translation.height, translation.height)
|
2022-08-28 22:48:49 +05:30
|
|
|
state = translation
|
|
|
|
}
|
2024-05-16 22:29:54 +05:30
|
|
|
#endif
|
2022-08-28 22:48:49 +05:30
|
|
|
.updating($dragGestureState) { _, state, _ in
|
|
|
|
state = true
|
|
|
|
}
|
|
|
|
.onChanged { value in
|
2024-05-16 22:29:54 +05:30
|
|
|
guard player.presentingPlayer,
|
|
|
|
!controlsOverlayModel.presenting,
|
2024-09-01 05:24:24 +05:30
|
|
|
dragGestureState,
|
|
|
|
!disableToggleGesture else { return }
|
2022-08-28 22:48:49 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
if player.controls.presentingControls, !player.musicMode {
|
|
|
|
player.controls.presentingControls = false
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
if player.musicMode {
|
|
|
|
player.backend.stopControlsUpdates()
|
|
|
|
}
|
2022-08-29 17:25:23 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
let verticalDrag = value.translation.height
|
|
|
|
let horizontalDrag = value.translation.width
|
2022-08-28 22:48:49 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
#if os(iOS)
|
2024-08-24 17:26:25 +05:30
|
|
|
if viewDragOffset > 0, !isVerticalDrag {
|
|
|
|
isVerticalDrag = true
|
2024-05-16 22:29:54 +05:30
|
|
|
}
|
|
|
|
#endif
|
2022-08-28 22:48:49 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
if !isVerticalDrag,
|
|
|
|
horizontalPlayerGestureEnabled,
|
|
|
|
abs(horizontalDrag) > seekGestureSensitivity,
|
|
|
|
!isHorizontalDrag,
|
|
|
|
player.activeBackend == .mpv || !avPlayerUsesSystemControls
|
|
|
|
{
|
|
|
|
isHorizontalDrag = true
|
|
|
|
player.seek.onSeekGestureStart()
|
|
|
|
viewDragOffset = 0
|
|
|
|
}
|
2024-05-01 22:07:42 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
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
|
2024-05-01 22:07:42 +05:30
|
|
|
|
2024-05-16 22:29:54 +05:30
|
|
|
player.seek.gestureSeek = timeSeek
|
|
|
|
}
|
|
|
|
return
|
2024-05-01 22:07:42 +05:30
|
|
|
}
|
|
|
|
|
2024-09-01 05:24:24 +05:30
|
|
|
// Toggle fullscreen on upward drag only when not disabled
|
|
|
|
if verticalDrag < -50 {
|
|
|
|
if player.playingFullScreen {
|
|
|
|
player.exitFullScreen(showControls: false)
|
|
|
|
} else {
|
|
|
|
player.enterFullScreen()
|
|
|
|
}
|
|
|
|
disableGestureTemporarily()
|
|
|
|
return
|
|
|
|
}
|
2024-05-01 22:07:42 +05:30
|
|
|
|
2024-09-01 05:24:24 +05:30
|
|
|
// Ignore downward swipes when in fullscreen
|
|
|
|
guard verticalDrag > 0 && !player.playingFullScreen else {
|
|
|
|
return
|
2024-05-01 22:07:42 +05:30
|
|
|
}
|
2024-09-01 05:24:24 +05:30
|
|
|
viewDragOffset = verticalDrag
|
2024-05-16 22:29:54 +05:30
|
|
|
}
|
|
|
|
.onEnded { _ in
|
|
|
|
onPlayerDragGestureEnded()
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|
|
|
|
|
2022-08-29 21:28:40 +05:30
|
|
|
func onPlayerDragGestureEnded() {
|
2022-08-28 22:48:49 +05:30
|
|
|
if horizontalPlayerGestureEnabled, isHorizontalDrag {
|
|
|
|
isHorizontalDrag = false
|
2022-08-29 17:25:23 +05:30
|
|
|
player.seek.onSeekGestureEnd()
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
isVerticalDrag = false
|
|
|
|
|
|
|
|
guard player.presentingPlayer,
|
2022-09-02 04:35:31 +05:30
|
|
|
!controlsOverlayModel.presenting else { return }
|
2022-08-28 22:48:49 +05:30
|
|
|
|
|
|
|
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-09-01 05:24:24 +05:30
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|