From 13f3227220ed4d7c09065faecbc2f49fda1fb1e1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 09:39:04 -0800 Subject: [PATCH] Match Objects: rotate says which way it turns; scaleStrokes is journaled The gate's turn_direction test failed because the `rotate` param of addon.arrange.match did not say which way it turns. Its description now says each target turns clockwise or counter-clockwise to the source's angle. Match, Set Size of Each, Swap, Replace and Sort now record the resolved `scaleStrokes` value in their journal entry, so a replay scales strokes the same way after the preference changes. --- crates/engine/src/cmd/addons/arrange.rs | 20 +++++++--- crates/engine/src/cmd/addons/arrange/tests.rs | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/addons/arrange.rs b/crates/engine/src/cmd/addons/arrange.rs index 1588ec1162..bb16af321a 100644 --- a/crates/engine/src/cmd/addons/arrange.rs +++ b/crates/engine/src/cmd/addons/arrange.rs @@ -85,7 +85,7 @@ pub fn specs() -> Vec { "Match Objects", ["Object", "Plug-ins", "Arrange Tools"], None, - "{source?: \"key\" (the key object, select.key) | \"top\" (the frontmost selected object) | \"bottom\" (the backmost) (default: the key object when the selection has one, else \"top\"), size?: \"none\" | \"width\" (each target takes the source's width, its height following in proportion) | \"height\" (the source's height, the width following in proportion) | \"both\" (the source's width and height, each on its own) (\"none\"), rotate?: false (true: each target takes the source's bounding-box angle), stacking?: false (true: the targets go directly above the source in the stacking order, in its layer or group, in the order they had), reference?: 0..8 (the 9-point grid row by row from the top-left; 4, the center: the point of each target that stays put as it is sized and turned), scaleStrokes?: the Scale Strokes & Effects preference, patterns?: the Transform Pattern Tiles preference (at least one of size, rotate and stacking)} make the other selected objects (the top-level ones, at most 20000) match the source, which stays as it is, as one undo step (none when nothing changes). Sizes are measured and set as object.setBounds measures them (Use Preview Bounds), each in the object's own bounding-box frame; to match places, use object.align {to: \"key\"} → {source, count}", + "{source?: \"key\" (the key object, select.key) | \"top\" (the frontmost selected object) | \"bottom\" (the backmost) (default: the key object when the selection has one, else \"top\"), size?: \"none\" | \"width\" (each target takes the source's width, its height following in proportion) | \"height\" (the source's height, the width following in proportion) | \"both\" (the source's width and height, each on its own) (\"none\"), rotate?: false (true: each target turns, clockwise or counter-clockwise, to the source's bounding-box angle), stacking?: false (true: the targets go directly above the source in the stacking order, in its layer or group, in the order they had), reference?: 0..8 (the 9-point grid row by row from the top-left; 4, the center: the point of each target that stays put as it is sized and turned), scaleStrokes?: the Scale Strokes & Effects preference, patterns?: the Transform Pattern Tiles preference (at least one of size, rotate and stacking)} make the other selected objects (the top-level ones, at most 20000) match the source, which stays as it is, as one undo step (none when nothing changes). Sizes are measured and set as object.setBounds measures them (Use Preview Bounds), each in the object's own bounding-box frame; to match places, use object.align {to: \"key\"} → {source, count}", has_multi, match_objects ), @@ -177,6 +177,14 @@ fn transform_each(s: &mut Session, label: &str, strokes: bool, xfs: &[(NodeId, A }) } +/// The `scaleStrokes` param, else the Scale Strokes & Effects preference. The value joins the +/// command's journal entry, so replaying it scales the same way whatever the preference is then. +fn scale_strokes(s: &mut Session, p: &Value, cmd: &str) -> Result { + let on = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, cmd)?; + s.note_journal("scaleStrokes", json!(on)); + Ok(on) +} + /// The box of object `id` as `object.setBounds` measures it. fn box_of(s: &Session, id: NodeId) -> Result { s.transform_box(&[id]).ok_or_else(|| EngineError::Other("no bounds".into())) @@ -241,7 +249,7 @@ fn grid(s: &mut Session, p: &Value) -> Result { fn swap(s: &mut Session, p: &Value) -> Result { const C: &str = "addon.arrange.swap"; let size = switch_or(p, "size", false, C)?; - let strokes = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, C)?; + let strokes = scale_strokes(s, p, C)?; let roots = selected_roots(s)?; let &[a, b] = roots.as_slice() else { return Err(bad(C, "select exactly two objects")) }; let (ba, bb) = (box_of(s, a)?, box_of(s, b)?); @@ -254,7 +262,7 @@ fn swap(s: &mut Session, p: &Value) -> Result { fn replace(s: &mut Session, p: &Value) -> Result { const C: &str = "addon.arrange.replace"; let fit = choice(p, C, "fit", &["keep", "bounds", "proportional"])?.unwrap_or("keep"); - let strokes = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, C)?; + let strokes = scale_strokes(s, p, C)?; let keep = switch_or(p, "keep", false, C)?; let roots = selected_roots(s)?; let Some((&top, others)) = roots.split_last().filter(|(_, others)| !others.is_empty()) else { @@ -305,7 +313,7 @@ fn size(s: &mut Session, p: &Value) -> Result { } let proportional = switch_or(p, "proportional", false, C)?; let reference = whole_in(p, "reference", (0, 8), C)?.unwrap_or(4); - let strokes = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, C)?; + let strokes = scale_strokes(s, p, C)?; let mut q = json!({ "reference": reference, "strokes": strokes, "proportional": proportional && (width.is_none() || height.is_none()) }); if let Some(w) = width { q["width"] = json!(w); @@ -350,7 +358,7 @@ fn match_objects(s: &mut Session, p: &Value) -> Result { let rotate = switch_or(p, "rotate", false, C)?; let stacking = switch_or(p, "stacking", false, C)?; let reference = whole_in(p, "reference", (0, 8), C)?.unwrap_or(4); - let strokes = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, C)?; + let strokes = scale_strokes(s, p, C)?; let patterns = switch_or(p, "patterns", s.prefs.transform_pattern_tiles, C)?; if size == "none" && !rotate && !stacking { return Err(bad(C, "give `size`, `rotate` or `stacking`")); @@ -569,7 +577,7 @@ fn sort(s: &mut Session, p: &Value) -> Result { let order = choice(p, C, "order", &["ascending", "descending", "random"])?.unwrap_or("ascending"); let distribute = switch_or(p, "distribute", false, C)?; let mut rng = Rng::new(whole_or(p, "seed", 0, C)?); - let strokes = switch_or(p, "scaleStrokes", s.prefs.scale_strokes, C)?; + let strokes = scale_strokes(s, p, C)?; let ids = checked_ids_or_selection(s, p, C)?; if ids.len() > MAX_OBJECTS { return Err(bad(C, "too many objects selected (at most 20000)")); diff --git a/crates/engine/src/cmd/addons/arrange/tests.rs b/crates/engine/src/cmd/addons/arrange/tests.rs index 7cf91abfb7..8dc7ab5c43 100644 --- a/crates/engine/src/cmd/addons/arrange/tests.rs +++ b/crates/engine/src/cmd/addons/arrange/tests.rs @@ -368,6 +368,43 @@ fn match_scales_strokes_as_asked() { assert!(near(bounds(&s, a), (-5.0, -5.0, 20.0, 20.0)), "{:?}", bounds(&s, a)); } +/// Running `cmd` with Scale Strokes & Effects on doubles the 2 pt stroke of a 10 pt square, and +/// replaying its journal entry after the preference is turned off scales the stroke the same way. +fn replays_the_stroke_scaling_it_ran_with(cmd: &str, p: Value) { + let mut s = session(); + let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0); + let b = rect(&mut s, 100.0, 0.0, 20.0, 20.0); + select(&mut s, &[a, b]); + s.execute("paint.setStroke", &json!({"color": "#000000"})).unwrap(); + s.execute("stroke.set", &json!({"weight": 2})).unwrap(); + let weight = |s: &Session| s.doc().unwrap().doc.node(a).unwrap().appearance.stroke_width(); + s.execute("prefs.set", &json!({"key": "scaleStrokes", "value": true})).unwrap(); + s.execute(cmd, &p).unwrap(); + assert!((weight(&s) - 4.0).abs() < 1e-9, "{cmd}: {}", weight(&s)); + let (id, q) = s.journal.last().unwrap().clone(); + assert_eq!(id, cmd); + s.execute("edit.undo", &json!({})).unwrap(); + s.execute("prefs.set", &json!({"key": "scaleStrokes", "value": false})).unwrap(); + select(&mut s, &[a, b]); + s.execute(&id, &q).unwrap(); + assert!((weight(&s) - 4.0).abs() < 1e-9, "{cmd} replayed: {}", weight(&s)); +} + +#[test] +fn match_replays_with_the_stroke_scaling_it_ran_with() { + replays_the_stroke_scaling_it_ran_with("addon.arrange.match", json!({"size": "both"})); +} + +#[test] +fn set_size_of_each_replays_with_the_stroke_scaling_it_ran_with() { + replays_the_stroke_scaling_it_ran_with("addon.arrange.size", json!({"width": 20, "height": 20})); +} + +#[test] +fn swap_replays_with_the_stroke_scaling_it_ran_with() { + replays_the_stroke_scaling_it_ran_with("addon.arrange.swap", json!({"size": true})); +} + #[test] fn match_refuses_bad_params() { let mut s = session();