diff --git a/crates/ui-egui/src/history_panel_select_tests.rs b/crates/ui-egui/src/history_panel_select_tests.rs index 14bfb344ca..8a591a739a 100644 --- a/crates/ui-egui/src/history_panel_select_tests.rs +++ b/crates/ui-egui/src/history_panel_select_tests.rs @@ -225,3 +225,25 @@ fn a_new_state_at_the_history_limit_still_scrolls_into_view() { h.run_steps(4); assert!(shown(&h, 0), "a new state at the limit scrolls into view"); } + +#[test] +fn a_new_state_at_the_history_limit_deselects_the_snapshot() { + let mut h = long_harness(); + h.state_mut().run("history.newSnapshot", json!({})).unwrap(); + h.run_steps(2); + // What a click on the snapshot row does: the command, then the selection at the state it made. + let id = h.state().session.active().unwrap().snapshots[0].id; + h.state_mut().run("history.selectSnapshot", json!({"snapshot": id})).unwrap(); + select_snapshot(&h.ctx, h.state().session.active().unwrap(), id); + h.run_steps(4); + assert_eq!(selected_snapshot(&h.ctx, h.state().session.active().unwrap()), Some(id), "the snapshot is selected"); + // At the limit, selecting the snapshot again by command adds a state with the same name and + // drops the oldest: only the newest state tells the two apart. + let st = h.state_mut().session.active_mut().unwrap(); + st.history.max_states = st.history.past_len(); + let past = st.history.past_len(); + h.state_mut().run("history.selectSnapshot", json!({"snapshot": id})).unwrap(); + assert_eq!(h.state().session.active().unwrap().history.past_len(), past, "the oldest state was dropped"); + h.run_steps(4); + assert_eq!(selected_snapshot(&h.ctx, h.state().session.active().unwrap()), None, "a new state deselects the snapshot"); +} diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 5a430205c8..33dc5185cb 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -3360,10 +3360,7 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let redo: Vec = st.history.redo_labels().map(str::to_string).collect(); let source = photocraft_engine::history_source_cmds::source(st); let selected_snap = selected_snapshot(ui.ctx(), st); - // The newest past state is a new document on every step, Undo and Redo, also at the state - // limit, where the counts and a repeated name stay the same. - let newest = st.history.past_len().checked_sub(1).and_then(|i| st.history.state(i)).map(|d| std::sync::Arc::as_ptr(&d) as usize); - let spot = (doc.id, history_spot(st), newest); + let spot = (doc.id, history_spot(st)); let ctx = ui.ctx().clone(); // A row dragged onto the trash (#5324); a drag that never saw its release must not outlive it. if !ctx.input(|i| i.pointer.primary_down() || i.pointer.any_released()) { @@ -3396,7 +3393,7 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // layer click, a channel eye) and a selected snapshot leave the list where the user put it. let follow_id = egui::Id::new("history-follow"); let moved = ui.ctx().data_mut(|d| { - let moved = d.get_temp::<(photocraft_doc::DocId, HistorySpot, Option)>(follow_id).as_ref() != Some(&spot); + let moved = d.get_temp::<(photocraft_doc::DocId, HistorySpot)>(follow_id).as_ref() != Some(&spot); d.insert_temp(follow_id, spot); moved }); @@ -3563,12 +3560,15 @@ fn history_drag_key() -> egui::Id { } /// The snapshot the History panel shows selected (#5324), and where the history stood after its -/// click: the number of past states, the current state's name and the number of redo states. +/// click: the number of past states, the current state's name, the number of redo states and the +/// newest past state. That last one is a new document on every step, also at the state limit, +/// where the counts and a repeated name stay the same. type SelectedSnapshot = (u64, HistorySpot); -type HistorySpot = (usize, Option, usize); +type HistorySpot = (usize, Option, usize, Option); fn history_spot(st: &photocraft_engine::DocState) -> HistorySpot { - (st.history.past_len(), st.history.entries().pop(), st.history.redo_labels().count()) + let newest = st.history.past_len().checked_sub(1).and_then(|i| st.history.state(i)).map(|d| std::sync::Arc::as_ptr(&d) as usize); + (st.history.past_len(), st.history.entries().pop(), st.history.redo_labels().count(), newest) } fn selected_snapshot_key(doc: photocraft_doc::DocId) -> egui::Id {