diff --git a/crates/draw/src/ui/pen/auto.rs b/crates/draw/src/ui/pen/auto.rs index fef15b5ab..53398cce4 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/mod.rs b/crates/draw/src/ui/pen/bezier/mod.rs index d1e49dde0..ec8779ad6 100644 --- a/crates/draw/src/ui/pen/bezier/mod.rs +++ b/crates/draw/src/ui/pen/bezier/mod.rs @@ -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 @@ -225,6 +227,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 { @@ -523,6 +531,14 @@ impl<'a> BezierOverlay<'a> { 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 { @@ -623,7 +639,14 @@ impl<'a> BezierOverlay<'a> { if mods.alt { let anchors = Self::subpath_anchors(paths, path, segment.subpath); self.report.select = Some(self.selecting(path, anchors.clone(), None)); - Target::Anchors { path, anchors, from } + // 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(self.selecting(path, Vec::new(), Some(segment))); Target::Bend { path, segment, t, from } @@ -658,6 +681,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, }) } @@ -815,8 +839,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)), + _ => {} } } } @@ -835,6 +861,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; } diff --git a/crates/draw/src/ui/pen/bezier/model.rs b/crates/draw/src/ui/pen/bezier/model.rs index 26427b16e..aeff532df 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. @@ -339,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. @@ -401,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 { @@ -411,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 6a0e4d4dc..4bf2b4a7a 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 f94131861..56cecf02d 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 diff --git a/crates/draw/src/ui/pen/mode.rs b/crates/draw/src/ui/pen/mode.rs index 9a1223e7e..436ae5dac 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/tests/it/bezier.rs b/crates/draw/tests/it/bezier.rs index d323454f0..4e4399aa2 100644 --- a/crates/draw/tests/it/bezier.rs +++ b/crates/draw/tests/it/bezier.rs @@ -624,3 +624,68 @@ fn after_a_delete_the_following_anchor_is_selected_and_its_subpath_shown() { 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); +}