diff --git a/crates/engine-core/src/cmd.rs b/crates/engine-core/src/cmd.rs index 16cdb14180..615117a8b9 100644 --- a/crates/engine-core/src/cmd.rs +++ b/crates/engine-core/src/cmd.rs @@ -121,6 +121,8 @@ pub mod solid_fill_cmds; pub mod style_asl_cmds; pub mod swatch_cmds; pub mod symmetry_cmds; +#[cfg(test)] +mod symmetry_path_delete_tests; pub mod table; pub mod timeline_cmds; pub mod transform_cmds; diff --git a/crates/engine-core/src/cmd/symmetry_cmds.rs b/crates/engine-core/src/cmd/symmetry_cmds.rs index 58d4ab487e..0668f80dea 100644 --- a/crates/engine-core/src/cmd/symmetry_cmds.rs +++ b/crates/engine-core/src/cmd/symmetry_cmds.rs @@ -8,7 +8,7 @@ use photocraft_paint::{ }; use serde_json::{Value, json}; -use crate::{EngineError, Result, Session, commands::CommandSpec}; +use crate::{DocState, EngineError, Result, Session, commands::CommandSpec}; // Keep nearest-segment searches bounded for long interactive brush strokes. const MAX_SEGMENTS: usize = 1024; @@ -222,6 +222,46 @@ fn disable(s: &mut Session) -> Result { Ok(json!({"enabled": false})) } +/// Before Delete Path: the history state it starts from and the symmetry in force, when the path +/// being deleted is the one path symmetry mirrors across. Deleting it leaves symmetry off (#17384). +pub(crate) fn symmetry_before_path_delete(s: &Session, path: &str) -> Option<(u64, PaintingSymmetry)> { + let st = s.active()?; + let symmetry = st.symmetry.clone()?; + if symmetry.path_source() != Some(path) { + return None; + } + Some((st.history.row_id(st.history.past_len())?, symmetry)) +} + +/// After Delete Path removed the path `before` was taken from: symmetry is off, and undoing that +/// step puts the one it was back. The entries of steps history no longer holds are dropped. +pub(crate) fn path_deleted(s: &mut Session, before: Option<(u64, PaintingSymmetry)>) { + let Some(st) = s.active_mut() else { return }; + let Some((from, symmetry)) = before else { return }; + st.symmetry = None; + let Some(to) = st.history.row_id(st.history.past_len()) else { return }; + let history = &st.history; + st.undo_symmetry.retain(|(kept_from, kept_to, _)| *kept_to != to && history.row_of(*kept_from).is_some() && history.row_of(*kept_to).is_some()); + st.undo_symmetry.push((from, to, symmetry)); +} + +/// Undo just left the state `left` for the current one: when that undid Delete Path, bring the +/// symmetry it turned off back. +pub(crate) fn restore_symmetry_after_undo(st: &mut DocState, left: Option) { + let Some(left) = left else { return }; + let Some(id) = st.history.row_id(st.history.past_len()) else { return }; + let Some((_, _, symmetry)) = st.undo_symmetry.iter().find(|(from, to, _)| *from == id && *to == left) else { return }; + st.symmetry = Some(symmetry.clone()); +} + +/// Redo of Delete Path deletes the path again, so the symmetry it turned off goes off again too. +pub(crate) fn clear_symmetry_after_redo(st: &mut DocState) { + let Some(current) = st.history.row_id(st.history.past_len()) else { return }; + if st.undo_symmetry.iter().any(|(_, to, _)| *to == current) { + st.symmetry = None; + } +} + pub fn specs() -> Vec { vec![ CommandSpec { diff --git a/crates/engine-core/src/cmd/symmetry_path_delete_tests.rs b/crates/engine-core/src/cmd/symmetry_path_delete_tests.rs new file mode 100644 index 0000000000..177a1ef60e --- /dev/null +++ b/crates/engine-core/src/cmd/symmetry_path_delete_tests.rs @@ -0,0 +1,89 @@ +//! #17384: deleting the path that is the active painting symmetry turns the symmetry off, and +//! undo of the delete brings the path and the symmetry back. Deleting another path, or any path +//! while a preset symmetry is active, changes nothing. + +use serde_json::{Value, json}; + +use crate::Session; + +fn session() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + s +} + +/// A three-point polyline: two segments, like the issue's measured path. +fn add_path(s: &mut Session, name: &str) { + s.execute("path.set", json!({"name": name, "path": {"subpaths": [{"knots": [[100, 50], [150, 50], [200, 100]]}]}})).unwrap(); +} + +fn symmetry(s: &Session) -> Value { + crate::inspect::document(s.active().unwrap())["symmetry"].clone() +} + +fn has_path(s: &Session, name: &str) -> bool { + s.active().unwrap().doc.paths.iter().any(|p| p.name == name) +} + +#[test] +fn deleting_the_symmetry_path_turns_symmetry_off_and_undo_brings_it_back() { + let mut s = session(); + add_path(&mut s, "Path 1"); + s.execute("paint.symmetryFromPath", json!({"name": "Path 1"})).unwrap(); + assert_eq!(symmetry(&s)["source"], "Path 1"); + let original = symmetry(&s); + + s.execute("path.delete", json!({"name": "Path 1"})).unwrap(); + assert!(symmetry(&s).is_null(), "deleting the source path turns symmetry off"); + assert!(s.active().unwrap().symmetry.is_none(), "nothing is mirrored any more"); + assert!(!has_path(&s, "Path 1"), "the path is gone"); + + assert!(s.undo()); + assert!(has_path(&s, "Path 1"), "undo restores the path"); + assert_eq!(symmetry(&s), original, "undo restores the symmetry"); + + assert!(s.redo()); + assert!(symmetry(&s).is_null(), "redo deletes the path again, so symmetry is off again"); +} + +/// The work path answers to "work", "" and "Work Path" in `path.delete`, while symmetry stores +/// it as "work"; every alias must still turn symmetry off. +#[test] +fn deleting_the_work_path_by_an_alias_turns_symmetry_off_too() { + for alias in ["Work Path", ""] { + let mut s = session(); + s.execute("path.set", json!({"name": "work", "path": {"subpaths": [{"knots": [[100, 50], [200, 100]]}]}})).unwrap(); + s.execute("paint.symmetryFromPath", json!({"name": "work"})).unwrap(); + s.execute("path.delete", json!({"name": alias})).unwrap(); + assert!(symmetry(&s).is_null(), "{alias:?} deletes the work path, so symmetry is off"); + assert!(s.undo()); + assert_eq!(symmetry(&s)["source"], "work", "{alias:?}: undo brings the symmetry back"); + } +} + +#[test] +fn deleting_the_work_path_turns_symmetry_off_too() { + let mut s = session(); + s.execute("path.set", json!({"name": "work", "path": {"subpaths": [{"knots": [[100, 50], [200, 100]]}]}})).unwrap(); + s.execute("paint.symmetryFromPath", json!({"name": "work"})).unwrap(); + s.execute("path.delete", json!({})).unwrap(); + assert!(symmetry(&s).is_null()); + assert!(s.undo()); + assert_eq!(symmetry(&s)["source"], "work", "undo puts the work path's symmetry back"); +} + +#[test] +fn deleting_another_path_or_a_path_beside_a_preset_changes_nothing() { + let mut s = session(); + add_path(&mut s, "Path 1"); + add_path(&mut s, "Path 2"); + s.execute("paint.symmetryFromPath", json!({"name": "Path 1"})).unwrap(); + let path_symmetry = symmetry(&s); + s.execute("path.delete", json!({"name": "Path 2"})).unwrap(); + assert_eq!(symmetry(&s), path_symmetry, "another path leaves the path symmetry alone"); + + s.execute("paint.setSymmetry", json!({"mode": "vertical"})).unwrap(); + let preset = symmetry(&s); + s.execute("path.delete", json!({"name": "Path 1"})).unwrap(); + assert_eq!(symmetry(&s), preset, "a preset symmetry survives the path being deleted"); +} diff --git a/crates/engine-core/src/cmd/vector_cmds/paths.rs b/crates/engine-core/src/cmd/vector_cmds/paths.rs index f523c68a00..d3e07b2a2d 100644 --- a/crates/engine-core/src/cmd/vector_cmds/paths.rs +++ b/crates/engine-core/src/cmd/vector_cmds/paths.rs @@ -141,6 +141,11 @@ pub(super) fn path_set(s: &mut Session, p: &Value) -> Result { pub(super) fn path_delete(s: &mut Session, p: &Value) -> Result { let name = p.get("name").and_then(Value::as_str).map(str::to_string); + // Deleting the path that painting symmetry mirrors across leaves symmetry off (#17384). The + // work path answers to "work", "" and "Work Path", while symmetry stores it as "work". + let deleted = name.as_deref().unwrap_or("work"); + let source = if is_work(Some(deleted)) { "work" } else { deleted }; + let symmetry = crate::symmetry_cmds::symmetry_before_path_delete(s, source); s.edit("Delete Path", |doc, _| { if is_work(name.as_deref()) { return doc.work_path.take().map(|_| ()).ok_or_else(|| EngineError::Other("no work path".into())); @@ -156,6 +161,7 @@ pub(super) fn path_delete(s: &mut Session, p: &Value) -> Result { } Ok(()) })?; + crate::symmetry_cmds::path_deleted(s, symmetry); Ok(Value::Null) } diff --git a/crates/engine-core/src/doc_state.rs b/crates/engine-core/src/doc_state.rs index fdb9f452f8..8ac2581f79 100644 --- a/crates/engine-core/src/doc_state.rs +++ b/crates/engine-core/src/doc_state.rs @@ -34,6 +34,10 @@ pub struct DocState { /// The channel views undo brings back, by the step they belong to: the ids of the history /// states New Channel or Calculations started from and made (#14683). View state, not history. pub(crate) undo_channel_views: Vec<(u64, u64, channel_cmds::ChannelView)>, + /// The painting symmetry undo brings back, by the step it belongs to: the ids of the history + /// states Delete Path started from and made, when the path deleted was its source (#17384). + /// Tool state, not history. + pub(crate) undo_symmetry: Vec<(u64, u64, symmetry_cmds::PaintingSymmetry)>, /// Select › Isolate Layers: the Layers panel lists only these layers (empty = off; view state). pub isolated_layers: Vec, /// Painting symmetry preset or sampled path (tool state, not document pixels). @@ -148,6 +152,7 @@ impl DocState { coalesce: None, channel_view, undo_channel_views: Vec::new(), + undo_symmetry: Vec::new(), isolated_layers: Vec::new(), symmetry: None, fx_collapsed: Vec::new(), diff --git a/crates/engine-core/src/session/history.rs b/crates/engine-core/src/session/history.rs index b8ae73c4d6..7c6f45d223 100644 --- a/crates/engine-core/src/session/history.rs +++ b/crates/engine-core/src/session/history.rs @@ -57,6 +57,7 @@ impl Session { let keep_clean = st.keeps_clean(&before); restore_target(st, layers); channel_cmds::restore_view_after_undo(st, left); + crate::symmetry_cmds::restore_symmetry_after_undo(st, left); st.bump_revision(keep_clean); st.last_damage = damage; st.record_damage(); @@ -97,6 +98,7 @@ impl Session { let before = std::mem::replace(st.doc_mut(), d); let keep_clean = st.keeps_clean(&before); restore_target(st, layers); + crate::symmetry_cmds::clear_symmetry_after_redo(st); st.bump_revision(keep_clean); st.last_damage = damage; st.record_damage();