diff --git a/crates/engine/src/history_options_cmds.rs b/crates/engine/src/history_options_cmds.rs index 5f04c55888..2b6815ea77 100644 --- a/crates/engine/src/history_options_cmds.rs +++ b/crates/engine/src/history_options_cmds.rs @@ -158,8 +158,14 @@ pub(crate) fn delete_state(s: &mut Session, i: usize, current: usize) -> Result< if !removed { return Err(EngineError::Other(format!("state {i} is not held"))); } - // The document is unchanged, but the History panel's rows are not. + // The document is unchanged, but the History panel's rows are not: a view-only change + // that keeps a clean document clean. + let clean = st.saved_revision == st.revision; st.revision += 1; + st.view_revision += 1; + if clean { + st.saved_revision = st.revision; + } st.last_damage = Some(photocraft_geom::Rect::EMPTY); return Ok(()); } @@ -605,6 +611,56 @@ mod tests { assert!(!s.is_enabled("history.deleteState"), "nothing left but the oldest state"); } + /// Non-linear Delete of a past or later state leaves the document as it is, so a saved + /// (clean) document stays clean; deleting the current state steps back and modifies it. + #[test] + fn non_linear_delete_of_another_state_keeps_a_clean_document_clean() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + s.execute(CMD, json!({"allowNonLinearHistory": true})).unwrap(); + for c in ["#ff0000", "#00ff00", "#0000ff", "#808080"] { + s.execute("edit.fill", json!({"color": c})).unwrap(); + } + s.execute("edit.undo", json!({})).unwrap(); + // States: New, Fill 1-3 (3 current), the dimmed Fill 4. As if just saved. + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; + let rev = st.revision; + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + let st = s.active().unwrap(); + assert!(!st.is_dirty(), "deleting a past state left the saved document unmodified"); + assert!(st.revision > rev, "the History panel still sees the change"); + s.execute("history.deleteState", json!({"state": 3})).unwrap(); + assert!(!s.active().unwrap().is_dirty(), "deleting a later state left it unmodified too"); + assert_eq!(s.active().unwrap().history.redo_labels().count(), 0); + // A modified document stays modified. + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("history.deleteState", json!({"state": 1})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + // The current state: the document steps back, which is a change. + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; + s.execute("history.deleteState", json!({})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + } + + /// With the option off, a recorded step that changes visibility along with other properties + /// (layer.setProps visible + opacity is one Layer Properties state) is undone and redone as a + /// whole: the carried-over visibility skips the layers the step itself changed. + #[test] + fn undoing_a_mixed_visibility_and_opacity_change_undoes_both() { + let (mut s, id) = doc(); + s.execute(CMD, json!({"makeLayerVisibilityChangesUndoable": false})).unwrap(); + let n = entries(&s).len(); + s.execute("layer.setProps", json!({"layer": id, "visible": false, "opacity": 0.5})).unwrap(); + assert_eq!(entries(&s).len(), n + 1, "the mixed change is a state"); + let opacity = |s: &Session| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().opacity; + s.execute("edit.undo", json!({})).unwrap(); + assert!(visible(&s, id) && (opacity(&s) - 1.0).abs() < 1e-6, "undo shows the layer again and restores its opacity"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(!visible(&s, id) && (opacity(&s) - 0.5).abs() < 1e-6, "redo hides it again"); + } + /// Non-linear Delete reaches a later (dimmed) state too, and deletes only it; linear history /// still refuses one past the current state. #[test] diff --git a/crates/ui-egui/src/history_panel_select_tests.rs b/crates/ui-egui/src/history_panel_select_tests.rs index 92c458e37f..0a0cd6f3c5 100644 --- a/crates/ui-egui/src/history_panel_select_tests.rs +++ b/crates/ui-egui/src/history_panel_select_tests.rs @@ -58,3 +58,41 @@ fn clicking_a_state_jumps_there_with_one_command() { h.run_steps(2); assert_eq!(state(&h), (4, 0, rev + 2)); } + +/// The ghost label that follows a History row being dragged to the trash (the text painted at +/// the pointer, `layer_transfer::ghost`), if one is drawn. +fn ghost_label(h: &Harness<'_, PhotocraftApp>, p: egui::Pos2) -> Option { + let at = p + vec2(20.0, -6.0); + h.output().shapes.iter().find_map(|c| match &c.shape { + egui::Shape::Text(t) if (t.pos - at).length() < 1.0 => Some(t.galley.job.text.clone()), + _ => None, + }) +} + +/// Allow Non-Linear History lets a later (dimmed) state be dragged onto the trash: its row's +/// label follows the pointer, as a past state's does. +#[test] +fn dragging_a_later_state_shows_its_label_at_the_pointer() { + let mut h = harness(); + h.state_mut().run("history.options", json!({"allowNonLinearHistory": true})).unwrap(); + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run_steps(2); + // Rows: Open, New Layer, Fill, New Layer (current), the dimmed Fill. + for (label, row) in [("New Layer", 1), ("Fill", 1)] { + let from = h.get_all_by_label(label).nth(row).expect("the row").rect().center(); + let to = from + vec2(-60.0, 40.0); + h.hover_at(from); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + for i in 1..=4 { + h.hover_at(from + (to - from) * (i as f32 / 4.0)); + h.run_steps(1); + } + assert_eq!(ghost_label(&h, to).as_deref(), Some(label), "the dragged {label} row's label follows the pointer"); + // Released off the trash: nothing is deleted. + h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + } + assert_eq!(h.state().session.active().unwrap().history.entries().len() + h.state().session.active().unwrap().history.redo_labels().count(), 5); +} diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index e7c52589f3..828d0f968b 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -3407,7 +3407,8 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) { { let label = match drag { HistoryDrag::Snapshot(id) => app.session.active().and_then(|st| st.snapshots.iter().find(|s| s.id == id)).map(|s| s.name.clone()), - HistoryDrag::State(i) => entries.get(i).cloned(), + // Rows number the past states, then the later (dimmed) ones a non-linear drag can lift. + HistoryDrag::State(i) => entries.get(i).or_else(|| i.checked_sub(entries.len()).and_then(|j| redo.get(j))).cloned(), }; crate::layer_transfer::ghost(&ctx, p, &label.unwrap_or_default()); }