diff --git a/crates/engine/src/cmd/addons/pathtools/trim.rs b/crates/engine/src/cmd/addons/pathtools/trim.rs index f1ffab9ba0..e1a21393f1 100644 --- a/crates/engine/src/cmd/addons/pathtools/trim.rs +++ b/crates/engine/src/cmd/addons/pathtools/trim.rs @@ -4,6 +4,8 @@ //! be edited, selected or not. What it removes is worked out by //! [`vectorcraft_tools::addons::trim::plan`], which the tool also uses to show it beforehand. +use std::collections::HashSet; + use serde_json::{Value, json}; use vectorcraft_doc::{NodeId, NodeKind}; use vectorcraft_geom::{Point, Rect}; @@ -100,20 +102,21 @@ fn trim(s: &mut Session, p: &Value) -> Result { compounds.dedup(); let mut beside = Beside::replacing(d, &trimmed); let mut all = vec![]; + // The selection is read through a set and changed once at the end, so a trim of many + // paths does not go through the selected objects once per path and per piece. + let mut selected_ids: HashSet = sel.objects.iter().copied().collect(); + let mut joining: Vec = vec![]; + let mut gone: Vec = vec![]; for (id, pieces) in plan { - let selected = sel.contains(id); + let selected = selected_ids.contains(&id); sel.anchors.remove(&id); sel.segments.remove(&id); let made = replace_with_pieces(d, &mut beside, id, pieces)?; // The pieces of a selected path are selected; what was not selected stays so. if made.is_empty() { - sel.remove(id); + gone.push(id); } else if selected { - for n in &made { - if !sel.contains(*n) { - sel.add(*n); - } - } + joining.extend(made.iter().copied().filter(|n| selected_ids.insert(*n))); } all.extend(made); } @@ -121,9 +124,15 @@ fn trim(s: &mut Session, p: &Value) -> Result { for c in compounds { if d.node(c).is_some_and(|n| n.children().is_some_and(|ch| ch.is_empty())) { d.remove(c)?; - sel.remove(c); + gone.push(c); } } + sel.remove_all(gone); + if let Some((first, rest)) = joining.split_first() { + // `add` clears what any change to the object selection clears; the rest are new ids. + sel.add(*first); + sel.objects.extend_from_slice(rest); + } Ok(all) })?; Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::>(), "removed": removed })) diff --git a/crates/engine/src/cmd/addons/pathtools/trim/tests.rs b/crates/engine/src/cmd/addons/pathtools/trim/tests.rs index a36c705e4f..6e0e2c057a 100644 --- a/crates/engine/src/cmd/addons/pathtools/trim/tests.rs +++ b/crates/engine/src/cmd/addons/pathtools/trim/tests.rs @@ -2,8 +2,9 @@ //! one undo step, and what takes no part. use serde_json::{Value, json}; -use vectorcraft_doc::{NodeId, NodeKind}; +use vectorcraft_doc::{NodeId, NodeKind, Selection}; use vectorcraft_geom::{PathData, Point}; +use vectorcraft_tools::addons::trim::plan::{self, Limits, Stretches}; use crate::Session; @@ -420,6 +421,139 @@ fn the_selection_stays_as_it_was_and_the_pieces_of_a_selected_path_are_selected( assert!(s.doc().unwrap().selection.is_empty(), "a removed path leaves the selection"); } +/// Run the command with `p`, and check that it leaves the selection exactly as changing it one +/// id at a time does: for each trimmed path in the plan's order, its anchors and segments let go, +/// a path with no pieces left removed, and each piece of a selected path added; then each emptied +/// compound path removed. Returns the selection. +fn same_selection_as_one_id_at_a_time(s: &mut Session, p: Value) -> Selection { + let st = s.doc().unwrap(); + let pick = super::pick(&p).unwrap(); + let root = crate::cmd::ordinary_isolation(st); + let plan: Vec<(NodeId, usize)> = + plan::plan(&st.doc, root, &pick, Limits::COMMAND, &mut Stretches::default()).unwrap().into_iter().map(|t| (t.id, t.pieces.len())).collect(); + let mut compounds: Vec = plan + .iter() + .filter_map(|(id, _)| st.doc.parent_of(*id)) + .filter(|c| matches!(st.doc.node(*c).map(|n| &n.kind), Some(NodeKind::Compound { .. }))) + .collect(); + compounds.dedup(); + let mut want = st.selection.clone(); + let made = ids(&s.execute(TRIM, &p).unwrap()); + assert_eq!(made.len(), plan.iter().map(|(_, n)| n).sum::()); + let mut made = made.into_iter(); + for (id, n) in plan { + let selected = want.contains(id); + want.anchors.remove(&id); + want.segments.remove(&id); + let pieces: Vec = made.by_ref().take(n).collect(); + if pieces.is_empty() { + want.remove(id); + } else if selected { + for piece in pieces { + if !want.contains(piece) { + want.add(piece); + } + } + } + } + let st = s.doc().unwrap(); + for c in compounds { + if st.doc.node(c).is_none() { + want.remove(c); + } + } + want.prune(&st.doc); + assert_eq!(st.selection, want); + st.selection.clone() +} + +/// Two lines across split by a rectangle, one of them selected; a selected line nothing crosses +/// and a selected compound path of two such lines, which the rectangle removes whole; and a +/// selected line elsewhere with an anchor and a segment direct-selected. Returns (the selected +/// line across, the line removed whole, the compound path, the line elsewhere). +fn trim_scene(s: &mut Session) -> (NodeId, NodeId, NodeId, NodeId) { + let (h, ..) = across_and_uprights(s); + line(s, &[(0.0, 120.0), (300.0, 120.0)], false); + let alone = line(s, &[(145.0, 200.0), (155.0, 200.0)], false); + let c1 = line(s, &[(145.0, 170.0), (155.0, 170.0)], false); + let c2 = line(s, &[(145.0, 180.0), (155.0, 180.0)], false); + s.execute("select.set", &json!({"ids": [c1.0, c2.0]})).unwrap(); + let compound = NodeId(s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap()); + let elsewhere = line(s, &[(0.0, 250.0), (10.0, 250.0), (20.0, 250.0)], false); + s.execute("select.set", &json!({"ids": [elsewhere.0, h.0, alone.0, compound.0]})).unwrap(); + s.edit("direct", |_, sel| { + sel.anchors.insert(elsewhere, [(0, 1)].into()); + sel.anchors.insert(h, [(0, 0)].into()); + sel.segments.insert(elsewhere, [(0, 0)].into()); + Ok(()) + }) + .unwrap(); + (h, alone, compound, elsewhere) +} + +#[test] +fn the_selection_after_a_trim_is_the_one_changing_it_one_id_at_a_time_gives() { + // A removed key object stops being the key. + let mut s = session(); + let (h, alone, compound, elsewhere) = trim_scene(&mut s); + s.edit("key", |_, sel| { + sel.key = Some(alone); + Ok(()) + }) + .unwrap(); + let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 120]})); + assert_eq!(sel.key, None); + assert!(!sel.contains(alone) && !sel.contains(compound)); + assert_eq!(&sel.objects[..2], [elsewhere, h]); + assert_eq!(sel.objects.len(), 3, "{:?}", sel.objects); + assert!(sel.anchors.contains_key(&elsewhere) && sel.segments.contains_key(&elsewhere) && !sel.anchors.contains_key(&h)); + // A key object that keeps its first piece stays the key. + let mut s = session(); + let (h, alone, compound, _) = trim_scene(&mut s); + s.edit("key", |_, sel| { + sel.key = Some(h); + Ok(()) + }) + .unwrap(); + let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 120]})); + assert_eq!(sel.key, Some(h)); + assert!(!sel.contains(alone) && !sel.contains(compound)); + // Trimming only paths that are not selected leaves a targeted object targeted. + let mut s = session(); + let (h, _, _, elsewhere) = trim_scene(&mut s); + s.execute("select.set", &json!({"ids": [elsewhere.0]})).unwrap(); + s.edit("target", |_, sel| { + sel.target = Some(elsewhere); + Ok(()) + }) + .unwrap(); + let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 40]})); + assert_eq!((sel.objects.as_slice(), sel.target), ([elsewhere].as_slice(), Some(elsewhere))); + assert!(exists(&s, h)); + // A selected path that gains a piece changes the object selection, which lets go of the target. + let mut s = session(); + let (h, _, _, elsewhere) = trim_scene(&mut s); + s.execute("select.set", &json!({"ids": [elsewhere.0, h.0]})).unwrap(); + s.edit("target", |_, sel| { + sel.target = Some(elsewhere); + Ok(()) + }) + .unwrap(); + let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 40]})); + assert_eq!((sel.objects.len(), sel.target), (3, None)); + // So does a selected path that is removed whole. + let mut s = session(); + let (_, alone, _, elsewhere) = trim_scene(&mut s); + s.execute("select.set", &json!({"ids": [elsewhere.0, alone.0]})).unwrap(); + s.edit("target", |_, sel| { + sel.target = Some(elsewhere); + Ok(()) + }) + .unwrap(); + let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 190, 20, 20]})); + assert_eq!((sel.objects.as_slice(), sel.target), ([elsewhere].as_slice(), None)); +} + #[test] fn many_paths_far_from_the_trimmed_one_cost_no_crossing_search() { let mut s = session(); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 469d6c95f3..f83675f1a1 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2599,6 +2599,8 @@ A VectorCraft addition, not a feature of the reference app, so no area score mov A VectorCraft addition, not a feature of the reference app, so no area score moves. Object › Plug-ins › Path Precision Tools › Trim at Crossings Tool (also in the Scissors tool's group in both shipped toolbars, after the Knife, since 2026-11-16; a toolbar edited before keeps its saved layout until Reset Toolbar; no shortcut) removes stretches of paths between the places where they are crossed by another path or by themselves, on any path that can be edited, selected or not. A click removes the stretch under the pointer, a drag removes every stretch its rectangle touches, and an Alt-drag removes every stretch its freehand line crosses; each is one undo step, and Escape during a drag removes nothing. A closed path that loses a stretch becomes open, an open path that loses a middle stretch becomes two paths with its appearance in its place in the stacking order, a path that nothing crosses is removed whole, and the stretches that stay get no new anchors. Hidden and locked paths, guides and clipping paths are not trimmed and do not count as crossing. Add Points at / Split at Crossings share the crossing search: segments that meet at an anchor now count where they cross again elsewhere, and with Self Crossings a curved segment that loops over itself crosses itself. Command `addon.pathtools.trim`; `vectorcraft_pathops::addons::pathtools::{crossings_on, spans, without_spans}`. Hovering shows in red the stretch a click would remove, worked out by the same code the command uses (`vectorcraft_tools::addons::trim::plan`), and a drag that reaches at most 32 paths shows what its release would remove; a preview spends at most a twentieth of the crossing work a click may (2026-11-15; tests `hovering_*`, `in_isolation_mode_*`, `a_path_whose_crossings_*` and `a_small_drag_*` in `addons::trim` in `vectorcraft-tools`, `hovering_with_the_tool_shows_the_stretch_its_click_removes` and `what_the_tool_shows_*` in `vectorcraft-engine`). Not yet: crossings with type, images and symbol instances. Tests: `crossings_on_*`, `segments_*`, `a_segment_that_loops_over_itself_crosses_itself`, `one_work_budget_*`, `spans_divide_*`, `a_subpath_without_*` and `a_slice_*` in `addons::pathtools::tests` in `vectorcraft-pathops`, `addons::trim` in `vectorcraft-tools` (click, rectangle, freehand line, Escape), `cmd::addons::pathtools::trim::tests` in `vectorcraft-engine` (a middle and an end stretch, a closed path opening, a rectangle and a line as one undo step, locked and hidden paths, guides and clipping paths, paths nothing crosses, compound path members, the selection, 3,000 other paths, a path too large to search left alone until touched, bad params refused), `the_plugins_submenu_selects_the_tools_and_opens_the_anchor_inspector` in `vectorcraft-ui-egui`, `edit_toolbar::tests` and `command_sweep`. Commands: `docs/mcp.md` › Path Precision Tools add-on. The tool shows the Knife's cursor, now drawn as a knife with the blade's point on the hotspot (2026-11-16; tests `trim_at_crossings_is_in_the_scissors_flyout` and `a_saved_toolbar_without_trim_at_crossings_keeps_its_layout` in `edit_toolbar::tests`, `trim_at_crossings_is_in_the_scissors_group_once` in `addons::tests`, `headless_backend_methods` in `vectorcraft-mcp`, `the_knife_shows_a_knife_with_its_point_on_the_hotspot` in `cursors::tests` and `drawing_tools_show_their_own_cursor` in `vectorcraft-tools`). +A trim reads the selection through a set and changes it once at the end, instead of going through the selected objects once for each trimmed path and each piece, with the same resulting selection as before. With 2,000 selected lines each split in two, the selection update took 7.7 ms and now takes 0.2 ms (the whole command about 75 ms); with 7,000 lines it took 96 ms and now takes 0.7 ms. The times are wall-clock medians of 7 runs while other builds shared the CPU. Test: `the_selection_after_a_trim_is_the_one_changing_it_one_id_at_a_time_gives` in `cmd::addons::pathtools::trim::tests` in `vectorcraft-engine`. + ## Document Tools add-on: Save a Timestamped Copy (2026-11-01) A VectorCraft addition, not a feature of the reference app, so no area score moves. Object › Plug-ins › Document Tools › Save a Timestamped Copy writes a copy of the document named `_YYYY-MM-DD_HH-MM.` (the date and time in UTC, through chrono; `-2`, `-3`… added when that name is taken) next to the document, or into a folder picked (the folder picker) when it was never saved, in the document's own format or another save format. It writes through File › Save a Copy's save, so the document keeps its path, title and modified state, no undo step is made, and the write stays inside the automation roots in force. It is off on the web, which has no clock. Tests: `cmd::addons::document::tests` in `vectorcraft-engine` (the name and place, numbered names, the document unchanged and its content in the copy, an unsaved document needing a folder, other formats, bad params refused, automation roots), `addons::document::tests` in `vectorcraft-ui-egui` (the submenu, beside the document, the folder picked or cancelled) and `command_sweep`. Commands: `docs/mcp.md` › Document Tools add-on.