diff --git a/crates/draw/src/ui/pen/auto.rs b/crates/draw/src/ui/pen/auto.rs index fef15b5ab5..53398cce4e 100644 --- a/crates/draw/src/ui/pen/auto.rs +++ b/crates/draw/src/ui/pen/auto.rs @@ -20,7 +20,7 @@ pub(super) enum AutoEdit { /// The draft as the one path of a hit test. fn as_path(draft: &EditSubpath) -> EditPath { - EditPath { key: 0, subpaths: vec![draft.clone()], color: None } + EditPath::new(0, vec![draft.clone()]) } impl PenTool { diff --git a/crates/draw/src/ui/pen/bezier/hit_test.rs b/crates/draw/src/ui/pen/bezier/hit_test.rs index 9f1da25f04..e2c3e3a7e0 100644 --- a/crates/draw/src/ui/pen/bezier/hit_test.rs +++ b/crates/draw/src/ui/pen/bezier/hit_test.rs @@ -52,7 +52,8 @@ impl BezierOverlay<'_> { } } -/// The handles to show, in path order, in then out, leaving out retracted ones. +/// The handles to show, in path order, in then out, leaving out retracted ones and those of subpaths the selection +/// does not show. pub(super) fn shown_handles(paths: &[EditPath], sel: &PathSelection, handles: HandlesShown, view: &CanvasView) -> Vec { let mut shown = Vec::new(); for (index, path) in paths.iter().enumerate() { @@ -60,7 +61,7 @@ pub(super) fn shown_handles(paths: &[EditPath], sel: &PathSelection, handles: Ha let (a, b) = path.subpaths.get(s.subpath)?.segment_ends(s.knot)?; Some((KnotRef::new(s.subpath, a), KnotRef::new(s.subpath, b))) }); - for knot_ref in path.knot_refs() { + for knot_ref in path.knot_refs().filter(|k| sel.shows(path.key, k.subpath)) { let Some(knot) = path.knot(knot_ref) else { continue }; for side in [HandleSide::In, HandleSide::Out] { let wanted = match handles { diff --git a/crates/draw/src/ui/pen/bezier/mod.rs b/crates/draw/src/ui/pen/bezier/mod.rs index 6bf60b784f..b818fdfb2b 100644 --- a/crates/draw/src/ui/pen/bezier/mod.rs +++ b/crates/draw/src/ui/pen/bezier/mod.rs @@ -11,11 +11,11 @@ mod style; mod tools; mod words; -use craft_behavior::geometry::{SelectionOperation, turn_to_45}; +use craft_behavior::geometry::{SelectionOperation, axis_or_diagonal}; use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2}; use craft_behavior::{ - Axis, CanvasView, EditCommand, Granularity, HitLevel, HitPriority, HitShape, Intent, MoveMods, NavMove, NavShape, PartKey, PartRef, - PointerButton, PressPhase, PressStart, Roving, Sense, + Axis, CanvasView, CursorPriority, EditCommand, Granularity, HitLevel, HitPriority, HitShape, Intent, MoveMods, NavMove, NavShape, PartKey, + PartRef, PointerButton, PressPhase, PressStart, Roving, Sense, }; use craft_keymap::{Chord, Key, Mods}; use craft_theme::Srgba; @@ -54,7 +54,9 @@ pub const MAX_ANCHORS: usize = 100_000; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum BezierTool { - /// Direct Selection: select and move anchors, turn handles, bend segments, marquee. + /// Direct Selection: select and move anchors, turn handles, bend segments, marquee. ⌥ on a segment selects its + /// subpath, and a drag moves a copy of it ([`PathEdit::DuplicateSubpath`]; the subpath itself with ⌘, Ctrl or ⇧ + /// held too, or on a path that takes no copies, [`EditPath::copies`]). #[default] Direct, /// Convert Point (also the Pen with ⌥): a click on an anchor makes a corner, a drag from it pulls out smooth @@ -65,7 +67,8 @@ pub enum BezierTool { Anchors, /// Add Anchor Point: a press on a segment inserts an anchor at the hit's `t` ([`PathEdit::InsertAnchor`]); a drag /// moves the new anchor, which is selected; ⌥ makes it Delete Anchor Point. Hover: the segment mark with the - /// anchor a click adds ([`HitMark`]). + /// anchor a click adds ([`HitMark`]) and the pen with "+"; over an anchor the arrow; the crosshair under the + /// canvas's precise cursor ([`CanvasSpace::precise`]). AddAnchor, /// Delete Anchor Point: a click on an anchor reports [`delete`](BezierOutput::delete) of that anchor; ⌥ makes it /// Add Anchor Point, except that a ⌥ click on an anchor selects its whole subpath. Hover: the anchor mark. @@ -225,6 +228,12 @@ enum Target { t: f64, from: Point, }, + /// Direct Selection's ⌥ drag on a segment: a copy of `subpath` dragged from `from`, the grabbed point. + Duplicate { + path: u64, + subpath: usize, + from: Point, + }, /// ⌥ with Delete Anchor on an anchor: a click picks the anchor's whole subpath, a drag does nothing. /// Reported on the click's release, so a drag selects nothing. Pick { @@ -284,6 +293,9 @@ struct Item { interior: bool, /// A segment with both handles retracted. straight: bool, + /// An anchor or handle the selection shows ([`PathSelection::shows`]). A hidden anchor is not drawn and not in + /// the keyboard order, and still takes a press (a Delete Anchor click on it shows its subpath). + shown: bool, /// An anchor's place among the path's anchors, from 1, and their number. ordinal: (usize, usize), } @@ -428,6 +440,7 @@ impl<'a> BezierOverlay<'a> { let anchor = view.to_screen(knot.anchor); let sub = &path.subpaths[knot_ref.subpath]; let interior = sub.closed || (knot_ref.knot != 0 && knot_ref.knot + 1 != sub.knots.len()); + let anchor_shown = self.selection.shows(path.key, knot_ref.subpath); if near.contains(anchor) { items.push(Item { kind: Kind::Anchor, @@ -438,6 +451,7 @@ impl<'a> BezierOverlay<'a> { shape: HitShape::circle(anchor, reach), interior, straight: false, + shown: anchor_shown, ordinal: (n + 1, count), }); } @@ -452,6 +466,7 @@ impl<'a> BezierOverlay<'a> { shape: HitShape::circle(end, reach), interior, straight: false, + shown: anchor_shown, ordinal: (n + 1, count), }); } @@ -482,6 +497,7 @@ impl<'a> BezierOverlay<'a> { shape: HitShape::polyline(points, false), interior: true, straight: sub.is_straight(i), + shown: true, ordinal: (0, 0), }); } @@ -512,8 +528,31 @@ impl<'a> BezierOverlay<'a> { } /// The selection that has only the anchor a segment insert adds. - fn inserted(path: u64, segment: KnotRef) -> PathSelection { - PathSelection { path: Some(path), anchors: vec![KnotRef::new(segment.subpath, segment.knot + 1)], segment: None } + fn inserted(&self, path: u64, segment: KnotRef) -> PathSelection { + self.selecting(path, vec![KnotRef::new(segment.subpath, segment.knot + 1)], None) + } + + /// The selection of every anchor of the copy a [`PathEdit::DuplicateSubpath`] of `subpath` adds to `path` of + /// `start`, the paths before the copy. + fn copied(&self, start: &[EditPath], path: u64, subpath: usize) -> PathSelection { + let copy = Self::path(start, path).map_or(0, |p| p.subpaths.len()); + let count = Self::path(start, path).and_then(|p| p.subpaths.get(subpath)).map_or(0, |s| s.knots.len()); + self.selecting(path, (0..count).map(|knot| KnotRef::new(copy, knot)).collect(), None) + } + + /// The selection of `anchors` and `segment` of `path`. On the selection's path it keeps the subpaths shown and + /// adds those of the selected anchors and segment, so a selected anchor always shows. + fn selecting(&self, path: u64, anchors: Vec, segment: Option) -> PathSelection { + let shown = self.selection.shown.as_ref().filter(|_| self.selection.path == Some(path)).map(|shown| { + let mut shown = shown.clone(); + for subpath in anchors.iter().chain(&segment).map(|k| k.subpath) { + if !shown.contains(&subpath) { + shown.push(subpath); + } + } + shown + }); + PathSelection::new(Some(path), anchors, segment).shown(shown) } /// The target a press drags: for the tools that act on the press ([`BezierTool::clicks`]) an insert into a @@ -541,7 +580,7 @@ impl<'a> BezierOverlay<'a> { Target::Handle { path, knot, side, from: knot_at(path, knot).map_or(Point::ZERO, |k| k.handle(side)) } } (BezierTool::Curvature, BezierHit::Anchor { path, knot }) => { - self.report.select = Some(PathSelection { path: Some(path), anchors: vec![knot], segment: None }); + self.report.select = Some(self.selecting(path, vec![knot], None)); if start.clicks == 2 { Target::Toggle { path, knot } } else { @@ -563,7 +602,7 @@ impl<'a> BezierOverlay<'a> { let chosen: &[KnotRef] = if self.selection.path == Some(path) { &self.selection.anchors } else { &[] }; let all: Vec = shown.knot_refs().collect(); if chosen.is_empty() { - self.report.select = Some(PathSelection { path: Some(path), anchors: Vec::new(), segment: Some(segment) }); + self.report.select = Some(self.selecting(path, Vec::new(), Some(segment))); Target::Bend { path, segment, t, from } } else if all.iter().all(|k| chosen.contains(k)) { Target::Anchors { path, anchors: all, from } @@ -572,7 +611,7 @@ impl<'a> BezierOverlay<'a> { } } (BezierTool::Convert, BezierHit::Anchor { path, knot }) => { - self.report.select = Some(PathSelection { path: Some(path), anchors: vec![knot], segment: None }); + self.report.select = Some(self.selecting(path, vec![knot], None)); Target::Convert { path, knot } } (_, BezierHit::Anchor { path, knot }) => { @@ -582,7 +621,7 @@ impl<'a> BezierOverlay<'a> { Self::subpath_anchors(paths, path, knot.subpath) } else if mods.shift && selected { let rest: Vec = self.selection.anchors.iter().copied().filter(|&k| k != knot).collect(); - self.report.select = Some(PathSelection { path: Some(path), anchors: rest, segment: None }); + self.report.select = Some(self.selecting(path, rest, None)); return Target::Nothing; } else if mods.shift && self.selection.path == Some(path) { self.selection.anchors.iter().copied().chain([knot]).collect() @@ -592,7 +631,7 @@ impl<'a> BezierOverlay<'a> { vec![knot] }; if !selected || mods.alt || mods.shift { - self.report.select = Some(PathSelection { path: Some(path), anchors: anchors.clone(), segment: None }); + self.report.select = Some(self.selecting(path, anchors.clone(), None)); } Target::Anchors { path, anchors, from } } @@ -600,10 +639,17 @@ impl<'a> BezierOverlay<'a> { let from = Self::segment_point(paths, path, segment, t); if mods.alt { let anchors = Self::subpath_anchors(paths, path, segment.subpath); - self.report.select = Some(PathSelection { path: Some(path), anchors: anchors.clone(), segment: None }); - Target::Anchors { path, anchors, from } + self.report.select = Some(self.selecting(path, anchors.clone(), None)); + // pc: crates/ui-egui/src/tools/direct_select.rs `alt_duplicates` (#13211): Photoshop was not + // measured with ⌘, Ctrl or ⇧ held too; those move the subpath. + let copies = self.tool == BezierTool::Direct + && !mods.cmd + && !mods.ctrl + && !mods.shift + && Self::path(paths, path).is_some_and(|p| p.copies); + if copies { Target::Duplicate { path, subpath: segment.subpath, from } } else { Target::Anchors { path, anchors, from } } } else { - self.report.select = Some(PathSelection { path: Some(path), anchors: Vec::new(), segment: Some(segment) }); + self.report.select = Some(self.selecting(path, Vec::new(), Some(segment))); Target::Bend { path, segment, t, from } } } @@ -614,7 +660,8 @@ impl<'a> BezierOverlay<'a> { /// The edit of a press at `origin` dragged to `pointer`: the whole move from the press. fn edit(view: &CanvasView, snap: Option<&Snap>, tool: BezierTool, target: &Target, origin: Pos2, pointer: Pos2, mods: Mods) -> Option { let delta = view.to_doc(pointer) - view.to_doc(origin); - let locked = |delta: Vec2| if mods.shift { turn_to_45(Point::ZERO, Point::ZERO + delta).to_vec2() } else { delta }; + // ⇧ keeps an anchor move on the axis within the axis band and on the diagonal past it, as the Line tool does. + let locked = |delta: Vec2| if mods.shift { axis_or_diagonal(Point::ZERO, Point::ZERO + delta).to_vec2() } else { delta }; let snapped = |p: Point| match snap { Some(snap) => Point::new(snap.apply(Axis::Y, p.x, mods), snap.apply(Axis::X, p.y, mods)), None => p, @@ -635,6 +682,7 @@ impl<'a> BezierOverlay<'a> { PathEdit::InsertCurvature { path, segment, t, by: snapped(from + locked(delta)) - from } } &Target::Insert { path, segment, t, from } => PathEdit::InsertAnchor { path, segment, t, by: snapped(from + locked(delta)) - from }, + &Target::Duplicate { path, subpath, from } => PathEdit::DuplicateSubpath { path, subpath, by: snapped(from + locked(delta)) - from }, Target::Nothing | Target::Toggle { .. } | Target::Pick { .. } | Target::Marquee => return None, }) } @@ -674,7 +722,7 @@ impl<'a> BezierOverlay<'a> { SelectionOperation::Intersect => current.into_iter().filter(|k| knots.contains(k)).collect(), _ => knots, }; - Some(PathSelection { path: Some(path), anchors, segment: None }) + Some(self.selecting(path, anchors, None)) } /// The pointer on the parts: selection, drags, clicks, the hover mark and the context request. @@ -707,7 +755,7 @@ impl<'a> BezierOverlay<'a> { let target = self.press_target(hit, start, press.mods, paths); if target == Target::Nothing && self.tool.clicks() { // Delete Anchor and Auto Add/Delete act on an anchor at the press. - tools::anchor_click(self.tool, hit, press.mods, &mut self.report); + tools::anchor_click(self.tool, hit, press.mods, self.selection, &mut self.report); *cx.press_slot::() = None; } else { *cx.press_slot::() = Some(Held { origin: start.origin, target, start: paths.to_vec(), began: false }); @@ -744,7 +792,10 @@ impl<'a> BezierOverlay<'a> { } if let Some((_, hit, mods)) = hovered { self.report.hover = Some(hit); - self.hover_mark = tools::hover_mark(self.tool, hit, mods); + self.hover_mark = tools::hover_mark(self.tool, hit, mods, self.selection); + if let Some(cursor) = tools::hover_cursor(self.tool, hit, mods, space.precise()) { + cx.cursor(cursor, CursorPriority::Part); + } } } @@ -764,7 +815,7 @@ impl<'a> BezierOverlay<'a> { if first && moves { held.began = true; if let Target::CurvatureInsert { path, segment, .. } | Target::Insert { path, segment, .. } = held.target { - self.report.select = Some(Self::inserted(path, segment)); + self.report.select = Some(self.inserted(path, segment)); } if held.target != Target::Marquee { cx.begin(); @@ -792,8 +843,10 @@ impl<'a> BezierOverlay<'a> { let target = self.press_target(item.hit(paths, view, start.origin), start, mods, paths); if let Some(edit) = Self::edit(view, snap, self.tool, &target, start.origin, pos, mods) { self.set(cx, paths, edit); - if let Target::Insert { path, segment, .. } = target { - self.report.select = Some(Self::inserted(path, segment)); + match target { + Target::Insert { path, segment, .. } => self.report.select = Some(self.inserted(path, segment)), + Target::Duplicate { path, subpath, .. } => self.report.select = Some(self.copied(paths, path, subpath)), + _ => {} } } } @@ -812,6 +865,10 @@ impl<'a> BezierOverlay<'a> { if let Some(edit) = Self::edit(view, snap, self.tool, &held.target, start.origin, pos, mods) { self.propose(cx, &held.start, edit); } + // The copy is what was dragged: it ends up selected, not the source. + if let Target::Duplicate { path, subpath, .. } = held.target { + self.report.select = Some(self.copied(&held.start, path, subpath)); + } cx.commit(); return; } @@ -820,16 +877,16 @@ impl<'a> BezierOverlay<'a> { Target::Toggle { path, knot } => self.set(cx, &held.start, PathEdit::ToggleCurvature { path, knot }), Target::CurvatureInsert { path, segment, t, .. } => { self.set(cx, &held.start, PathEdit::InsertCurvature { path, segment, t, by: Vec2::ZERO }); - self.report.select = Some(Self::inserted(path, segment)); + self.report.select = Some(self.inserted(path, segment)); } Target::Insert { path, segment, t, .. } => { self.set(cx, &held.start, PathEdit::InsertAnchor { path, segment, t, by: Vec2::ZERO }); - self.report.select = Some(Self::inserted(path, segment)); + self.report.select = Some(self.inserted(path, segment)); } // A click without a drag (a press past the slop is no click) picks the anchor's whole subpath. Target::Pick { path, knot } if click => { let anchors = Self::subpath_anchors(paths, path, knot.subpath); - self.report.select = Some(PathSelection { path: Some(path), anchors, segment: None }); + self.report.select = Some(self.selecting(path, anchors, None)); } _ => {} } @@ -837,7 +894,7 @@ impl<'a> BezierOverlay<'a> { /// The keys of the focused overlay: picking parts, select all, nudges, Insert, Delete, Enter and Escape. fn run_keys(&mut self, cx: &mut Cx<'_, Self>, space: &CanvasSpace, scene: &Scene, paths: &[EditPath]) { - let keyboard: Vec<&Item> = scene.items.iter().filter(|item| item.kind != Kind::Segment).collect(); + let keyboard: Vec<&Item> = scene.items.iter().filter(|item| item.kind != Kind::Segment && item.shown).collect(); let active = cx.active_part(); let current: Option<&Item> = active.and_then(|part| keyboard.iter().copied().find(|item| item.part() == part)); let keyed = self.tool.keyed(); @@ -851,7 +908,7 @@ impl<'a> BezierOverlay<'a> { && item.kind == Kind::Anchor && !self.selection.has_anchor(item.key, item.knot) { - self.report.select = Some(PathSelection { path: Some(item.key), anchors: vec![item.knot], segment: None }); + self.report.select = Some(self.selecting(item.key, vec![item.knot], None)); } // With Cmd (Ctrl) a page key is the canvas's: it scrolls sideways and picks no part. let steps = |mv: NavMove, mods: MoveMods| !(matches!(mv, NavMove::PageDown | NavMove::PageUp) && mods.lead); @@ -875,7 +932,7 @@ impl<'a> BezierOverlay<'a> { if !anchors.contains(&item.knot) { anchors.push(item.knot); } - self.report.select = Some(PathSelection { path: Some(item.key), anchors, segment: None }); + self.report.select = Some(self.selecting(item.key, anchors, None)); } } while let Some(command) = cx.intents().take_edit() { @@ -883,7 +940,7 @@ impl<'a> BezierOverlay<'a> { && keyed && let Some(path) = self.focused_path(paths, current) { - self.report.select = Some(PathSelection { path: Some(path.key), anchors: path.knot_refs().collect(), segment: None }); + self.report.select = Some(self.selecting(path.key, path.knot_refs().collect(), None)); } } if keyed && let Some((direction, mods)) = cx.intents().take_nudge() { @@ -1082,7 +1139,7 @@ impl Component for BezierOverlay<'_> { let arrival = self.selection.path.zip(self.selection.anchors.first().copied()); for item in &scene.items { let press = self.takes(item); - let keyboard = item.kind != Kind::Segment && focusable; + let keyboard = item.kind != Kind::Segment && item.shown && focusable; let selected = item.kind == Kind::Anchor && self.selection.has_anchor(item.key, item.knot); let part = cx.part(item.part(), item.shape.clone()).priority(item.priority()); let part = if press { part.sense(PRESS) } else { part }; @@ -1098,7 +1155,7 @@ impl Component for BezierOverlay<'_> { part.selected(); } } - if focusable && scene.items.iter().any(|item| item.kind != Kind::Segment) { + if focusable && scene.items.iter().any(|item| item.kind != Kind::Segment && item.shown) { cx.roving(Roving::new(NavShape::Flat).home_end(false)); } } @@ -1172,7 +1229,7 @@ impl Component for BezierOverlay<'_> { }); paint_handle(&draw, &style.control, true, item.at, fill, path.color); } - Kind::Anchor => { + Kind::Anchor if item.shown => { let fill = if self.selection.has_anchor(path.key, item.knot) { Fill::Path(style.line) } else { @@ -1184,7 +1241,7 @@ impl Component for BezierOverlay<'_> { }; paint_handle(&draw, &style.anchor, false, item.at, fill, path.color); } - Kind::Segment => {} + Kind::Anchor | Kind::Segment => {} } } if let Some(hit) = self.hover_mark @@ -1212,7 +1269,7 @@ impl Component for BezierOverlay<'_> { let (Some(space), Some(paths)) = (canvas_space(&cx.env), cx.value()) else { return }; let scene = self.scene(paths, &space.view, cx.rect, cx.style.reach); let (words, units, rulers) = (cx.words::(), cx.env.units(), space.rulers()); - for item in scene.items.iter().filter(|item| item.kind != Kind::Segment) { + for item in scene.items.iter().filter(|item| item.kind != Kind::Segment && item.shown) { let Some(knot) = paths[item.path].knot(item.knot) else { continue }; let (point, name) = match item.kind { Kind::Handle(HandleSide::In) => (knot.in_ctrl, words.in_handle.as_str().to_owned()), diff --git a/crates/draw/src/ui/pen/bezier/model.rs b/crates/draw/src/ui/pen/bezier/model.rs index 0c208eddd0..aeff532df0 100644 --- a/crates/draw/src/ui/pen/bezier/model.rs +++ b/crates/draw/src/ui/pen/bezier/model.rs @@ -114,8 +114,8 @@ impl EditSubpath { } } -/// A path the overlays show: the host's key for it, its subpaths and its colour. -#[derive(Clone, Debug, Default, PartialEq)] +/// A path the overlays show: the host's key for it, its subpaths, its colour and what it allows. +#[derive(Clone, Debug, PartialEq)] #[non_exhaustive] pub struct EditPath { /// The host's key, stable across frames. @@ -125,12 +125,30 @@ pub struct EditPath { /// The layer colour its outline, anchors and handles are drawn in (DesignCraft, VectorCraft); /// the overlay's line colour without one. pub color: Option, + /// Whether the path takes copies of its subpaths: Direct Selection's ⌥ drag on a segment drags a copy of the + /// subpath ([`PathEdit::DuplicateSubpath`]) instead of the subpath itself. The host turns it off for a path + /// whose subpaths are not copied that way (PhotoCraft: the pen draft and Type Paths, where the drag moves the + /// subpath). + pub copies: bool, +} + +impl Default for EditPath { + fn default() -> Self { + Self::new(0, Vec::new()) + } } impl EditPath { - /// The path the host keys `key`, made of `subpaths`, in the overlay's colour. + /// The path the host keys `key`, made of `subpaths`, in the overlay's colour, taking copies of its subpaths. pub fn new(key: u64, subpaths: Vec) -> Self { - Self { key, subpaths, color: None } + Self { key, subpaths, color: None, copies: true } + } + + /// The path taking copies of its subpaths ([`copies`](Self::copies)) or not. + #[must_use] + pub fn copies(mut self, copies: bool) -> Self { + self.copies = copies; + self } /// The path in its own colour (a layer's), else the overlay's. @@ -173,8 +191,8 @@ impl KnotRef { } } -/// The host's path selection: the path the overlay edits, its selected anchors and a selected -/// segment (whose two handles show). +/// The host's path selection: the path the overlay edits, its selected anchors, a selected +/// segment (whose two handles show) and the subpaths whose anchors show. #[derive(Clone, Debug, Default, PartialEq)] #[non_exhaustive] pub struct PathSelection { @@ -184,18 +202,47 @@ pub struct PathSelection { pub anchors: Vec, /// The segment leaving this knot. pub segment: Option, + /// The subpaths of that path whose anchors and handles show; `None` shows every subpath's. A Delete Anchor + /// click narrows it to the subpath it acts on (Photoshop's Delete Anchor Point, #12769); the outlines always show. + pub shown: Option>, } impl PathSelection { - /// The selection of `anchors` of the path keyed `path`, and of a segment. + /// The selection of `anchors` of the path keyed `path`, and of a segment, with every subpath's anchors shown. pub fn new(path: Option, anchors: Vec, segment: Option) -> Self { - Self { path, anchors, segment } + Self { path, anchors, segment, shown: None } + } + + /// The selection with the anchors of only the `shown` subpaths drawn (`None`: every subpath's). + #[must_use] + pub fn shown(mut self, shown: Option>) -> Self { + self.shown = shown; + self } /// Whether `knot` of `path` is a selected anchor. pub fn has_anchor(&self, path: u64, knot: KnotRef) -> bool { self.path == Some(path) && self.anchors.contains(&knot) } + + /// Whether the anchors of `subpath` of `path` show: always for a path other than the selection's. + pub fn shows(&self, path: u64, subpath: usize) -> bool { + self.path != Some(path) || self.shown.as_ref().is_none_or(|shown| shown.contains(&subpath)) + } + + /// The selection after the host deleted the anchor `deleted` of a Delete Anchor click, from `path` as the delete + /// left it, which had `subpaths_before` subpaths: the anchor that followed the deleted one (the first of a + /// closed subpath after its last), with only that subpath's anchors shown. When the delete removed a subpath, + /// the indices moved: nothing is selected and every subpath shows. + pub fn after_delete(path: &EditPath, deleted: KnotRef, subpaths_before: usize) -> PathSelection { + // pc: crates/ui-egui/src/tools/anchor_tools.rs `select_follower` (#12769). + let Some(sub) = path.subpaths.get(deleted.subpath).filter(|_| path.subpaths.len() == subpaths_before) else { + return PathSelection::new(Some(path.key), Vec::new(), None); + }; + let next = if deleted.knot < sub.knots.len() { Some(deleted.knot) } else { (sub.closed && !sub.knots.is_empty()).then_some(0) }; + let anchors = next.map(|knot| KnotRef::new(deleted.subpath, knot)).into_iter().collect(); + PathSelection::new(Some(path.key), anchors, None).shown(Some(vec![deleted.subpath])) + } } /// Which of a knot's two direction handles. @@ -310,6 +357,17 @@ pub enum PathEdit { /// The toggled anchor. knot: KnotRef, }, + /// Direct Selection's ⌥ drag on a segment: a copy of subpath `subpath` goes last in the path and moves, with + /// its handles, by `by`; the source stays. Photoshop names the step "Drag Path". + #[non_exhaustive] + DuplicateSubpath { + /// The path's host key. + path: u64, + /// The copied subpath. + subpath: usize, + /// The copy's move, in document units. + by: Vec2, + }, } /// A request to delete anchors of a path. @@ -372,6 +430,11 @@ impl PathEdit { PathEdit::ToggleCurvature { path, knot } } + /// A copy of `subpath` of the path keyed `path`, appended and moved by `by`. + pub fn duplicate_subpath(path: u64, subpath: usize, by: Vec2) -> Self { + PathEdit::DuplicateSubpath { path, subpath, by } + } + /// The edit's path. pub fn path(&self) -> u64 { match *self { @@ -382,7 +445,8 @@ impl PathEdit { | PathEdit::MoveCurvature { path, .. } | PathEdit::InsertCurvature { path, .. } | PathEdit::InsertAnchor { path, .. } - | PathEdit::ToggleCurvature { path, .. } => path, + | PathEdit::ToggleCurvature { path, .. } + | PathEdit::DuplicateSubpath { path, .. } => path, } } } diff --git a/crates/draw/src/ui/pen/bezier/ops.rs b/crates/draw/src/ui/pen/bezier/ops.rs index 6a0e4d4dc4..4bf2b4a7a2 100644 --- a/crates/draw/src/ui/pen/bezier/ops.rs +++ b/crates/draw/src/ui/pen/bezier/ops.rs @@ -139,6 +139,8 @@ impl PathEdit { /// reflow and no smoothing, so a straight segment gets a corner knot. /// - `ToggleCurvature`: a smooth knot becomes a corner with retracted handles, a corner /// becomes smooth, then the subpath reflows. + /// - `DuplicateSubpath`: a copy of the subpath goes last and moves, with its handles, by `by`; + /// the source stays. pub fn apply(&self, path: &mut EditPath) -> bool { if path.key != self.path() { return false; @@ -223,6 +225,13 @@ impl PathEdit { k.out_ctrl += *by; sp.reflow(); } + PathEdit::DuplicateSubpath { subpath, by, .. } => { + let Some(copy) = path.subpaths.get(*subpath).cloned() else { return false }; + let last = path.subpaths.len(); + let anchors: Vec = (0..copy.knots.len()).map(|knot| KnotRef::new(last, knot)).collect(); + path.subpaths.push(copy); + move_knots(path, &anchors, *by); + } PathEdit::ToggleCurvature { knot, .. } => { if !knot_ok(path, *knot) { return false; diff --git a/crates/draw/src/ui/pen/bezier/stories.rs b/crates/draw/src/ui/pen/bezier/stories.rs index 31cfe4275a..56cecf02d6 100644 --- a/crates/draw/src/ui/pen/bezier/stories.rs +++ b/crates/draw/src/ui/pen/bezier/stories.rs @@ -15,7 +15,7 @@ fn paths() -> Vec { let open = vec![Knot::corner(Point::new(200.0, 130.0)), Knot::corner(Point::new(300.0, 130.0)), curve]; let triangle = [(200.0, 230.0), (300.0, 230.0), (250.0, 310.0)].map(|(x, y)| Knot::corner(Point::new(x, y))).to_vec(); let subpaths = vec![EditSubpath { knots: triangle, closed: true }]; - vec![EditPath { key: 1, subpaths: vec![EditSubpath { knots: open, closed: false }], color: None }, EditPath { key: 2, subpaths, color: None }] + vec![EditPath::new(1, vec![EditSubpath { knots: open, closed: false }]), EditPath::new(2, subpaths)] } /// Shows the overlay `overlay` builds and shows as the only canvas control of a canvas whose view has the document @@ -25,7 +25,7 @@ fn on_canvas(ui: &mut Ui, mut overlay: impl FnMut(&mut Ui, egui::Rect) -> Bezier } fn selected() -> PathSelection { - PathSelection { path: Some(1), anchors: vec![KnotRef::new(0, 2)], segment: None } + PathSelection::new(Some(1), vec![KnotRef::new(0, 2)], None) } #[story(BezierOverlay<'static>, covers = [Default], drive = Drive::Drag(Grip::part(ANCHOR.at(index(0, KnotRef { subpath: 0, knot: 0 }))).toward(Vec2::X)))] diff --git a/crates/draw/src/ui/pen/bezier/tools.rs b/crates/draw/src/ui/pen/bezier/tools.rs index ce40e11fd1..6324dac7b7 100644 --- a/crates/draw/src/ui/pen/bezier/tools.rs +++ b/crates/draw/src/ui/pen/bezier/tools.rs @@ -1,9 +1,13 @@ //! The Add Anchor Point, Delete Anchor Point, Auto Add/Delete and Curvature tools of [`BezierOverlay`](super::BezierOverlay): //! which parts each tool takes a press on, what a click reports and what the hover mark shows. +use craft_behavior::{Cursor, CursorBadge, PaintedCursor}; use craft_keymap::Mods; +use craft_ui::primitives::ToolIcon; +use egui::Vec2; -use super::{AnchorDelete, BezierHit, BezierTool, Report}; +use super::{AnchorDelete, BezierHit, BezierTool, PathSelection, Report}; +use crate::ui::pen::tool::ICON_BOX; impl BezierTool { /// The tool a press acts as: [`BezierTool::AddAnchor`] and [`BezierTool::DeleteAnchor`] swap while ⌥ is held; @@ -86,16 +90,26 @@ impl BezierTool { /// place). The pen draft, when the host passes it as one more path /// ([`PathMode::resolve`](crate::ui::pen::PathMode::resolve)), is hit and reported like any path, under the key the /// host gave it. -pub(super) fn anchor_click(tool: BezierTool, hit: BezierHit, mods: Mods, report: &mut Report) { - // pc: crates/ui-egui/src/tools/anchor_tools.rs:88-114 `pointer`, `click`; :130-150 `apply`. - if let (BezierHit::Anchor { path, knot }, BezierTool::DeleteAnchor | BezierTool::AutoAddDelete) = (hit, tool.acting(mods)) { - report.delete = Some(AnchorDelete::new(path, vec![knot], false)); +/// +/// Delete Anchor on an anchor whose subpath `selection` does not show deletes nothing: it selects the anchor and +/// shows only its subpath (Photoshop, #12769). The host selects the anchor after a delete with +/// [`PathSelection::after_delete`]. +pub(super) fn anchor_click(tool: BezierTool, hit: BezierHit, mods: Mods, selection: &PathSelection, report: &mut Report) { + // pc: crates/ui-egui/src/tools/anchor_tools.rs:88-114 `pointer`, `click`; :130-150 `apply`; `click` (#12769). + let BezierHit::Anchor { path, knot } = hit else { return }; + match tool.acting(mods) { + BezierTool::DeleteAnchor if !selection.shows(path, knot.subpath) => { + report.select = Some(PathSelection::new(Some(path), vec![knot], None).shown(Some(vec![knot.subpath]))); + } + BezierTool::DeleteAnchor | BezierTool::AutoAddDelete => report.delete = Some(AnchorDelete::new(path, vec![knot], false)), + _ => {} } } /// The part the hover mark shows for `hit` with `mods` held: what a press would edit. Under Add an anchor and under -/// Delete a segment are taken and do nothing (PhotoCraft draws no mark there). Curvature marks every part it takes. -pub(super) fn hover_mark(tool: BezierTool, hit: BezierHit, mods: Mods) -> Option { +/// Delete a segment are taken and do nothing (PhotoCraft draws no mark there), nor is a hidden anchor under Delete, +/// which a click only shows. Curvature marks every part it takes. +pub(super) fn hover_mark(tool: BezierTool, hit: BezierHit, mods: Mods, selection: &PathSelection) -> Option { // pc: crates/ui-egui/src/tools/anchor_tools.rs:152-169 `draw_hover`, `draw_pen_hover`; // crates/ui-egui/src/tools/direct_select.rs:378-389 `curvature_hover`. if !tool.marks() || mods.cmd { @@ -111,13 +125,31 @@ pub(super) fn hover_mark(tool: BezierTool, hit: BezierHit, mods: Mods) -> Option BezierHit::Segment { .. } => tool.adds(mods), _ => { let mut would = Report::default(); - anchor_click(tool, hit, mods, &mut would); + anchor_click(tool, hit, mods, selection, &mut would); would.delete.is_some() } }; marked.then_some(hit) } +/// The cursor over `hit`, a part the tool takes, with `mods` held; `precise` is the canvas's precise cursor +/// ([`CanvasSpace::precise`](craft_ui::canvas::CanvasSpace::precise)). Add Anchor shows the pen with "+" over a +/// segment a click splits and the arrow over an anchor, the crosshair when precise. Under ⌥ (Delete Anchor) and ⌘ the +/// cursor stays the host tool's, as it does off the parts. +pub(super) fn hover_cursor(tool: BezierTool, hit: BezierHit, mods: Mods, precise: bool) -> Option { + // pc: crates/ui-egui/src/canvas/tool_cursor.rs, crates/ui-egui/src/tools/anchor_tools.rs (#12729). + if tool.acting(mods) != BezierTool::AddAnchor || mods.cmd { + return None; + } + let cursor = match hit { + _ if precise => PaintedCursor::Crosshair, + // The pen's tip is the bottom left corner of the icon's box. + BezierHit::Segment { .. } => PaintedCursor::tool(ToolIcon::Pen.icon(), Vec2::Y * ICON_BOX, Some(CursorBadge::Add)), + BezierHit::Anchor { .. } | BezierHit::Handle { .. } => PaintedCursor::arrow(false), + }; + Some(Cursor::Painted(cursor)) +} + /// `t` clamped to the range the Curvature tool inserts at. pub(super) fn curvature_t(t: f64) -> f64 { use crate::geometry::curvature::CURVATURE_T_RANGE; diff --git a/crates/draw/src/ui/pen/mode.rs b/crates/draw/src/ui/pen/mode.rs index 9a1223e7e0..436ae5dac9 100644 --- a/crates/draw/src/ui/pen/mode.rs +++ b/crates/draw/src/ui/pen/mode.rs @@ -72,7 +72,7 @@ impl PathMode { /// Where the draft shows without sensing (⌘, Add Anchor, Delete Anchor, Convert Point, Direct Selection), the /// anchor tools edit it as they edit a path: the host passes the open draft to the /// [`BezierOverlay`](super::BezierOverlay) as one more [`EditPath`](super::EditPath), last in the slice under a - /// key of its own, and applies the overlay's `insert`, `delete` and `edit` for that key to its draft + /// key of its own and taking no copies ([`EditPath::copies`](super::EditPath::copies)), and applies the overlay's `insert`, `delete` and `edit` for that key to its draft /// ([`EditSubpath::insert_at`](super::EditSubpath::insert_at), /// [`remove_knot`](super::EditSubpath::remove_knot), [`PathEdit::apply`](super::PathEdit::apply)). pub fn resolve(tool: PathTool, mods: Mods, drawing: bool, auto_add_delete: bool) -> PathMode { diff --git a/crates/draw/src/ui/pen/tool.rs b/crates/draw/src/ui/pen/tool.rs index 906829dfa4..c8788e3e87 100644 --- a/crates/draw/src/ui/pen/tool.rs +++ b/crates/draw/src/ui/pen/tool.rs @@ -18,7 +18,7 @@ use craft_ui::style::{HandleLook, StyleColor, StyleCx}; use egui::{CornerRadius, Pos2, Rect, Vec2}; /// The side of the box a tool icon is drawn on; an icon's hotspot is in these units. -const ICON_BOX: f32 = 24.0; +pub(super) const ICON_BOX: f32 = 24.0; /// Why a draft ended. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/draw/tests/it/bezier.rs b/crates/draw/tests/it/bezier.rs index 7212a245d3..97dad1893b 100644 --- a/crates/draw/tests/it/bezier.rs +++ b/crates/draw/tests/it/bezier.rs @@ -159,6 +159,8 @@ struct State { flip: bool, /// The canvas shows a document and keeps the view it reports, so page keys move it. pans: bool, + /// The canvas's precise cursors (Preferences > Cursors > Other Cursors). + precise: bool, view: Option, id: Id, seen: Vec, @@ -222,6 +224,7 @@ fn harness_with(paths: Vec, pans: bool) -> Harness { rotation: 0.0, flip: false, pans, + precise: false, view: None, id: Id::NULL, seen: Vec::new(), @@ -249,7 +252,7 @@ fn harness_with(paths: Vec, pans: bool) -> Harness { st.view = Some(view); let selection = st.selection.clone(); let canvas_id = ui.scope_id().with("document canvas"); - let canvas = Canvas::new("Document").id(canvas_id).view(view); + let canvas = Canvas::new("Document").id(canvas_id).view(view).precise_cursors(st.precise); let canvas = if st.pans { canvas.document(Size::new(4000.0, 4000.0)) } else { canvas }; let canvas = canvas.show(ui, |ui, body| { // The guides lie under the paths: a stack position hides the levels of the ones below it. @@ -551,3 +554,177 @@ fn insert_on_a_picked_anchor_adds_one_halfway_along_the_segment_leaving_it() { h.fixture.key(egui::Key::End, egui::Modifiers::NONE).key(egui::Key::Insert, egui::Modifiers::NONE).settle(); assert_eq!(h.state.borrow().inserts.len(), 1, "nothing to insert after the last anchor of an open subpath"); } + +// ⇧ during an anchor drag: a 5:12 move stays on the horizontal, also with the f32 noise of the screen mapping, a 12:5 +// one on the vertical, a 45° move goes on the diagonal; the drag proposes what it commits (#13684). +#[trial] +fn shift_keeps_an_anchor_drag_on_the_axis_or_the_diagonal() { + let cases = + [((60.0, -25.0), p(220.0, 60.0)), ((60.0, -25.00005), p(220.0, 60.0)), ((25.0, 60.0), p(160.0, 120.0)), ((50.0, -50.0), p(210.0, 10.0))]; + for ((dx, dy), want) in cases { + let mut h = two_paths(); + let anchor = h.screen(p(160.0, 60.0)); + let to = anchor + vec2(dx, dy); + h.press_at(anchor); + // ⇧ at the press would add the anchor to the selection: it comes once the drag is under way. + h.move_to(anchor + vec2(dx, dy) * 0.5); + h.fixture.hold(egui::Modifiers::SHIFT); + h.move_to(to); + let Some(PathEdit::MoveAnchors { by, .. }) = h.last_change() else { panic!("an anchor move: {:?}", h.last_change()) }; + let proposed = p(160.0, 60.0) + by; + h.release_at(to); + h.fixture.hold(egui::Modifiers::NONE); + let st = h.state.borrow(); + let committed = st.paths[0].subpaths[0].knots[1].anchor; + assert!(proposed.distance(want) < 1e-3, "({dx}, {dy}) proposes {proposed:?}, wants {want:?}"); + assert!(committed.distance(proposed) < 1e-9, "({dx}, {dy}) commits {committed:?}, proposed {proposed:?}"); + assert_eq!(st.seen.last(), Some(&Seen::Commit)); + } +} + +// Delete Anchor with only some subpaths' anchors shown (#12769): a click on a hidden anchor deletes nothing and +// edits nothing, selects that anchor and shows only its subpath; the next click on it deletes it. +#[trial] +fn a_delete_anchor_click_on_a_hidden_anchor_shows_its_subpath_and_deletes_nothing() { + let mut h = harness(vec![path_b()]); + h.update(|st| (st.tool, st.selection) = (BezierTool::DeleteAnchor, PathSelection::new(Some(2), Vec::new(), None).shown(Some(vec![0])))); + let hidden = h.screen(p(200.0, 220.0)); + h.press_at(hidden); + h.release_at(hidden); + { + let st = h.state.borrow(); + assert!(st.deletes.is_empty() && st.changes.is_empty() && st.seen.is_empty(), "{:?} {:?} {:?}", st.deletes, st.changes, st.seen); + assert_eq!(st.selection, sel(2, &[(1, 0)]).shown(Some(vec![1]))); + } + h.press_at(hidden); + h.release_at(hidden); + assert_eq!(h.state.borrow().deletes, [AnchorDelete::new(2, vec![KnotRef::new(1, 0)], false)]); +} + +// The keyboard reaches only the anchors shown: with the square hidden, Home picks the line's first anchor, and the +// selection keeps showing only the line. +#[trial] +fn hidden_anchors_take_no_keyboard_stop() { + let mut h = harness(vec![path_b()]); + h.update(|st| st.selection = PathSelection::new(Some(2), Vec::new(), None).shown(Some(vec![1]))); + h.fixture + .key(egui::Key::Tab, egui::Modifiers::NONE) + .key(egui::Key::Tab, egui::Modifiers::NONE) + .key(egui::Key::Home, egui::Modifiers::NONE) + .settle(); + assert_eq!(h.state.borrow().selection, sel(2, &[(1, 0)]).shown(Some(vec![1]))); +} + +// After the host deleted an anchor of a Delete Anchor click: the anchor that followed is selected with only its +// subpath shown, the first of a closed subpath after its last, none after an open subpath's last; when the delete +// removed a subpath nothing is selected and every subpath shows. +#[trial] +fn after_a_delete_the_following_anchor_is_selected_and_its_subpath_shown() { + let deleted = |subpath: usize, knot: usize| { + let mut path = path_b(); + assert!(path.subpaths[subpath].remove_knot(knot)); + PathSelection::after_delete(&path, KnotRef::new(subpath, knot), 2) + }; + assert_eq!(deleted(0, 1), sel(2, &[(0, 1)]).shown(Some(vec![0]))); + assert_eq!(deleted(0, 3), sel(2, &[(0, 0)]).shown(Some(vec![0])), "a closed subpath wraps to its first anchor"); + assert_eq!(deleted(1, 1), sel(2, &[]).shown(Some(vec![1])), "nothing follows an open subpath's last anchor"); + let mut path = path_b(); + path.subpaths.remove(1); + assert_eq!(PathSelection::after_delete(&path, KnotRef::new(1, 0), 2), sel(2, &[])); +} + +// Direct Selection's ⌥ drag on a segment drags a copy of its subpath (#13756): the copy goes last, moved by the drag, +// the source stays, and the copy ends selected, its anchors shown beside those already shown. ⇧ during the drag takes +// the anchor ⇧ rule. A ⌥ click selects the subpath and edits nothing; on a path that takes no copies the drag moves +// the subpath. +#[trial] +fn an_alt_drag_on_a_segment_drags_a_copy_of_its_subpath() { + let on_square = |h: &Harness| h.screen(p(100.0, 200.0)); + let square = || path_b().subpaths[0].clone(); + + let mut h = harness(vec![path_b()]); + h.update(|st| st.selection = PathSelection::new(Some(2), Vec::new(), None).shown(Some(vec![1]))); + let on = on_square(&h); + h.fixture.hold(egui::Modifiers::ALT); + h.drag(on, vec2(0.0, 80.0)); + h.fixture.hold(egui::Modifiers::NONE); + { + let st = h.state.borrow(); + assert!(matches!(st.changes.last(), Some(PathEdit::DuplicateSubpath { path: 2, subpath: 0, .. })), "{:?}", st.changes); + let subpaths = &st.paths[0].subpaths; + assert_eq!(subpaths.len(), 3); + assert_eq!(subpaths[..2], path_b().subpaths[..], "the source stays"); + let moved = + subpaths[2].knots.iter().zip(&square().knots).all(|(copy, source)| copy.anchor.distance(source.anchor + Vec2::new(0.0, 80.0)) < 1e-3); + assert!(moved && subpaths[2].closed, "{:?}", subpaths[2]); + assert_eq!(st.selection, sel(2, &[(2, 0), (2, 1), (2, 2), (2, 3)]).shown(Some(vec![1, 0, 2]))); + assert_eq!((st.seen.first(), st.seen.last()), (Some(&Seen::Begin), Some(&Seen::Commit)), "{:?}", st.seen); + } + + // ⇧ once the drag is under way: a 5:12 move stays on the horizontal. + let mut h = harness(vec![path_b()]); + let on = on_square(&h); + h.fixture.hold(egui::Modifiers::ALT); + h.press_at(on); + h.move_to(on + vec2(30.0, -12.5)); + h.fixture.hold(egui::Modifiers::ALT | egui::Modifiers::SHIFT); + h.move_to(on + vec2(60.0, -25.0)); + h.release_at(on + vec2(60.0, -25.0)); + h.fixture.hold(egui::Modifiers::NONE); + let first = h.state.borrow().paths[0].subpaths[2].knots[0].anchor; + assert!(first.distance(p(120.0, 200.0)) < 1e-3, "{first:?}"); + + // A ⌥ click selects the subpath. + let mut h = harness(vec![path_b()]); + let on = on_square(&h); + h.fixture.hold(egui::Modifiers::ALT); + h.press_at(on); + h.release_at(on); + h.fixture.hold(egui::Modifiers::NONE); + { + let st = h.state.borrow(); + assert_eq!(st.selection, sel(2, &[(0, 0), (0, 1), (0, 2), (0, 3)])); + assert!(st.changes.is_empty() && st.seen.is_empty(), "{:?} {:?}", st.changes, st.seen); + assert_eq!(st.paths, [path_b()]); + } + + // A path that takes no copies: the subpath moves. + let mut h = harness(vec![path_b().copies(false)]); + let on = on_square(&h); + h.fixture.hold(egui::Modifiers::ALT); + h.drag(on, vec2(0.0, 80.0)); + h.fixture.hold(egui::Modifiers::NONE); + let st = h.state.borrow(); + assert!(matches!(st.changes.last(), Some(PathEdit::MoveAnchors { path: 2, anchors, .. }) if anchors.len() == 4), "{:?}", st.changes); + assert_eq!(st.paths[0].subpaths.len(), 2); +} + +// Add Anchor's cursor says what a click does (#13292): the pen with "+" over a segment a click splits, the arrow over +// an anchor; under ⌘ the cursor is the host tool's; the canvas's precise cursor makes it the crosshair. +#[trial] +fn add_anchor_shows_the_pen_with_plus_over_a_segment_and_the_arrow_over_an_anchor() { + use craft_behavior::{Cursor, CursorBadge, PaintedCursor}; + let cursor_at = |h: &mut Harness, at: Point, mods: egui::Modifiers| { + let at = h.screen(at); + h.fixture.hold(mods); + h.fixture.frame(vec![pointer::moved(at)]); + h.fixture.frame(Vec::new()); + h.fixture.hold(egui::Modifiers::NONE); + craft_ui::runtime::cursor::current(h.fixture.ctx()) + }; + let pen_plus = |cursor: &Option| matches!(cursor, Some(Cursor::Painted(PaintedCursor::Tool { badge: Some(CursorBadge::Add), .. }))); + let (segment, anchor) = (p(110.0, 60.0), p(160.0, 60.0)); + + let mut h = two_paths(); + h.update(|st| st.tool = BezierTool::AddAnchor); + let over_segment = cursor_at(&mut h, segment, egui::Modifiers::NONE); + assert!(pen_plus(&over_segment), "{over_segment:?}"); + assert_eq!(cursor_at(&mut h, anchor, egui::Modifiers::NONE), Some(Cursor::Painted(PaintedCursor::arrow(false)))); + let with_cmd = cursor_at(&mut h, segment, egui::Modifiers::COMMAND); + assert!(!pen_plus(&with_cmd), "{with_cmd:?}"); + + h.update(|st| st.precise = true); + for at in [segment, anchor] { + assert_eq!(cursor_at(&mut h, at, egui::Modifiers::NONE), Some(Cursor::Painted(PaintedCursor::Crosshair)), "{at:?}"); + } +} diff --git a/crates/imaging/src/ui/select/magnetic.rs b/crates/imaging/src/ui/select/magnetic.rs index 4548b10e9e..f46b7a05ba 100644 --- a/crates/imaging/src/ui/select/magnetic.rs +++ b/crates/imaging/src/ui/select/magnetic.rs @@ -255,8 +255,8 @@ fn pen_pressure(pointer: &ToolPointer) -> Option { /// drops it. No selection drag: a press inside the selection draws. /// /// Drawn: the outline as ants, a square with an edge at each fastening point, and over the first point the closing -/// ring beside the pointer. The cursor is the lasso icon; with Other Cursors > Precise -/// ([`precise_cursor`](Self::precise_cursor)) or Caps Lock it is the width circle. +/// ring beside the pointer. The cursor is the lasso icon; under the canvas's precise cursor (Other Cursors > Precise, +/// swapped by Caps Lock: `ToolContext::precise`) it is the width circle. #[derive(Clone, Debug)] pub struct MagneticLassoTool { tracer: T, @@ -265,8 +265,6 @@ pub struct MagneticLassoTool { outline: Option, /// The modifiers of the last pass, to see Alt pressed or released during a drag. mods: Mods, - /// Other Cursors › Precise: the width circle shows round the crosshair. - precise: bool, report: Report, } @@ -279,7 +277,6 @@ impl MagneticLassoTool { bar: SelectionOperation::New, outline: None, mods: Mods::NONE, - precise: false, report: Report::default(), } } @@ -305,13 +302,6 @@ impl MagneticLassoTool { self } - /// Preferences › Cursors › Other Cursors is Precise (`CursorOptions::other_precise`): the - /// width circle shows round the crosshair. - pub fn precise_cursor(&mut self, precise: bool) -> &mut Self { - self.precise = precise; - self - } - /// The tool as the `CanvasTool` for one pass, asking `probe` about the document. pub fn pass<'a>(&'a mut self, probe: &'a dyn SelectionProbe) -> SelectToolPass<'a, Self> { SelectToolPass::new(self, probe) @@ -486,11 +476,11 @@ impl CanvasTool for SelectToolPass<'_, MagneticLassoTool> { if self.tool.outline.as_ref().is_some_and(|o| !o.down) { ToolClaims::NONE.keyboard().deletes() } else { ToolClaims::NONE } } - /// The width circle with Other Cursors > Precise or Caps Lock (`width * zoom`, at least the slop); otherwise the - /// lasso icon with the operation's badge. + /// The width circle under the canvas's precise cursor (`width * zoom`, at least the slop); otherwise the lasso + /// icon with the operation's badge. fn cursor(&self, cx: &ToolContext<'_>) -> Cursor { let tool = &*self.tool; - if (tool.precise || cx.caps_lock) + if cx.precise() && let Some(place) = Place::of(cx) { let (min, max) = MagneticSettings::WIDTH_RANGE; diff --git a/crates/imaging/tests/it/select.rs b/crates/imaging/tests/it/select.rs index 0ac9f6f029..1c73f777c0 100644 --- a/crates/imaging/tests/it/select.rs +++ b/crates/imaging/tests/it/select.rs @@ -891,3 +891,22 @@ fn magnetic_closes_by_start_click_double_click_or_enter_and_retracts_to_the_firs // narrows it. // While the outline is open the tool is one accessible object with its point count (fastening points and the live trace). + +// The Magnetic Lasso's cursor follows the canvas's precise cursor (Other Cursors > Precise, swapped by Caps Lock): +// the width circle while it is on, the lasso icon otherwise (#11690). +#[trial] +fn the_magnetic_lasso_shows_the_width_circle_under_the_precise_cursor() { + use craft_behavior::{Cursor, PaintedCursor}; + let (mut rig, _) = magnetic(); + let over = rig.canvas().center(); + let ctx = rig.fixture().ctx().clone(); + rig.fixture().frame(vec![pointer::moved(over)]); + for caps_lock in [false, true, false] { + craft_ui::runtime::windows::WindowHost::of(&ctx).set_caps_lock(Some(caps_lock)); + rig.fixture().frame(Vec::new()); + rig.fixture().frame(Vec::new()); + let cursor = craft_ui::runtime::cursor::current(&ctx); + let circle = matches!(cursor, Some(Cursor::Painted(PaintedCursor::Circle { .. }))); + assert_eq!(circle, caps_lock, "Caps Lock {caps_lock}: {cursor:?}"); + } +}