diff --git a/src/changeset.rs b/src/changeset.rs index 7f290ea..8fd3b8f 100644 --- a/src/changeset.rs +++ b/src/changeset.rs @@ -37,14 +37,6 @@ impl EventAction { } } - pub fn before(&self) -> Option<&TrackEvent> { - match self { - EventAction::Delete(ev) => Some(ev), - EventAction::Update(ev, _) => Some(ev), - EventAction::Insert(_) => None, - } - } - pub fn revert(&self) -> Self { match self { EventAction::Delete(ev) => EventAction::Insert(ev.clone()), diff --git a/src/stave.rs b/src/stave.rs index 44374e9..f9caa40 100644 --- a/src/stave.rs +++ b/src/stave.rs @@ -460,8 +460,8 @@ impl Stave { egui::Key::ArrowRight, )) }) { - self.history.update_track(|_track| { - shift_tail(&(self.cursor_position), &Stave::KEYBOARD_TIME_STEP) + self.history.update_track(|track| { + shift_tail(track, &(self.cursor_position), &Stave::KEYBOARD_TIME_STEP) }); } if response.ctx.input_mut(|i| { @@ -470,8 +470,8 @@ impl Stave { egui::Key::ArrowLeft, )) }) { - self.history.update_track(|_track| { - shift_tail(&(self.cursor_position), &-Stave::KEYBOARD_TIME_STEP) + self.history.update_track(|track| { + shift_tail(track, &(self.cursor_position), &-Stave::KEYBOARD_TIME_STEP) }); } diff --git a/src/track_edit.rs b/src/track_edit.rs index 82172c3..15fa588 100644 --- a/src/track_edit.rs +++ b/src/track_edit.rs @@ -36,7 +36,7 @@ pub enum CommandDiff { TailShift { at: Time, delta: Time }, } -pub type AppliedCommand = (EditCommandId, Vec); +pub type AppliedCommand = Option<(EditCommandId, Vec)>; pub fn apply_diffs(track: &mut Track, diffs: &Vec) { for d in diffs { @@ -72,32 +72,25 @@ pub fn revert_diff(track: &Track, diff: &CommandDiff, changeset: &mut Changeset) } } -pub fn shift_tail(at: &Time, delta: &Time) -> (EditCommandId, Vec) { - ( - EditCommandId::ShiftTail, - vec![CommandDiff::TailShift { - at: *at, - delta: *delta, - }], - ) +pub fn shift_tail(track: &Track, at: &Time, delta: &Time) -> AppliedCommand { + checked_tail_shift(&track, &at, &at, &delta).map(|tail_shift| { + ( + EditCommandId::ShiftTail, + vec![CommandDiff::TailShift { + at: *at, + delta: *delta, + }], + ) + }) } // TODO (cleanup) Organize the naming? Functions updating changeset, vs functions producing CommandDiffs fn do_shift_tail(track: &Track, at: &Time, delta: &Time, changeset: &mut Changeset) { - // TODO (improvement) When moving earlier adjust last delta so the events will start exactly at 'at'. let mut actions = vec![]; for ev in &track.events { if *at < ev.at { - /* The action should be replayable and undoable. If we allow events to move earlier - then 'at' time, then on undo we should somehow find them still while not confusing them - with unchanged events in (at - delta, at] range (when if delta > 0). - Track events are expected to be in sorted order, so this will likely trigger early. - TODO (implementation) This is a normal situation, should just not apply the command, - and maybe hint user that we hit the wall already. Need a way to interrupt this - gracefully, or do the check before applying the command diff. - */ assert!( - *at <= (ev.at + delta), + *at < (ev.at + delta), "the shift_tail is not undoable at={}, delta={}", at, delta @@ -114,7 +107,7 @@ pub fn tape_insert(range: &Range