Skip to content

The swiss army knife of lossless video/audio editing

Move timeline wheel logic into a pure function with tests

Claude32262c95e992
diff --git a/src/renderer/src/Timeline.tsx b/src/renderer/src/Timeline.tsx
index 2fab8985..0662bc17 100644
--- a/src/renderer/src/Timeline.tsx
+++ b/src/renderer/src/Timeline.tsx
@@ -19,7 +19,7 @@ import type { Frame } from './ffmpeg';
import type { FormatTimecode, InverseCutSegment, OverviewWaveform, RenderableWaveform, WaveformSlice, StateSegment, Thumbnail } from './types';
import Button from './components/Button';
import type { UseSegments } from './hooks/useSegments';
-import { keyMap } from './hooks/useTimelineScroll';
+import { keyMap } from './timelineWheel';
import { calculateTimelinePercent as calculateTimelinePercent2, calculateTimelinePos } from './util';
diff --git a/src/renderer/src/hooks/useTimelineScroll.ts b/src/renderer/src/hooks/useTimelineScroll.ts
index da8da090..541474f4 100644
--- a/src/renderer/src/hooks/useTimelineScroll.ts
+++ b/src/renderer/src/hooks/useTimelineScroll.ts
@@ -5,13 +5,7 @@ import { t } from 'i18next';
import normalizeWheel from './normalizeWheel';
import type { ModifierKey } from '../../../common/types';
import { getMetaKeyName } from '../util';
-
-export const keyMap = {
- ctrl: 'ctrlKey',
- shift: 'shiftKey',
- alt: 'altKey',
- meta: 'metaKey',
-} as const;
+import { getTimelineWheelAction } from '../timelineWheel';
export const getModifierKeyNames = () => ({
ctrl: t('Ctrl'),
@@ -35,22 +29,16 @@ function useTimelineScroll({ wheelSensitivity, mouseWheelZoomModifierKey, mouseW
}) {
const onWheel = useCallback<WheelEventHandler<Element>>((wheelEvent) => {
const { pixelX, pixelY } = normalizeWheel(wheelEvent);
- // console.log({ spinX, spinY, pixelX, pixelY });
-
- const direction = invertTimelineScroll ? 1 : -1;
-
- const makeUnit = (v: number) => ((direction * v) > 0 ? 1 : -1);
-
- if (wheelEvent[keyMap[mouseWheelZoomModifierKey]]) {
- // Wheel up / pinch out (negative delta) zooms in, like ctrl+wheel in browsers. Not affected by invertTimelineScroll, which is for seeking only.
- // https://github.com/mifi/lossless-cut/issues/2703 https://github.com/mifi/lossless-cut/issues/2962
- zoomRel(-pixelY * wheelSensitivity * 0.4);
- } else if (wheelEvent[keyMap[mouseWheelFrameSeekModifierKey]]) {
- shortStep(makeUnit(pixelX + pixelY));
- } else if (wheelEvent[keyMap[mouseWheelKeyframeSeekModifierKey]]) {
- seekClosestKeyframe(makeUnit(pixelX + pixelY));
- } else {
- seekRel(direction * (pixelX + pixelY) * wheelSensitivity * 0.2);
+ const { ctrlKey, shiftKey, altKey, metaKey } = wheelEvent;
+
+ const action = getTimelineWheelAction({ pixelX, pixelY, modifiers: { ctrlKey, shiftKey, altKey, metaKey }, wheelSensitivity, mouseWheelZoomModifierKey, mouseWheelFrameSeekModifierKey, mouseWheelKeyframeSeekModifierKey, invertTimelineScroll });
+
+ switch (action.type) {
+ case 'zoom': { zoomRel(action.amount); break; }
+ case 'frameSeek': { shortStep(action.direction); break; }
+ case 'keyframeSeek': { seekClosestKeyframe(action.direction); break; }
+ case 'seek': { seekRel(action.amount); break; }
+ default: { const exhaustive: never = action; throw new Error(`Unknown wheel action ${JSON.stringify(exhaustive)}`); }
}
}, [invertTimelineScroll, mouseWheelZoomModifierKey, mouseWheelFrameSeekModifierKey, mouseWheelKeyframeSeekModifierKey, zoomRel, wheelSensitivity, shortStep, seekClosestKeyframe, seekRel]);
diff --git a/src/renderer/src/timelineWheel.test.ts b/src/renderer/src/timelineWheel.test.ts
new file mode 100644
index 00000000..ffb804cb
--- /dev/null
+++ b/src/renderer/src/timelineWheel.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, test } from 'vitest';
+
+import { getTimelineWheelAction } from './timelineWheel';
+import type { ModifierKey } from '../../common/types';
+
+const noModifiers = { ctrlKey: false, shiftKey: false, altKey: false, metaKey: false };
+
+function getAction({ pixelX = 0, pixelY = 0, modifiers = {}, invertTimelineScroll = false, zoomKey = 'ctrl', frameSeekKey = 'alt', keyframeSeekKey = 'shift' }: {
+ pixelX?: number,
+ pixelY?: number,
+ modifiers?: Partial<typeof noModifiers>,
+ invertTimelineScroll?: boolean,
+ zoomKey?: ModifierKey,
+ frameSeekKey?: ModifierKey,
+ keyframeSeekKey?: ModifierKey,
+}) {
+ return getTimelineWheelAction({
+ pixelX,
+ pixelY,
+ modifiers: { ...noModifiers, ...modifiers },
+ wheelSensitivity: 1,
+ mouseWheelZoomModifierKey: zoomKey,
+ mouseWheelFrameSeekModifierKey: frameSeekKey,
+ mouseWheelKeyframeSeekModifierKey: keyframeSeekKey,
+ invertTimelineScroll,
+ });
+}
+
+// deltaY < 0 means wheel up (or trackpad pinch out)
+describe('zoom', () => {
+ test.each([false, true])('ctrl+wheel up zooms in, down zooms out, regardless of invertTimelineScroll=%s', (invertTimelineScroll) => {
+ expect(getAction({ pixelY: -100, modifiers: { ctrlKey: true }, invertTimelineScroll })).toEqual({ type: 'zoom', amount: 40 });
+ expect(getAction({ pixelY: 100, modifiers: { ctrlKey: true }, invertTimelineScroll })).toEqual({ type: 'zoom', amount: -40 });
+ });
+
+ test('custom zoom modifier', () => {
+ expect(getAction({ pixelY: -100, modifiers: { metaKey: true }, zoomKey: 'meta' })).toEqual({ type: 'zoom', amount: 40 });
+ });
+
+ test('ctrl with other zoom modifier seeks', () => {
+ expect(getAction({ pixelY: -100, modifiers: { ctrlKey: true }, zoomKey: 'meta' })).toEqual({ type: 'seek', amount: 20 });
+ expect(getAction({ pixelY: -100, modifiers: { ctrlKey: true }, zoomKey: 'meta', frameSeekKey: 'ctrl' })).toEqual({ type: 'frameSeek', direction: 1 });
+ });
+});
+
+describe('seek', () => {
+ test('wheel up seeks forward, inverted with invertTimelineScroll', () => {
+ expect(getAction({ pixelY: -100 })).toEqual({ type: 'seek', amount: 20 });
+ expect(getAction({ pixelY: -100, invertTimelineScroll: true })).toEqual({ type: 'seek', amount: -20 });
+ });
+
+ test('frame and keyframe seek follow invertTimelineScroll', () => {
+ expect(getAction({ pixelY: -100, modifiers: { altKey: true } })).toEqual({ type: 'frameSeek', direction: 1 });
+ expect(getAction({ pixelY: -100, modifiers: { altKey: true }, invertTimelineScroll: true })).toEqual({ type: 'frameSeek', direction: -1 });
+ expect(getAction({ pixelX: -100, modifiers: { shiftKey: true } })).toEqual({ type: 'keyframeSeek', direction: 1 });
+ expect(getAction({ pixelX: -100, modifiers: { shiftKey: true }, invertTimelineScroll: true })).toEqual({ type: 'keyframeSeek', direction: -1 });
+ });
+});
diff --git a/src/renderer/src/timelineWheel.ts b/src/renderer/src/timelineWheel.ts
new file mode 100644
index 00000000..29952853
--- /dev/null
+++ b/src/renderer/src/timelineWheel.ts
@@ -0,0 +1,42 @@
+import type { ModifierKey } from '../../common/types';
+
+export const keyMap = {
+ ctrl: 'ctrlKey',
+ shift: 'shiftKey',
+ alt: 'altKey',
+ meta: 'metaKey',
+} as const;
+
+export type WheelAction =
+ | { type: 'zoom', amount: number }
+ | { type: 'frameSeek', direction: number }
+ | { type: 'keyframeSeek', direction: number }
+ | { type: 'seek', amount: number };
+
+export function getTimelineWheelAction({ pixelX, pixelY, modifiers, wheelSensitivity, mouseWheelZoomModifierKey, mouseWheelFrameSeekModifierKey, mouseWheelKeyframeSeekModifierKey, invertTimelineScroll }: {
+ pixelX: number,
+ pixelY: number,
+ modifiers: Record<typeof keyMap[ModifierKey], boolean>,
+ wheelSensitivity: number,
+ mouseWheelZoomModifierKey: ModifierKey,
+ mouseWheelFrameSeekModifierKey: ModifierKey,
+ mouseWheelKeyframeSeekModifierKey: ModifierKey,
+ invertTimelineScroll?: boolean | undefined,
+}): WheelAction {
+ const isPressed = (key: ModifierKey) => modifiers[keyMap[key]];
+
+ // Wheel up / pinch out (negative delta) zooms in, like ctrl+wheel in browsers. Not affected by invertTimelineScroll, which is for seeking only.
+ // https://github.com/mifi/lossless-cut/issues/2703 https://github.com/mifi/lossless-cut/issues/2962
+ const getZoomAction = (delta: number): WheelAction => ({ type: 'zoom', amount: -delta * wheelSensitivity * 0.4 });
+
+ if (isPressed(mouseWheelZoomModifierKey)) return getZoomAction(pixelY);
+
+ const delta = pixelX + pixelY;
+
+ const direction = invertTimelineScroll ? 1 : -1;
+ const unit = direction * delta > 0 ? 1 : -1;
+
+ if (isPressed(mouseWheelFrameSeekModifierKey)) return { type: 'frameSeek', direction: unit };
+ if (isPressed(mouseWheelKeyframeSeekModifierKey)) return { type: 'keyframeSeek', direction: unit };
+ return { type: 'seek', amount: direction * delta * wheelSensitivity * 0.2 };
+}