From 21fe7cb4f64e7715d5738e1ad34c6b8af3c7351b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 11 Aug 2026 01:22:46 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(timeline):=20a=20track=20you?= =?UTF-8?q?=20added=20comes=20off=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/app/src/keymap.rs | 29 +++++++-- crates/app/src/main.rs | 95 +++++++++++++++++++++++++++++- crates/engine/src/playback.rs | 7 +++ crates/engine/src/project.rs | 108 ++++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 5 deletions(-) diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index a617a54..6090c25 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -66,6 +66,8 @@ pub enum ActionId { Undo, AddVideoLane, AddAudioLane, + RemoveVideoLane, + RemoveAudioLane, ToggleMute, VolumeUp, VolumeDown, @@ -78,7 +80,7 @@ pub enum ActionId { impl ActionId { /// Display order everywhere -- the editor lists them in it and /// [`Keymap::defaults`] binds them in it. - pub const ALL: [ActionId; 33] = [ + pub const ALL: [ActionId; 35] = [ ActionId::Play, ActionId::StepBack, ActionId::StepForward, @@ -105,6 +107,8 @@ impl ActionId { ActionId::Undo, ActionId::AddVideoLane, ActionId::AddAudioLane, + ActionId::RemoveVideoLane, + ActionId::RemoveAudioLane, ActionId::ToggleMute, ActionId::VolumeUp, ActionId::VolumeDown, @@ -143,6 +147,8 @@ impl ActionId { ActionId::Undo => "Undo", ActionId::AddVideoLane => "Add a video track", ActionId::AddAudioLane => "Add an audio track", + ActionId::RemoveVideoLane => "Remove the last video track (it must be empty)", + ActionId::RemoveAudioLane => "Remove the last audio track (it must be empty)", ActionId::ToggleMute => "Mute / Unmute", ActionId::VolumeUp => "Volume up", ActionId::VolumeDown => "Volume down", @@ -183,6 +189,8 @@ impl ActionId { ActionId::Undo => "undo", ActionId::AddVideoLane => "add-video-lane", ActionId::AddAudioLane => "add-audio-lane", + ActionId::RemoveVideoLane => "remove-video-lane", + ActionId::RemoveAudioLane => "remove-audio-lane", ActionId::ToggleMute => "toggle-mute", ActionId::VolumeUp => "volume-up", ActionId::VolumeDown => "volume-down", @@ -235,7 +243,9 @@ impl ActionId { | ActionId::Group | ActionId::Undo | ActionId::AddVideoLane - | ActionId::AddAudioLane => Category::Editing, + | ActionId::AddAudioLane + | ActionId::RemoveVideoLane + | ActionId::RemoveAudioLane => Category::Editing, ActionId::ToggleMute | ActionId::VolumeUp | ActionId::VolumeDown @@ -528,6 +538,13 @@ impl Keymap { // is added often enough to deserve a key that is one press. b(ActionId::AddVideoLane, "v", false), b(ActionId::AddAudioLane, "a", false), + // ...and the pair that takes one back, on ctrl chords: removing + // a track is not a stroke to hit by accident (the resolution key + // is out of the way for the same reason). Ctrl+v would be the + // matching initial and is the paste, so the video one takes the + // key beside it. + b(ActionId::RemoveVideoLane, "b", true), + b(ActionId::RemoveAudioLane, "a", true), b(ActionId::ToggleMute, "m", false), // The unshifted pair, which is what gpui reports for those two // keys ("=" and "-", platform.rs:862): the volume keys every @@ -809,7 +826,7 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 35); + assert_eq!(k.entries().len(), 37); assert_eq!(k.lookup("space", false), Some(ActionId::Play)); // The seek keys: bare arrows a frame, ctrl arrows a second, and the two // ends of the timeline. @@ -843,6 +860,10 @@ mod tests { // The track keys are the bare letters; the ctrl ones stay copy/paste. assert_eq!(k.lookup("v", false), Some(ActionId::AddVideoLane)); assert_eq!(k.lookup("a", false), Some(ActionId::AddAudioLane)); + // ...and the ctrl ones take a track away, out of the way of a stray + // press. Ctrl+v is the paste, so the video one sits beside it. + assert_eq!(k.lookup("b", true), Some(ActionId::RemoveVideoLane)); + assert_eq!(k.lookup("a", true), Some(ActionId::RemoveAudioLane)); assert_eq!(k.lookup("m", false), Some(ActionId::ToggleMute)); assert_eq!(k.lookup("q", false), Some(ActionId::Equalizer)); // The volume pair is the unshifted one, so neither needs a modifier @@ -1038,7 +1059,7 @@ mod tests { assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound"); // The label is the editor's column, and never the file's word for it. assert_eq!(ActionId::CancelExport.label(), "Cancel export"); - assert_eq!(ActionId::ALL.len(), 33); + assert_eq!(ActionId::ALL.len(), 35); } #[test] diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 30482e8..f798957 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -822,6 +822,11 @@ impl Player { ActionId::Undo => self.undo(cx), ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx), ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx), + // The last track of that kind: the one the add key put there, so the + // two strokes undo each other press for press. Any other track goes + // through the × in its own header. + ActionId::RemoveVideoLane => self.remove_last_lane(LaneKind::Video, cx), + ActionId::RemoveAudioLane => self.remove_last_lane(LaneKind::Audio, cx), ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx), ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx), ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx), @@ -2397,6 +2402,64 @@ impl Player { cx.notify(); } + /// The × in a track's header: the add taken back, one undo step, and the + /// engine's own words when it refuses -- those name the clips still on the + /// track, so the notice says what to delete first. A removal never deletes a + /// clip. + /// + /// Everything holding a `(lane, idx)` is dropped, because the tracks below + /// the one that went have just moved up an `ord` + /// ([`engine::Project::remove_lane`]): a selection or an open card kept + /// across it would be pointing at the *next* track's clip. + fn remove_lane(&mut self, lane: Lane, cx: &mut Context) { + if self.exporting().is_some() { + return; + } + let removed = self + .session + .as_mut() + .map(|session| session.remove_lane(lane)); + let text = match removed { + Some(Ok(())) => { + self.selected = None; + self.context_menu = None; + self.eq_open = None; + self.color_open = None; + self.speed_open = None; + self.close_silence(); + format!( + "{} REMOVED — {} brings it back", + lane.label(), + self.keymap.display(ActionId::Undo) + ) + } + Some(Err(e)) => format!("NO TRACK REMOVED — {e}"), + None => "NO TRACK REMOVED — open a file first".to_string(), + }; + self.notice = Some(text.into()); + cx.notify(); + } + + /// What the remove keys act on: the last track of that kind, which is the + /// one the matching add key appended. Nothing at all before a file is open, + /// where the timeline drawn is a placeholder pair. + fn remove_last_lane(&mut self, kind: LaneKind, cx: &mut Context) { + let last = self.session.as_ref().and_then(|session| { + session + .lanes() + .into_iter() + .filter(|l| l.kind == kind) + .next_back() + }); + match last { + Some(lane) => self.remove_lane(lane, cx), + None => { + self.notice = Some("NO TRACK REMOVED — open a file first".into()); + cx.notify(); + } + } + } + fn undo(&mut self, cx: &mut Context) { if self.session.as_mut().is_some_and(PlaybackSession::undo) { self.reset_after_reseek(); @@ -5077,6 +5140,12 @@ impl Player { }); let name = lane.label(); let row_id: SharedString = format!("{name}-clip").into(); + let remove_id: SharedString = format!("{name}-remove").into(); + let remove_tip: SharedString = format!( + "Remove {name} — it must be empty first, and {} brings it back", + self.keymap.display(ActionId::Undo) + ) + .into(); let sources = self .session .as_ref() @@ -5106,13 +5175,37 @@ impl Player { .w(px(HEADER_W)) .h_full() .flex() + .flex_col() .items_center() .justify_center() .rounded(px(3.)) .bg(rgb(SURFACE)) .text_size(px(11.)) .text_color(rgb(INK_DIM)) - .child(name), + .child(div().flex_1().flex().items_center().child(name)) + // The one thing a header does: take this track away again. + // A `HIT_MIN` target rather than a glyph-sized one, and it + // stays put on a track holding clips instead of hiding -- + // the refusal names them, and a control that vanishes + // teaches nothing. + .child( + div() + .id(remove_id) + .flex_none() + .w_full() + .h(px(HIT_MIN)) + .flex() + .items_center() + .justify_center() + .rounded(px(3.)) + .cursor_pointer() + .hover(|s| s.bg(rgb(HOVER))) + .tooltip(move |_, cx| cx.new(|_| Tip(remove_tip.clone())).into()) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.remove_lane(lane, cx) + })) + .child("×"), + ), ) .child( // Clips are placed at their own start rather than queued edge diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 19aa574..9519e40 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -876,6 +876,13 @@ impl PlaybackSession { self.project.add_lane(kind) } + /// Drops that empty lane again, in [`Project::remove_lane`]'s words when it + /// refuses. One undo step, and no reseek for the same reason the add needs + /// none: an empty lane was playing nothing. + pub fn remove_lane(&mut self, lane: Lane) -> crate::Result<()> { + self.project.remove_lane(lane) + } + /// Splits every lane at `timeline_secs`, so the two sides become two /// groups. Metadata only: a split never changes the timeline->source /// mapping, so the running decoder stays correct and playback does not diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 9d1b789..6937776 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -666,6 +666,67 @@ impl Project { Lane::new(kind, self.lane_count(kind) - 1) } + /// Drops an *empty* lane -- [`add_lane`](Project::add_lane) taken back, and + /// one undo step like it, which restores the lane where it stood. + /// + /// Refused, changing nothing, while the lane holds anything: the refusal + /// names the clips (file and first frame), because a track removal that + /// deleted takes with it would be the one edit nobody sees coming. Refused + /// too for the last lane of its kind, which is `V1` or `A1`: those two are + /// where an import and a paste land ([`Project::paste`] takes lane 0 of each + /// kind and *skips* a kind that is not there), so a project without one + /// would swallow half of every file dropped on it, silently. + /// + /// The lanes below it move up one `ord`, so a handle a caller was holding + /// (a selection, an open card) names the lane after it from here on -- the + /// one thing a front-end owes this call. + pub fn remove_lane(&mut self, lane: Lane) -> crate::Result<()> { + let Some(i) = self.index(lane) else { + return Err(format!("there is no {} to remove", lane.label()).into()); + }; + if self.lane_count(lane.kind) == 1 { + return Err(format!( + "{} is the only {} track: every import lands on it", + lane.label(), + match lane.kind { + LaneKind::Video => "video", + LaneKind::Audio => "audio", + } + ) + .into()); + } + let clips = &self.lanes[i].clips; + if !clips.is_empty() { + // Three names and a count: a lane can hold forty clips and a + // refusal nobody reads to the end says nothing at all. + let named: Vec = clips + .iter() + .take(3) + .map(|c| { + let name = self.sources.get(c.source).map_or_else( + || format!("source {}", c.source), + |s| s.path.display().to_string(), + ); + format!("{name} at frame {}", c.start) + }) + .collect(); + let rest = match clips.len().saturating_sub(named.len()) { + 0 => String::new(), + n => format!(" and {n} more"), + }; + return Err(format!( + "{} still holds {}{rest}: delete those clips (or drag them to \ + another track) first", + lane.label(), + named.join(", ") + ) + .into()); + } + self.snapshot(); + self.lanes.remove(i); + Ok(()) + } + /// Where `lane` sits in [`Project::lanes`], or `None` for a lane that is /// not there. The one place a handle becomes a position. fn index(&self, lane: Lane) -> Option { @@ -4697,6 +4758,53 @@ mod tests { assert_eq!(shape(&p), shape(&three())); } + /// The add taken back: an empty lane comes off, a lane holding clips never + /// does (and says which clips), the last lane of a kind never does, the + /// lanes below the one that went move up an `ord`, and one undo puts it + /// back where it stood. + #[test] + fn an_empty_lane_comes_off_again() { + let mut p = three(); + let v2 = p.add_lane(LaneKind::Video); + let v3 = p.add_lane(LaneKind::Video); + assert!(p.place(v3, 0, clip(0, 0, 3, 0))); + let why = |r: crate::Result<()>| r.expect_err("refused").to_string(); + let history = p.history.len(); + + // The last lane of its kind stays: `V1`/`A1` are where an import and a + // paste land, and a project missing one would swallow half of a file + // dropped on it. + assert!(why(p.remove_lane(Lane::A1)).contains("only audio track")); + assert_eq!( + why(p.remove_lane(Lane::new(LaneKind::Video, 9))), + "there is no V10 to remove" + ); + + // A lane with clips on it refuses and names them: a track removal never + // deletes a take. + let refusal = why(p.remove_lane(v3)); + assert!(refusal.contains("V3 still holds"), "{refusal}"); + assert!(refusal.contains(FILE), "{refusal}"); + assert!(refusal.contains("at frame 0"), "{refusal}"); + assert_eq!(p.lane_spans(v3), vec![(0, 3)], "the clip is still there"); + assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); + + // The empty one in the middle goes, and `V3` becomes `V2` -- clip and + // all. + p.remove_lane(v2).expect("V2 is empty"); + assert_eq!(p.lane_count(LaneKind::Video), 2); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); + assert_eq!(p.lane_spans(v2), vec![(0, 3)], "the lane below moved up"); + assert_eq!(p.history.len(), history + 1, "one snapshot per removal"); + invariants_hold(&p, "remove V2"); + + // ...and one stroke puts the empty lane back above it. + assert!(p.undo()); + assert_eq!(p.lane_count(LaneKind::Video), 3); + assert!(p.lane(v2).is_empty(), "back where it stood, still empty"); + assert_eq!(p.lane_spans(v3), vec![(0, 3)]); + } + /// The grouping rule across more than two lanes: a link id is one *span*, /// on however many lanes carry it -- not a video/audio pair and not a /// pairing by `ord`. So a split shares an id with every lane whose half