8000 feat(transport): contextual I/O hotkeys + stale-drag fix by johannboehme · Pull Request #22 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions frontend/src/editor/components/FxHardwarePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ function Encoder({ kind, param, tint }: EncoderProps) {
const >
(e: ReactPointerEvent<HTMLDivElement>) => {
if (!dragging) return;
// Stale-drag guard: if the up event was lost (cursor left the
// window during the drag) the next move would otherwise keep
// adjusting the knob without a button held.
if (e.buttons === 0) {
setDragging(false);
return;
}
e.preventDefault();
// Drag UP increases the value. 200 px maps to the full storage
// range; Shift slows this 6×.
Expand All @@ -758,6 +765,24 @@ function Encoder({ kind, param, tint }: EncoderProps) {

const => setDragging(false), []);

// Belt-and-suspenders for off-window releases. The local up handler
// never fires when the cursor lands outside the document, so we
// listen on window too.
useEffect(() => {
if (!dragging) return;
function clear() {
setDragging(false);
}
window.addEventListener("pointerup", clear);
window.addEventListener("pointercancel", clear);
window.addEventListener("blur", clear);
return () => {
window.removeEventListener("pointerup", clear);
window.removeEventListener("pointercancel", clear);
window.removeEventListener("blur", clear);
};
}, [dragging]);

const => {
setFxDefault(kind, param.id, param.defaultValue);
}, [kind, param.id, param.defaultValue, setFxDefault]);
Expand Down Expand Up @@ -1924,6 +1949,27 @@ function useAdsrDrag(opts: {
axis: AdsrAxis;
} | null>(null);

// Belt-and-suspenders against stuck drags when the pointerup is lost
// off-window or off-element. Pairs with the `e.buttons === 0` guard
// in onPointerMove for re-entries that arrive without an up event.
const setActive = opts.setActive;
useEffect(() => {
function clearStaleDrag() {
if (startRef.current) {
startRef.current = null;
setActive(null);
}
}
window.addEventListener("pointerup", clearStaleDrag);
window.addEventListener("pointercancel", clearStaleDrag);
window.addEventListener("blur", clearStaleDrag);
return () => {
window.removeEventListener("pointerup", clearStaleDrag);
window.removeEventListener("pointercancel", clearStaleDrag);
window.removeEventListener("blur", clearStaleDrag);
};
}, [setActive]);

const beginDrag = (axis: AdsrAxis, e: ReactPointerEvent<Element>) => {
(e.target as Element).setPointerCapture?.(e.pointerId);
startRef.current = {
Expand All @@ -1938,6 +1984,15 @@ function useAdsrDrag(opts: {
const ReactPointerEvent<Element>) => {
const start = startRef.current;
if (!start) return;
// Stale-drag guard. setPointerCapture can lose the up event when
// the cursor leaves the browser window — without this, the next
// move snaps the knot back to the cursor as if the user were still
// holding the button. `e.buttons === 0` means no button is pressed.
if (e.buttons === 0) {
startRef.current = null;
opts.setActive(null);
return;
}
const sens = e.shiftKey ? 6 : 1;
const dxPx = (e.clientX - start.x) / sens;
const dyPx = (e.clientY - start.y) / sens;
Expand Down
59 changes: 58 additions & 1 deletion frontend/src/editor/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,34 @@ export function Timeline({
updateLaneScroll();
}, [clips.length]);

// Global drag terminator. setPointerCapture on the canvas SHOULD
// route a pointerup to us regardless of where the cursor lands, but
// in practice the up event can be lost (cursor leaves the browser
// window, OS swallows the up, capture interrupted by a layout
// change). When that happens the dragRef stays armed and the next
// pointermove over the canvas snaps the trim/clip to the cursor as
// if the user were still holding the button — the bug the user
// reports as "stays in zieh-modus". Listening on window covers the
// off-canvas releases too. Pairs with the `e.buttons === 0` guard
// in onPointerMove for re-entry without a release event at all.
useEffect(() => {
function clearStaleDrag() {
if (dragRef.current) {
dragRef.current = null;
setActiveClipMoveDragId(null);
frozenTimelineRangeRef.current = null;
}
}
window.addEventListener("pointerup", clearStaleDrag);
window.addEventListener("pointercancel", clearStaleDrag);
window.addEventListener("blur", clearStaleDrag);
return () => {
window.removeEventListener("pointerup", clearStaleDrag);
window.removeEventListener("pointercancel", clearStaleDrag);
window.removeEventListener("blur", clearStaleDrag);
};
}, [setActiveClipMoveDragId]);

// ---- Per-cam thumbnail Image objects ----
// Video clips: load the frames-strip (multiple stills laid out
// horizontally). Image clips: load the image asset itself so the lane
Expand Down Expand Up @@ -673,6 +701,18 @@ export function Timeline({
return null;
}

/** Lane-only hit-test: returns the clip whose lane band contains `y`,
* regardless of whether the pointer's `x` lands inside the pill. Used
* to make the WHOLE row a selection target so the user can park the
* playhead with a normal click and still send I/O to that clip. */
function findLaneAt(y: number): { clip: Clip; band: { top: number; bottom: number } } | null {
for (let i = 0; i < clips.length; i++) {
const band = videoBands[i];
if (y >= band.top && y < band.bottom) return { clip: clips[i], band };
}
return null;
}

/** Edge-handle hit-test for clip resizing. Detects whether the pointer
* is on the LEFT or RIGHT edge of a clip's pill (within HANDLE_HIT px).
* Image clips: both edges are draggable. Video clips: trim handles on
Expand Down Expand Up @@ -888,7 +928,13 @@ export function Timeline({
dragRef.current = { kind: "playhead" };
}
} else {
setSelectedClipId(null);
// Empty area inside a video lane → still treat the lane as a
// selection target so I/O hotkeys (and any per-clip controls) can
// address that clip without forcing the playhead onto its pill.
// Falls back to deselecting only if the pointer is outside any
// lane band.
const lane = findLaneAt(y);
setSelectedClipId(lane ? lane.clip.id : null);
seek(snapped(tRaw, e));
dragRef.current = { kind: "playhead" };
}
Expand All @@ -899,6 +945,17 @@ export function Timeline({
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;

// Stale-drag guard. If we have a pending drag but the pointer is
// moving WITHOUT any mouse button pressed, the pointerup must have
// fired outside our capture (e.g. user released the mouse off-canvas
// or in a place where the OS swallowed the up event). Clear the drag
// so the next move doesn't snap the trim/clip back to the cursor.
// `e.buttons` is a bitmask of currently-held buttons — 0 means none.
if (dragRef.current && e.buttons === 0) {
onPointerUp();
return;
}

// Update tracked pointer position whenever the pointer is captured
// here. The map only contains pointers that went through the
// canvas's onPointerDown, so plain hover events are skipped.
Expand Down
144 changes: 118 additions & 26 deletions frontend/src/editor/components/TransportBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import { useEffect } from "react";
import { useEditorStore } from "../store";
import { effectiveAudioStartS } from "../selectors/timing";
import {
classifyIOTarget,
imageInAtPlayhead,
imageOutAtPlayhead,
videoSourceTimeAtPlayhead,
} from "../io-points";
import { useRegisterShortcut } from "../shortcuts/useRegisterShortcut";
import { useIsNarrowViewport } from "../use-is-narrow";
import { ChunkyButton } from "./ChunkyButton";
Expand All @@ -20,6 +26,31 @@ import {
StepFwdIcon,
} from "./icons";

type IOContextKind = "loop" | "video" | "image" | "master";

function ioInDescription(k: IOContextKind): string {
switch (k) {
case "loop":
return "Set loop in-point at the playhead";
case "video":
case "image":
return "Set clip in-point at the playhead";
case "master":
return "Set in-point at the playhead";
}
}
function ioOutDescription(k: IOContextKind): string {
switch (k) {
case "loop":
return "Set loop out-point at the playhead";
case "video":
case "image":
return "Set clip out-point at the playhead";
case "master":
return "Set out-point at the playhead";
}
}

export function TransportBar() {
const meta = useEditorStore((s) => s.jobMeta);
const isPlaying = useEditorStore((s) => s.playback.isPlaying);
Expand All @@ -36,6 +67,21 @@ export function TransportBar() {
const seek = useEditorStore((s) => s.seek);
const stepByActiveSnap = useEditorStore((s) => s.stepByActiveSnap);
const shiftLoop = useEditorStore((s) => s.shiftLoop);
// Per-clip trim/placement actions: I/O routes here when a video or
// image clip is selected. Same store actions the move-mode drag
// handles call, so behavior + clamps stay identical.
const setVideoClipTrim = useEditorStore((s) => s.setVideoClipTrim);
const setImageClipDuration = useEditorStore((s) => s.setImageClipDuration);
const setClipStartOffset = useEditorStore((s) => s.setClipStartOffset);
// Read-only subscriptions to drive aria-labels + shortcut hints.
const selectedClipId = useEditorStore((s) => s.selectedClipId);
const ioContextKind = useEditorStore((s) => {
if (s.playback.loop) return "loop" as const;
if (s.selectedClipId === null) return "master" as const;
const clip = s.clips.find((c) => c.id === s.selectedClipId);
if (!clip) return "master" as const;
return clip.kind === "image" ? ("image" as const) : ("video" as const);
});
// Phone-sized viewports get an aggressively compacted transport bar:
// every transport stepper + Play + IN/OUT/LOOP collapses to xs
// (h-8 ≈ 28 px wide icon-only, no min-w) so the entire 8-button
Expand All @@ -61,28 +107,74 @@ export function TransportBar() {
seek(t + deltaSec);
}

// IN/OUT semantics are modal: while a loop region is active they edit
// the loop boundaries (so the user can sculpt a practice loop with the
// playhead); otherwise they edit the trim/export region. Same logic in
// both the buttons and the I/O shortcuts.
// IN/OUT semantics are contextual:
// loop active → edit the loop boundaries
// video clip selected → edit that clip's source-trim (clamped to
// [0, sourceDurationS] by the store action)
// image clip selected → edit that clip's master-pill edges
// (mirror of the move-mode drag handles)
// else → edit master trim (export region)
// Same logic for buttons and keyboard shortcuts.
function setInPointAtPlayhead() {
const t = useEditorStore.getState().playback.currentTime;
if (loop) {
const newStart = t;
const newEnd = Math.max(loop.end, newStart + 1 / fps);
setLoop({ start: newStart, end: newEnd });
} else {
setTrim({ in: t, out: trim.out });
const s = useEditorStore.getState();
const t = s.playback.currentTime;
const target = classifyIOTarget({
loop,
selectedClipId: s.selectedClipId,
clips: s.clips,
});
switch (target.kind) {
case "loop": {
const newStart = t;
const newEnd = Math.max(loop!.end, newStart + 1 / fps);
setLoop({ start: newStart, end: newEnd });
return;
}
case "video": {
const sourceT = videoSourceTimeAtPlayhead(target.clip, t);
const outS = target.clip.trimOutS ?? target.clip.sourceDurationS;
setVideoClipTrim(target.clip.id, sourceT, outS);
return;
}
case "image": {
const next = imageInAtPlayhead(target.clip, t);
setClipStartOffset(target.clip.id, next.startOffsetS);
setImageClipDuration(target.clip.id, next.durationS);
return;
}
case "master":
setTrim({ in: t, out: trim.out });
return;
}
}
function setOutPointAtPlayhead() {
const t = useEditorStore.getState().playback.currentTime;
if (loop) {
const newEnd = t;
const newStart = Math.min(loop.start, newEnd - 1 / fps);
setLoop({ start: newStart, end: newEnd });
} else {
setTrim({ in: trim.in, out: t });
const s = useEditorStore.getState();
const t = s.playback.currentTime;
const target = classifyIOTarget({
loop,
selectedClipId: s.selectedClipId,
clips: s.clips,
});
switch (target.kind) {
case "loop": {
const newEnd = t;
const newStart = Math.min(loop!.start, newEnd - 1 / fps);
setLoop({ start: newStart, end: newEnd });
return;
}
case "video": {
const sourceT = videoSourceTimeAtPlayhead(target.clip, t);
const inS = target.clip.trimInS ?? 0;
setVideoClipTrim(target.clip.id, inS, sourceT);
return;
}
case "image": {
setImageClipDuration(target.clip.id, imageOutAtPlayhead(target.clip, t));
return;
}
case "master":
setTrim({ in: trim.in, out: t });
return;
}
}

Expand Down Expand Up @@ -156,6 +248,10 @@ export function TransportBar() {
setPlaying,
setLoop,
setTrim,
setVideoClipTrim,
setImageClipDuration,
setClipStartOffset,
selectedClipId,
trim.in,
trim.out,
stepByActiveSnap,
Expand Down Expand Up @@ -186,18 +282,14 @@ export function TransportBar() {
useRegisterShortcut({
id: "transport.in",
keys: ["I"],
description: loop
? "Set loop in-point at the playhead"
: "Set trim in-point at the playhead",
description: ioInDescription(ioContextKind),
group: "Transport",
icon: <InIcon />,
});
useRegisterShortcut({
id: "transport.out",
keys: ["O"],
description: loop
? "Set loop out-point at the playhead"
: "Set trim out-point at the playhead",
description: ioOutDescription(ioContextKind),
group: "Transport",
icon: <OutIcon />,
});
Expand Down Expand Up @@ -293,7 +385,7 @@ export function TransportBar() {
size={trimSize}
>
iconLeft={<InIcon />}
aria-label={loop ? "Set loop in-point at the playhead" : "Set in-point at the playhead"}
aria-label={ioInDescription(ioContextKind)}
>
{!isNarrow && "IN"}
</ChunkyButton>
Expand All @@ -302,7 +394,7 @@ export function TransportBar() {
size={trimSize}
>
iconLeft={<OutIcon />}
aria-label={loop ? "Set loop out-point at the playhead" : "Set out-point at the playhead"}
aria-label={ioOutDescription(ioContextKind)}
>
{!isNarrow && "OUT"}
</ChunkyButton>
Expand Down
Loading
Loading
0