From 49a1af3d2e11eff79458edb95e69300614bd1798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 01:24:07 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(app):=20the=20timeline=20can?= =?UTF-8?q?=20redo,=20mirroring=20undo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Project grows a redo stack: undo pushes the lanes it leaves onto it, redo pops one back, and any fresh edit (snapshot) clears it since the branch undo left behind is no longer where the edit leads. Wired through PlaybackSession::redo, Player::redo and ActionId::Redo, bound to ctrl+y -- bare "y" is already SubtitleStyle's, so redo takes only the ctrl chord. --- crates/app/src/keymap.rs | 10 ++++- crates/app/src/player/actions.rs | 9 +++++ crates/engine/src/playback.rs | 6 +++ crates/engine/src/project.rs | 65 +++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index 559068a..26216ea 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -104,6 +104,7 @@ actions! { ZoomOut, ZoomFit, Undo, + Redo, AddVideoLane, AddAudioLane, RemoveVideoLane, @@ -194,6 +195,7 @@ impl ActionId { ActionId::ZoomOut => "Zoom out of the timeline", ActionId::ZoomFit => "Fit the whole timeline on screen", ActionId::Undo => "Undo", + ActionId::Redo => "Redo", ActionId::AddVideoLane => "Add a video track", ActionId::AddAudioLane => "Add an audio track", ActionId::RemoveVideoLane => "Remove the last video track (it must be empty)", @@ -256,6 +258,7 @@ impl ActionId { ActionId::ZoomOut => "zoom-out", ActionId::ZoomFit => "zoom-fit", ActionId::Undo => "undo", + ActionId::Redo => "redo", ActionId::AddVideoLane => "add-video-lane", ActionId::AddAudioLane => "add-audio-lane", ActionId::RemoveVideoLane => "remove-video-lane", @@ -353,6 +356,7 @@ impl ActionId { | ActionId::Detach | ActionId::Group | ActionId::Undo + | ActionId::Redo | ActionId::AddVideoLane | ActionId::AddAudioLane | ActionId::RemoveVideoLane @@ -874,6 +878,9 @@ impl Keymap { b(ActionId::ZoomFit, "0", true), b(ActionId::Undo, "z", false), b(ActionId::Undo, "z", true), + // Bare "y" is SubtitleStyle's, so redo takes only the ctrl + // chord -- the conventional pairing with ctrl+z either way. + b(ActionId::Redo, "y", true), // The unshifted initials of what they add. Both were free -- // the copy and paste chords are the *ctrl* ones -- and a track // is added often enough to deserve a key that is one press. @@ -1231,7 +1238,7 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 57); + assert_eq!(k.entries().len(), 58); 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)); @@ -1276,6 +1283,7 @@ mod tests { assert_eq!(k.lookup("r", true), Some(ActionId::Resolution)); assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); + assert_eq!(k.lookup("y", true), Some(ActionId::Redo)); // 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)); diff --git a/crates/app/src/player/actions.rs b/crates/app/src/player/actions.rs index 78f817b..8635dcd 100644 --- a/crates/app/src/player/actions.rs +++ b/crates/app/src/player/actions.rs @@ -73,6 +73,7 @@ impl Player { ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx), ActionId::ZoomFit => self.zoom_fit(cx), ActionId::Undo => self.undo(cx), + ActionId::Redo => self.redo(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 @@ -390,6 +391,14 @@ impl Player { cx.notify(); } + pub(crate) fn redo(&mut self, cx: &mut Context) { + if self.session.as_mut().is_some_and(PlaybackSession::redo) { + self.reset_after_reseek(); + } + self.selected.clear(); + cx.notify(); + } + /// The stroke a waiting row was after: it becomes the whole of what reaches /// that action, which is what the row was showing. A chord another action /// already holds is refused by the keymap and the row keeps waiting, so the diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index fe593ba..4c700ed 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -2800,6 +2800,12 @@ impl PlaybackSession { self.edit(Dirty::Both, Project::undo) } + /// Redoes the last edit [`undo`](Self::undo) took back, and reseeks like + /// a delete. + pub fn redo(&mut self) -> bool { + self.edit(Dirty::Both, Project::redo) + } + /// Takes `path` into the **library**: it becomes a source of this session, /// with its length noted ([`file_frames`](Self::file_frames)), and nothing /// is placed on any lane. What reaches the timeline is decided afterwards, diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index b11442d..eabd521 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -782,6 +782,10 @@ pub struct Project { /// whole lane list, so adding a lane undoes as well. Bounded by /// [`HISTORY_CAP`]: at the cap the oldest step goes. history: Vec>, + /// Lane lists `undo` has stepped past; `redo` pops one. Cleared by + /// [`Project::snapshot`], since a fresh edit invalidates whatever branch + /// the undone steps came from. Not saved to `.edith`, matching `history`. + redo: Vec>, /// Never rolled back by an undo: an id retired by an undone split must not /// come back and group two clips that were never together. next_link: u32, @@ -828,6 +832,7 @@ impl Project { eq: Vec::new(), color: Vec::new(), history: Vec::new(), + redo: Vec::new(), next_link: 1, subtitles: Vec::new(), limiter: Limiter::default(), @@ -957,6 +962,7 @@ impl Project { eq, color, history: Vec::new(), + redo: Vec::new(), next_link, subtitles: Vec::new(), limiter: Limiter::default(), @@ -4210,6 +4216,13 @@ impl Project { self.history.len() } + /// How many redo steps are stacked: what the sweep asks to check `undo` + /// left a branch behind, and that a fresh edit clears it. + #[cfg(test)] + pub(crate) fn redo_len(&self) -> usize { + self.redo.len() + } + /// Every part of the project that an edit can change, as comparable /// values: what the sweep's undo round-trip asks "byte identical?" of. #[cfg(test)] @@ -4222,11 +4235,27 @@ impl Project { } /// Restore every lane from before the last successful edit -- the clips and - /// the lane list both. `false` when there is nothing left to undo. + /// the lane list both. `false` when there is nothing left to undo. Pushes + /// the lanes just left onto the redo stack, so [`Project::redo`] can walk + /// forward again. pub fn undo(&mut self) -> bool { match self.history.pop() { Some(prev) => { - self.lanes = prev; + self.redo.push(std::mem::replace(&mut self.lanes, prev)); + true + } + None => false, + } + } + + /// Restore every lane from before the last [`Project::undo`] -- the + /// mirror of `undo`, walking the redo stack it fills. `false` when there + /// is nothing left to redo, which is also true after any fresh edit: a + /// new [`Project::snapshot`] clears the branch `undo` left behind. + pub fn redo(&mut self) -> bool { + match self.redo.pop() { + Some(next) => { + self.history.push(std::mem::replace(&mut self.lanes, next)); true } None => false, @@ -4244,6 +4273,7 @@ impl Project { eq: self.eq.clone(), color: self.color.clone(), history: Vec::new(), + redo: Vec::new(), next_link: self.next_link, subtitles: self.subtitles.clone(), limiter: self.limiter, @@ -4418,11 +4448,14 @@ impl Project { /// Pushes the undo snapshot. Every mutating method calls this once, *after* /// it has decided it will succeed -- a refusal must not cost an undo step. + /// Clears the redo stack: a fresh edit branches off from here, so whatever + /// `undo` had left to redo is no longer where this edit leads back to. fn snapshot(&mut self) { if self.history.len() == HISTORY_CAP { self.history.remove(0); } self.history.push(self.lanes.clone()); + self.redo.clear(); } fn new_link(&mut self) -> u32 { @@ -6959,6 +6992,34 @@ mod tests { assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); } + #[test] + fn redo_restores_exactly() { + let mut p = three(); + assert!(p.delete(1)); + let after_delete = p.parts(); + assert!(p.undo()); + assert!(p.redo()); + assert_eq!(p.parts(), after_delete, "redo lands back byte-identical"); + assert!(!p.redo(), "empty redo stack"); + } + + #[test] + fn edit_after_undo_empties_redo() { + let mut p = three(); + assert!(p.delete(1)); + assert!(p.undo()); + assert_eq!(p.redo_len(), 1, "undo left a branch to redo"); + assert!(p.split(1), "a fresh edit"); + assert_eq!(p.redo_len(), 0, "the fresh edit clears it"); + assert!(!p.redo(), "so redo has nothing left"); + } + + #[test] + fn redo_on_empty_stack_is_false() { + let mut p = three(); + assert!(!p.redo(), "nothing has been undone yet"); + } + /// Past the cap the *oldest* step goes, so a long session undoes exactly /// `HISTORY_CAP` gestures and then runs dry -- it does not grow for ever. #[test]