From c53698127e5e0dd8fa7cfb3914aa9931048c2b83 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 21:15:37 +0000 Subject: [PATCH 1/5] TransformBox snapping follows PhotoCraft: inside drags snap the box's bounds, the reference point snaps to the box's nine points only, Distort corners snap to guides only and Ctrl keeps their snapping, and a folded-short corner reports no line --- crates/behavior/src/geometry/snap.rs | 9 +- crates/draw/src/ui/transform/transform_box.rs | 83 +++++++++---- crates/draw/tests/it/transform_box.rs | 111 +++++++++++++++++- crates/ui-canvas/src/canvas/snap.rs | 2 +- 4 files changed, 179 insertions(+), 26 deletions(-) diff --git a/crates/behavior/src/geometry/snap.rs b/crates/behavior/src/geometry/snap.rs index 275a92936..50889a4f2 100644 --- a/crates/behavior/src/geometry/snap.rs +++ b/crates/behavior/src/geometry/snap.rs @@ -59,6 +59,11 @@ impl SnapKinds { } } + /// The set of `kind` alone. + pub const fn only(kind: SnapKind) -> Self { + SnapKinds(Self::bit(kind)) + } + /// This set without `kind`. pub fn without(self, kind: SnapKind) -> Self { SnapKinds(self.0 & !Self::bit(kind)) @@ -313,8 +318,8 @@ impl SnapTargets { self.snap_point_of(point, tolerance, SnapKinds::ALL) } - /// [`snap_point`](Self::snap_point) to the targets of a kind in `kinds` only (a Distort corner skips layer edges - /// and centres). + /// [`snap_point`](Self::snap_point) to the targets of a kind in `kinds` only (a Distort corner snaps to guides + /// only). /// /// Public: craft-ui-canvas's `Snap::point` filters through it. pub fn snap_point_of(&self, point: Point, tolerance: f64, kinds: SnapKinds) -> (Point, Vec) { diff --git a/crates/draw/src/ui/transform/transform_box.rs b/crates/draw/src/ui/transform/transform_box.rs index 6539cf437..df96e2ca7 100644 --- a/crates/draw/src/ui/transform/transform_box.rs +++ b/crates/draw/src/ui/transform/transform_box.rs @@ -5,7 +5,7 @@ #[cfg(feature = "screen-testing")] mod stories; -use craft_behavior::geometry::SnapLine; +use craft_behavior::geometry::{SNAP_PX, SnapKind, SnapKinds, SnapLine}; use craft_behavior::geometry::quad::{self, BoxAllow, BoxDrag, BoxPart, BoxRules, BoxState, ScaleRule, TransformMode}; use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2 as DocVec}; use craft_behavior::{ @@ -252,9 +252,11 @@ fn slop_sense(part: BoxPart) -> Sense { /// corner winning where both reach), the inside or the outside. Parts the [rules](Self::rules) refuse take no /// press, except the outside in [`RotateZone::Outside`], where a click applies. An Alt press away from the handles /// moves the reference point there and drags it, whether it shows or not. -/// - Snapping is the canvas's ([`canvas_snap`]): a dragged point snaps through [`Snap::point`], and the lines it -/// snapped to are [`TransformBoxOutput::snapped`] for the host's one drawing site. Without a canvas snap nothing -/// snaps. A pass reports the lines of its own drag events only. +/// - Snapping is the canvas's ([`canvas_snap`]): a dragged handle snaps through [`Snap::point`] and a box moved from +/// inside snaps its bounds through [`Snap::rect_of`]; the lines the result reaches are +/// [`TransformBoxOutput::snapped`] for the host's one drawing site. Without a canvas snap nothing snaps but the +/// reference point, which snaps to the box's nine points only. In Distort only the corners snap, to guides. A pass +/// reports the lines of its own drag events only. /// - The box is one Tab stop with one part group, whose arrival is the picked part. Page Down and Page Up pick the /// next and previous part (the whole box, the corners and edges clockwise from the top left, the reference /// point), Home and End the first and last. The arrows move the picked part as a plain drag of it would, one @@ -403,31 +405,55 @@ impl<'a> TransformBox<'a> { quad::drag(state, state, part, at, at + by, Mods::NONE, &self.rules) } - /// The box the pointer at screen position `pos` gives a drag, snapped through the canvas's `snap` and rounded, with - /// the lines it snapped to. A corner, an edge or the reference point snaps the grabbed point, not the pointer: a - /// press a few pixels off the handle still lands the handle on the line. The Control key suspends the snapping - /// unless it changes what the drag does (off macOS Ctrl is ⌘: a corner distorts, an edge skews). + /// The box the pointer at screen position `pos` gives a drag, snapped and rounded, with the lines it snapped to. + /// + /// - A corner or an edge snaps the grabbed handle through the canvas's `snap`, not the pointer: a press a few + /// pixels off the handle still lands the handle on the line. A distorting corner reports only the lines it + /// reaches once the box's folding rule has placed it: a corner stopped short of a line claims no alignment. + /// - A drag from inside snaps the bounds of the moved box (its edges and centre). + /// - The reference point snaps to the nine points of the box at the press (corners, edge midpoints, centre) and + /// to nothing else, with or without the canvas's snap, the Control key held or not. + /// - In [`TransformMode::Distort`] only the corners snap, and to guides only. The outside (rotating) never snaps. + /// - The Control key suspends the snapping unless the drag reads it (off macOS Ctrl is ⌘): where it changes what + /// the drag does (a corner distorts, an edge skews) or on a corner that distorts. fn proposal(&self, space: &CanvasSpace, snap: Option<&Snap>, grip: &Grip, pos: Pos2, mods: Mods) -> (BoxState, Vec) { let to = space.view.to_doc(pos); - let reads_control = self.rules.drag_kind(grip.part, mods) != self.rules.drag_kind(grip.part, Mods { cmd: false, ..mods }); - let quad = &grip.start.quad; - let grabbed = match grip.part { - BoxPart::Corner(i) => Some(quad[usize::from(i % 4)]), - BoxPart::Edge(i) => Some(quad[usize::from(i % 4)].midpoint(quad[usize::from(i + 1) % 4])), - BoxPart::Pivot => Some(grip.start.pivot), - _ => None, - }; - let (to, lines) = match (snap, grabbed) { - (Some(snap), Some(grabbed)) => { + let kind = self.rules.drag_kind(grip.part, mods); + let reads_control = mods.cmd && (kind != self.rules.drag_kind(grip.part, Mods { cmd: false, ..mods }) || kind == Some(BoxDrag::Distort)); + let distort_mode = self.rules.effective_mode() == TransformMode::Distort; + let kinds = if distort_mode { SnapKinds::only(SnapKind::Guide) } else { SnapKinds::ALL }; + let gesture = GestureSnap::default().reads_control(reads_control).kinds(kinds); + let grabbed = handle_point(&grip.start, grip.part); + let (to, mut lines) = match (grip.part, snap, grabbed) { + (BoxPart::Pivot, _, Some(grabbed)) if !distort_mode => { let off = grip.from - grabbed; - let (at, lines) = snap.point(to - off, mods, GestureSnap::default().reads_control(reads_control)); + (box_point_pull(&grip.start, to - off, space.view.doc_len(SNAP_PX as f32)) + off, Vec::new()) + } + (BoxPart::Corner(_), Some(snap), Some(grabbed)) | (BoxPart::Edge(_), Some(snap), Some(grabbed)) + if !distort_mode || matches!(grip.part, BoxPart::Corner(_)) => + { + let off = grip.from - grabbed; + let (at, lines) = snap.point(to - off, mods, gesture); (at + off, lines) } - (Some(snap), None) => snap.point(to, mods, GestureSnap::default().reads_control(reads_control)), - (None, _) => (to, Vec::new()), + (BoxPart::Inside, Some(snap), _) if !distort_mode => { + let moved = bounds_of(&grip.start.quad) + (to - grip.from); + let (by, lines) = snap.rect_of(moved, mods, kinds); + (to + by, lines) + } + _ => (to, Vec::new()), }; let dragged = quad::drag(&grip.start, &grip.last, grip.part, grip.from, to, mods, &self.rules); - (if self.pixel_grid { on_pixel_grid(dragged) } else { dragged }, lines) + let dragged = if self.pixel_grid { on_pixel_grid(dragged) } else { dragged }; + if kind == Some(BoxDrag::Distort) + && let Some(at) = handle_point(&dragged, grip.part) + { + lines.retain(|line| { + let across = if matches!(line.along, Axis::Y) { at.x } else { at.y }; + (across - line.position).abs() < 1e-6 + }); + } + (dragged, lines) } /// The pointer for `part` at `pointer`, `dragging` during its drag. @@ -968,6 +994,19 @@ fn handle_point(state: &BoxState, part: BoxPart) -> Option { } } +/// The bounds of `quad`. +fn bounds_of(quad: &[Point; 4]) -> DocRect { + quad[1..].iter().fold(DocRect::from_points(quad[0], quad[0]), |r, p| r.union_pt(*p)) +} + +/// `p` pulled onto the nearest of the nine points of `state`'s box (its corners, edge midpoints and centre) within +/// `reach` document units, else `p`. +fn box_point_pull(state: &BoxState, p: Point, reach: f64) -> Point { + let q = &state.quad; + let points = [q[0], q[1], q[2], q[3], q[0].midpoint(q[1]), q[1].midpoint(q[2]), q[2].midpoint(q[3]), q[3].midpoint(q[0]), q[0].midpoint(q[2]).midpoint(q[1].midpoint(q[3]))]; + points.into_iter().map(|a| (a.distance(p), a)).filter(|(d, _)| *d <= reach).min_by(|a, b| a.0.total_cmp(&b.0)).map_or(p, |(_, a)| a) +} + /// The corner of `screen` nearest `p` and its distance. fn nearest_corner(screen: &BoxState, p: Pos2) -> (usize, f32) { let p = Point::new(f64::from(p.x), f64::from(p.y)); diff --git a/crates/draw/tests/it/transform_box.rs b/crates/draw/tests/it/transform_box.rs index 4cdef9c7d..c55bb22db 100644 --- a/crates/draw/tests/it/transform_box.rs +++ b/crates/draw/tests/it/transform_box.rs @@ -1,7 +1,7 @@ //! `TransformBox`, `TransformOverlay` and `TransformTool`: the parts the pointer takes, what each drag does, the //! keys, the readout and the overlay's edits. -use craft_behavior::geometry::quad::{BoxPart, BoxRules, BoxState, ScaleRule}; +use craft_behavior::geometry::quad::{BoxPart, BoxRules, BoxState, ScaleRule, TransformMode}; use craft_behavior::geometry::{SnapKind, SnapOptions, SnapTarget, SnapTargets}; use craft_behavior::kurbo::{Point, Rect}; use craft_behavior::{CanvasView, EditEvent, RulerUnits, Ticks}; @@ -51,6 +51,8 @@ struct Config { f32_host: bool, centre_mark: bool, line: Option, + /// Snap targets beside the guides at [`SNAP_XS`]. + targets: Vec, } /// What the host keeps of the box and what the passes reported. @@ -65,6 +67,8 @@ struct Host { id: egui::Id, view: CanvasView, guide_events: usize, + /// The snap lines the last pass reported. + snapped: usize, } struct Harness { @@ -92,6 +96,7 @@ fn defaults() -> Config { f32_host: false, centre_mark: false, line: None, + targets: Vec::new(), } } @@ -117,6 +122,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness { id: egui::Id::NULL, view: CanvasView::new(egui::Rect::NOTHING, Point::ZERO, 1.0), guide_events: 0, + snapped: 0, })); let shown_host = Rc::clone(&host); let mut guides = vec![Guide::new(GuideKey::new(1), craft_behavior::Axis::Y, 300.0)]; @@ -124,6 +130,9 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness { for x in SNAP_XS { targets.add(SnapTarget::new(craft_behavior::Axis::Y, x, SnapKind::Guide)); } + for target in &shown_config.borrow().targets { + targets.add(*target); + } let snap = Snap::new(Arc::new(targets), SnapOptions::default()); let mut fixture = Fixture::new(move |ui| { let room = ui.available_rect_before_wrap(); @@ -172,6 +181,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness { if let Some((output, guide_edits)) = canvas.inner { let mut host = shown_host.borrow_mut(); host.guide_events += guide_edits; + host.snapped = output.snapped.len(); host.id = output.edit.response.id; host.applied += usize::from(output.apply.is_some()); host.discarded += usize::from(output.discard); @@ -790,3 +800,102 @@ fn a_secondary_click_on_the_box_opens_the_mode_menu() { right_click(&mut fixture, screen(Point::new(200.0, 150.0))); assert!(fixture.by_label("Distort").is_some(), "a secondary click on the box opens the mode menu"); } + +/// A drag from inside snaps the moved box's edges, not the pointer: the right edge lands on the guide at 310 while the +/// pointer stays far from both guides. +#[trial] +fn a_box_moved_from_inside_snaps_its_edges() { + let mut h = harness(BoxState::from_rect(RECT), |config| config.snap = true); + let from = h.screen(Point::new(130.0, 120.0)); + frame(&mut h, vec![pointer::moved(from)]); + frame(&mut h, vec![pointer::press(from)]); + frame(&mut h, vec![pointer::moved(from + by(4.0, 0.0))]); + frame(&mut h, vec![pointer::moved(from + by(7.0, 0.0))]); + assert_eq!(h.host.borrow().snapped, 1, "the right edge's line"); + frame(&mut h, vec![pointer::release(from + by(7.0, 0.0))]); + let moved = h.state().quad[0].x - RECT.x0; + assert!((moved - 10.0).abs() < 1e-6, "the box moved {moved}, its right edge onto 310"); +} + +/// Free Transform's corner snaps to a layer edge and a guide alike; an explicit Distort corner snaps to guides only, +/// and a corner the folding rule stops short of a guide crossing claims no line. +#[trial] +fn a_distort_corner_snaps_to_guides_only_and_claims_no_line_it_stops_short_of() { + let layer_edge = SnapTarget::new(craft_behavior::Axis::X, 230.0, SnapKind::LayerEdge); + for (mode, expected, lines) in [(TransformMode::Free, Point::new(310.0, 230.0), 2), (TransformMode::Distort, Point::new(310.0, 227.0), 1)] { + let mut h = harness(BoxState::from_rect(RECT), |config| { + config.snap = true; + config.rules = BoxRules::in_mode(mode).with_aspect_linked(false); + config.targets = vec![layer_edge]; + }); + let from = h.screen(Point::new(300.0, 220.0)); + frame(&mut h, vec![pointer::moved(from)]); + frame(&mut h, vec![pointer::press(from)]); + frame(&mut h, vec![pointer::moved(from + by(4.0, 4.0))]); + frame(&mut h, vec![pointer::moved(from + by(7.0, 7.0))]); + let corner = h.state().quad[2]; + assert!(corner.distance(expected) < 1e-6, "{mode:?}: {corner:?}, expected {expected:?}"); + assert_eq!(h.host.borrow().snapped, lines, "{mode:?}"); + } + + // The guides cross at (250, 180), past the diagonal from corner 1 to corner 3: corner 0 there would fold the box. + let mut h = harness(BoxState::from_rect(RECT), |config| { + config.snap = true; + config.rules = BoxRules::in_mode(TransformMode::Distort); + config.targets = vec![ + SnapTarget::new(craft_behavior::Axis::Y, 250.0, SnapKind::Guide), + SnapTarget::new(craft_behavior::Axis::X, 180.0, SnapKind::Guide), + ]; + }); + let from = h.screen(Point::new(100.0, 80.0)); + frame(&mut h, vec![pointer::moved(from)]); + frame(&mut h, vec![pointer::press(from)]); + frame(&mut h, vec![pointer::moved(from + by(4.0, 4.0))]); + frame(&mut h, vec![pointer::moved(from + by(147.0, 97.0))]); + let q = h.state().quad; + assert!(q[0].distance(Point::new(250.0, 180.0)) > 1.0, "the crossing would fold the box: {q:?}"); + for i in 0..4 { + let (a, b, c) = (q[i], q[(i + 1) % 4], q[(i + 2) % 4]); + assert!((b - a).cross(c - b) > 0.0, "still convex: {q:?}"); + } + assert_eq!(h.host.borrow().snapped, 0, "a stopped corner reports no guide line"); +} + +/// Off macOS Control is ⌘: on an explicit Distort corner it keeps the snapping, in Skew and Perspective it suspends it. +#[trial] +fn control_keeps_snapping_on_an_explicit_distort_corner_only() { + for (mode, snaps) in [(TransformMode::Distort, true), (TransformMode::Skew, false), (TransformMode::Perspective, false)] { + let mut h = harness(BoxState::from_rect(RECT), |config| { + config.snap = true; + config.mods = Some(Mods::CMD); + config.rules = BoxRules::in_mode(mode); + }); + h.fixture.os(Platform::Windows); + let from = h.screen(Point::new(300.0, 220.0)); + frame(&mut h, vec![pointer::moved(from)]); + frame(&mut h, vec![pointer::press(from)]); + frame(&mut h, vec![pointer::moved(from + by(4.0, 0.0))]); + frame(&mut h, vec![pointer::moved(from + by(7.0, 0.0))]); + let x = h.state().quad[2].x; + assert_eq!((x - 310.0).abs() < 1e-6, snaps, "{mode:?}: corner at x {x}"); + } +} + +/// The reference point snaps to the turned box's nine points and to nothing else, with the canvas's snapping on or +/// off: 2 units off the guide at 160 it stays put. +#[trial] +fn the_reference_point_snaps_to_the_turned_boxs_points_only() { + let turned = BoxState::from_rect(RECT).turned_about_pivot(30f64.to_radians()); + for snap in [true, false] { + let mut h = harness(turned, |config| config.snap = snap); + let centre = turned.pivot; + drag(&mut h, centre, Point::new(158.0, 150.0)); + let pivot = h.state().pivot; + assert!(pivot.distance(Point::new(158.0, 150.0)) < 1e-6, "snap {snap}: no pull to the guide: {pivot:?}"); + let corner = turned.quad[1]; + drag(&mut h, pivot, corner + kurbo_vec(-2.0, 1.0)); + let pivot = h.state().pivot; + assert!(pivot.distance(corner) < 1e-6, "snap {snap}: onto the corner: {pivot:?} vs {corner:?}"); + assert_eq!(h.state().quad, turned.quad, "the box never moved"); + } +} diff --git a/crates/ui-canvas/src/canvas/snap.rs b/crates/ui-canvas/src/canvas/snap.rs index f750e04e8..490916fe0 100644 --- a/crates/ui-canvas/src/canvas/snap.rs +++ b/crates/ui-canvas/src/canvas/snap.rs @@ -28,7 +28,7 @@ pub struct Snap { pub struct GestureSnap { /// Whether the gesture reads the Control key itself, so Control does not suspend snapping. pub reads_control: bool, - /// The kinds of target the gesture snaps to (Distort corners skip layer edges and centres). + /// The kinds of target the gesture snaps to (an explicit Distort snaps its corners to guides only). pub kinds: craft_behavior::geometry::SnapKinds, } From ebf36dc279e4ad79da688913c8864ca115650b4b Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 21:21:53 +0000 Subject: [PATCH 2/5] A Single Row or Column Marquee click inside the selection selects the line at the press snapped as a point, leaving out the selection's own edges --- crates/imaging/src/ui/select/marquee.rs | 10 ++++++++-- crates/imaging/tests/it/select.rs | 8 +++++++- crates/ui-canvas/src/canvas/tool.rs | 6 ++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/imaging/src/ui/select/marquee.rs b/crates/imaging/src/ui/select/marquee.rs index c2ea7bf2c..46bd3ef01 100644 --- a/crates/imaging/src/ui/select/marquee.rs +++ b/crates/imaging/src/ui/select/marquee.rs @@ -1,7 +1,8 @@ //! [`MarqueeTool`]: the Rectangular, Elliptical, Single Row and Single Column Marquees on the tool host. use craft_behavior::geometry::{ - ConstrainStyle, Constraint, MarqueeGesture, MarqueeRect, SelectionOperation, fixed_size_pixels, marquee_pixels, square_pixels, + ConstrainStyle, Constraint, MarqueeGesture, MarqueeRect, SelectionOperation, SnapKind, SnapKinds, fixed_size_pixels, marquee_pixels, + square_pixels, }; use craft_behavior::{CancelReason, Cursor, CursorIcon, EditEvent, PaintedCursor}; use craft_keymap::{Key, Mods}; @@ -343,7 +344,12 @@ impl CanvasTool for SelectToolPass<'_, MarqueeTool> { let unmoved = inside.clone(); let event = inside.end(raw, pointer.mods, place.zoom(), &mut |rect| moved_selection_snap(cx, rect)); let still = matches!(&event, SelectEvent::DragSelection(EditEvent::Commit { after, .. }) if after.delta == [0, 0]); - let picked = line.filter(|_| still).and_then(|line| Some((single_line(tool.kind, line.size, line.at)?, line.operation))); + // The click selects the line at the press snapped as a point, not to the selection it replaces (on a + // one-pixel line its edges would pick the line beside it). + let not_selection = SnapKinds::ALL.without(SnapKind::Selection); + let picked = line + .filter(|_| still) + .and_then(|line| Some((single_line(tool.kind, line.size, cx.snap_of(line.at, not_selection))?, line.operation))); match picked { Some((rect, operation)) => { // The press was a click: the drag ends with no change, and the click selects the line. diff --git a/crates/imaging/tests/it/select.rs b/crates/imaging/tests/it/select.rs index a27811572..31372842a 100644 --- a/crates/imaging/tests/it/select.rs +++ b/crates/imaging/tests/it/select.rs @@ -478,7 +478,8 @@ fn a_marquee_corner_and_a_dragged_selection_snap_to_a_guide() { hold(&mut rig, Modifiers::NONE); assert_eq!(tags(&rig.tool_mut().events()), ["begin", "update 58,0", "commit 58,0"], "Control suspends the box snap"); - // A click inside a selection whose edge lies within the tolerance of the guide moves nothing and calls no snap. + // A click inside a selection whose edge lies within the tolerance of the guide moves nothing and calls no snap of + // its bounds. let near = Rect::new(72.0, 30.0, 97.0, 60.0); let mut rig = snapped_rig("Marquee", MarqueeTool::new(MarqueeKind::Rectangle), Fake::selected(near)); click(&mut rig, (80.0, 45.0), NONE); @@ -488,6 +489,11 @@ fn a_marquee_corner_and_a_dragged_selection_snap_to_a_guide() { let events = rig.tool_mut().events(); assert_eq!(tags(&events[..2]), ["begin", "cancel"], "{events:?}"); assert_eq!(events[2..], [commit((0.0, 45.0, 400.0, 46.0), SelectionOperation::New)], "the click selects the row"); + // The clicked line snaps as a point: 4.5 pixels left of the guide, the column on it. + let mut rig = snapped_rig("Marquee", MarqueeTool::new(MarqueeKind::SingleColumn), Fake::selected(near)); + click(&mut rig, (95.5, 45.0), NONE); + let events = rig.tool_mut().events(); + assert_eq!(events[2..], [commit((100.0, 0.0, 101.0, 300.0), SelectionOperation::New)], "the click selects the guide's column"); } // Inside the click distance a selection drag still moves: one that snapping pulls back to its start (its edge on the diff --git a/crates/ui-canvas/src/canvas/tool.rs b/crates/ui-canvas/src/canvas/tool.rs index 542904056..7fe9fdd67 100644 --- a/crates/ui-canvas/src/canvas/tool.rs +++ b/crates/ui-canvas/src/canvas/tool.rs @@ -406,6 +406,12 @@ impl<'a> ToolContext<'a> { Point::new(self.snap_along(Axis::Y, point.x), self.snap_along(Axis::X, point.y)) } + /// [`snap`](Self::snap) to the targets of a kind in `kinds` only, with no lines: a click that selects at the point + /// leaves out the selection it replaces. + pub fn snap_of(&self, point: Point, kinds: SnapKinds) -> Point { + self.snap.map_or(point, |snap| snap.point(point, self.mods, super::GestureSnap::default().kinds(kinds)).0) + } + /// The coordinate `position` across a guide that would run along `along`, snapped as [`snap`](Self::snap) does: /// an x coordinate is across `Axis::Y`. pub fn snap_along(&self, along: Axis, position: f64) -> f64 { From b8416e7cc7120800151108bcf8639b5b1197555a Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 21:26:42 +0000 Subject: [PATCH 3/5] An open press keeps the pointer until its button comes up: a press of a held button starts nothing, and injected input drops a Down for a button it holds --- crates/behavior/src/press.rs | 39 +++++++++++++++--- .../ui-contract/src/runtime/egui/injected.rs | 7 +++- .../ui-contract/tests/it/runtime/windows.rs | 41 ++++++++++++++++++- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/crates/behavior/src/press.rs b/crates/behavior/src/press.rs index 89b1ea56e..a3a60fbac 100644 --- a/crates/behavior/src/press.rs +++ b/crates/behavior/src/press.rs @@ -578,13 +578,14 @@ impl PressMachine { released = Some((open.target, open.start, up, open.dragging, click)); self.open = None; } - // A middle or secondary press captures for a drag (a canvas pan, a drag with the secondary button) and - // never clicks or starts over an open press; a primary press does not replace one of them. A target - // that senses `MIDDLE_CLICK` also takes a middle press that can click (a tab closes on it), and one - // that senses `CONTEXT_CLICK` takes a secondary press, which a click-only target would not capture. + // An open press keeps the pointer until its button comes up: no press starts over it, of its own + // button (a repeated Down, as a window never sends one) or another. Its moves and its release still + // reach it when a modal opened over its target since. A middle or secondary press captures for a + // drag (a canvas pan, a drag with the secondary button) and never clicks. A target that senses + // `MIDDLE_CLICK` also takes a middle press that can click (a tab closes on it), and one that senses + // `CONTEXT_CLICK` takes a secondary press, which a click-only target would not capture. let capture_only = button != PointerButton::Primary; - let over_other = self.open.is_some_and(|open| capture_only || open.start.button != button); - if !matches!(button, PointerButton::Primary | PointerButton::Middle | PointerButton::Secondary) || over_other { + if !matches!(button, PointerButton::Primary | PointerButton::Middle | PointerButton::Secondary) || self.open.is_some() { continue; } self.open = pick(at) @@ -711,6 +712,32 @@ mod tests { .join(" ") } + /// A press of the held button, where nothing takes a press (a modal opened over the drag's target), leaves the open + /// press its drag, and the release ends that press from its own start. Replacing it would end the drag with no + /// release, and its owner would never see the button come up. + #[test] + fn a_press_of_the_held_button_leaves_the_open_drag_its_moves_and_release() { + let first = pos2(0.0, 0.0); + let mut machine = PressMachine::new(); + let mut step = |events: &[PointerEvent]| { + machine.step(PassInput::new(events, SLOP, first, true), |at| (at == first).then_some((1u8, Sense::DRAG))) + }; + step(&[press(0.0, 1.0)]); + step(&[PointerEvent::Moved { at: pos2(40.0, 0.0), sample: None }]); + let repeated = step(&[press(50.0, 1.2)]); + assert_eq!(repeated.target, Some(1), "the open press keeps its target"); + assert!(matches!(repeated.phase, Some(PressPhase::Drag { start, .. }) if start.origin == first), "{:?}", repeated.phase); + let moved = step(&[PointerEvent::Moved { at: pos2(60.0, 0.0), sample: None }]); + assert!(matches!(moved.phase, Some(PressPhase::Drag { pos, .. }) if pos == pos2(60.0, 0.0)), "{:?}", moved.phase); + let up = step(&[PointerEvent::released(pos2(60.0, 0.0))]); + assert!( + matches!(up.phase, Some(PressPhase::Release { start, pos, .. }) if start.origin == first && pos == pos2(60.0, 0.0)), + "{:?}", + up.phase + ); + assert!(machine.target().is_none(), "the release ends the press"); + } + #[test] fn held_reports_the_pointer_of_its_pass_within_slop() { let mut machine = PressMachine::new(); diff --git a/crates/ui-contract/src/runtime/egui/injected.rs b/crates/ui-contract/src/runtime/egui/injected.rs index 4d48680c4..f89ab3419 100644 --- a/crates/ui-contract/src/runtime/egui/injected.rs +++ b/crates/ui-contract/src/runtime/egui/injected.rs @@ -527,7 +527,8 @@ pub(crate) fn inject_release(ctx: &Context, viewport: ViewportId, at: Option Date: Mon, 16 Nov 2026 21:31:42 +0000 Subject: [PATCH 4/5] Crop tool trial: a Straighten line inside a frame snaps its ends as points, never as a frame move --- crates/draw/tests/it/crop_tool.rs | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/draw/tests/it/crop_tool.rs b/crates/draw/tests/it/crop_tool.rs index 29e990e26..142912d03 100644 --- a/crates/draw/tests/it/crop_tool.rs +++ b/crates/draw/tests/it/crop_tool.rs @@ -1,5 +1,5 @@ //! The Crop tool and its session through the public API: the pass, the keys the overlay leaves to the tool, the -//! session's box history, and one drag through the overlay on a canvas. +//! session's box history, drags through the overlay on a canvas, and the snapping of a Straighten line's ends. use craft_behavior::CanvasView; use craft_behavior::geometry::CropBox; @@ -475,6 +475,39 @@ fn a_straighten_line_fits_when_armed_turns_in_place_when_held_and_always_spends_ assert!(!host.tool.straighten_armed()); } +/// A Straighten line drawn inside a frame levels the frame on the line as drawn: its ends snap as points, never by the +/// offset that would snap the frame if the press moved it. Moved by the line's (277, 30), the frame's left edge would +/// land 3 units from the guide at x = 300. +#[trial] +fn a_straighten_line_inside_the_frame_snaps_its_ends_as_points_not_as_a_frame_move() { + use craft_behavior::geometry::{SnapKind, SnapOptions, SnapTarget, SnapTargets}; + use std::sync::Arc; + + let angle_of = |from: (f64, f64), to: (f64, f64)| { + let mut rig = Rig::new(classic()); + let mut targets = SnapTargets::new(); + targets.add(SnapTarget::new(craft_behavior::Axis::Y, 300.0, SnapKind::Guide)); + { + let mut host = rig.host.borrow_mut(); + host.snap = Some(Snap::new(Arc::new(targets), SnapOptions::default())); + let _ = host.tool.crop_to(upright(20.0, 20.0, 390.0, 290.0)); + host.tool.toggle_straighten(); + } + rig.run(); + rig.drag(from, to); + rig.frame().expect("a frame").angle + }; + let level = |from: (f64, f64), to: (f64, f64)| CropBox::level_angle(Point::new(from.0, from.1), Point::new(to.0, to.1)).expect("a line"); + + let angle = angle_of((60.0, 100.0), (337.0, 130.0)); + let drawn = level((60.0, 100.0), (337.0, 130.0)); + assert!((angle - drawn).abs() < 1e-3, "{angle} vs {drawn}: both ends clear of the guide stay where the pointer was"); + + let angle = angle_of((60.0, 100.0), (298.0, 130.0)); + let snapped = level((60.0, 100.0), (300.0, 130.0)); + assert!((angle - snapped).abs() < 1e-3, "{angle} vs {snapped}: an end within reach of the guide snaps onto it"); +} + #[trial] fn image_mode_moves_and_turns_the_view_and_classic_mode_the_frame() { // A turned view comes upright when the tool runs, in either mode. From 19f06c9322aafd5c307ee9d98de4502d314f66b1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 21:33:31 +0000 Subject: [PATCH 5/5] Transform session: Undo or Redo inside the nudge pause records the nudges as their own step first, so Redo finds nothing after them --- crates/draw/src/ui/transform/session.rs | 5 ++++- crates/draw/tests/it/transform_session.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/draw/src/ui/transform/session.rs b/crates/draw/src/ui/transform/session.rs index f97c822dc..2d0905a07 100644 --- a/crates/draw/src/ui/transform/session.rs +++ b/crates/draw/src/ui/transform/session.rs @@ -588,12 +588,15 @@ impl TransformSession { } } - /// Undo or redo one step; refused while a gesture runs. + /// Undo or redo one step; refused while a gesture runs. A change not yet at rest (a run of nudges inside the + /// nudge pause) becomes its own step first, so Undo takes it back and Redo finds nothing after it. fn step(&mut self, step: Step) -> bool { + // pc: crates/ui-egui/src/tools/transform_tool/commit.rs:441 (`step` settles before it pops). if self.dragging() { return false; } self.last_nudge = None; + self.record_rest(); let moved = self.history.step(&mut self.draft, step); self.rested = self.draft.clone(); self.changed = false; diff --git a/crates/draw/tests/it/transform_session.rs b/crates/draw/tests/it/transform_session.rs index 04826599e..75bb84c90 100644 --- a/crates/draw/tests/it/transform_session.rs +++ b/crates/draw/tests/it/transform_session.rs @@ -117,6 +117,22 @@ fn nudges_within_the_pause_fold_into_one_step() { assert_eq!(steps(&mut plain), 2); } +// Undo or Redo inside the nudge pause ends the run first: the nudges are a step of their own, which drops what was +// undone before them. +#[trial] +fn undo_or_redo_inside_the_nudge_pause_keeps_the_nudges_as_their_own_step() { + let at = |s: &TransformSession| s.quad()[0]; + let mut s = session().nudge_pause(Some(1.0)); + drag(&mut s, moved(doc(30.0, 0.0)), 0.0); + drag(&mut s, moved(doc(60.0, 0.0)), 0.1); + assert!(s.undo()); + nudge(&mut s, moved(doc(31.0, 0.0)), 0.2); + assert!(!s.redo(), "the nudge dropped the undone drag"); + assert!(near(at(&s), Point::new(31.0, 0.0)), "{:?}", at(&s)); + assert!(s.undo(), "the nudge is a step"); + assert!(near(at(&s), Point::new(30.0, 0.0)), "{:?}", at(&s)); +} + // Between a Box Begin and its Commit, undo and redo return false and change nothing. // `homography` maps the frame's corners onto the box's corners and its centre into the box; a box