From ba764f902e0e2434faf7bdc196a7639fa42d3017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 01:53:33 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(app):=20keyboard=20trim-to-pla?= =?UTF-8?q?yhead=20(debt=20#42)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trim was pointer-only. Adds TrimInToPlayhead/TrimOutToPlayhead on ctrl+{/ctrl+} (the bracket family's last free rung: bare [ ] nudge, { } walk selection, ctrl+[ ctrl+] jump sync points), wired through the same PlaybackSession::trim_clip primitive the pointer drag commits with, gated by the oracle on a subject cut with the playhead on it, and given the same spine widget home as the nudge pair. --- DESIGN.md | 4 ++ crates/app/src/keymap.rs | 30 ++++++++++- crates/app/src/oracle.rs | 12 +++++ crates/app/src/player/actions.rs | 37 +++++++++++++ crates/app/src/tests/editing.rs | 88 +++++++++++++++++++++++++++++++ crates/app/src/ui/spine_stance.rs | 23 ++++++++ 6 files changed, 193 insertions(+), 1 deletion(-) diff --git a/DESIGN.md b/DESIGN.md index 5e00ef3..a6a242e 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -142,6 +142,10 @@ The cut is the subject. Grammar (implement before any cosmetics): - `,` `.` — walk cuts (shift = stride 10). The cut readout is the odometer. - `[` `]` — no-aim trim of the subject cut, frame detents, works at any zoom (even 4px clips). +- `ctrl+{` `ctrl+}` — trim-to-playhead (debt #42): snaps the subject cut's in/out edge straight to + the playhead, one press, one undo step -- the keyboard's own version of dragging the edge to a + spot instead of nudging it a detent at a time. Needs a subject cut with the playhead resting on + it, same as the pair above. - `/` — loop-trim: loop around the subject cut while trimming (the modernized Avid trim mode). - Two-up OUT|IN on the screen when resting on a cut. - `j k l` — shuttle; `s` — split; `↵` context-dependent commit; `esc` always retreats. diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index 68afeb8..1b43c31 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -175,6 +175,16 @@ actions! { /// Loop-trim: loops around the subject cut while trimming it (DESIGN.md /// §6, "the modernized Avid trim mode"). LoopTrim, + /// The keyboard's own version of the pointer's drag-the-edge-to-a-spot + /// trim (debt #42: trim used to be pointer-only): snaps the subject + /// cut's in point straight to the playhead, one press, one undo step -- + /// [`ActionId::TrimIn`]'s frame-detent nudge covers the other case. Bare + /// `[`/`{`/ctrl+`[` are all spoken for (nudge, select-prev, sync point), + /// so this takes the next rung of the same bracket family: ctrl held on + /// the shifted bracket, `ctrl+{` (what ctrl+shift+[ actually types). + TrimInToPlayhead, + /// The pair's other half, the subject cut's out point, on `ctrl+}`. + TrimOutToPlayhead, /// Dissolves the selected video clip into its neighbour /// ([`Project::set_transition_out`]): two adjacent clips picked on one /// lane, or a single pick dissolved into the clip right after it. A @@ -296,6 +306,8 @@ impl ActionId { ActionId::TrimIn => "Trim the subject cut's in point", ActionId::TrimOut => "Trim the subject cut's out point", ActionId::LoopTrim => "Loop-trim the subject cut", + ActionId::TrimInToPlayhead => "Trim the subject cut's in point to the playhead", + ActionId::TrimOutToPlayhead => "Trim the subject cut's out point to the playhead", ActionId::Dissolve => "Dissolve into the next clip", ActionId::Mix => "Mix: track volumes and the limiter…", ActionId::ToggleSnap => "Snap on / off (edges, the playhead, the start)", @@ -377,6 +389,8 @@ impl ActionId { ActionId::TrimIn => "trim-in", ActionId::TrimOut => "trim-out", ActionId::LoopTrim => "loop-trim", + ActionId::TrimInToPlayhead => "trim-in-to-playhead", + ActionId::TrimOutToPlayhead => "trim-out-to-playhead", ActionId::Dissolve => "dissolve", ActionId::Mix => "mix", ActionId::ToggleSnap => "toggle-snap", @@ -444,6 +458,8 @@ impl ActionId { | ActionId::TrimIn | ActionId::TrimOut | ActionId::LoopTrim + | ActionId::TrimInToPlayhead + | ActionId::TrimOutToPlayhead | ActionId::Dissolve => Category::Clips, // The project's own picture size is not a clip's business, and not // a file operation either: it is what the viewer is looking at. @@ -1182,6 +1198,15 @@ impl Keymap { // Loop-trim (DESIGN.md §6, "the modernized Avid trim mode"): // bare "/" was free. b(ActionId::LoopTrim, "/", false), + // Trim-to-playhead (debt #42): the whole bracket family is + // already spoken for -- bare `[`/`]` nudge, `{`/`}` walk the + // selection, ctrl+`[`/ctrl+`]` jump sync points -- so this + // takes the one rung left in the same family, ctrl held on + // the shifted bracket (`ctrl+{`/`ctrl+}`, what + // ctrl+shift+[/ctrl+shift+] actually type per this table's + // own shift note above). + b(ActionId::TrimInToPlayhead, "{", true), + b(ActionId::TrimOutToPlayhead, "}", true), // Dissolve takes ctrl+x: the join it draws on the timeline is // an X (widgets.rs, dissolve_glyph) -- "d" itself is taken // both plain (Detach) and, in the keymap's own rebind test, @@ -1491,7 +1516,7 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 76); + assert_eq!(k.entries().len(), 78); // The cut odometer: bare walks one, `<`/`>` (what shift+,/shift+. // actually type) strides ten. assert_eq!(k.lookup(".", false), Some(ActionId::WalkCutNext)); @@ -1502,6 +1527,9 @@ mod tests { // The trim pair, on its designed bare brackets (DESIGN.md §6). assert_eq!(k.lookup("[", false), Some(ActionId::TrimIn)); assert_eq!(k.lookup("]", false), Some(ActionId::TrimOut)); + // Trim-to-playhead (debt #42): the same family, one rung further. + assert_eq!(k.lookup("{", true), Some(ActionId::TrimInToPlayhead)); + assert_eq!(k.lookup("}", true), Some(ActionId::TrimOutToPlayhead)); assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); diff --git a/crates/app/src/oracle.rs b/crates/app/src/oracle.rs index c1c3c77..5f02969 100644 --- a/crates/app/src/oracle.rs +++ b/crates/app/src/oracle.rs @@ -302,6 +302,18 @@ pub(crate) fn enable(action: ActionId, ctx: Ctx) -> Enable { { Enable::No("no subject cut") } + // Trim-to-playhead (debt #42): the keyboard's own version of the + // pointer's drag-the-edge-to-a-spot trim, so it needs the two things + // that gesture needs -- a subject cut, and the playhead actually + // resting somewhere on it, or the "spot" the edge would snap to is + // not on the clip at all. + ActionId::TrimInToPlayhead | ActionId::TrimOutToPlayhead => match ctx.clip { + None => Enable::No("no subject cut"), + Some((clip, _)) if ctx.playhead < clip.start || ctx.playhead > clip.end() => { + Enable::No("playhead is outside the subject cut") + } + _ => Enable::Yes, + }, ActionId::Paste if !ctx.clipboard => Enable::No("nothing copied yet"), // Nothing to draw over the picture, so nothing to switch off: the // library says how subtitles arrive, and this row would flip a state diff --git a/crates/app/src/player/actions.rs b/crates/app/src/player/actions.rs index 6f53aa1..ccdca0a 100644 --- a/crates/app/src/player/actions.rs +++ b/crates/app/src/player/actions.rs @@ -201,6 +201,11 @@ impl Player { ActionId::TrimIn => self.nudge_cut(Edge::Start, 1, cx), ActionId::TrimOut => self.nudge_cut(Edge::End, -1, cx), ActionId::LoopTrim => self.toggle_loop_trim(cx), + // Trim-to-playhead (debt #42): the same `Edge::Start`/`Edge::End` + // split as the nudge pair above, at the playhead's frame instead + // of a one-frame detent. + ActionId::TrimInToPlayhead => self.trim_cut_to_playhead(Edge::Start, cx), + ActionId::TrimOutToPlayhead => self.trim_cut_to_playhead(Edge::End, cx), } } @@ -599,6 +604,38 @@ impl Player { cx.notify(); } + /// Trim-to-playhead (debt #42): the keyboard's own version of the + /// pointer's drag-the-edge-to-a-spot trim ([`Player::trim_to`] / + /// [`Player::commit_trim`]) -- same [`PlaybackSession::trim_clip`] + /// primitive either path ends at, so a hand on the keyboard gets the + /// same undo step and the same room clamp a drag gets, just aimed at + /// the playhead instead of wherever the pointer stopped. The oracle + /// already refused this with no subject cut or a playhead off it, so an + /// anchor and a frame are here to read. + pub(crate) fn trim_cut_to_playhead(&mut self, edge: Edge, cx: &mut Context) { + let Some((lane, idx)) = self.selected.anchor() else { + return; + }; + let Some(session) = &self.session else { + return; + }; + let to = frame_at(session.now(), self.fps); + self.mark_dirty(); + let trimmed = self + .session + .as_mut() + .is_some_and(|s| s.trim_clip(lane, idx, edge, to)); + if trimmed { + self.reset_after_reseek(); + // Loop-trim follows the edge it is trimming, same as the nudge + // pair above. + if self.loop_trim.is_some() { + self.loop_trim = self.cut_span(lane, idx); + } + } + cx.notify(); + } + /// Loop-trim (DESIGN.md §6, `/`): loops the transport around the subject /// cut's own span while `[`/`]` move its edges -- the modernized Avid /// trim mode. Neither direction touches play/pause: off drops the window diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index 1f837e7..517d48a 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -1797,6 +1797,94 @@ fn a_clip_trimmed_by_its_edge_plays_less_of_its_file() { assert_eq!(session.lane_clips(Lane::V1)[0], whole); } +/// Trim-to-playhead (debt #42): [`Player::trim_cut_to_playhead`] reads +/// `frame_at(session.now(), fps)` and calls exactly the [`PlaybackSession:: +/// trim_clip`] primitive [`Player::commit_trim`]'s pointer path does, so +/// this exercises that same door at the playhead's own frame -- the clip's +/// start moves to where the playhead rests, one undo step takes it back. +#[test] +fn trim_to_playhead_moves_the_selected_clips_edge_and_undoes_in_one_step() { + use engine::project::Edge; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let whole = session.lane_clips(Lane::V1)[0]; + let fps = session.meta().frame_rate; + + // Playhead mid-clip, a second in on a five-second timeline. + session.seek(1.0); + let playhead = frame_at(session.now(), fps); + assert!(playhead > whole.start && playhead < whole.end(), "mid-clip"); + + // Trim-in: the head follows the playhead, same as a pointer drag would + // stop there. + assert!(session.trim_clip(Lane::V1, 0, Edge::Start, playhead)); + assert_eq!(session.lane_clips(Lane::V1)[0].start, playhead); + assert!(session.undo(), "one step for the whole press"); + assert_eq!(session.lane_clips(Lane::V1)[0], whole, "the head is back"); + + // Trim-out: the same door, the tail this time. + session.seek(4.0); + let playhead = frame_at(session.now(), fps); + assert!(playhead > whole.start && playhead < whole.end(), "mid-clip"); + assert!(session.trim_clip(Lane::V1, 0, Edge::End, playhead)); + assert_eq!(session.lane_clips(Lane::V1)[0].end(), playhead); + assert!(session.undo(), "one step for the whole press"); + assert_eq!(session.lane_clips(Lane::V1)[0], whole, "the tail is back"); +} + +/// The oracle side of trim-to-playhead (debt #42): needs a subject cut *and* +/// the playhead actually resting on it, unlike the nudge pair above it which +/// only needs a cut -- the "spot" a keyless drag would snap to has to be on +/// the clip at all. +#[test] +fn trim_to_playhead_needs_a_subject_cut_with_the_playhead_on_it() { + let clip = Clip { + fade_in: 0, + fade_out: 0, + transition_out: 0, + start: 100, + in_frame: 0, + out_frame: 60, + source: 0, + link: None, + eq: None, + color: None, + transform: None, + fit: FitPolicy::default(), + speed: Speed::NORMAL, + }; + assert_eq!( + enable( + ActionId::TrimInToPlayhead, + Ctx { + timeline: true, + ..Ctx::default() + } + ), + Enable::No("no subject cut"), + "nothing marked" + ); + let off = Ctx { + clip: Some((clip, Lane::V1)), + playhead: 5, + timeline: true, + ..Ctx::default() + }; + assert_eq!( + enable(ActionId::TrimOutToPlayhead, off), + Enable::No("playhead is outside the subject cut") + ); + let on = Ctx { + clip: Some((clip, Lane::V1)), + playhead: clip.start, + timeline: true, + ..Ctx::default() + }; + assert_eq!(enable(ActionId::TrimInToPlayhead, on), Enable::Yes); + assert_eq!(enable(ActionId::TrimOutToPlayhead, on), Enable::Yes); +} + /// The move-a-clip-between-tracks path through the door the drop uses /// ([`Player::move_clip`] calls exactly this): the clip changes row, the /// *picture* comes from the new row afterwards -- which is what "it plays diff --git a/crates/app/src/ui/spine_stance.rs b/crates/app/src/ui/spine_stance.rs index 57d4871..1a69e02 100644 --- a/crates/app/src/ui/spine_stance.rs +++ b/crates/app/src/ui/spine_stance.rs @@ -302,6 +302,29 @@ pub(crate) fn render(player: &Player, cx: &mut Context) -> impl IntoElem ), )) .child(trim_control(player.loop_trim.is_some(), player, cx)) + .child(pair( + // Trim-to-playhead (debt #42): the keyboard's own version of the + // pointer's drag-to-a-spot trim, beside the nudge pair it shares + // its primitive with. + glyph( + "spine-trim-in-playhead", + "[▮", + ActionId::TrimInToPlayhead, + false, + false, + player, + cx, + ), + glyph( + "spine-trim-out-playhead", + "▮]", + ActionId::TrimOutToPlayhead, + false, + false, + player, + cx, + ), + )) .child(glyph( "spine-loop-trim", "↻",