Merge pull request #10344 from dotCooCoo/fix/selection-tool-hides-anchors

Selection: a path selected whole shows its anchors without direction handles
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 19:12:54 -08:00
commit e4da493676
2 files changed
+116 -19

No files matched your search

+42 -6
View File
@@ -29,7 +29,6 @@ use super::outline::{ViewHides, c32, linked_crosses, node_outline, selection_hit
// PLAN: `BodyKind`, `Win` and `ViewKey::from(CanvasView)` are canvas-w10's (cv.S1, S2, S5); their paths here follow the contract.
use super::{BodyKind, VectorcraftApp, Win};
// PLAN: `anchors` takes `tool_points` because the Curvature tool's own points come from `session.overlays`, which only `show` may call (`&mut app`).
// PLAN: BezierOverlay selects anchors of every path under `HandlesShown`, so a whole-selected container draws handles that the old marks hid outside Direct Selection.
/// The anchor square's side at Selection & Anchor Display › Size 3, the default, in points.
const ANCHOR_AT_DEFAULT_SIZE: f32 = 5.0;
@@ -60,6 +59,12 @@ pub(super) struct Anchors {
pub paths: Vec<EditPath>,
/// The selected anchors and the selected segment.
pub selection: PathSelection,
/// The paths whose anchors show selected without direction handles: paths selected whole, or inside a container
/// selected whole, under a tool that does not drag handles, and the paths inside a container selected whole under
/// one that does ([`vectorcraft_tools::catalog::edits_anchors`]).
pub plain: Vec<EditPath>,
/// Every anchor of `plain`, selected.
pub plain_selection: PathSelection,
/// Anchors as squares, or as circles under the Curvature tool.
pub shown: AnchorsShown,
/// Whose direction handles show.
@@ -259,15 +264,21 @@ fn grid_path(key: u64, points: impl Iterator<Item = Point>, color: Color32) -> E
/// The anchors and handles of the selection. An object selected whole has every anchor selected, and so do the
/// paths inside a group, compound path or clip group selected whole; with Direct Selection's partial selection only
/// the chosen anchors are, and a selected segment hollows the others. `tool_points` is whether the tool draws its own
/// points (the Curvature tool while it draws), which leaves nothing here. `tool` is the tool whose marks show
/// ([`super::shown_tool`]). Nothing too big to outline, and nothing during Halftone Preview.
/// the chosen anchors are, and a selected segment hollows the others. Direction handles show for a partial selection's
/// anchors, for a selected segment, and for a path selected whole when `tool` drags handles
/// ([`vectorcraft_tools::catalog::edits_anchors`]); every other path goes to [`Anchors::plain`], whose anchors show
/// without handles. `tool_points` is whether the tool draws its own points (the Curvature tool while it draws), which
/// leaves nothing here. `tool` is the tool whose marks show ([`super::shown_tool`]). Nothing too big to outline, and
/// nothing during Halftone Preview.
pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_points: bool) -> Anchors {
let curvature = tool == "curvature";
let drags_handles = vectorcraft_tools::catalog::edits_anchors(tool);
let prefs = &app.session.prefs;
let mut out = Anchors {
paths: vec![],
selection: PathSelection::default(),
plain: vec![],
plain_selection: PathSelection::default(),
shown: if curvature { AnchorsShown::Curvature } else { AnchorsShown::All },
handles: if prefs.show_handles_multiple_anchors { HandlesShown::Selected } else { HandlesShown::OneAnchor },
segment_mark: SegmentMark::Line,
@@ -279,6 +290,7 @@ pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_poin
let (sel, hides) = (&st.selection, ViewHides::of(app));
// The selected anchors by path, and the selected segment with its path.
let mut chosen: Vec<(u64, Vec<KnotRef>)> = vec![];
let mut plain: Vec<(u64, Vec<KnotRef>)> = vec![];
let mut segment: Option<(u64, KnotRef)> = None;
for id in &sel.objects {
let Some(node) = st.doc.node(*id) else { continue };
@@ -295,8 +307,15 @@ pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_poin
return;
}
let NodeKind::Path { path, .. } = &c.kind else { return };
out.paths.push(edit_path(c.id.0, path, color));
let segments = sel.segments.get(&c.id);
// The handles the tools drag ([`vectorcraft_tools::direct`]'s `handle_anchors`): a partial selection's, a
// selected segment's, and those of a path selected itself under a tool that edits anchors.
if partial.is_none() && segments.is_none() && !(drags_handles && c.id == *id) {
out.plain.push(edit_path(c.id.0, path, color));
plain.push((c.id.0, path.anchors().map(|(si, ai, _)| KnotRef::new(si, ai)).collect()));
return;
}
out.paths.push(edit_path(c.id.0, path, color));
if let Some(&(si, seg)) = segments.and_then(|s| s.iter().next()) {
segment.get_or_insert((c.id.0, KnotRef::new(si, seg)));
}
@@ -326,6 +345,14 @@ pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_poin
);
selection.others = by_key(chosen.into_iter().filter(|(key, _)| Some(*key) != primary));
out.selection = selection;
// `show` draws `plain` with `HandlesShown::OneAnchor`, which shows handles only while one anchor is selected. A
// lone anchor is drawn with its handles retracted instead; the selection's outline still draws its path.
if plain.iter().map(|(_, knots)| knots.len()).sum::<usize>() == 1 {
for knot in out.plain.iter_mut().flat_map(|p| &mut p.subpaths).flat_map(|sp| &mut sp.knots) {
(knot.in_ctrl, knot.out_ctrl) = (knot.anchor, knot.anchor);
}
}
out.plain_selection.others = by_key(plain);
out
}
@@ -660,6 +687,15 @@ pub(super) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, space: &CanvasSp
.path_options(&anchors.options)
.show_at(ui, rect);
}
if !anchors.plain.is_empty() {
let mut paths = anchors.plain.clone();
let _ = BezierOverlay::new("Selected anchors", Bind::bound(&mut paths), &anchors.plain_selection)
.tool(BezierTool::Anchors)
.handles(HandlesShown::OneAnchor)
.anchors(anchors.shown)
.path_options(&anchors.options)
.show_at(ui, rect);
}
let dots = &marks.dots;
if !(dots.centers.is_empty() && dots.origins.is_empty() && dots.key_anchor.is_none()) {
SelectionDots { common: Common::new(), dots }.label("Selection dots").show_at(ui, rect);
@@ -734,7 +770,7 @@ impl SelectionMarks {
o.paths.iter_mut().chain(o.key.as_mut()).for_each(|path| path.apply_affine(shift));
o.revision = outline_revision(&o.paths, o.key.as_ref(), o.color.to_array());
}
for knot in self.anchors.paths.iter_mut().flat_map(|p| &mut p.subpaths).flat_map(|sp| &mut sp.knots) {
for knot in self.anchors.paths.iter_mut().chain(&mut self.anchors.plain).flat_map(|p| &mut p.subpaths).flat_map(|sp| &mut sp.knots) {
knot.anchor += by;
knot.in_ctrl += by;
knot.out_ctrl += by;
+74 -13
View File
@@ -50,6 +50,11 @@ fn marks(app: &VectorcraftApp) -> Anchors {
anchors(app, app.session.active().unwrap(), app.session.tool_id(), false)
}
/// Every path whose anchors show, with handles or without.
fn shown_paths(anchors: &Anchors) -> Vec<&EditPath> {
anchors.paths.iter().chain(&anchors.plain).collect()
}
fn all_selected(selection: &PathSelection) -> usize {
selection.anchors.len() + selection.others.iter().map(|(_, knots)| knots.len()).sum::<usize>()
}
@@ -215,7 +220,7 @@ fn a_clip_group_over_a_huge_path_shows_its_clipping_path() {
vec![vectorcraft_geom::Rect::new(100.0, 100.0, 200.0, 200.0)],
"the square's outline, not the bounds of the huge art"
);
assert_eq!(marks(&app).paths.iter().map(|p| p.anchor_count()).sum::<usize>(), 4, "and its four anchors");
assert_eq!(shown_paths(&marks(&app)).iter().map(|p| p.anchor_count()).sum::<usize>(), 4, "and its four anchors");
}
#[test]
@@ -228,13 +233,13 @@ fn a_huge_selection_shows_its_bounds_instead_of_every_outline() {
assert_eq!((huge.len(), huge[0].0, huge[0].1.len()), (1, color, 1), "one bounds path in the layer colour");
let st = app.session.active().unwrap();
assert!(outlines(&app, st)[0].paths[0].elements().len() < 10, "a rectangle, not the outline");
assert!(marks(&app).paths.is_empty(), "no anchors either");
assert!(shown_paths(&marks(&app)).is_empty(), "no anchors either");
assert!(dots(&app, st).origins.is_empty());
app.run("edit.undo", json!({})).unwrap();
app.run("path.create", json!({"d": format!("M20 20 {} Z", points(200))})).unwrap();
let st = app.session.active().unwrap();
assert!(outlines(&app, st)[0].paths[0].elements().len() > 200, "a path of few points is outlined whole");
assert_eq!(marks(&app).paths.len(), 1);
assert_eq!(shown_paths(&marks(&app)).len(), 1);
}
#[test]
@@ -389,7 +394,7 @@ fn a_selected_group_leaves_its_hidden_members_unmarked() {
app.run("select.set", json!({"ids": [group.0]})).unwrap();
let st = app.session.active().unwrap();
let outlined: Vec<Vec<PathEl>> = outlines(app, st).into_iter().flat_map(|o| o.paths).map(|p| p.elements().to_vec()).collect();
let anchored = marks(app).paths.iter().map(|p| p.anchor_count()).collect::<Vec<_>>();
let anchored = shown_paths(&marks(app)).iter().map(|p| p.anchor_count()).collect::<Vec<_>>();
let hovered = outline_of(st, group, ViewHides::of(app)).map(|h| h.path.elements().to_vec());
(outlined, anchored, hovered)
};
@@ -422,7 +427,7 @@ fn a_selected_group_leaves_out_its_guides_while_guides_are_hidden() {
app.run("select.set", json!({"ids": [group.0]})).unwrap();
let st = app.session.active().unwrap();
let outlined: Vec<vectorcraft_geom::Rect> = outlines(app, st).into_iter().flat_map(|o| o.paths).map(|p| p.bounding_box()).collect();
let anchored = marks(app).paths.iter().map(|p| p.anchor_count()).collect::<Vec<_>>();
let anchored = shown_paths(&marks(app)).iter().map(|p| p.anchor_count()).collect::<Vec<_>>();
let hovered = outline_of(st, group, ViewHides::of(app)).map(|h| h.path.bounding_box());
(outlined, anchored, hovered)
};
@@ -554,9 +559,64 @@ fn the_anchors_of_a_container_selected_whole_draw_selected() {
app.select_tool("directSelection");
app.run("select.set", json!({"ids": [group.0]})).unwrap();
let shown = marks(&app);
// The ellipse's four anchors and the square's four.
assert_eq!(all_selected(&shown.selection), 8);
assert_eq!(shown.paths.len(), 2);
// The ellipse's four anchors and the square's four, drawn without handles: the tools drag only the handles of a
// path selected itself.
assert_eq!(all_selected(&shown.plain_selection), 8);
assert_eq!(shown.plain.len(), 2);
assert!(shown.paths.is_empty() && all_selected(&shown.selection) == 0, "no member's handles show");
}
/// The Selection tool shows a path selected whole with every anchor selected and no direction handles; Direct
/// Selection shows the same path's handles, and a partial selection's anchors keep theirs under either tool.
#[test]
fn the_selection_tool_shows_a_whole_paths_anchors_without_handles() {
let (mut app, curve) = scene();
let count = {
let NodeKind::Path { path, .. } = &app.session.active().unwrap().doc.node(curve).unwrap().kind else { panic!("a path") };
path.anchor_count()
};
app.select_tool("selection");
app.run("select.set", json!({"ids": [curve.0]})).unwrap();
let shown = marks(&app);
assert!(shown.paths.is_empty(), "no path of the Selection tool's marks shows handles");
assert_eq!(shown.plain.iter().map(|p| p.key).collect::<Vec<_>>(), vec![curve.0]);
assert_eq!(all_selected(&shown.plain_selection), count, "every anchor draws selected");
app.select_tool("directSelection");
let direct = marks(&app);
assert!(direct.plain.is_empty(), "Direct Selection draws the path with its handles");
assert_eq!((direct.selection.path, direct.selection.anchors.len()), (Some(curve.0), count));
app.select_tool("selection");
app.run("select.anchors", json!({"id": curve.0, "anchors": [[0, 2]], "mode": "set"})).unwrap();
let partial = marks(&app);
assert!(partial.plain.is_empty(), "a partial selection keeps its anchor's handles");
assert_eq!(partial.selection.anchors, vec![KnotRef::new(0, 2)]);
}
/// A lone anchor selected whole under the Selection tool is drawn with its handles retracted, since the overlay shows
/// handles while one anchor is selected.
#[test]
fn a_lone_anchor_selected_whole_shows_no_handles() {
let mut app = blank();
let point = id_of(&app.run("path.create", json!({"d": "M40 40"})).unwrap());
app.run("path.setHandle", json!({"id": point.0, "anchor": 0, "which": "out", "x": 60, "y": 10, "independent": true})).unwrap();
let NodeKind::Path { path, .. } = &app.session.active().unwrap().doc.node(point).unwrap().kind else { panic!("a path") };
assert_eq!((path.anchor_count(), path.subpaths[0].anchors[0].h_out), (1, Point::new(60.0, 10.0)), "one anchor with a handle");
app.select_tool("selection");
app.run("select.set", json!({"ids": [point.0]})).unwrap();
let shown = marks(&app);
assert_eq!(all_selected(&shown.plain_selection), 1);
let knots: Vec<_> = shown.plain.iter().flat_map(|p| &p.subpaths).flat_map(|sp| &sp.knots).collect();
assert_eq!(knots.len(), 1);
assert!(knots.iter().all(|k| k.in_ctrl == k.anchor && k.out_ctrl == k.anchor), "the lone anchor's handles are retracted");
// Two anchors keep their handles in the marks: the overlay shows none while more than one is selected.
let curve = id_of(&app.run("path.create", json!({"d": "M40 140 C60 110 90 110 110 140"})).unwrap());
app.run("select.set", json!({"ids": [curve.0]})).unwrap();
let two = marks(&app);
assert_eq!(all_selected(&two.plain_selection), 2);
assert!(two.plain.iter().flat_map(|p| &p.subpaths).flat_map(|sp| &sp.knots).any(|k| k.out_ctrl != k.anchor), "the curve keeps its shape");
}
#[test]
@@ -844,9 +904,9 @@ fn the_outline_revision_follows_what_is_drawn() {
#[test]
fn an_empty_selection_has_no_marks() {
let (mut app, _) = scene();
assert!(!seen(&app).is_empty() && !marks(&app).paths.is_empty());
assert!(!seen(&app).is_empty() && !shown_paths(&marks(&app)).is_empty());
app.run("select.none", json!({})).unwrap();
assert!(seen(&app).is_empty() && marks(&app).paths.is_empty());
assert!(seen(&app).is_empty() && shown_paths(&marks(&app)).is_empty());
let st = app.session.active().unwrap();
assert_eq!(dots(&app, st).centers.len() + dots(&app, st).origins.len(), 0);
}
@@ -891,7 +951,7 @@ thread_local! {
fn kept(app: &VectorcraftApp) -> (Vec<Vec<vectorcraft_geom::Rect>>, usize) {
let marks = app.canvas.selection_marks.as_ref().expect("the canvas drew the selection's marks");
let boxes = marks.outlines.iter().map(|o| o.paths.iter().map(|p| p.bounding_box()).collect()).collect();
(boxes, all_selected(&marks.anchors.selection))
(boxes, all_selected(&marks.anchors.selection) + all_selected(&marks.anchors.plain_selection))
}
// Whole-app frames build the selection's marks once for each change: none in frames that change nothing, one after
@@ -914,7 +974,8 @@ fn the_canvas_builds_the_selection_marks_once_per_change() {
assert_eq!(frames(&mut app, 5), 1, "a new selection builds its marks once");
let st = app.session.active().unwrap();
let boxes = outlines(&app, st).iter().map(|o| o.paths.iter().map(|p| p.bounding_box()).collect()).collect::<Vec<_>>();
assert_eq!(kept(&app), (boxes, all_selected(&marks(&app).selection)));
let shown = marks(&app);
assert_eq!(kept(&app), (boxes, all_selected(&shown.selection) + all_selected(&shown.plain_selection)));
app.select_tool("directSelection");
assert_eq!(frames(&mut app, 5), 1, "another tool builds the marks once");
}
@@ -932,7 +993,7 @@ fn points_of(outlines: &[Outlined], anchors: &Anchors, dots: &Dots) -> Vec<Point
}
}
}
for knot in anchors.paths.iter().flat_map(|p| &p.subpaths).flat_map(|sp| &sp.knots) {
for knot in anchors.paths.iter().chain(&anchors.plain).flat_map(|p| &p.subpaths).flat_map(|sp| &sp.knots) {
out.extend([knot.anchor, knot.in_ctrl, knot.out_ctrl]);
}
out.extend(dots.origins.iter().chain(&dots.centers).chain(&dots.key_anchor).map(|(at, _)| *at));