The timeline can redo, mirroring undo
Browse files at this commit49a1af3d2e11eff79458edb95e69300614bd1798Parent: 8f0c500- feat
- the timeline can redo, mirroring undo
4 changed files +87 -3
- crates/app/src/keymap.rs +9 -1
- crates/app/src/player/actions.rs +9 -0
- crates/engine/src/playback.rs +6 -0
- crates/engine/src/project.rs +63 -2
crates/app/src/keymap.rsmodified+9 -1
| @@ -104,6 +104,7 @@ actions! { | |||
| 104 | ZoomOut, | 104 | ZoomOut, |
| 105 | ZoomFit, | 105 | ZoomFit, |
| 106 | Undo, | 106 | Undo, |
| 107 | + Redo, | ||
| 107 | AddVideoLane, | 108 | AddVideoLane, |
| 108 | AddAudioLane, | 109 | AddAudioLane, |
| 109 | RemoveVideoLane, | 110 | RemoveVideoLane, |
| @@ -194,6 +195,7 @@ impl ActionId { | |||
| 194 | ActionId::ZoomOut => "Zoom out of the timeline", | 195 | ActionId::ZoomOut => "Zoom out of the timeline", |
| 195 | ActionId::ZoomFit => "Fit the whole timeline on screen", | 196 | ActionId::ZoomFit => "Fit the whole timeline on screen", |
| 196 | ActionId::Undo => "Undo", | 197 | ActionId::Undo => "Undo", |
| 198 | + ActionId::Redo => "Redo", | ||
| 197 | ActionId::AddVideoLane => "Add a video track", | 199 | ActionId::AddVideoLane => "Add a video track", |
| 198 | ActionId::AddAudioLane => "Add an audio track", | 200 | ActionId::AddAudioLane => "Add an audio track", |
| 199 | ActionId::RemoveVideoLane => "Remove the last video track (it must be empty)", | 201 | ActionId::RemoveVideoLane => "Remove the last video track (it must be empty)", |
| @@ -256,6 +258,7 @@ impl ActionId { | |||
| 256 | ActionId::ZoomOut => "zoom-out", | 258 | ActionId::ZoomOut => "zoom-out", |
| 257 | ActionId::ZoomFit => "zoom-fit", | 259 | ActionId::ZoomFit => "zoom-fit", |
| 258 | ActionId::Undo => "undo", | 260 | ActionId::Undo => "undo", |
| 261 | + ActionId::Redo => "redo", | ||
| 259 | ActionId::AddVideoLane => "add-video-lane", | 262 | ActionId::AddVideoLane => "add-video-lane", |
| 260 | ActionId::AddAudioLane => "add-audio-lane", | 263 | ActionId::AddAudioLane => "add-audio-lane", |
| 261 | ActionId::RemoveVideoLane => "remove-video-lane", | 264 | ActionId::RemoveVideoLane => "remove-video-lane", |
| @@ -353,6 +356,7 @@ impl ActionId { | |||
| 353 | | ActionId::Detach | 356 | | ActionId::Detach |
| 354 | | ActionId::Group | 357 | | ActionId::Group |
| 355 | | ActionId::Undo | 358 | | ActionId::Undo |
| 359 | + | ActionId::Redo | ||
| 356 | | ActionId::AddVideoLane | 360 | | ActionId::AddVideoLane |
| 357 | | ActionId::AddAudioLane | 361 | | ActionId::AddAudioLane |
| 358 | | ActionId::RemoveVideoLane | 362 | | ActionId::RemoveVideoLane |
| @@ -874,6 +878,9 @@ impl Keymap { | |||
| 874 | b(ActionId::ZoomFit, "0", true), | 878 | b(ActionId::ZoomFit, "0", true), |
| 875 | b(ActionId::Undo, "z", false), | 879 | b(ActionId::Undo, "z", false), |
| 876 | b(ActionId::Undo, "z", true), | 880 | b(ActionId::Undo, "z", true), |
| 881 | + // Bare "y" is SubtitleStyle's, so redo takes only the ctrl | ||
| 882 | + // chord -- the conventional pairing with ctrl+z either way. | ||
| 883 | + b(ActionId::Redo, "y", true), | ||
| 877 | // The unshifted initials of what they add. Both were free -- | 884 | // The unshifted initials of what they add. Both were free -- |
| 878 | // the copy and paste chords are the *ctrl* ones -- and a track | 885 | // the copy and paste chords are the *ctrl* ones -- and a track |
| 879 | // is added often enough to deserve a key that is one press. | 886 | // is added often enough to deserve a key that is one press. |
| @@ -1231,7 +1238,7 @@ mod tests { | |||
| 1231 | #[test] | 1238 | #[test] |
| 1232 | fn every_default_stroke_reaches_its_action() { | 1239 | fn every_default_stroke_reaches_its_action() { |
| 1233 | let k = Keymap::defaults(); | 1240 | let k = Keymap::defaults(); |
| 1234 | - assert_eq!(k.entries().len(), 57); | 1241 | + assert_eq!(k.entries().len(), 58); |
| 1235 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); | 1242 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); |
| 1236 | assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); | 1243 | assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); |
| 1237 | assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); | 1244 | assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); |
| @@ -1276,6 +1283,7 @@ mod tests { | |||
| 1276 | assert_eq!(k.lookup("r", true), Some(ActionId::Resolution)); | 1283 | assert_eq!(k.lookup("r", true), Some(ActionId::Resolution)); |
| 1277 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); | 1284 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); |
| 1278 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); | 1285 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); |
| 1286 | + assert_eq!(k.lookup("y", true), Some(ActionId::Redo)); | ||
| 1279 | // The track keys are the bare letters; the ctrl ones stay copy/paste. | 1287 | // The track keys are the bare letters; the ctrl ones stay copy/paste. |
| 1280 | assert_eq!(k.lookup("v", false), Some(ActionId::AddVideoLane)); | 1288 | assert_eq!(k.lookup("v", false), Some(ActionId::AddVideoLane)); |
| 1281 | assert_eq!(k.lookup("a", false), Some(ActionId::AddAudioLane)); | 1289 | assert_eq!(k.lookup("a", false), Some(ActionId::AddAudioLane)); |
crates/app/src/player/actions.rsmodified+9 -0
| @@ -73,6 +73,7 @@ impl Player { | |||
| 73 | ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx), | 73 | ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx), |
| 74 | ActionId::ZoomFit => self.zoom_fit(cx), | 74 | ActionId::ZoomFit => self.zoom_fit(cx), |
| 75 | ActionId::Undo => self.undo(cx), | 75 | ActionId::Undo => self.undo(cx), |
| 76 | + ActionId::Redo => self.redo(cx), | ||
| 76 | ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx), | 77 | ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx), |
| 77 | ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx), | 78 | ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx), |
| 78 | // The last track of that kind: the one the add key put there, so the | 79 | // The last track of that kind: the one the add key put there, so the |
| @@ -390,6 +391,14 @@ impl Player { | |||
| 390 | cx.notify(); | 391 | cx.notify(); |
| 391 | } | 392 | } |
| 392 | 393 | ||
| 394 | + pub(crate) fn redo(&mut self, cx: &mut Context<Self>) { | ||
| 395 | + if self.session.as_mut().is_some_and(PlaybackSession::redo) { | ||
| 396 | + self.reset_after_reseek(); | ||
| 397 | + } | ||
| 398 | + self.selected.clear(); | ||
| 399 | + cx.notify(); | ||
| 400 | + } | ||
| 401 | + | ||
| 393 | /// The stroke a waiting row was after: it becomes the whole of what reaches | 402 | /// The stroke a waiting row was after: it becomes the whole of what reaches |
| 394 | /// that action, which is what the row was showing. A chord another action | 403 | /// that action, which is what the row was showing. A chord another action |
| 395 | /// already holds is refused by the keymap and the row keeps waiting, so the | 404 | /// already holds is refused by the keymap and the row keeps waiting, so the |
crates/engine/src/playback.rsmodified+6 -0
| @@ -2800,6 +2800,12 @@ impl PlaybackSession { | |||
| 2800 | self.edit(Dirty::Both, Project::undo) | 2800 | self.edit(Dirty::Both, Project::undo) |
| 2801 | } | 2801 | } |
| 2802 | 2802 | ||
| 2803 | + /// Redoes the last edit [`undo`](Self::undo) took back, and reseeks like | ||
| 2804 | + /// a delete. | ||
| 2805 | + pub fn redo(&mut self) -> bool { | ||
| 2806 | + self.edit(Dirty::Both, Project::redo) | ||
| 2807 | + } | ||
| 2808 | + | ||
| 2803 | /// Takes `path` into the **library**: it becomes a source of this session, | 2809 | /// Takes `path` into the **library**: it becomes a source of this session, |
| 2804 | /// with its length noted ([`file_frames`](Self::file_frames)), and nothing | 2810 | /// with its length noted ([`file_frames`](Self::file_frames)), and nothing |
| 2805 | /// is placed on any lane. What reaches the timeline is decided afterwards, | 2811 | /// is placed on any lane. What reaches the timeline is decided afterwards, |
crates/engine/src/project.rsmodified+63 -2
| @@ -782,6 +782,10 @@ pub struct Project { | |||
| 782 | /// whole lane list, so adding a lane undoes as well. Bounded by | 782 | /// whole lane list, so adding a lane undoes as well. Bounded by |
| 783 | /// [`HISTORY_CAP`]: at the cap the oldest step goes. | 783 | /// [`HISTORY_CAP`]: at the cap the oldest step goes. |
| 784 | history: Vec<Vec<LaneData>>, | 784 | history: Vec<Vec<LaneData>>, |
| 785 | + /// Lane lists `undo` has stepped past; `redo` pops one. Cleared by | ||
| 786 | + /// [`Project::snapshot`], since a fresh edit invalidates whatever branch | ||
| 787 | + /// the undone steps came from. Not saved to `.edith`, matching `history`. | ||
| 788 | + redo: Vec<Vec<LaneData>>, | ||
| 785 | /// Never rolled back by an undo: an id retired by an undone split must not | 789 | /// Never rolled back by an undo: an id retired by an undone split must not |
| 786 | /// come back and group two clips that were never together. | 790 | /// come back and group two clips that were never together. |
| 787 | next_link: u32, | 791 | next_link: u32, |
| @@ -828,6 +832,7 @@ impl Project { | |||
| 828 | eq: Vec::new(), | 832 | eq: Vec::new(), |
| 829 | color: Vec::new(), | 833 | color: Vec::new(), |
| 830 | history: Vec::new(), | 834 | history: Vec::new(), |
| 835 | + redo: Vec::new(), | ||
| 831 | next_link: 1, | 836 | next_link: 1, |
| 832 | subtitles: Vec::new(), | 837 | subtitles: Vec::new(), |
| 833 | limiter: Limiter::default(), | 838 | limiter: Limiter::default(), |
| @@ -957,6 +962,7 @@ impl Project { | |||
| 957 | eq, | 962 | eq, |
| 958 | color, | 963 | color, |
| 959 | history: Vec::new(), | 964 | history: Vec::new(), |
| 965 | + redo: Vec::new(), | ||
| 960 | next_link, | 966 | next_link, |
| 961 | subtitles: Vec::new(), | 967 | subtitles: Vec::new(), |
| 962 | limiter: Limiter::default(), | 968 | limiter: Limiter::default(), |
| @@ -4210,6 +4216,13 @@ impl Project { | |||
| 4210 | self.history.len() | 4216 | self.history.len() |
| 4211 | } | 4217 | } |
| 4212 | 4218 | ||
| 4219 | + /// How many redo steps are stacked: what the sweep asks to check `undo` | ||
| 4220 | + /// left a branch behind, and that a fresh edit clears it. | ||
| 4221 | + #[cfg(test)] | ||
| 4222 | + pub(crate) fn redo_len(&self) -> usize { | ||
| 4223 | + self.redo.len() | ||
| 4224 | + } | ||
| 4225 | + | ||
| 4213 | /// Every part of the project that an edit can change, as comparable | 4226 | /// Every part of the project that an edit can change, as comparable |
| 4214 | /// values: what the sweep's undo round-trip asks "byte identical?" of. | 4227 | /// values: what the sweep's undo round-trip asks "byte identical?" of. |
| 4215 | #[cfg(test)] | 4228 | #[cfg(test)] |
| @@ -4222,11 +4235,27 @@ impl Project { | |||
| 4222 | } | 4235 | } |
| 4223 | 4236 | ||
| 4224 | /// Restore every lane from before the last successful edit -- the clips and | 4237 | /// Restore every lane from before the last successful edit -- the clips and |
| 4225 | - /// the lane list both. `false` when there is nothing left to undo. | 4238 | + /// the lane list both. `false` when there is nothing left to undo. Pushes |
| 4239 | + /// the lanes just left onto the redo stack, so [`Project::redo`] can walk | ||
| 4240 | + /// forward again. | ||
| 4226 | pub fn undo(&mut self) -> bool { | 4241 | pub fn undo(&mut self) -> bool { |
| 4227 | match self.history.pop() { | 4242 | match self.history.pop() { |
| 4228 | Some(prev) => { | 4243 | Some(prev) => { |
| 4229 | - self.lanes = prev; | 4244 | + self.redo.push(std::mem::replace(&mut self.lanes, prev)); |
| 4245 | + true | ||
| 4246 | + } | ||
| 4247 | + None => false, | ||
| 4248 | + } | ||
| 4249 | + } | ||
| 4250 | + | ||
| 4251 | + /// Restore every lane from before the last [`Project::undo`] -- the | ||
| 4252 | + /// mirror of `undo`, walking the redo stack it fills. `false` when there | ||
| 4253 | + /// is nothing left to redo, which is also true after any fresh edit: a | ||
| 4254 | + /// new [`Project::snapshot`] clears the branch `undo` left behind. | ||
| 4255 | + pub fn redo(&mut self) -> bool { | ||
| 4256 | + match self.redo.pop() { | ||
| 4257 | + Some(next) => { | ||
| 4258 | + self.history.push(std::mem::replace(&mut self.lanes, next)); | ||
| 4230 | true | 4259 | true |
| 4231 | } | 4260 | } |
| 4232 | None => false, | 4261 | None => false, |
| @@ -4244,6 +4273,7 @@ impl Project { | |||
| 4244 | eq: self.eq.clone(), | 4273 | eq: self.eq.clone(), |
| 4245 | color: self.color.clone(), | 4274 | color: self.color.clone(), |
| 4246 | history: Vec::new(), | 4275 | history: Vec::new(), |
| 4276 | + redo: Vec::new(), | ||
| 4247 | next_link: self.next_link, | 4277 | next_link: self.next_link, |
| 4248 | subtitles: self.subtitles.clone(), | 4278 | subtitles: self.subtitles.clone(), |
| 4249 | limiter: self.limiter, | 4279 | limiter: self.limiter, |
| @@ -4418,11 +4448,14 @@ impl Project { | |||
| 4418 | 4448 | ||
| 4419 | /// Pushes the undo snapshot. Every mutating method calls this once, *after* | 4449 | /// Pushes the undo snapshot. Every mutating method calls this once, *after* |
| 4420 | /// it has decided it will succeed -- a refusal must not cost an undo step. | 4450 | /// it has decided it will succeed -- a refusal must not cost an undo step. |
| 4451 | + /// Clears the redo stack: a fresh edit branches off from here, so whatever | ||
| 4452 | + /// `undo` had left to redo is no longer where this edit leads back to. | ||
| 4421 | fn snapshot(&mut self) { | 4453 | fn snapshot(&mut self) { |
| 4422 | if self.history.len() == HISTORY_CAP { | 4454 | if self.history.len() == HISTORY_CAP { |
| 4423 | self.history.remove(0); | 4455 | self.history.remove(0); |
| 4424 | } | 4456 | } |
| 4425 | self.history.push(self.lanes.clone()); | 4457 | self.history.push(self.lanes.clone()); |
| 4458 | + self.redo.clear(); | ||
| 4426 | } | 4459 | } |
| 4427 | 4460 | ||
| 4428 | fn new_link(&mut self) -> u32 { | 4461 | fn new_link(&mut self) -> u32 { |
| @@ -6959,6 +6992,34 @@ mod tests { | |||
| 6959 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); | 6992 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); |
| 6960 | } | 6993 | } |
| 6961 | 6994 | ||
| 6995 | + #[test] | ||
| 6996 | + fn redo_restores_exactly() { | ||
| 6997 | + let mut p = three(); | ||
| 6998 | + assert!(p.delete(1)); | ||
| 6999 | + let after_delete = p.parts(); | ||
| 7000 | + assert!(p.undo()); | ||
| 7001 | + assert!(p.redo()); | ||
| 7002 | + assert_eq!(p.parts(), after_delete, "redo lands back byte-identical"); | ||
| 7003 | + assert!(!p.redo(), "empty redo stack"); | ||
| 7004 | + } | ||
| 7005 | + | ||
| 7006 | + #[test] | ||
| 7007 | + fn edit_after_undo_empties_redo() { | ||
| 7008 | + let mut p = three(); | ||
| 7009 | + assert!(p.delete(1)); | ||
| 7010 | + assert!(p.undo()); | ||
| 7011 | + assert_eq!(p.redo_len(), 1, "undo left a branch to redo"); | ||
| 7012 | + assert!(p.split(1), "a fresh edit"); | ||
| 7013 | + assert_eq!(p.redo_len(), 0, "the fresh edit clears it"); | ||
| 7014 | + assert!(!p.redo(), "so redo has nothing left"); | ||
| 7015 | + } | ||
| 7016 | + | ||
| 7017 | + #[test] | ||
| 7018 | + fn redo_on_empty_stack_is_false() { | ||
| 7019 | + let mut p = three(); | ||
| 7020 | + assert!(!p.redo(), "nothing has been undone yet"); | ||
| 7021 | + } | ||
| 7022 | + | ||
| 6962 | /// Past the cap the *oldest* step goes, so a long session undoes exactly | 7023 | /// Past the cap the *oldest* step goes, so a long session undoes exactly |
| 6963 | /// `HISTORY_CAP` gestures and then runs dry -- it does not grow for ever. | 7024 | /// `HISTORY_CAP` gestures and then runs dry -- it does not grow for ever. |
| 6964 | #[test] | 7025 | #[test] |