mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-26 08:29:41 -05:00
more composables
This commit is contained in:
4 files changed
+784
-577
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { reactive, shallowRef } from "vue";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { state } from "@/store/index.ts";
|
||||
import { useGestureState } from "../useGestureState.ts";
|
||||
import { useSwipeNavigationGestures } from "../useSwipeNavigationGestures.ts";
|
||||
import { withSetup } from "./withSetup.ts";
|
||||
|
||||
vi.mock("@/store/index.ts", () => ({
|
||||
state: reactive({ navigation: { enabled: false, previousLink: "", nextLink: "" } }),
|
||||
getters: {
|
||||
currentPrompt: () => null,
|
||||
isPreviewPlaybackQueueNavMode: () => false,
|
||||
playbackQueueCanGoPrevious: () => false,
|
||||
playbackQueueCanGoNext: () => false,
|
||||
},
|
||||
mutations: { setNavigationGestureHint: vi.fn() },
|
||||
}));
|
||||
|
||||
function fakePlayer(fullscreenActive = false) {
|
||||
return {
|
||||
fullscreen: { active: fullscreenActive, exit: vi.fn(), toggle: vi.fn() },
|
||||
speed: 1,
|
||||
};
|
||||
}
|
||||
|
||||
function setup({ previewType = "video", player = fakePlayer(), lyricsActive = false } = {}) {
|
||||
const surface = document.createElement("div");
|
||||
document.body.appendChild(surface);
|
||||
const callbacks = { prev: vi.fn(), next: vi.fn(), close: vi.fn() };
|
||||
const { result: swipe } = withSetup(() => {
|
||||
const gestures = useGestureState();
|
||||
return useSwipeNavigationGestures({
|
||||
// biome-ignore lint/suspicious/noExplicitAny: minimal fake of the Plyr instance
|
||||
player: shallowRef(player as any),
|
||||
gestures,
|
||||
getPreviewType: () => previewType,
|
||||
isMobileLyricsActive: () => lyricsActive,
|
||||
getGestureSurface: () => surface,
|
||||
isControlOrMenuTarget: () => false,
|
||||
onNavigatePrevious: callbacks.prev,
|
||||
onNavigateNext: callbacks.next,
|
||||
onClosePreview: callbacks.close,
|
||||
});
|
||||
});
|
||||
swipe.setupSwipeGestures();
|
||||
return { swipe, surface, callbacks, player };
|
||||
}
|
||||
|
||||
function drag(surface: HTMLElement, dx: number, dy: number) {
|
||||
surface.dispatchEvent(new MouseEvent("mousedown", { button: 0, clientX: 200, clientY: 200, bubbles: true }));
|
||||
document.dispatchEvent(new MouseEvent("mousemove", { clientX: 200 + dx, clientY: 200 + dy, bubbles: true }));
|
||||
document.dispatchEvent(new MouseEvent("mouseup", { clientX: 200 + dx, clientY: 200 + dy, bubbles: true }));
|
||||
}
|
||||
|
||||
describe("useSwipeNavigationGestures", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
state.navigation.enabled = true;
|
||||
state.navigation.previousLink = "/prev";
|
||||
state.navigation.nextLink = "/next";
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
it("navigates to the previous file after a long right swipe", () => {
|
||||
const { surface, callbacks } = setup();
|
||||
drag(surface, 140, 0);
|
||||
expect(callbacks.prev).toHaveBeenCalledTimes(1);
|
||||
expect(callbacks.next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("navigates to the next file after a long left swipe", () => {
|
||||
const { surface, callbacks } = setup();
|
||||
drag(surface, -140, 0);
|
||||
expect(callbacks.next).toHaveBeenCalledTimes(1);
|
||||
expect(callbacks.prev).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not navigate on a short swipe or when there is no previous file", () => {
|
||||
const { surface, callbacks } = setup();
|
||||
drag(surface, 100, 0);
|
||||
state.navigation.previousLink = "";
|
||||
drag(surface, 200, 0);
|
||||
expect(callbacks.prev).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("closes the preview after a long downward swipe (delayed for the dismiss animation)", () => {
|
||||
const { surface, callbacks } = setup();
|
||||
drag(surface, 0, 120);
|
||||
expect(callbacks.close).not.toHaveBeenCalled();
|
||||
vi.advanceTimersByTime(120);
|
||||
expect(callbacks.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("leaves fullscreen instead of closing when swiping down in fullscreen", () => {
|
||||
const player = fakePlayer(true);
|
||||
const { surface, callbacks } = setup({ player });
|
||||
drag(surface, 0, 120);
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(player.fullscreen.exit).toHaveBeenCalledTimes(1);
|
||||
expect(callbacks.close).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("toggles fullscreen on a long upward swipe for video only", () => {
|
||||
const video = setup();
|
||||
drag(video.surface, 0, -120);
|
||||
expect(video.player.fullscreen.toggle).toHaveBeenCalledTimes(1);
|
||||
|
||||
const audio = setup({ previewType: "audio" });
|
||||
drag(audio.surface, 0, -120);
|
||||
expect(audio.player.fullscreen.toggle).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ignores gestures while the mobile lyrics overlay is open", () => {
|
||||
const { surface, callbacks } = setup({ lyricsActive: true });
|
||||
drag(surface, 140, 0);
|
||||
expect(callbacks.prev).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stops reacting after teardown", () => {
|
||||
const { swipe, surface, callbacks } = setup();
|
||||
swipe.teardownSwipeGestures();
|
||||
drag(surface, 140, 0);
|
||||
expect(callbacks.prev).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { ref } from "vue";
|
||||
|
||||
interface FullscreenStateOptions {
|
||||
/** Runs when the player enters fullscreen, before the orientation is unlocked (e.g. to cancel a swipe in progress). */
|
||||
onEnter?: () => void;
|
||||
}
|
||||
|
||||
/** Tracks the player's fullscreen state and frees the screen orientation while in fullscreen. */
|
||||
export function useFullscreenState({ onEnter }: FullscreenStateOptions = {}) {
|
||||
const isFullscreen = ref(false);
|
||||
|
||||
async function onFullscreenEnter(): Promise<void> {
|
||||
isFullscreen.value = true;
|
||||
onEnter?.();
|
||||
// Allow free rotation when video enters full screen mode. This works even if the device's orientation is currently locked.
|
||||
try {
|
||||
await screen.orientation.lock("any");
|
||||
} catch (error) {
|
||||
// The NotSupportedError is thrown for non-mobile browsers and there seems to be no way to pre-check if it is supported.
|
||||
// -> Swallow NotSupportedError but let other errors be thrown.
|
||||
if ((error as DOMException).name !== "NotSupportedError") throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function onFullscreenExit(): void {
|
||||
isFullscreen.value = false;
|
||||
if (!screen.orientation?.unlock) return;
|
||||
try {
|
||||
screen.orientation.unlock();
|
||||
} catch (error) {
|
||||
if ((error as DOMException).name !== "NotSupportedError") throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return { isFullscreen, onFullscreenEnter, onFullscreenExit };
|
||||
}
|
||||
@@ -0,0 +1,600 @@
|
||||
import { computed, ref } from "vue";
|
||||
import type { ShallowRef } from "vue";
|
||||
import type Plyr from "plyr";
|
||||
import { getters, mutations, state } from "@/store/index.ts";
|
||||
import type { GestureState } from "./useGestureState.ts";
|
||||
|
||||
type EdgeKind = "horizontal" | "vertical-dismiss" | "vertical-fullscreen";
|
||||
type NavHintDir = "prev" | "next";
|
||||
|
||||
interface SwipeNavigationOptions {
|
||||
player: Readonly<ShallowRef<Plyr | null>>;
|
||||
gestures: GestureState;
|
||||
getPreviewType: () => string;
|
||||
/** The swipe is paused while the mobile lyrics overlay is scrolling. */
|
||||
isMobileLyricsActive: () => boolean;
|
||||
/** The element the touch/mouse listeners are attached to. */
|
||||
getGestureSurface: () => HTMLElement | null | undefined;
|
||||
isControlOrMenuTarget: (target: EventTarget | null) => boolean;
|
||||
onNavigatePrevious: () => void;
|
||||
onNavigateNext: () => void;
|
||||
onClosePreview: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Full-frame edge/swipe gestures for audio and video: horizontal swipe to the
|
||||
* previous/next file, vertical swipe down to close (or leave fullscreen), swipe
|
||||
* up to toggle fullscreen on video, and the touch long-press 2x speed. Shared by
|
||||
* both players. State is plain (non-reactive) on purpose: nothing renders from it.
|
||||
*/
|
||||
export function useSwipeNavigationGestures({
|
||||
player,
|
||||
gestures,
|
||||
getPreviewType,
|
||||
isMobileLyricsActive,
|
||||
getGestureSurface,
|
||||
isControlOrMenuTarget,
|
||||
onNavigatePrevious,
|
||||
onNavigateNext,
|
||||
onClosePreview,
|
||||
}: SwipeNavigationOptions) {
|
||||
let edgeKind: EdgeKind | null = null;
|
||||
let edgeStartX = 0;
|
||||
let edgeStartY = 0;
|
||||
let edgeDx = 0;
|
||||
let edgeDy = 0;
|
||||
let dragOffsetX = 0;
|
||||
let dragOffsetY = 0;
|
||||
let gestureSnapBack = false;
|
||||
let edgeMouseActive = false;
|
||||
let showNavHint = false;
|
||||
let navHintDir: NavHintDir = "next";
|
||||
let showDismissHint = false;
|
||||
let dismissFlashActive = false;
|
||||
const edgeHintPx = 44;
|
||||
const edgeCommitX = 130;
|
||||
const edgeCommitY = 110;
|
||||
const edgeRubberMax = 100;
|
||||
let swipeCleanup: (() => void) | null = null;
|
||||
/** When audio scrub/menu steals touchstart, ignore move/end for this touch id (Plyr has no video wrapper). */
|
||||
let swipeSuppressedTouchId: number | null = null;
|
||||
let dismissCloseTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let dismissHintTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
/** Toast shown while the long-press 2x speed is held. */
|
||||
const speedToastVisible = ref(false);
|
||||
const speedToastMessage = ref("");
|
||||
|
||||
const swipeGesturesActive = computed(
|
||||
() =>
|
||||
(getPreviewType() === "video" || getPreviewType() === "audio") &&
|
||||
!!player.value &&
|
||||
!isMobileLyricsActive(),
|
||||
);
|
||||
const navigationGestureAllowed = computed(
|
||||
() => state.navigation.enabled && getters.currentPrompt() === null,
|
||||
);
|
||||
const hasPreviousNav = computed(() => {
|
||||
if (getters.isPreviewPlaybackQueueNavMode()) {
|
||||
return navigationGestureAllowed.value && getters.playbackQueueCanGoPrevious();
|
||||
}
|
||||
return navigationGestureAllowed.value && state.navigation.previousLink !== "";
|
||||
});
|
||||
const hasNextNav = computed(() => {
|
||||
if (getters.isPreviewPlaybackQueueNavMode()) {
|
||||
return navigationGestureAllowed.value && getters.playbackQueueCanGoNext();
|
||||
}
|
||||
return navigationGestureAllowed.value && state.navigation.nextLink !== "";
|
||||
});
|
||||
|
||||
function applySwipeTransform() {
|
||||
const el = getGestureSurface();
|
||||
if (!el) {
|
||||
return;
|
||||
}
|
||||
const transition = gestureSnapBack
|
||||
? 'transform 0.22s cubic-bezier(0.32, 0.72, 0, 1)'
|
||||
: 'none';
|
||||
el.style.transition = transition;
|
||||
const x = dragOffsetX;
|
||||
const y = dragOffsetY;
|
||||
el.style.transform = x || y ? `translate(${x}px, ${y}px)` : '';
|
||||
}
|
||||
function syncNavigationGestureHintToStore() {
|
||||
const ax = Math.abs(edgeDx);
|
||||
const ay = Math.abs(edgeDy);
|
||||
const navPrevReady =
|
||||
hasPreviousNav.value &&
|
||||
edgeDx >= edgeCommitX &&
|
||||
ax >= ay;
|
||||
const navNextReady =
|
||||
hasNextNav.value &&
|
||||
edgeDx <= -edgeCommitX &&
|
||||
ax >= ay;
|
||||
const dismissReady =
|
||||
edgeDy >= edgeCommitY && ay >= ax;
|
||||
let kind: "close" | "previous" | "next" | null = null;
|
||||
let commitReady = false;
|
||||
let flashClose = false;
|
||||
if (dismissFlashActive) {
|
||||
kind = 'close';
|
||||
commitReady = dismissReady;
|
||||
flashClose = true;
|
||||
} else if (showDismissHint) {
|
||||
kind = 'close';
|
||||
commitReady = dismissReady;
|
||||
} else if (showNavHint && navHintDir === 'prev' && hasPreviousNav.value) {
|
||||
kind = 'previous';
|
||||
commitReady = navPrevReady;
|
||||
} else if (showNavHint && navHintDir === 'next' && hasNextNav.value) {
|
||||
kind = 'next';
|
||||
commitReady = navNextReady;
|
||||
}
|
||||
mutations.setNavigationGestureHint({ kind, commitReady, flashClose });
|
||||
}
|
||||
function rubberband(value: number, max: number): number {
|
||||
const sign = value < 0 ? -1 : 1;
|
||||
const a = Math.abs(value);
|
||||
if (a <= max) {
|
||||
return value;
|
||||
}
|
||||
return sign * (max + (a - max) * 0.32);
|
||||
}
|
||||
function decideEdgeKind() {
|
||||
if (edgeKind) return;
|
||||
const ax = Math.abs(edgeDx);
|
||||
const ay = Math.abs(edgeDy);
|
||||
if (ax < 12 && ay < 12) return;
|
||||
if (getPreviewType() === 'audio') {
|
||||
if (ay > ax * 1.12 && ay > 14 && edgeDy > 0) {
|
||||
edgeKind = 'vertical-dismiss';
|
||||
} else if (ax > ay * 1.12 && ax > 14) {
|
||||
edgeKind = 'horizontal';
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (ay > ax * 1.12 && ay > 14) {
|
||||
edgeKind = edgeDy > 0 ? 'vertical-dismiss' : 'vertical-fullscreen';
|
||||
} else if (ax > ay * 1.12 && ax > 14) {
|
||||
edgeKind = 'horizontal';
|
||||
}
|
||||
}
|
||||
function applyEdgeVisuals() {
|
||||
if (getPreviewType() === 'audio' && edgeDy < 0 && Math.abs(edgeDy) > Math.abs(edgeDx)) {
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
showNavHint = false;
|
||||
showDismissHint = false;
|
||||
applySwipeTransform();
|
||||
syncNavigationGestureHintToStore();
|
||||
return;
|
||||
}
|
||||
|
||||
const kind = edgeKind;
|
||||
if (kind === 'horizontal') {
|
||||
dragOffsetX = rubberband(edgeDx, edgeRubberMax);
|
||||
dragOffsetY = 0;
|
||||
const adx = Math.abs(edgeDx);
|
||||
showNavHint = adx >= edgeHintPx;
|
||||
navHintDir = edgeDx > 0 ? 'prev' : 'next';
|
||||
if (navHintDir === 'prev' && !hasPreviousNav.value) showNavHint = false;
|
||||
if (navHintDir === 'next' && !hasNextNav.value) showNavHint = false;
|
||||
showDismissHint = false;
|
||||
} else if (kind === 'vertical-dismiss') {
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = edgeDy;
|
||||
showDismissHint = edgeDy >= edgeHintPx;
|
||||
showNavHint = false;
|
||||
} else if (kind === 'vertical-fullscreen') {
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = edgeDy;
|
||||
showDismissHint = false;
|
||||
showNavHint = false;
|
||||
} else {
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
showNavHint = false;
|
||||
showDismissHint = false;
|
||||
}
|
||||
applySwipeTransform();
|
||||
syncNavigationGestureHintToStore();
|
||||
}
|
||||
function snapBackEdgeGesture() {
|
||||
gestureSnapBack = true;
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
showNavHint = false;
|
||||
showDismissHint = false;
|
||||
edgeKind = null;
|
||||
edgeDx = 0;
|
||||
edgeDy = 0;
|
||||
gestures.setSkipNextTap(200);
|
||||
applySwipeTransform();
|
||||
mutations.setNavigationGestureHint({});
|
||||
setTimeout(() => {
|
||||
gestureSnapBack = false;
|
||||
applySwipeTransform();
|
||||
}, 240);
|
||||
}
|
||||
function resetSwipeVisualState() {
|
||||
clearDismissAnimTimers();
|
||||
swipeSuppressedTouchId = null;
|
||||
edgeKind = null;
|
||||
edgeDx = 0;
|
||||
edgeDy = 0;
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
showNavHint = false;
|
||||
showDismissHint = false;
|
||||
gestureSnapBack = false;
|
||||
dismissFlashActive = false;
|
||||
applySwipeTransform();
|
||||
mutations.setNavigationGestureHint({});
|
||||
}
|
||||
function resetEdgeGestureImmediate() {
|
||||
resetSwipeVisualState();
|
||||
gestures.clearEdgeTapGestureState();
|
||||
gestures.clearSkipNextTap();
|
||||
}
|
||||
function clearDismissAnimTimers() {
|
||||
if (dismissCloseTimer) {
|
||||
clearTimeout(dismissCloseTimer);
|
||||
dismissCloseTimer = null;
|
||||
}
|
||||
if (dismissHintTimer) {
|
||||
clearTimeout(dismissHintTimer);
|
||||
dismissHintTimer = null;
|
||||
}
|
||||
}
|
||||
function finishEdgeGesture() {
|
||||
if (!swipeGesturesActive.value) {
|
||||
resetEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
const ax0 = Math.abs(edgeDx);
|
||||
const ay0 = Math.abs(edgeDy);
|
||||
if (!edgeKind && ax0 < 5 && ay0 < 5) {
|
||||
resetSwipeVisualState();
|
||||
return;
|
||||
}
|
||||
let kind = edgeKind;
|
||||
if (!kind) {
|
||||
const ax = Math.abs(edgeDx);
|
||||
const ay = Math.abs(edgeDy);
|
||||
if (ax < edgeHintPx && ay < edgeHintPx) {
|
||||
snapBackEdgeGesture();
|
||||
return;
|
||||
}
|
||||
kind = ax >= ay ? 'horizontal' : 'vertical-dismiss';
|
||||
}
|
||||
if (kind === 'horizontal') {
|
||||
if (edgeDx >= edgeCommitX && hasPreviousNav.value) {
|
||||
onNavigatePrevious();
|
||||
resetEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
if (edgeDx <= -edgeCommitX && hasNextNav.value) {
|
||||
onNavigateNext();
|
||||
resetEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
} else if (kind === 'vertical-dismiss') {
|
||||
if (edgeDy >= edgeCommitY) {
|
||||
clearDismissAnimTimers();
|
||||
dismissFlashActive = true;
|
||||
showDismissHint = true;
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
edgeKind = null;
|
||||
applySwipeTransform();
|
||||
syncNavigationGestureHintToStore();
|
||||
gestures.setSkipNextTap(200);
|
||||
// If we're in fullscreen, exit fullscreen instead of closing preview
|
||||
if (player.value?.fullscreen?.active) {
|
||||
player.value.fullscreen.exit();
|
||||
dismissHintTimer = setTimeout(() => {
|
||||
dismissHintTimer = null;
|
||||
dismissFlashActive = false;
|
||||
showDismissHint = false;
|
||||
mutations.setNavigationGestureHint({});
|
||||
}, 420);
|
||||
return;
|
||||
}
|
||||
// Normal close preview
|
||||
dismissCloseTimer = setTimeout(() => {
|
||||
dismissCloseTimer = null;
|
||||
onClosePreview();
|
||||
}, 120);
|
||||
dismissHintTimer = setTimeout(() => {
|
||||
dismissHintTimer = null;
|
||||
dismissFlashActive = false;
|
||||
showDismissHint = false;
|
||||
mutations.setNavigationGestureHint({});
|
||||
}, 420);
|
||||
return;
|
||||
}
|
||||
} else if (kind === 'vertical-fullscreen') {
|
||||
if (!player.value) {
|
||||
resetEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
if (edgeDy <= -edgeCommitY) {
|
||||
player.value.fullscreen.toggle();
|
||||
resetEdgeGestureImmediate();
|
||||
// Set skipNextTap to prevent play/pause toggle
|
||||
gestures.setSkipNextTap(300);
|
||||
return;
|
||||
}
|
||||
}
|
||||
snapBackEdgeGesture();
|
||||
}
|
||||
function teardownSwipeMouseDocListeners() {
|
||||
document.removeEventListener('mousemove', onSwipeMouseDocMove, true);
|
||||
document.removeEventListener('mouseup', onSwipeMouseDocUp, true);
|
||||
edgeMouseActive = false;
|
||||
}
|
||||
function onSwipeMouseDocMove(event: MouseEvent) {
|
||||
if (!edgeMouseActive || !swipeGesturesActive.value) {
|
||||
return;
|
||||
}
|
||||
edgeDx = event.clientX - edgeStartX;
|
||||
edgeDy = event.clientY - edgeStartY;
|
||||
decideEdgeKind();
|
||||
applyEdgeVisuals();
|
||||
if (Math.abs(edgeDx) > 3 || Math.abs(edgeDy) > 3) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
function onSwipeMouseDocUp(event: MouseEvent) {
|
||||
if (!edgeMouseActive) {
|
||||
teardownSwipeMouseDocListeners();
|
||||
return;
|
||||
}
|
||||
edgeDx = event.clientX - edgeStartX;
|
||||
edgeDy = event.clientY - edgeStartY;
|
||||
finishEdgeGesture();
|
||||
teardownSwipeMouseDocListeners();
|
||||
if (Math.abs(edgeDx) > 3 || Math.abs(edgeDy) > 3) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
function onSwipeMouseDown(event: MouseEvent) {
|
||||
if (event.button !== 0 || !swipeGesturesActive.value) return;
|
||||
// Don't start a gesture if we are selecting some text
|
||||
if ((window.getSelection()?.toString().length ?? 0) > 0) return;
|
||||
if (isControlOrMenuTarget(event.target)) {
|
||||
return;
|
||||
}
|
||||
clearDismissAnimTimers();
|
||||
teardownSwipeMouseDocListeners();
|
||||
edgeMouseActive = true;
|
||||
edgeStartX = event.clientX;
|
||||
edgeStartY = event.clientY;
|
||||
edgeDx = 0;
|
||||
edgeDy = 0;
|
||||
edgeKind = null;
|
||||
document.addEventListener('mousemove', onSwipeMouseDocMove, true);
|
||||
document.addEventListener('mouseup', onSwipeMouseDocUp, true);
|
||||
}
|
||||
function onSwipeTouchStart(event: TouchEvent) {
|
||||
if (!swipeGesturesActive.value || event.targetTouches.length !== 1) return;
|
||||
// Don't start a gesture if we are selecting some text
|
||||
if ((window.getSelection()?.toString().length ?? 0) > 0) return;
|
||||
if (isControlOrMenuTarget(event.target)) {
|
||||
swipeSuppressedTouchId = event.targetTouches[0]?.identifier ?? null;
|
||||
return;
|
||||
}
|
||||
swipeSuppressedTouchId = null;
|
||||
clearDismissAnimTimers();
|
||||
|
||||
// long-press timer
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.longPressPending.value = true;
|
||||
gestures.longPressTriggered.value = false;
|
||||
gestures.longPressPreviousSpeed.value = player.value?.speed || 1;
|
||||
|
||||
// only start timer if not already at 2x
|
||||
if (player.value && player.value.speed !== 2) {
|
||||
gestures.longPressTimer.value = window.setTimeout(() => {
|
||||
gestures.longPressTimer.value = null;
|
||||
if (player.value && gestures.longPressPending.value) {
|
||||
gestures.longPressPreviousSpeed.value = player.value.speed || 1;
|
||||
player.value.speed = 2;
|
||||
gestures.longPressTriggered.value = true;
|
||||
gestures.longPressPending.value = false;
|
||||
// Show toast
|
||||
speedToastVisible.value = true;
|
||||
speedToastMessage.value = '2x';
|
||||
}
|
||||
}, 500);
|
||||
} else {
|
||||
// If already at 2x don't change speed again
|
||||
if (player.value?.speed === 2) {
|
||||
gestures.longPressPending.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
const touch = event.targetTouches[0];
|
||||
if (!touch) return;
|
||||
edgeStartX = touch.pageX;
|
||||
edgeStartY = touch.pageY;
|
||||
edgeDx = 0;
|
||||
edgeDy = 0;
|
||||
edgeKind = null;
|
||||
dragOffsetX = 0;
|
||||
dragOffsetY = 0;
|
||||
}
|
||||
function onSwipeTouchMove(event: TouchEvent) {
|
||||
if (gestures.longPressTriggered.value) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// If there's significant movement cancel any pending long press so if when using swipe gestures they don't change speed
|
||||
if (gestures.longPressPending.value) {
|
||||
const touch = event.targetTouches[0];
|
||||
if (!touch) return;
|
||||
const dx = Math.abs(touch.pageX - edgeStartX);
|
||||
const dy = Math.abs(touch.pageY - edgeStartY);
|
||||
if (dx > 20 || dy > 20) {
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.longPressPending.value = false;
|
||||
gestures.clearEdgeTapGestureState();
|
||||
}
|
||||
}
|
||||
|
||||
if (!swipeGesturesActive.value || event.targetTouches.length !== 1) {
|
||||
if (edgeKind || edgeDx || edgeDy) {
|
||||
resetEdgeGestureImmediate();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const touch = event.targetTouches[0];
|
||||
if (!touch) return;
|
||||
if (
|
||||
swipeSuppressedTouchId !== null &&
|
||||
touch.identifier === swipeSuppressedTouchId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
edgeDx = touch.pageX - edgeStartX;
|
||||
edgeDy = touch.pageY - edgeStartY;
|
||||
decideEdgeKind();
|
||||
applyEdgeVisuals();
|
||||
const ax = Math.abs(edgeDx);
|
||||
const ay = Math.abs(edgeDy);
|
||||
if (edgeKind || ax > 14 || ay > 14) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
function onSwipeTouchEnd(event: TouchEvent) {
|
||||
if (!swipeGesturesActive.value || event.changedTouches.length === 0) {
|
||||
return;
|
||||
}
|
||||
const t = event.changedTouches[0];
|
||||
if (!t) return;
|
||||
if (
|
||||
swipeSuppressedTouchId !== null &&
|
||||
t.identifier === swipeSuppressedTouchId
|
||||
) {
|
||||
swipeSuppressedTouchId = null;
|
||||
return;
|
||||
}
|
||||
if (gestures.longPressPending.value) {
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.longPressPending.value = false;
|
||||
}
|
||||
|
||||
// Handle long-press release
|
||||
if (gestures.longPressTriggered.value) {
|
||||
gestures.clearLongPressTimer();
|
||||
speedToastVisible.value = false;
|
||||
if (player.value && gestures.longPressPreviousSpeed.value !== 2) {
|
||||
player.value.speed = gestures.longPressPreviousSpeed.value;
|
||||
}
|
||||
gestures.longPressTriggered.value = false;
|
||||
gestures.clearEdgeTapGestureState();
|
||||
resetEdgeGestureImmediate();
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal swipe gesture
|
||||
edgeDx = t.pageX - edgeStartX;
|
||||
edgeDy = t.pageY - edgeStartY;
|
||||
const ax = Math.abs(edgeDx);
|
||||
const ay = Math.abs(edgeDy);
|
||||
const hadLockedKind = edgeKind !== null;
|
||||
finishEdgeGesture();
|
||||
if (hadLockedKind || ax > 14 || ay > 14) {
|
||||
gestures.clearEdgeTapGestureState();
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
function onSwipeTouchCancel(event?: TouchEvent) {
|
||||
// If long-press was triggered, handle release
|
||||
if (gestures.longPressTriggered.value) {
|
||||
gestures.clearLongPressTimer();
|
||||
speedToastVisible.value = false;
|
||||
if (player.value && gestures.longPressPreviousSpeed.value !== 2) {
|
||||
player.value.speed = gestures.longPressPreviousSpeed.value;
|
||||
}
|
||||
gestures.longPressTriggered.value = false;
|
||||
gestures.longPressPending.value = false;
|
||||
resetEdgeGestureImmediate();
|
||||
if (event) event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// If long-press was pending clear it
|
||||
if (gestures.longPressPending.value) {
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.longPressPending.value = false;
|
||||
}
|
||||
|
||||
if (event?.changedTouches?.length) {
|
||||
const t = event.changedTouches[0];
|
||||
if (!t) return;
|
||||
if (
|
||||
swipeSuppressedTouchId !== null &&
|
||||
t.identifier === swipeSuppressedTouchId
|
||||
) {
|
||||
swipeSuppressedTouchId = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (edgeKind || edgeDx || edgeDy) {
|
||||
snapBackEdgeGesture();
|
||||
}
|
||||
}
|
||||
function setupSwipeGestures() {
|
||||
teardownSwipeGestures();
|
||||
if ((getPreviewType() !== 'video' && getPreviewType() !== 'audio') || !player.value) {
|
||||
return;
|
||||
}
|
||||
const surface = getGestureSurface();
|
||||
if (!surface) {
|
||||
return;
|
||||
}
|
||||
const touchOpts: AddEventListenerOptions = { passive: false };
|
||||
surface.addEventListener('touchstart', onSwipeTouchStart, touchOpts);
|
||||
surface.addEventListener('touchmove', onSwipeTouchMove, touchOpts);
|
||||
surface.addEventListener('touchend', onSwipeTouchEnd, touchOpts);
|
||||
surface.addEventListener('touchcancel', onSwipeTouchCancel, touchOpts);
|
||||
surface.addEventListener('mousedown', onSwipeMouseDown);
|
||||
|
||||
swipeCleanup = () => {
|
||||
surface.removeEventListener('touchstart', onSwipeTouchStart, touchOpts);
|
||||
surface.removeEventListener('touchmove', onSwipeTouchMove, touchOpts);
|
||||
surface.removeEventListener('touchend', onSwipeTouchEnd, touchOpts);
|
||||
surface.removeEventListener('touchcancel', onSwipeTouchCancel, touchOpts);
|
||||
surface.removeEventListener('mousedown', onSwipeMouseDown);
|
||||
teardownSwipeMouseDocListeners();
|
||||
};
|
||||
}
|
||||
function teardownSwipeGestures() {
|
||||
if (typeof swipeCleanup === 'function') {
|
||||
swipeCleanup();
|
||||
swipeCleanup = null;
|
||||
}
|
||||
clearDismissAnimTimers();
|
||||
resetEdgeGestureImmediate();
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.clearLongPressTimer();
|
||||
gestures.longPressPending.value = false;
|
||||
speedToastVisible.value = false;
|
||||
if (gestures.longPressTriggered.value && player.value && gestures.longPressPreviousSpeed.value !== 2) {
|
||||
player.value.speed = gestures.longPressPreviousSpeed.value;
|
||||
}
|
||||
gestures.longPressTriggered.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
speedToastVisible,
|
||||
speedToastMessage,
|
||||
setupSwipeGestures,
|
||||
teardownSwipeGestures,
|
||||
clearDismissAnimTimers,
|
||||
resetEdgeGestureImmediate,
|
||||
};
|
||||
}
|
||||
@@ -179,7 +179,9 @@
|
||||
<script>
|
||||
import { getCurrentInstance, shallowRef } from 'vue';
|
||||
import { useDoubleTapSeek } from '@/composables/player/useDoubleTapSeek.ts';
|
||||
import { useFullscreenState } from '@/composables/player/useFullscreenState.ts';
|
||||
import { useGestureState } from '@/composables/player/useGestureState.ts';
|
||||
import { useSwipeNavigationGestures } from '@/composables/player/useSwipeNavigationGestures.ts';
|
||||
import { useMediaPlayer } from '@/composables/player/useMediaPlayer.ts';
|
||||
import { useMediaKeyboard } from '@/composables/player/useMediaKeyboard.ts';
|
||||
import { usePlaybackQueue } from '@/composables/player/usePlaybackQueue.ts';
|
||||
@@ -301,7 +303,7 @@ export default {
|
||||
'close-preview': () => true,
|
||||
'needs-transcode': () => true,
|
||||
},
|
||||
setup(props) {
|
||||
setup(props, { emit }) {
|
||||
// The Plyr instance is held in a shallow ref: it is a third-party object and must not be deep-proxied.
|
||||
const player = shallowRef(null);
|
||||
const { applyCaptionSizeClass, syncCaptionSizeSettingsVisibility } = useCaptionSize({
|
||||
@@ -325,6 +327,18 @@ export default {
|
||||
getAudioGestureRoot: () => audioPlayerGestureRoot.value,
|
||||
requestPlay: () => vm?.requestPlay(),
|
||||
});
|
||||
const swipe = useSwipeNavigationGestures({
|
||||
player,
|
||||
gestures,
|
||||
getPreviewType: () => props.previewType,
|
||||
isMobileLyricsActive: () => vm?.mobileLyricsActive ?? false,
|
||||
getGestureSurface: doubleTap.getGestureSurface,
|
||||
isControlOrMenuTarget: doubleTap.isControlOrMenuTarget,
|
||||
onNavigatePrevious: () => emit('navigate-previous'),
|
||||
onNavigateNext: () => emit('navigate-next'),
|
||||
onClosePreview: () => emit('close-preview'),
|
||||
});
|
||||
const fullscreen = useFullscreenState({ onEnter: swipe.resetEdgeGestureImmediate });
|
||||
const playbackQueue = usePlaybackQueue({
|
||||
getReq: () => props.req,
|
||||
getListing: () => props.listing,
|
||||
@@ -356,6 +370,8 @@ export default {
|
||||
...playbackQueue,
|
||||
...gestures,
|
||||
...doubleTap,
|
||||
...swipe,
|
||||
...fullscreen,
|
||||
audioPlayerGestureRoot,
|
||||
applyCaptionSizeClass,
|
||||
syncCaptionSizeSettingsVisibility,
|
||||
@@ -383,35 +399,7 @@ export default {
|
||||
// Playback settings
|
||||
showDesktopPanel: readDesktopPanelOpen(),
|
||||
showMobileLyrics: readMobileLyricsOpen(),
|
||||
isFullscreen: false,
|
||||
|
||||
// Plyr video: full-frame edge gestures
|
||||
videoEdgeKind: null,
|
||||
videoEdgeStartX: 0,
|
||||
videoEdgeStartY: 0,
|
||||
videoEdgeDx: 0,
|
||||
videoEdgeDy: 0,
|
||||
videoDragOffsetX: 0,
|
||||
videoDragOffsetY: 0,
|
||||
videoGestureSnapBack: false,
|
||||
videoEdgeMouseActive: false,
|
||||
videoShowNavHint: false,
|
||||
videoNavHintDir: 'next',
|
||||
videoShowDismissHint: false,
|
||||
videoDismissFlashActive: false,
|
||||
videoEdgeHintPx: 44,
|
||||
videoEdgeCommitX: 130,
|
||||
videoEdgeCommitY: 110,
|
||||
videoEdgeRubberMax: 100,
|
||||
videoSwipeCleanup: null,
|
||||
/** When audio scrub/menu steals touchstart, ignore move/end for this touch id (Plyr has no video wrapper). */
|
||||
videoSwipeSuppressedTouchId: null,
|
||||
videoDismissCloseTimer: null,
|
||||
videoDismissHintTimer: null,
|
||||
|
||||
speedToastVisible: false,
|
||||
speedToastMessage: '',
|
||||
speedToastTimer: null,
|
||||
|
||||
captionSizeMenuInitialized: false,
|
||||
/** Native video: defer stream URL until play so opening preview does not range-fetch the file. */
|
||||
@@ -608,27 +596,6 @@ export default {
|
||||
fileName() {
|
||||
return this.req.name ? this.req.name.replace(/\.[^/.]+$/, "") : '';
|
||||
},
|
||||
videoSwipeGesturesActive() {
|
||||
return (
|
||||
(this.previewType === 'video' || this.previewType === 'audio') &&
|
||||
!!this.player && !this.mobileLyricsActive
|
||||
);
|
||||
},
|
||||
videoNavigationGestureAllowed() {
|
||||
return state.navigation.enabled && getters.currentPrompt() === null;
|
||||
},
|
||||
hasVideoPreviousNav() {
|
||||
if (getters.isPreviewPlaybackQueueNavMode()) {
|
||||
return this.videoNavigationGestureAllowed && getters.playbackQueueCanGoPrevious();
|
||||
}
|
||||
return this.videoNavigationGestureAllowed && state.navigation.previousLink !== '';
|
||||
},
|
||||
hasVideoNextNav() {
|
||||
if (getters.isPreviewPlaybackQueueNavMode()) {
|
||||
return this.videoNavigationGestureAllowed && getters.playbackQueueCanGoNext();
|
||||
}
|
||||
return this.videoNavigationGestureAllowed && state.navigation.nextLink !== '';
|
||||
},
|
||||
isMobile() {
|
||||
return getters.isMobile();
|
||||
},
|
||||
@@ -853,12 +820,7 @@ export default {
|
||||
},
|
||||
beforeUnmount() {
|
||||
// Cleanup timeouts
|
||||
for (const timeout of [
|
||||
this.videoDismissCloseTimer,
|
||||
this.videoDismissHintTimer,
|
||||
]) {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
}
|
||||
this.clearDismissAnimTimers();
|
||||
// Cleanup Plyr. Always finish listener/media-session cleanup even if a
|
||||
// teardown step throws, so an orphaned element cannot keep playing.
|
||||
this.disarmResumeOnNextGesture();
|
||||
@@ -938,7 +900,7 @@ export default {
|
||||
this.rememberFullscreenForTransition();
|
||||
}
|
||||
this.restoreNativePlay();
|
||||
this.teardownVideoSwipeGestures();
|
||||
this.teardownSwipeGestures();
|
||||
this.teardownDoubleTapSeek();
|
||||
this.clearRewindForwardFeedback();
|
||||
this.cleanupAudioVisualizer();
|
||||
@@ -2066,532 +2028,13 @@ export default {
|
||||
}
|
||||
if (this.previewType === 'video' || this.previewType === 'audio') {
|
||||
this.setupDoubleTapSeek();
|
||||
this.setupVideoSwipeGestures();
|
||||
this.setupSwipeGestures();
|
||||
this.setupRewindForwardFeedback();
|
||||
}
|
||||
if (this.previewType === 'video') {
|
||||
this.setupOverlaidHintController();
|
||||
}
|
||||
},
|
||||
applyVideoSwipeTransform() {
|
||||
const el = this.getGestureSurface();
|
||||
if (!el) {
|
||||
return;
|
||||
}
|
||||
const transition = this.videoGestureSnapBack
|
||||
? 'transform 0.22s cubic-bezier(0.32, 0.72, 0, 1)'
|
||||
: 'none';
|
||||
el.style.transition = transition;
|
||||
const x = this.videoDragOffsetX;
|
||||
const y = this.videoDragOffsetY;
|
||||
el.style.transform = x || y ? `translate(${x}px, ${y}px)` : '';
|
||||
},
|
||||
syncVideoNavigationGestureHintToStore() {
|
||||
const ax = Math.abs(this.videoEdgeDx);
|
||||
const ay = Math.abs(this.videoEdgeDy);
|
||||
const navPrevReady =
|
||||
this.hasVideoPreviousNav &&
|
||||
this.videoEdgeDx >= this.videoEdgeCommitX &&
|
||||
ax >= ay;
|
||||
const navNextReady =
|
||||
this.hasVideoNextNav &&
|
||||
this.videoEdgeDx <= -this.videoEdgeCommitX &&
|
||||
ax >= ay;
|
||||
const dismissReady =
|
||||
this.videoEdgeDy >= this.videoEdgeCommitY && ay >= ax;
|
||||
let kind = null;
|
||||
let commitReady = false;
|
||||
let flashClose = false;
|
||||
if (this.videoDismissFlashActive) {
|
||||
kind = 'close';
|
||||
commitReady = dismissReady;
|
||||
flashClose = true;
|
||||
} else if (this.videoShowDismissHint) {
|
||||
kind = 'close';
|
||||
commitReady = dismissReady;
|
||||
} else if (this.videoShowNavHint && this.videoNavHintDir === 'prev' && this.hasVideoPreviousNav) {
|
||||
kind = 'previous';
|
||||
commitReady = navPrevReady;
|
||||
} else if (this.videoShowNavHint && this.videoNavHintDir === 'next' && this.hasVideoNextNav) {
|
||||
kind = 'next';
|
||||
commitReady = navNextReady;
|
||||
}
|
||||
mutations.setNavigationGestureHint({ kind, commitReady, flashClose });
|
||||
},
|
||||
videoRubberband(value, max) {
|
||||
const sign = value < 0 ? -1 : 1;
|
||||
const a = Math.abs(value);
|
||||
if (a <= max) {
|
||||
return value;
|
||||
}
|
||||
return sign * (max + (a - max) * 0.32);
|
||||
},
|
||||
decideVideoEdgeKind() {
|
||||
if (this.videoEdgeKind) return;
|
||||
const ax = Math.abs(this.videoEdgeDx);
|
||||
const ay = Math.abs(this.videoEdgeDy);
|
||||
if (ax < 12 && ay < 12) return;
|
||||
if (this.previewType === 'audio') {
|
||||
if (ay > ax * 1.12 && ay > 14 && this.videoEdgeDy > 0) {
|
||||
this.videoEdgeKind = 'vertical-dismiss';
|
||||
} else if (ax > ay * 1.12 && ax > 14) {
|
||||
this.videoEdgeKind = 'horizontal';
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (ay > ax * 1.12 && ay > 14) {
|
||||
this.videoEdgeKind = this.videoEdgeDy > 0 ? 'vertical-dismiss' : 'vertical-fullscreen';
|
||||
} else if (ax > ay * 1.12 && ax > 14) {
|
||||
this.videoEdgeKind = 'horizontal';
|
||||
}
|
||||
},
|
||||
applyVideoEdgeVisuals() {
|
||||
if (this.previewType === 'audio' && this.videoEdgeDy < 0 && Math.abs(this.videoEdgeDy) > Math.abs(this.videoEdgeDx)) {
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
this.videoShowNavHint = false;
|
||||
this.videoShowDismissHint = false;
|
||||
this.applyVideoSwipeTransform();
|
||||
this.syncVideoNavigationGestureHintToStore();
|
||||
return;
|
||||
}
|
||||
|
||||
const kind = this.videoEdgeKind;
|
||||
if (kind === 'horizontal') {
|
||||
this.videoDragOffsetX = this.videoRubberband(this.videoEdgeDx, this.videoEdgeRubberMax);
|
||||
this.videoDragOffsetY = 0;
|
||||
const adx = Math.abs(this.videoEdgeDx);
|
||||
this.videoShowNavHint = adx >= this.videoEdgeHintPx;
|
||||
this.videoNavHintDir = this.videoEdgeDx > 0 ? 'prev' : 'next';
|
||||
if (this.videoNavHintDir === 'prev' && !this.hasVideoPreviousNav) this.videoShowNavHint = false;
|
||||
if (this.videoNavHintDir === 'next' && !this.hasVideoNextNav) this.videoShowNavHint = false;
|
||||
this.videoShowDismissHint = false;
|
||||
} else if (kind === 'vertical-dismiss') {
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = this.videoEdgeDy;
|
||||
this.videoShowDismissHint = this.videoEdgeDy >= this.videoEdgeHintPx;
|
||||
this.videoShowNavHint = false;
|
||||
} else if (kind === 'vertical-fullscreen') {
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = this.videoEdgeDy;
|
||||
this.videoShowDismissHint = false;
|
||||
this.videoShowNavHint = false;
|
||||
} else {
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
this.videoShowNavHint = false;
|
||||
this.videoShowDismissHint = false;
|
||||
}
|
||||
this.applyVideoSwipeTransform();
|
||||
this.syncVideoNavigationGestureHintToStore();
|
||||
},
|
||||
snapBackVideoEdgeGesture() {
|
||||
this.videoGestureSnapBack = true;
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
this.videoShowNavHint = false;
|
||||
this.videoShowDismissHint = false;
|
||||
this.videoEdgeKind = null;
|
||||
this.videoEdgeDx = 0;
|
||||
this.videoEdgeDy = 0;
|
||||
this.setSkipNextTap(200);
|
||||
this.applyVideoSwipeTransform();
|
||||
mutations.setNavigationGestureHint({});
|
||||
setTimeout(() => {
|
||||
this.videoGestureSnapBack = false;
|
||||
this.applyVideoSwipeTransform();
|
||||
}, 240);
|
||||
},
|
||||
resetVideoSwipeVisualState() {
|
||||
this.clearVideoDismissAnimTimers();
|
||||
this.videoSwipeSuppressedTouchId = null;
|
||||
this.videoEdgeKind = null;
|
||||
this.videoEdgeDx = 0;
|
||||
this.videoEdgeDy = 0;
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
this.videoShowNavHint = false;
|
||||
this.videoShowDismissHint = false;
|
||||
this.videoGestureSnapBack = false;
|
||||
this.videoDismissFlashActive = false;
|
||||
this.applyVideoSwipeTransform();
|
||||
mutations.setNavigationGestureHint({});
|
||||
},
|
||||
resetVideoEdgeGestureImmediate() {
|
||||
this.resetVideoSwipeVisualState();
|
||||
this.clearEdgeTapGestureState();
|
||||
this.clearSkipNextTap();
|
||||
},
|
||||
clearVideoDismissAnimTimers() {
|
||||
if (this.videoDismissCloseTimer) {
|
||||
clearTimeout(this.videoDismissCloseTimer);
|
||||
this.videoDismissCloseTimer = null;
|
||||
}
|
||||
if (this.videoDismissHintTimer) {
|
||||
clearTimeout(this.videoDismissHintTimer);
|
||||
this.videoDismissHintTimer = null;
|
||||
}
|
||||
},
|
||||
finishVideoEdgeGesture() {
|
||||
if (!this.videoSwipeGesturesActive) {
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
const ax0 = Math.abs(this.videoEdgeDx);
|
||||
const ay0 = Math.abs(this.videoEdgeDy);
|
||||
if (!this.videoEdgeKind && ax0 < 5 && ay0 < 5) {
|
||||
this.resetVideoSwipeVisualState();
|
||||
return;
|
||||
}
|
||||
let kind = this.videoEdgeKind;
|
||||
if (!kind) {
|
||||
const ax = Math.abs(this.videoEdgeDx);
|
||||
const ay = Math.abs(this.videoEdgeDy);
|
||||
if (ax < this.videoEdgeHintPx && ay < this.videoEdgeHintPx) {
|
||||
this.snapBackVideoEdgeGesture();
|
||||
return;
|
||||
}
|
||||
kind = ax >= ay ? 'horizontal' : 'vertical-dismiss';
|
||||
}
|
||||
if (kind === 'horizontal') {
|
||||
if (this.videoEdgeDx >= this.videoEdgeCommitX && this.hasVideoPreviousNav) {
|
||||
this.$emit('navigate-previous');
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
if (this.videoEdgeDx <= -this.videoEdgeCommitX && this.hasVideoNextNav) {
|
||||
this.$emit('navigate-next');
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
} else if (kind === 'vertical-dismiss') {
|
||||
if (this.videoEdgeDy >= this.videoEdgeCommitY) {
|
||||
this.clearVideoDismissAnimTimers();
|
||||
this.videoDismissFlashActive = true;
|
||||
this.videoShowDismissHint = true;
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
this.videoEdgeKind = null;
|
||||
this.applyVideoSwipeTransform();
|
||||
this.syncVideoNavigationGestureHintToStore();
|
||||
this.setSkipNextTap(200);
|
||||
// If we're in fullscreen, exit fullscreen instead of closing preview
|
||||
if (this.player?.fullscreen?.active) {
|
||||
this.player.fullscreen.exit();
|
||||
this.videoDismissHintTimer = setTimeout(() => {
|
||||
this.videoDismissHintTimer = null;
|
||||
this.videoDismissFlashActive = false;
|
||||
this.videoShowDismissHint = false;
|
||||
mutations.setNavigationGestureHint({});
|
||||
}, 420);
|
||||
return;
|
||||
}
|
||||
// Normal close preview
|
||||
this.videoDismissCloseTimer = setTimeout(() => {
|
||||
this.videoDismissCloseTimer = null;
|
||||
this.$emit('close-preview');
|
||||
}, 120);
|
||||
this.videoDismissHintTimer = setTimeout(() => {
|
||||
this.videoDismissHintTimer = null;
|
||||
this.videoDismissFlashActive = false;
|
||||
this.videoShowDismissHint = false;
|
||||
mutations.setNavigationGestureHint({});
|
||||
}, 420);
|
||||
return;
|
||||
}
|
||||
} else if (kind === 'vertical-fullscreen') {
|
||||
if (!this.player) {
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
return;
|
||||
}
|
||||
if (this.videoEdgeDy <= -this.videoEdgeCommitY) {
|
||||
this.player.fullscreen.toggle();
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
// Set skipNextTap to prevent play/pause toggle
|
||||
this.setSkipNextTap(300);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.snapBackVideoEdgeGesture();
|
||||
},
|
||||
teardownVideoSwipeMouseDocListeners() {
|
||||
document.removeEventListener('mousemove', this.onVideoSwipeMouseDocMove, true);
|
||||
document.removeEventListener('mouseup', this.onVideoSwipeMouseDocUp, true);
|
||||
this.videoEdgeMouseActive = false;
|
||||
},
|
||||
onVideoSwipeMouseDocMove(event) {
|
||||
if (!this.videoEdgeMouseActive || !this.videoSwipeGesturesActive) {
|
||||
return;
|
||||
}
|
||||
this.videoEdgeDx = event.clientX - this.videoEdgeStartX;
|
||||
this.videoEdgeDy = event.clientY - this.videoEdgeStartY;
|
||||
this.decideVideoEdgeKind();
|
||||
this.applyVideoEdgeVisuals();
|
||||
if (Math.abs(this.videoEdgeDx) > 3 || Math.abs(this.videoEdgeDy) > 3) {
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
onVideoSwipeMouseDocUp(event) {
|
||||
if (!this.videoEdgeMouseActive) {
|
||||
this.teardownVideoSwipeMouseDocListeners();
|
||||
return;
|
||||
}
|
||||
this.videoEdgeDx = event.clientX - this.videoEdgeStartX;
|
||||
this.videoEdgeDy = event.clientY - this.videoEdgeStartY;
|
||||
this.finishVideoEdgeGesture();
|
||||
this.teardownVideoSwipeMouseDocListeners();
|
||||
if (Math.abs(this.videoEdgeDx) > 3 || Math.abs(this.videoEdgeDy) > 3) {
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
onVideoSwipeMouseDown(event) {
|
||||
if (event.button !== 0 || !this.videoSwipeGesturesActive) return;
|
||||
// Don't start a gesture if we are selecting some text
|
||||
if (window.getSelection()?.toString().length > 0) return;
|
||||
if (this.isControlOrMenuTarget(event.target)) {
|
||||
return;
|
||||
}
|
||||
this.clearVideoDismissAnimTimers();
|
||||
this.teardownVideoSwipeMouseDocListeners();
|
||||
this.videoEdgeMouseActive = true;
|
||||
this.videoEdgeStartX = event.clientX;
|
||||
this.videoEdgeStartY = event.clientY;
|
||||
this.videoEdgeDx = 0;
|
||||
this.videoEdgeDy = 0;
|
||||
this.videoEdgeKind = null;
|
||||
document.addEventListener('mousemove', this.onVideoSwipeMouseDocMove, true);
|
||||
document.addEventListener('mouseup', this.onVideoSwipeMouseDocUp, true);
|
||||
},
|
||||
onVideoSwipeTouchStart(event) {
|
||||
if (!this.videoSwipeGesturesActive || event.targetTouches.length !== 1) return;
|
||||
// Don't start a gesture if we are selecting some text
|
||||
if (window.getSelection()?.toString().length > 0) return;
|
||||
if (this.isControlOrMenuTarget(event.target)) {
|
||||
this.videoSwipeSuppressedTouchId = event.targetTouches[0].identifier;
|
||||
return;
|
||||
}
|
||||
this.videoSwipeSuppressedTouchId = null;
|
||||
this.clearVideoDismissAnimTimers();
|
||||
|
||||
// long-press timer
|
||||
this.clearLongPressTimer();
|
||||
this.longPressPending = true;
|
||||
this.longPressTriggered = false;
|
||||
this.longPressPreviousSpeed = this.player?.speed || 1;
|
||||
|
||||
// only start timer if not already at 2x
|
||||
if (this.player && this.player.speed !== 2) {
|
||||
this.longPressTimer = setTimeout(() => {
|
||||
this.longPressTimer = null;
|
||||
if (this.player && this.longPressPending) {
|
||||
this.longPressPreviousSpeed = this.player.speed || 1;
|
||||
this.player.speed = 2;
|
||||
this.longPressTriggered = true;
|
||||
this.longPressPending = false;
|
||||
// Show toast
|
||||
this.speedToastVisible = true;
|
||||
this.speedToastMessage = '2x';
|
||||
}
|
||||
}, 500);
|
||||
} else {
|
||||
// If already at 2x don't change speed again
|
||||
if (this.player?.speed === 2) {
|
||||
this.longPressPending = false;
|
||||
}
|
||||
}
|
||||
|
||||
const touch = event.targetTouches[0];
|
||||
this.videoEdgeStartX = touch.pageX;
|
||||
this.videoEdgeStartY = touch.pageY;
|
||||
this.videoEdgeDx = 0;
|
||||
this.videoEdgeDy = 0;
|
||||
this.videoEdgeKind = null;
|
||||
this.videoDragOffsetX = 0;
|
||||
this.videoDragOffsetY = 0;
|
||||
},
|
||||
onVideoSwipeTouchMove(event) {
|
||||
if (this.longPressTriggered) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// If there's significant movement cancel any pending long press so if when using swipe gestures they don't change speed
|
||||
if (this.longPressPending) {
|
||||
const touch = event.targetTouches[0];
|
||||
const dx = Math.abs(touch.pageX - this.videoEdgeStartX);
|
||||
const dy = Math.abs(touch.pageY - this.videoEdgeStartY);
|
||||
if (dx > 20 || dy > 20) {
|
||||
this.clearLongPressTimer();
|
||||
this.longPressPending = false;
|
||||
this.clearEdgeTapGestureState();
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.videoSwipeGesturesActive || event.targetTouches.length !== 1) {
|
||||
if (this.videoEdgeKind || this.videoEdgeDx || this.videoEdgeDy) {
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const touch = event.targetTouches[0];
|
||||
if (
|
||||
this.videoSwipeSuppressedTouchId !== null &&
|
||||
touch.identifier === this.videoSwipeSuppressedTouchId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this.videoEdgeDx = touch.pageX - this.videoEdgeStartX;
|
||||
this.videoEdgeDy = touch.pageY - this.videoEdgeStartY;
|
||||
this.decideVideoEdgeKind();
|
||||
this.applyVideoEdgeVisuals();
|
||||
const ax = Math.abs(this.videoEdgeDx);
|
||||
const ay = Math.abs(this.videoEdgeDy);
|
||||
if (this.videoEdgeKind || ax > 14 || ay > 14) {
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
onVideoSwipeTouchEnd(event) {
|
||||
if (!this.videoSwipeGesturesActive || event.changedTouches.length === 0) {
|
||||
return;
|
||||
}
|
||||
const t = event.changedTouches[0];
|
||||
if (
|
||||
this.videoSwipeSuppressedTouchId !== null &&
|
||||
t.identifier === this.videoSwipeSuppressedTouchId
|
||||
) {
|
||||
this.videoSwipeSuppressedTouchId = null;
|
||||
return;
|
||||
}
|
||||
if (this.longPressPending) {
|
||||
this.clearLongPressTimer();
|
||||
this.longPressPending = false;
|
||||
}
|
||||
|
||||
// Handle long-press release
|
||||
if (this.longPressTriggered) {
|
||||
this.clearLongPressTimer();
|
||||
this.speedToastVisible = false;
|
||||
if (this.player && this.longPressPreviousSpeed !== 2) {
|
||||
this.player.speed = this.longPressPreviousSpeed;
|
||||
}
|
||||
this.longPressTriggered = false;
|
||||
this.clearEdgeTapGestureState();
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal swipe gesture
|
||||
this.videoEdgeDx = t.pageX - this.videoEdgeStartX;
|
||||
this.videoEdgeDy = t.pageY - this.videoEdgeStartY;
|
||||
const ax = Math.abs(this.videoEdgeDx);
|
||||
const ay = Math.abs(this.videoEdgeDy);
|
||||
const hadLockedKind = this.videoEdgeKind !== null;
|
||||
this.finishVideoEdgeGesture();
|
||||
if (hadLockedKind || ax > 14 || ay > 14) {
|
||||
this.clearEdgeTapGestureState();
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
onVideoSwipeTouchCancel(event) {
|
||||
// If long-press was triggered, handle release
|
||||
if (this.longPressTriggered) {
|
||||
this.clearLongPressTimer();
|
||||
this.speedToastVisible = false;
|
||||
if (this.player && this.longPressPreviousSpeed !== 2) {
|
||||
this.player.speed = this.longPressPreviousSpeed;
|
||||
}
|
||||
this.longPressTriggered = false;
|
||||
this.longPressPending = false;
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
if (event) event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// If long-press was pending clear it
|
||||
if (this.longPressPending) {
|
||||
this.clearLongPressTimer();
|
||||
this.longPressPending = false;
|
||||
}
|
||||
|
||||
if (event?.changedTouches?.length) {
|
||||
const t = event.changedTouches[0];
|
||||
if (
|
||||
this.videoSwipeSuppressedTouchId !== null &&
|
||||
t.identifier === this.videoSwipeSuppressedTouchId
|
||||
) {
|
||||
this.videoSwipeSuppressedTouchId = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (this.videoEdgeKind || this.videoEdgeDx || this.videoEdgeDy) {
|
||||
this.snapBackVideoEdgeGesture();
|
||||
}
|
||||
},
|
||||
setupVideoSwipeGestures() {
|
||||
this.teardownVideoSwipeGestures();
|
||||
if ((this.previewType !== 'video' && this.previewType !== 'audio') || !this.player) {
|
||||
return;
|
||||
}
|
||||
const surface = this.getGestureSurface();
|
||||
if (!surface) {
|
||||
return;
|
||||
}
|
||||
const touchOpts = { passive: false };
|
||||
surface.addEventListener('touchstart', this.onVideoSwipeTouchStart, touchOpts);
|
||||
surface.addEventListener('touchmove', this.onVideoSwipeTouchMove, touchOpts);
|
||||
surface.addEventListener('touchend', this.onVideoSwipeTouchEnd, touchOpts);
|
||||
surface.addEventListener('touchcancel', this.onVideoSwipeTouchCancel, touchOpts);
|
||||
surface.addEventListener('mousedown', this.onVideoSwipeMouseDown);
|
||||
|
||||
this.videoSwipeCleanup = () => {
|
||||
surface.removeEventListener('touchstart', this.onVideoSwipeTouchStart, touchOpts);
|
||||
surface.removeEventListener('touchmove', this.onVideoSwipeTouchMove, touchOpts);
|
||||
surface.removeEventListener('touchend', this.onVideoSwipeTouchEnd, touchOpts);
|
||||
surface.removeEventListener('touchcancel', this.onVideoSwipeTouchCancel, touchOpts);
|
||||
surface.removeEventListener('mousedown', this.onVideoSwipeMouseDown);
|
||||
this.teardownVideoSwipeMouseDocListeners();
|
||||
};
|
||||
},
|
||||
teardownVideoSwipeGestures() {
|
||||
if (typeof this.videoSwipeCleanup === 'function') {
|
||||
this.videoSwipeCleanup();
|
||||
this.videoSwipeCleanup = null;
|
||||
}
|
||||
this.clearVideoDismissAnimTimers();
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
this.clearLongPressTimer();
|
||||
this.clearLongPressTimer();
|
||||
this.longPressPending = false;
|
||||
this.speedToastVisible = false;
|
||||
if (this.longPressTriggered && this.player && this.longPressPreviousSpeed !== 2) {
|
||||
this.player.speed = this.longPressPreviousSpeed;
|
||||
}
|
||||
this.longPressTriggered = false;
|
||||
},
|
||||
async onFullscreenEnter() {
|
||||
this.isFullscreen = true;
|
||||
this.resetVideoEdgeGestureImmediate();
|
||||
// Allow free rotation when video enters full screen mode. This works even if the device's orientation is currently locked.
|
||||
try {
|
||||
await screen.orientation.lock('any');
|
||||
} catch (error) {
|
||||
// The NotSupportedError is thrown for non-mobile browsers and there seems to be no way to pre-check if it is supported.
|
||||
// -> Swallow NotSupportedError but let other errors be thrown.
|
||||
if (error.name !== 'NotSupportedError')
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
onFullscreenExit() {
|
||||
this.isFullscreen = false;
|
||||
if (!screen.orientation?.unlock) return;
|
||||
try {
|
||||
screen.orientation.unlock();
|
||||
} catch (error) {
|
||||
if (error.name !== 'NotSupportedError') throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Builds playback and caption menus once, then updates state without rebuilding again.
|
||||
applyCustomSettings(player) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user