From 28685c64d00f92f3d43d547677d2ccd117bb31a9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 19 Nov 2026 07:38:27 -0800 Subject: [PATCH] Pen: Quick Pen drags end where let go and keep outside edits A dragged piece now ends where the press is let go, also when the pointer moved after the last drag event, and a press let go a drag's distance away with no drag in between places the piece there. An edit made to the path by another command during a press (a point moved, its stroke changed) ends the path: the next drag no longer previews the stored path over the edit, and the press keeps the document as it is. An edit made to other art during a drag stays too, and the piece stays as last shown. C and the bracket keys change the sharp-corner arc being dragged, inside the press's one undo step. Typed brackets now reach the Quick Pen while a press is under way. With Align New Objects to Pixel Grid on, the tool goes on drawing the same path: it compares the path with what the document held after its own last edit, and each piece is drawn on the points the path already has, so earlier points keep their place on the grid. Follows #8690. --- crates/engine/src/tests/quickpen.rs | 199 ++++++++++++++++++++++ crates/tools/src/quickpen.rs | 231 +++++++++++++++++++------- crates/ui-egui/src/shortcuts.rs | 15 +- crates/ui-egui/src/shortcuts/tests.rs | 19 +++ docs/mcp.md | 7 +- docs/target-app-parity.md | 2 +- 6 files changed, 409 insertions(+), 64 deletions(-) diff --git a/crates/engine/src/tests/quickpen.rs b/crates/engine/src/tests/quickpen.rs index 66283183b9..890e279893 100644 --- a/crates/engine/src/tests/quickpen.rs +++ b/crates/engine/src/tests/quickpen.rs @@ -183,6 +183,205 @@ fn escape_ends_the_path_and_a_drag_places_the_piece() { assert_eq!(paths(&s).len(), 2); } +/// The piece a press draws ends where the press is let go, past the last drag too, and a press let +/// go a drag's distance away with no drag in between places it there as well. +#[test] +fn the_piece_ends_where_the_press_is_let_go() { + let mut s = session(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 150.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 180.0, 120.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 250.0, 150.0), v).unwrap(); + assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(250.0, 150.0)); + let undo = undo_steps(&s); + s.pointer(&PointerEvent::new(PointerKind::Down, 300.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 340.0, 100.0), v).unwrap(); + assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(340.0, 100.0)); + assert_eq!(undo_steps(&s), undo + 1); +} + +/// C and `[` `]` during a drag change the sharp-corner arc being dragged, within the press's one +/// undo step. +#[test] +fn c_and_brackets_during_a_drag_change_the_arc_being_dragged() { + let mut s = session(); + quick_pen(&mut s); + s.set_tool_option_cmd(&json!({"tool": "quickPen", "key": "action", "value": "sharpArc"})).unwrap(); + click(&mut s, 100.0, 300.0); + let undo = undo_steps(&s); + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 280.0, 300.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 300.0), v).unwrap(); + assert!(s.tool_claims_key(ToolKey::Letter('c'), view())); + key(&mut s, ToolKey::Letter('c')); + assert!(only_path(&s).anchors.get(1).unwrap().p.distance(pt(200.0, 400.0)) < 1e-9, "flipped while dragging"); + key(&mut s, ToolKey::BracketLeft); + assert_eq!(s.tool_options()["turn"], 165.0); + // A 165° arc is two segments, and its middle point lies the arc's sagitta below the chord + // (a 180° arc's lies 100 below). + let half = 82.5f64.to_radians(); + let mid_165 = pt(200.0, 300.0 + 100.0 / half.sin() * (1.0 - half.cos())); + assert_eq!(only_path(&s).anchors.len(), 3); + assert!(only_path(&s).anchors.get(1).unwrap().p.distance(mid_165) < 1e-6, "the dragged arc is 165°"); + s.pointer(&PointerEvent::new(PointerKind::Up, 300.0, 300.0), v).unwrap(); + let sp = only_path(&s); + assert_eq!(sp.anchors.len(), 3); + assert!(sp.anchors.get(1).unwrap().p.distance(mid_165) < 1e-6, "the flip and the size stay after the release"); + assert_eq!(undo_steps(&s), undo + 1); +} + +/// An edit made by another command during a drag ends the path: the next drag does not draw over +/// the edit, the piece stays as last shown, and the next click starts a new path. +#[test] +fn an_edit_during_a_drag_ends_the_path() { + let mut s = session(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let id = paths(&s)[0].0; + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap(); + s.execute("path.moveAnchor", &json!({"id": id.0, "anchor": 0, "x": 90, "y": 90})).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 250.0, 250.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap(); + let ends: Vec = only_path(&s).anchors.iter().map(|a| a.p).collect(); + assert_eq!(ends, [pt(90.0, 90.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the edit and the piece as last shown"); + click(&mut s, 400.0, 400.0); + assert_eq!(paths(&s).len(), 2, "a new path"); +} + +/// With Real-time Drawing off, a sharp-corner arc let go past the last drag is placed where it was +/// let go, and C then flips that arc. +#[test] +fn a_deferred_arc_let_go_past_the_last_drag_flips() { + let mut s = session(); + s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap(); + quick_pen(&mut s); + s.set_tool_option_cmd(&json!({"tool": "quickPen", "key": "action", "value": "sharpArc"})).unwrap(); + click(&mut s, 100.0, 300.0); + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 250.0, 300.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 270.0, 300.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 300.0, 300.0), v).unwrap(); + let sp = only_path(&s); + assert_eq!(sp.anchors.len(), 3); + assert_eq!(sp.anchors.last().unwrap().p, pt(300.0, 300.0)); + let mid = sp.anchors.get(1).unwrap().p; + key(&mut s, ToolKey::Letter('c')); + let flipped = only_path(&s); + assert_eq!(flipped.anchors.len(), 3); + assert!(flipped.anchors.get(1).unwrap().p.distance(pt(200.0, 600.0 - mid.y)) < 1e-9, "flipped across the chord"); +} + +/// An edit made by another command to the path's look during a drag ends the path too. +#[test] +fn a_look_edit_during_a_drag_ends_the_path() { + let mut s = session(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let id = paths(&s)[0].0; + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [id.0], "color": "#00ff00"})).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap(); + let ends: Vec = only_path(&s).anchors.iter().map(|a| a.p).collect(); + assert_eq!(ends, [pt(100.0, 100.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the piece as last shown"); + click(&mut s, 400.0, 400.0); + assert_eq!(paths(&s).len(), 2, "a new path"); +} + +/// An edit made by another command to another object during a drag stays: the press previews no +/// more, and its piece stays as last shown. +#[test] +fn an_edit_to_another_object_during_a_drag_stays() { + let mut s = session(); + s.execute("shape.rectangle", &json!({"x": 500, "y": 400, "width": 50, "height": 50})).unwrap(); + let rect = s.doc().unwrap().selection.objects[0]; + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 200.0), v).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [rect.0], "color": "#00ff00"})).unwrap(); + let edited = s.doc().unwrap().doc.node(rect).unwrap().clone(); + s.pointer(&PointerEvent::new(PointerKind::Up, 260.0, 260.0), v).unwrap(); + assert_eq!(*s.doc().unwrap().doc.node(rect).unwrap(), edited, "the other object's edit stays"); + let pen = paths(&s).into_iter().find(|(id, _)| *id != rect).unwrap().1; + let ends: Vec = pen.subpaths[0].anchors.iter().map(|a| a.p).collect(); + assert_eq!(ends, [pt(100.0, 100.0), pt(200.0, 100.0), pt(200.0, 200.0)], "the piece as last shown"); +} + +/// An edit made by another command during a click (a press let go where it went down) ends the +/// path too: the next click starts a new path instead of drawing over the edit. +#[test] +fn an_edit_during_a_click_ends_the_path() { + let mut s = session(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let id = paths(&s)[0].0; + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 180.0), v).unwrap(); + s.execute("path.moveAnchor", &json!({"id": id.0, "anchor": 0, "x": 90, "y": 90})).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Up, 200.0, 180.0), v).unwrap(); + click(&mut s, 400.0, 400.0); + assert_eq!(paths(&s).len(), 2, "a new path"); + let ends: Vec = paths(&s)[0].1.subpaths[0].anchors.iter().map(|a| a.p).collect(); + assert_eq!(ends, [pt(90.0, 90.0), pt(200.0, 100.0), pt(200.0, 180.0)], "the edit stays"); +} + +/// With Align New Objects to Pixel Grid on, the path the tool draws is pixel-aligned: its points +/// move onto the grid, and the next click and drag still go on drawing the same path. +#[test] +fn a_pixel_aligned_path_goes_on_drawing() { + let mut s = session(); + s.execute("document.setup", &json!({"alignNewObjectsToPixelGrid": true})).unwrap(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let ends = |s: &Session| only_path(s).anchors.iter().map(|a| a.p).collect::>(); + let two = ends(&s); + assert_eq!(two.len(), 2); + assert_eq!(two[0].y, two[1].y, "a level line"); + press(&mut s, &[(300.0, 150.0), (310.0, 170.0), (320.0, 200.0)]); + let three = ends(&s); + assert_eq!(three.len(), 3, "one path of three points"); + assert_eq!(three[..2], two[..], "the points before the piece stay as they were"); + assert!(three[2].distance(pt(320.0, 200.0)) <= 1.0, "the drag's piece ends near where it was let go: {:?}", three[2]); + click(&mut s, 400.0, 100.0); + let four = ends(&s); + assert_eq!(four.len(), 4); + assert_eq!(four[..3], three[..]); +} + +/// A dropped press takes back only its own piece, and with Real-time Drawing off a drag places its +/// piece on the release: in both cases the next click goes on drawing the same path. +#[test] +fn a_dropped_press_and_a_deferred_drag_keep_the_path() { + let mut s = session(); + quick_pen(&mut s); + click(&mut s, 100.0, 100.0); + click(&mut s, 200.0, 100.0); + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 300.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 320.0, 150.0), v).unwrap(); + s.drop_tool_press(v).unwrap(); + assert_eq!(only_path(&s).anchors.len(), 2, "the dropped piece is gone"); + click(&mut s, 300.0, 200.0); + assert_eq!(only_path(&s).anchors.len(), 3, "the path goes on"); + s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap(); + press(&mut s, &[(400.0, 200.0), (420.0, 250.0), (450.0, 300.0)]); + assert_eq!(only_path(&s).anchors.last().unwrap().p, pt(450.0, 300.0)); + click(&mut s, 500.0, 100.0); + assert_eq!(only_path(&s).anchors.len(), 5, "the path goes on after the deferred piece"); +} + /// An anchor moved by another command ends the path being drawn: C does not flip the old arc back /// over the edit, and the next click starts a new path instead of rebuilding the old one. #[test] diff --git a/crates/tools/src/quickpen.rs b/crates/tools/src/quickpen.rs index 140278719d..18230345e4 100644 --- a/crates/tools/src/quickpen.rs +++ b/crates/tools/src/quickpen.rs @@ -7,15 +7,17 @@ //! sharp-corner arc (it turns through the arc size, 180° by default, and meets the path before it //! at a corner). Alt (Option) pressed and let go on its own switches to the next action. C flips //! the last sharp-corner arc to the other side of its chord, and [ and ] make the arc size 15° -//! smaller or larger, for the last sharp-corner arc and the ones after it. A click on the first +//! smaller or larger, for the last sharp-corner arc and the ones after it; during a drag they +//! change the arc being dragged. A dragged piece ends where the press is let go. A click on the first //! point closes the path, a click on an end of an open path goes on drawing from it, and Escape //! or Enter ends the path. The path's points show as circles, without handles, and a preview //! shows the piece the next click adds. Points snap to Smart Guides. Each piece is one undo step. use std::f64::consts::{FRAC_PI_2, PI}; +use std::sync::Arc; use serde_json::{Value, json}; -use vectorcraft_doc::{NodeId, NodeKind}; +use vectorcraft_doc::{Document, Node, NodeId, NodeKind}; use vectorcraft_geom::{Anchor, AnchorKind, BezPath, Point, SubPath, Vec2}; use crate::draw2::FEEDBACK; @@ -39,6 +41,11 @@ const DRAG_PX: f64 = 3.0; /// A continuous arc whose end lies within this angle (radians) of straight ahead, or of straight /// behind, is drawn as a line: no circle runs there in the path's direction. const STRAIGHT: f64 = 1e-6; +/// The notice the tool asks for after each of its own edits, to see the path as the document then +/// has it. +const DRAWN: &str = "drawn"; +/// The notice after a press's piece is committed. +const PLACED: &str = "placed"; /// What the next click adds. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -214,21 +221,50 @@ pub fn build(base: &SubPath, pieces: &[Piece], closed: bool) -> SubPath { sp } -/// The path being drawn: subpath `si` of `id`, whose points were `base` before the tool drew on -/// (both none until the engine made a new path), then `pieces`. A new path starts at `start`. -#[derive(Clone, Debug, PartialEq)] +/// The path being drawn: subpath `si` of `id` (none until the engine made a new path, which starts +/// at `start`), and the `pieces` the tool added to it, each after the number of points in `kept` +/// the subpath had before it. `seen` is the path object as the document had it after the tool's +/// last edit, pixel alignment and other changes the engine makes after each edit included, and +/// `before_press` as it was when the press under way began. A piece is drawn again on the points +/// the document holds before it, so the points before it stay as they are. +#[derive(Clone, Debug, Default, PartialEq)] struct Drawing { id: Option, si: usize, - base: Option, start: Option, pieces: Vec, + kept: Vec, + seen: Option>, + before_press: Option>, + /// The document's revision ([`ToolContext::revision`]) after the tool's last edit. + revision: (u64, u64), +} + +/// The node `id` as the document shares it, found through the document's index. +fn shared_node(doc: &Document, id: NodeId) -> Option<&Arc> { + let path = doc.index_path(id)?; + let (first, rest) = path.split_first()?; + let mut cur = doc.layers.get(*first)?; + for &i in rest { + cur = cur.children()?.get(i)?; + } + Some(cur) +} + +/// Is `now` the node `then` was: the same shared node, or one equal to it? +fn same_node(now: &Node, then: &Arc) -> bool { + std::ptr::eq(now, Arc::as_ptr(then)) || *now == **then } impl Drawing { - /// The subpath as the tool drew it. - fn drawn(&self) -> Option { - Some(build(self.base.as_ref()?, &self.pieces, false)) + fn push(&mut self, piece: Piece, kept: usize) { + self.pieces.push(piece); + self.kept.push(kept); + } + + fn pop(&mut self) { + self.pieces.pop(); + self.kept.pop(); } } @@ -266,30 +302,41 @@ fn point_tol(cx: &ToolContext) -> f64 { cx.tol(5.0).max(cx.point_tol()) } +/// Asks to see the path once the actions before it are applied. +fn drawn() -> Action { + Action::Notify(DRAWN.into()) +} + impl QuickPenTool { - /// The subpath being drawn, while it is selected, editable, open and still exactly what the tool - /// drew: an edit made outside the tool (a point moved, the path it went on from changed) ends - /// the drawing, so the next piece does not undo it. + /// The subpath being drawn, while it is selected, editable, open and its path still exactly as + /// the tool's last edit left it: an edit made to the path outside the tool (a point moved, its + /// look changed, the path it went on from changed), also during a press, ends the drawing, so + /// the next piece does not undo it. fn active<'a>(&self, cx: &'a ToolContext) -> Option<(NodeId, &'a SubPath)> { let dr = self.drawing.as_ref()?; let id = dr.id?; if !selected_alone(cx, id) || !cx.doc.is_editable(id) { return None; } - let sp = cx.doc.node(id)?.path_data()?.subpaths.get(dr.si)?; - (!sp.closed && dr.drawn()?.anchors == sp.anchors).then_some((id, sp)) + let node = cx.doc.node(id)?; + if !same_node(node, dr.seen.as_ref()?) { + return None; + } + let sp = node.path_data()?.subpaths.get(dr.si)?; + (!sp.closed).then_some((id, sp)) } - /// `path.quickPen`'s params for the path being drawn, closed when `closed`. + /// `path.quickPen`'s params for the path being drawn: its last piece on the points before it, + /// closed when `closed`. fn params(&self, closed: bool) -> Value { let Some(dr) = &self.drawing else { return json!({}) }; - let pieces: Vec = dr.pieces.iter().map(Piece::to_json).collect(); + let pieces: Vec = dr.pieces.last().map(Piece::to_json).into_iter().collect(); let mut v = json!({"pieces": pieces}); match (dr.id, dr.start) { (Some(id), _) => { v["id"] = json!(id.0); v["subpath"] = json!(dr.si); - v["keep"] = json!(dr.base.as_ref().map_or(1, |b| b.anchors.len().max(1))); + v["keep"] = json!(dr.kept.last().copied().unwrap_or(1).max(1)); } (None, Some(s)) => v["start"] = json!([s.x, s.y]), (None, None) => {} @@ -300,15 +347,57 @@ impl QuickPenTool { v } - /// The last piece, while it is a sharp-corner arc and no press is under way. + /// The last piece, while it is a sharp-corner arc: the one drawn last while no press is under + /// way, or the one a press is drawing. fn last_sharp_arc(&self, cx: &ToolContext) -> Option { - if self.press.is_some() { - return None; + match self.press { + Some(pr) if !pr.begun => return None, + Some(_) => self.held(cx).then_some(())?, + None => self.active(cx).map(|_| ())?, } - self.active(cx)?; self.drawing.as_ref()?.pieces.last().copied().filter(|p| p.stroke == Stroke::SharpArc) } + /// During a press: is the path being drawn still active, and the document as the tool's last + /// preview left it? A preview starts again from the document as the press found it, so after + /// an edit made outside the tool the press previews no more. + fn held(&self, cx: &ToolContext) -> bool { + self.active(cx).is_some() && self.drawing.as_ref().is_some_and(|dr| dr.revision == cx.revision) + } + + /// Moves the end of the piece a press draws to `p` (snapped) and previews it. An edit made + /// outside the tool since the last preview ends the drawing instead: the press then commits + /// the document as it is. + fn drag_to(&mut self, cx: &ToolContext, p: Point) -> Vec { + if !self.held(cx) { + self.drawing = None; + self.snap.clear(); + return vec![]; + } + // The snapping leaves from the point before the piece being dragged, as the press found it. + let from = self.drawing.as_ref().and_then(|dr| { + let sp = dr.before_press.as_ref()?.path_data()?.subpaths.get(dr.si)?; + sp.anchors.get(dr.kept.last()?.checked_sub(1)?).map(|a| Leave::segment(cx, a.p, false).without_contact()) + }); + let q = self.snap.drag(cx, p, from.as_ref()); + let Some(last) = self.drawing.as_mut().and_then(|dr| dr.pieces.last_mut()) else { return vec![] }; + if last.to == q { + return vec![]; + } + last.to = q; + vec![Action::Preview(COMMAND.into(), self.params(false)), drawn()] + } + + /// Draws the path again after its last piece changed: a preview while a press draws that + /// piece, else a step of its own. + fn redraw(&self) -> Vec { + if self.press.is_some_and(|pr| pr.begun) { + vec![Action::Preview(COMMAND.into(), self.params(false)), drawn()] + } else { + vec![Action::Exec(COMMAND.into(), self.params(false)), drawn()] + } + } + fn piece_to(&self, to: Point) -> Piece { Piece { to, stroke: self.action, turn: self.turn, flip: false } } @@ -332,9 +421,9 @@ impl QuickPenTool { _ => return vec![], }; if sp.anchors.len() >= 2 && p.distance(first) <= tol && p.distance(first) <= p.distance(last) { - let piece = self.piece_to(first); + let (piece, kept) = (self.piece_to(first), sp.anchors.len()); if let Some(dr) = self.drawing.as_mut() { - dr.pieces.push(piece); + dr.push(piece, kept); } let out = vec![Action::Exec(COMMAND.into(), self.params(true))]; self.drawing = None; @@ -345,27 +434,25 @@ impl QuickPenTool { } let from = self.leave(cx).map(|l| l.1); let q = self.snap.press(cx, p, &[id], from.as_ref()); - let piece = self.piece_to(q); + let (piece, kept) = (self.piece_to(q), sp.anchors.len()); if let Some(dr) = self.drawing.as_mut() { - dr.pieces.push(piece); + dr.push(piece, kept); + dr.before_press = shared_node(cx.doc, id).cloned(); } self.press = Some(Press { at: p, dragged: false, begun: true }); - return vec![Action::Begin("Quick Pen".into()), Action::Preview(COMMAND.into(), self.params(false))]; + return vec![Action::Begin("Quick Pen".into()), Action::Preview(COMMAND.into(), self.params(false)), drawn()]; } if let Some((id, si, at_first)) = open_end_at(cx, p, tol, None) && cx.doc.is_editable(id) - && let Some(sp) = cx.doc.node(id).and_then(|n| n.path_data()).and_then(|pd| pd.subpaths.get(si)) { - // From its start, `continue_from` reverses the path first. - let mut base = sp.clone(); - if at_first { - base.reverse(); - } - self.drawing = Some(Drawing { id: Some(id), si, base: Some(base), start: None, pieces: vec![] }); - return continue_from(cx, id, si, at_first); + // From its start, `continue_from` reverses the path first; the tool then sees it. + self.drawing = Some(Drawing { id: Some(id), si, ..Drawing::default() }); + let mut out = continue_from(cx, id, si, at_first); + out.push(drawn()); + return out; } let q = self.snap.press(cx, p, &[], None); - self.drawing = Some(Drawing { id: None, si: 0, base: None, start: Some(q), pieces: vec![] }); + self.drawing = Some(Drawing { start: Some(q), ..Drawing::default() }); vec![Action::Exec(COMMAND.into(), self.params(false)), Action::Notify("created".into())] } @@ -427,36 +514,58 @@ impl Tool for QuickPenTool { } pr.dragged = true; self.press = Some(pr); - // The snapping leaves from the point before the piece being dragged. - let from = self.drawing.as_ref().and_then(|dr| { - let sp = build(dr.base.as_ref()?, dr.pieces.split_last().map_or(&[][..], |(_, rest)| rest), false); - sp.anchors.last().map(|a| Leave::segment(cx, a.p, false).without_contact()) - }); - let q = self.snap.drag(cx, p, from.as_ref()); - if let Some(last) = self.drawing.as_mut().and_then(|dr| dr.pieces.last_mut()) { - last.to = q; + if !pr.begun { + return vec![]; } - vec![Action::Preview(COMMAND.into(), self.params(false))] + self.drag_to(cx, p) } PointerKind::Up => { - self.snap.clear(); - match self.press.take() { - Some(pr) if pr.begun => vec![Action::Commit], + let out = match self.press { + // The piece ends where the press is let go, also past the last drag. + Some(pr) if pr.begun => { + let mut out = vec![]; + if pr.dragged || p.distance(pr.at) >= cx.tol(DRAG_PX) { + out = self.drag_to(cx, p); + } else if self.active(cx).is_none() { + // An edit made outside the tool during the press ends the path. + self.drawing = None; + } + // With Real-time Drawing off, the commit applies the piece. + out.extend([Action::Commit, Action::Notify(PLACED.into())]); + out + } _ => vec![], - } + }; + self.press = None; + self.snap.clear(); + out } PointerKind::DoubleClick | PointerKind::TripleClick => vec![], } } fn notify(&mut self, cx: &ToolContext, what: &str) { + let Some(dr) = self.drawing.as_mut() else { return }; if what == "created" && let [id] = cx.selection.objects.as_slice() - && let Some(base) = cx.doc.node(*id).and_then(|n| n.path_data()).and_then(|pd| pd.subpaths.first()) - && let Some(dr) = self.drawing.as_mut() + && let Some(node) = shared_node(cx.doc, *id).filter(|n| n.path_data().is_some_and(|pd| !pd.subpaths.is_empty())) && dr.id.is_none() { - (dr.id, dr.si, dr.base, dr.start) = (Some(*id), 0, Some(base.clone()), None); + (dr.id, dr.si, dr.start, dr.seen) = (Some(*id), 0, None, Some(node.clone())); + } else if (what == DRAWN || what == PLACED) + && let Some(id) = dr.id + { + dr.seen = shared_node(cx.doc, id).cloned(); + dr.revision = cx.revision; + // Once committed, a piece that added no point to the path (the command refused it) is + // not kept for C and [ ] to change. + let points = dr.seen.as_ref().and_then(|n| n.path_data()).and_then(|pd| pd.subpaths.get(dr.si)).map(|sp| sp.anchors.len()); + if what == PLACED + && let (Some(points), Some(&kept)) = (points, dr.kept.last()) + && points <= kept + { + dr.pop(); + } } } @@ -475,13 +584,19 @@ impl Tool for QuickPenTool { } /// A dropped press does not end the path being drawn: the piece it added goes. - fn drop_press(&mut self, _cx: &ToolContext) -> Vec { + /// The drawing goes on only when the path is back as it was before the press. + fn drop_press(&mut self, cx: &ToolContext) -> Vec { + self.snap.clear(); if self.press.take().is_some_and(|pr| pr.begun) && let Some(dr) = self.drawing.as_mut() { - dr.pieces.pop(); + dr.pop(); + let now = dr.id.and_then(|id| cx.doc.node(id)); + match (now, dr.before_press.take()) { + (Some(now), Some(before)) if same_node(now, &before) => dr.seen = Some(before), + _ => self.drawing = None, + } } - self.snap.clear(); vec![] } @@ -495,7 +610,7 @@ impl Tool for QuickPenTool { if let Some(last) = self.drawing.as_mut().and_then(|dr| dr.pieces.last_mut()) { last.flip = !last.flip; } - vec![Action::Exec(COMMAND.into(), self.params(false))] + self.redraw() } ToolKey::BracketLeft | ToolKey::BracketRight => { let by = if key == ToolKey::BracketLeft { -TURN_STEP } else { TURN_STEP }; @@ -511,17 +626,17 @@ impl Tool for QuickPenTool { if let Some(last) = self.drawing.as_mut().and_then(|dr| dr.pieces.last_mut()) { last.turn = turn; } - vec![Action::Exec(COMMAND.into(), self.params(false))] + self.redraw() } ToolKey::AltTap => { self.action = self.action.next(); // A drag under way draws its piece with the new action. - if self.press.is_some_and(|pr| pr.begun) { + if self.press.is_some_and(|pr| pr.begun) && self.held(cx) { let (action, turn) = (self.action, self.turn); if let Some(last) = self.drawing.as_mut().and_then(|dr| dr.pieces.last_mut()) { (last.stroke, last.turn, last.flip) = (action, turn, false); } - return vec![Action::Preview(COMMAND.into(), self.params(false))]; + return vec![Action::Preview(COMMAND.into(), self.params(false)), drawn()]; } vec![] } diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index d4cde5a873..bf2dff9b68 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -970,6 +970,10 @@ fn busy_tool_keys(app: &mut VectorcraftApp, ctx: &egui::Context, view: ViewInfo) for d in digits { let _ = app.session.tool_key(ToolKey::Digit(d), Mods::default(), view); } + // `[` and `]` while the Quick Pen tool draws a piece: the arc being dragged takes the new size. + if app.session.tool_id() == vectorcraft_tools::quickpen::ID { + tool_brackets(app, ctx, view); + } // Tab, when the tool takes it (the Smart Pen Tool placing an anchor). if app.session.tool_claims_key(ToolKey::Tab, view) && ctx.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Tab)) { let _ = app.session.tool_key(ToolKey::Tab, Mods::default(), view); @@ -1073,9 +1077,14 @@ fn claimed_tool_keys(app: &mut VectorcraftApp, ctx: &egui::Context, view: ViewIn } } } - // `[` and `]` the active tool takes (the Blob Brush's and the Eraser's size, the Symbolism - // tools' diameter, with Shift their intensity). Read as typed, so - // layouts where a bracket takes Option or AltGr count too; `{` and `}` are Shift's. + tool_brackets(app, ctx, view) +} + +/// `[` and `]` the active tool takes (the Blob Brush's and the Eraser's size, the Symbolism tools' +/// diameter, with Shift their intensity). Read as typed, so layouts where a bracket takes Option or +/// AltGr count too; `{` and `}` are Shift's. Did the tool take one? +fn tool_brackets(app: &mut VectorcraftApp, ctx: &egui::Context, view: ViewInfo) -> bool { + let m = ctx.input(|i| i.modifiers); if !(m.command || m.ctrl) { let typed: Vec<(String, ToolKey, bool)> = ctx.input(|i| { i.events diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index f29308c4b5..c751c15611 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -2237,6 +2237,25 @@ fn brackets_size_the_blob_brush_and_the_eraser() { assert_eq!(app.session.paint.stroke_width, 3.0); } +/// A typed `[` or `]` reaches the Quick Pen tool while a press drags a sharp-corner arc: the arc +/// being dragged takes the new size. +#[test] +fn brackets_size_the_quick_pen_arc_being_dragged() { + use vectorcraft_tools::{PointerEvent, PointerKind}; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 400})).unwrap(); + app.select_tool(vectorcraft_tools::quickpen::ID); + app.session.set_tool_option_cmd(&json!({"tool": "quickPen", "key": "action", "value": "sharpArc"})).unwrap(); + let v = vectorcraft_engine::ViewInfo { smart_guides: false, ..Default::default() }; + app.session.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 300.0), v).unwrap(); + app.session.pointer(&PointerEvent::new(PointerKind::Up, 100.0, 300.0), v).unwrap(); + app.session.pointer(&PointerEvent::new(PointerKind::Down, 280.0, 300.0), v).unwrap(); + app.session.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 300.0), v).unwrap(); + assert!(app.session.tool_busy()); + frame(&mut app, vec![egui::Event::Text("[".into())]); + assert_eq!(app.session.tool_options()["turn"], 165.0); +} + fn remembered_paint_app() -> VectorcraftApp { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap(); diff --git a/docs/mcp.md b/docs/mcp.md index 3c058d6fff..7a4f49e794 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7165,8 +7165,11 @@ path's last point, drawn with the tool's `action` option: Alt (Option on macOS) pressed and let go with no other key, click or wheel turn in between switches to the next action: Line, Continuous Arc, Sharp Corner Arc, and back to Line. C flips the last piece, while it is a sharp-corner arc, to the other side of its chord. `[` and `]` make the arc size 15° smaller or larger, for the last sharp-corner arc and the -arcs after it. A click on the first point closes the path, and a click on an end of an open path goes on drawing from -that end. Escape or Enter ends the path, and so does an edit made to it by another command. The path's points show as circles without handles, and a preview shows the +arcs after it. During a drag, C and `[` `]` change the sharp-corner arc being dragged, within the drag's undo step, and +Alt switches its action. A dragged piece ends where the press is let go. An edit made to the path by another command +during a drag ends the path: the piece stays as last shown, and the edit stays. An edit to other art during a drag also +leaves the piece as last shown, and the edit stays. A click on the first point closes the +path, and a click on an end of an open path goes on drawing from that end. Escape or Enter ends the path, and so does an edit made to it by another command. The path's points show as circles without handles, and a preview shows the piece the next click adds. Points snap to Smart Guides. Each piece, flip and size change is one undo step. The arc size of 180°, its 15° steps, the side an arc bulges to and Enter ending the path are VectorCraft's own rules. The tool keeps `action` and `turn` with the preferences (`tool.setOption {tool: "quickPen", key, value}`). diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 4631f9a147..cdf7741f88 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -629,7 +629,7 @@ since 2026-10-01. - **Guide row highlight (#7344):** `guide_row_highlight_stays_selected_without_hover` in `crates/ui-egui/src/panels/layers/tests.rs` checks selected guide background after the pointer leaves, with unselected guide and ordinary row controls. Guide selection, dragging and deletion are unchanged. Scores and estimates unchanged. - **Recolor picker follow-up (#8031/#4028):** native Lab numeric entry, document/loaded-library swatches in the Color Picker, and Assign-row Pick from Swatches / Limit to Library menus. #4090 provided the double-click picker and #5113 the Assign sliders. Regression tests `native_lab_entry_in_color_picker` and `row_context_menu_pick_from_swatches_and_limit_to_library` exercise the fields and menu callbacks rather than writing their result state. Area score and hours unchanged. -- **Quick Pen tool (#2660, in part):** a tool in the Pen tool's group (Alt+P, Option+P on macOS) draws straight lines, continuous arcs and sharp-corner arcs, one undo step per piece; Alt pressed and let go on its own switches the action, C flips the last sharp-corner arc, `[` and `]` change the arc size by 15°, a click on the first point closes the path, a click on an open end continues it, and Escape or Enter ends it (`path.quickPen`), as [Draw with the Quick Pen tool](https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/draw-with-quick-pen-tool.html) and [Quick Pen tool keyboard shortcuts](https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/quick-pen-tool-keyboard-shortcuts.html) describe. The 180° default arc, its 15° steps and the side an arc bulges to are VectorCraft's own rules. Tests: tools `quickpen::tests`, engine `tests::quickpen`, ui-egui `shortcuts::alt_tap::tests` and `alt_p_chooses_the_quick_pen_and_an_alt_tap_switches_its_action`. Score and hours unchanged. +- **Quick Pen tool (#2660, in part):** a tool in the Pen tool's group (Alt+P, Option+P on macOS) draws straight lines, continuous arcs and sharp-corner arcs, one undo step per piece, a dragged piece ending where the press is let go; Alt pressed and let go on its own switches the action, C flips the last sharp-corner arc or the one being dragged, `[` and `]` change the arc size by 15°, a click on the first point closes the path, a click on an open end continues it, and Escape or Enter ends it (`path.quickPen`), as [Draw with the Quick Pen tool](https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/draw-with-quick-pen-tool.html) and [Quick Pen tool keyboard shortcuts](https://helpx.adobe.com/illustrator/web/add-and-edit-artwork/draw-and-edit-paths/quick-pen-tool-keyboard-shortcuts.html) describe. The 180° default arc, its 15° steps and the side an arc bulges to are VectorCraft's own rules. Tests: tools `quickpen::tests`, engine `tests::quickpen`, ui-egui `shortcuts::alt_tap::tests`, `alt_p_chooses_the_quick_pen_and_an_alt_tap_switches_its_action` and `brackets_size_the_quick_pen_arc_being_dragged`. Score and hours unchanged. - **Optional compact Tools (#7234):** Window > Toolbars > Compact Tools reduces row and column spacing and the main panel's allocated width in one or two columns, docked or floating. The setting is saved with the UI state and applies to Basic, Advanced and edited main Tools layouts; the selected tool, icon size, footer controls and tool-flyout behavior are retained. Turning it off restores the existing layout, which remains the default. Named custom toolbars and flyout spacing are unchanged. Public regressions in `crates/ui-egui/tests/toolbar_compact.rs` cover the actual toolbar layout against the default control, the command and saved-state roundtrip, tool selection, flyouts, footer controls and short-window reachability. This is an optional layout choice, without a styling-parity or score claim; hours are unchanged.