From 7a0eb4c0ee877b7d65c762fcaefabab3d6aec7e4 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 21:35:23 +0000 Subject: [PATCH] TransformOverlay reach: TransformReach::MoveOnly offers only the move reach outside the frame (no handles, sides, inside, outside, keys or menu; drawn only while its drag runs), replacing the f32 move reach; the box and overlay report the part their open press holds --- crates/draw/src/ui/transform/mod.rs | 2 +- .../draw/src/ui/transform/session_overlay.rs | 34 ++++-- crates/draw/src/ui/transform/transform_box.rs | 115 +++++++++++++----- crates/draw/tests/it/transform_box.rs | 61 ++++++---- 4 files changed, 144 insertions(+), 68 deletions(-) diff --git a/crates/draw/src/ui/transform/mod.rs b/crates/draw/src/ui/transform/mod.rs index d0192e033..2d4ecbd30 100644 --- a/crates/draw/src/ui/transform/mod.rs +++ b/crates/draw/src/ui/transform/mod.rs @@ -13,5 +13,5 @@ pub use session::{ }; pub use session_bar::{BarSlot, TransformBar, TransformBarOutput, TransformBarWords}; pub use session_overlay::{TransformOverlay, TransformOverlayOutput, TransformTool}; -pub use transform_box::{ApplyBy, BOX, LineLook, RotateZone, TransformBox, TransformBoxOutput, TransformBoxStyle, TransformBoxWords}; +pub use transform_box::{ApplyBy, BOX, LineLook, RotateZone, TransformBox, TransformBoxOutput, TransformBoxStyle, TransformBoxWords, TransformReach}; pub use transform_fields::{SizeUnit, TransformField, TransformFieldWords, TransformFields, TransformFieldsOutput, TransformGroups, TransformRow}; diff --git a/crates/draw/src/ui/transform/session_overlay.rs b/crates/draw/src/ui/transform/session_overlay.rs index 6f09a55d8..5b7524e4f 100644 --- a/crates/draw/src/ui/transform/session_overlay.rs +++ b/crates/draw/src/ui/transform/session_overlay.rs @@ -17,7 +17,8 @@ use craft_ui::runtime::env::{Env, Label}; use egui::{AsIdSalt, Id, IdSalt, Rect, Ui}; use super::{ - ApplyBy, TransformBox, TransformChoice, TransformEdit, TransformMenuWords, TransformPhase, TransformSession, TransformSettings, TransformWarp, + ApplyBy, TransformBox, TransformChoice, TransformEdit, TransformMenuWords, TransformPhase, TransformReach, TransformSession, TransformSettings, + TransformWarp, }; /// What the overlay reported this pass. @@ -32,6 +33,9 @@ pub struct TransformOverlayOutput { pub discard: bool, /// The keyboard's picked part while the box has focus. pub picked: Option, + /// The part the box's open press holds, from the press to its release or cancel: a host that keeps state for a + /// press (a capture) starts it at the press, before the drag begins. + pub held: Option, /// The snap lines of the point being dragged, from the box. pub snapped: Vec, /// The value of the app's menu row that was picked. @@ -49,6 +53,8 @@ pub struct TransformOverlayOutput { /// [`discard`](TransformOverlayOutput::discard). The app answers them with [`TransformSession::commit`] and /// [`discard`](TransformSession::discard) after the edits. /// - A secondary click on the canvas opens the session's mode menu; a pick reports as the edit it stands for. +/// - [`TransformReach::MoveOnly`] offers only the move reach outside the frame: no handle, side, inside, outside, key +/// or menu, and the box is drawn only while its drag runs. /// - Warp phase: draws nothing, senses nothing and claims nothing; the app's warp widgets own the canvas. /// - Keyboard, AccessKit and right-to-left are the box's. Undo and Redo are the app's Edit commands. #[derive(craft_ui::contract::Pattern)] @@ -61,7 +67,7 @@ pub struct TransformOverlay<'a, W> { pixel_grid: bool, mods: Option, menu_rows: &'a [Entry], - move_reach: f32, + reach: TransformReach, id: IdSource, } @@ -76,7 +82,7 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> { pixel_grid: false, mods: None, menu_rows: &[], - move_reach: 0.0, + reach: TransformReach::Box, id: IdSource::Auto, } } @@ -103,12 +109,13 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> { self } - /// While a session is open, a plain press within `points` screen points outside the frame moves the box (0: off), - /// unless it hits a handle. The press is the box's own move, so it reaches [`TransformOverlayOutput::edits`] as a - /// [`TransformEdit::Box`]. + /// What of the box takes the pointer and the keys: the whole box (the default), or only the move reach outside + /// the frame ([`TransformReach::MoveOnly`]), where a plain press moves the box and reaches + /// [`TransformOverlayOutput::edits`] as a [`TransformEdit::Box`]; every other press is the host's, and the box has + /// no menu. #[must_use] - pub fn move_reach(mut self, points: f32) -> Self { - self.move_reach = points; + pub fn reach(mut self, reach: TransformReach) -> Self { + self.reach = reach; self } @@ -138,7 +145,8 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> { /// Shows the box at `rect`, the canvas body's rect (Box phase); in the Warp phase returns an empty output. pub fn show_at(self, ui: &mut Ui, rect: Rect) -> TransformOverlayOutput { let session = self.session; - let mut out = TransformOverlayOutput { edits: Vec::new(), apply: None, discard: false, picked: None, snapped: Vec::new(), menu_row: None }; + let mut out = + TransformOverlayOutput { edits: Vec::new(), apply: None, discard: false, picked: None, held: None, snapped: Vec::new(), menu_row: None }; if session.phase() == TransformPhase::Warp { return out; } @@ -150,10 +158,8 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> { .rules(rules) .reference_point(session.reference_point()) .pixel_grid(self.pixel_grid) + .reach(self.reach) .id_source(self.id); - if self.move_reach > 0.0 { - tbox = tbox.move_reach(self.move_reach); - } if let Some(mods) = self.mods { tbox = tbox.mods(mods); } @@ -162,7 +168,11 @@ impl<'a, W: TransformWarp> TransformOverlay<'a, W> { out.apply = shown.apply; out.discard = shown.discard; out.picked = shown.picked; + out.held = shown.held; out.snapped = shown.snapped; + if self.reach != TransformReach::Box { + return out; + } let words = Env::of(ui).words::(); let entries = splice_rows(session.menu(&words), self.menu_rows); if let Some(choice) = Menu::context(self.label, &shown.edit.response).pick(ui, &entries).picked { diff --git a/crates/draw/src/ui/transform/transform_box.rs b/crates/draw/src/ui/transform/transform_box.rs index df96e2ca7..3fcbc0779 100644 --- a/crates/draw/src/ui/transform/transform_box.rs +++ b/crates/draw/src/ui/transform/transform_box.rs @@ -82,6 +82,23 @@ pub enum RotateZone { Band, } +/// What of the box takes the pointer and the keys ([`TransformOverlay::reach`](super::TransformOverlay::reach)). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +#[non_exhaustive] +pub enum TransformReach { + /// The whole box: its handles, sides, inside and outside, drawn, with its keys and its menu (Free Transform). + #[default] + Box, + /// Only the move reach: a plain press within `points` screen points outside the frame moves the box. No handle, + /// side, inside or outside takes a press, the box takes no key and has no menu, and it is drawn only while its + /// drag runs. PhotoCraft's type box while typing with no modifier held: a press on the text is the editor's, one + /// further out the tool's. + MoveOnly { + /// The reach outside the frame, in screen points. + points: f32, + }, +} + /// The box drawn as a live line's controls ([`TransformBox::line`]), in document units: a square at each end of the /// segment and the round widget at its middle, with no frame. `outline`, for a line with an arrowhead, is the box /// aligned with the segment that holds the arrowheads, drawn with a square at the middle of each side. @@ -200,6 +217,7 @@ struct Report { discard: bool, hovered: Option, picked: Option, + held: Option, snapped: Vec, } @@ -313,10 +331,10 @@ pub struct TransformBox<'a> { /// that handle does; while it is dragged, the box shows as it scales. No other part is declared. #[prop] line: Option, - /// A plain press within `points` screen points outside the frame moves the box (0: off). Crate-private: the - /// public entry is `TransformOverlay::move_reach`. - #[prop(quantity = "press reach outside the frame, in screen points", vis = "pub(crate)")] - move_reach: f32, + /// What of the box takes the pointer and the keys: the whole box, or only the move reach outside the frame. + /// Crate-private: the public entry is `TransformOverlay::reach`. + #[prop(vis = "pub(crate)")] + reach: TransformReach, /// Whether this pass holds a drag of the reference point, so the hidden point is drawn while it moves. pivot_held: bool, /// Whether this pass holds a drag that began, so a live line's box shows as it scales. @@ -343,7 +361,7 @@ impl<'a> TransformBox<'a> { session_arrows: true, centre_mark: false, line: None, - move_reach: 0.0, + reach: TransformReach::Box, pivot_held: false, dragging: false, report: Report::default(), @@ -363,19 +381,31 @@ impl<'a> TransformBox<'a> { Self::new(label, state, start, 0.0).rules(rules).reference_point(false).inside_moves(false).rotate_zone(RotateZone::Band) } - /// Whether the middle of the box is a move part: the reference point is hidden, so it does not take that place. - fn declares_centre(&self) -> bool { - !self.reference_point && self.inside_moves && self.rules.allows(BoxPart::Inside) + /// The width of the move strips outside the frame when the box offers only them; `None` for the whole box. + fn move_only(&self) -> Option { + match self.reach { + TransformReach::MoveOnly { points } => Some(points), + TransformReach::Box => None, + } } - /// Whether the strips outside the frame are move parts. - fn declares_ring(&self) -> bool { - self.move_reach > 0.0 && self.rules.allows(BoxPart::Inside) + /// Whether the middle of the box is a move part: the reference point is hidden, so it does not take that place. + fn declares_centre(&self) -> bool { + self.move_only().is_none() && !self.reference_point && self.inside_moves && self.rules.allows(BoxPart::Inside) + } + + /// The width of the strips outside the frame, when they are move parts. + fn ring_reach(&self) -> Option { + self.move_only().filter(|&points| points > 0.0 && self.rules.allows(BoxPart::Inside)) } /// The parts the keyboard walks, in order. A box whose inside does not move has no inside part, so a press or drag - /// over its body reaches what lies beneath (a type editor under a paragraph frame). + /// over its body reaches what lies beneath (a type editor under a paragraph frame). A box that offers only its + /// move reach has none. fn keyboard_parts(&self) -> Vec { + if self.move_only().is_some() { + return Vec::new(); + } if let Some(line) = &self.line { return line.ends().map(|(part, _)| part).filter(|&part| self.rules.allows(part)).collect(); } @@ -531,9 +561,13 @@ impl<'a> TransformBox<'a> { } /// The parts that take a press, with the box part each stands for. Mirrors what [`parts`](Component::parts) - /// declares: the keyboard parts, a segment per allowed edge, the centre and the move strips where declared, and the - /// outside. + /// declares: the keyboard parts, a segment per allowed edge, the centre where declared, and the outside; or only the + /// move strips. fn pointer_parts(&self) -> Vec<(PartRef, BoxPart)> { + if self.move_only().is_some() { + let sides = if self.ring_reach().is_some() { 0..4 } else { 0..0 }; + return sides.map(|side| (MOVE_RING.at(side), BoxPart::Inside)).collect(); + } let mut parts: Vec<_> = self.keyboard_parts().into_iter().map(|part| (BOX.at(part.index()), part)).collect(); if self.line.is_some() { return parts; @@ -542,9 +576,6 @@ impl<'a> TransformBox<'a> { if self.declares_centre() { parts.push((CENTRE.at(0), BoxPart::Inside)); } - if self.declares_ring() { - parts.extend((0..4).map(|side| (MOVE_RING.at(side), BoxPart::Inside))); - } parts.extend((0..4).map(|corner| (OUTSIDE.at(corner), BoxPart::Outside))); parts } @@ -569,7 +600,8 @@ impl<'a> TransformBox<'a> { match press.phase { PressPhase::Down { start, .. } if start.button == PointerButton::Primary => { let from = space.view.to_doc(start.origin); - let moves_pivot = mods.alt && matches!(part, BoxPart::Inside | BoxPart::Outside); + // The move reach only moves: an Alt press there is a move too. + let moves_pivot = mods.alt && self.move_only().is_none() && matches!(part, BoxPart::Inside | BoxPart::Outside); let follows = follows_in_slop(part); let (part, begin) = if moves_pivot { (BoxPart::Pivot, BoxState::new(shown.quad, from)) } else { (part, *shown) }; held = Some(Grip { part, start: begin, from, began: false, follows, moves_pivot, pressed: *shown, last: *shown }); @@ -605,7 +637,7 @@ impl<'a> TransformBox<'a> { } PressPhase::Release { start, click, .. } => { // A double click inside applies, whether or not the first press of the release was seen. - if click && start.clicks >= 2 && start.button == PointerButton::Primary && part == BoxPart::Inside { + if click && start.clicks >= 2 && start.button == PointerButton::Primary && part == BoxPart::Inside && self.move_only().is_none() { self.report.apply = Some(ApplyBy::DoubleClick); } let Some(grip) = held.take() else { continue }; @@ -713,6 +745,18 @@ fn paint_frame(draw: &Draw<'_>, style: &TransformBoxStyle, corners: [Pos2; 4]) { draw.stroke(&outline, StrokeWidth::Medium, style.line); } +/// The move strips along the sides of the box whose corners are `quad` on the screen, each `points` wide outside it. +fn declare_ring(cx: &mut PartsCx<'_, TransformBox<'_>>, quad: &[Pos2; 4], points: f32) { + let outer = quad::outset(quad, points); + for side in 0..4usize { + let next = (side + 1) % 4; + cx.part(MOVE_RING.at(side as u64), HitShape::Polygon(vec![quad[side], quad[next], outer[next], outer[side]])) + .sense(Sense::DRAG | Sense::CLICK | slop_sense(BoxPart::Inside)) + .priority(HitPriority::new(HitLevel::Body, RING_SUB)) + .pointer_only(MOVE_BY_KEY); + } +} + /// The reference point's glyph centred at `centre`: a circle crossed by two arms, over its halo. The one glyph of the /// reference point and of the centre mark. fn paint_centre_glyph(draw: &Draw<'_>, centre: Pos2, style: &TransformBoxStyle) { @@ -802,6 +846,12 @@ impl Component for TransformBox<'_> { let style = cx.style; let screen = on_screen(&space.view, &state); let quad: [Pos2; 4] = screen.quad.map(pos); + if self.move_only().is_some() { + if let Some(points) = self.ring_reach() { + declare_ring(cx, &quad, points); + } + return; + } let mut any = false; for part in self.keyboard_parts() { let (shape, sense, priority, focus) = match part { @@ -848,16 +898,6 @@ impl Component for TransformBox<'_> { .priority(HitPriority::new(HitLevel::Handle, HANDLE_SUB)) .pointer_only(MOVE_BY_KEY); } - if self.declares_ring() { - let outer = quad::outset(&quad, self.move_reach); - for side in 0..4usize { - let next = (side + 1) % 4; - cx.part(MOVE_RING.at(side as u64), HitShape::Polygon(vec![quad[side], quad[next], outer[next], outer[side]])) - .sense(Sense::DRAG | Sense::CLICK | slop_sense(BoxPart::Inside)) - .priority(HitPriority::new(HitLevel::Body, RING_SUB)) - .pointer_only(MOVE_BY_KEY); - } - } if any { cx.roving(Roving::new(NavShape::Flat).page(Some(1))); } @@ -886,13 +926,20 @@ impl Component for TransformBox<'_> { self.run_pointer(cx, &space, &shown); self.pivot_held = cx.press_slot::().is_some_and(|grip| grip.part == BoxPart::Pivot); self.dragging = cx.press_slot::().is_some_and(|grip| grip.began); + self.report.held = cx.press_slot::().map(|grip| grip.part); let pressing = held || cx.press_slot::().is_some(); - self.run_keys(cx, &space, &shown, pressing); + if self.move_only().is_none() { + self.run_keys(cx, &space, &shown, pressing); + } } /// The frame and its handles, with the centre mark under the handles; or, for a live line not being dragged, the - /// line's controls. The reference point over both. + /// line's controls. The reference point over both. A box that offers only its move reach is drawn only while its + /// drag runs. fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { + if self.move_only().is_some() && !self.dragging { + return; + } let (Some(space), Some(state)) = (canvas_space(&cx.env), cx.value()) else { return }; let screen = on_screen(&space.view, state); let style = cx.style; @@ -937,8 +984,8 @@ impl Component for TransformBox<'_> { } fn finish(self, cx: Cx<'_, Self>) -> TransformBoxOutput { - let Report { apply, discard, hovered, picked, snapped } = self.report; - TransformBoxOutput { edit: cx.edit_response(), apply, discard, picked, hovered, snapped } + let Report { apply, discard, hovered, picked, held, snapped } = self.report; + TransformBoxOutput { edit: cx.edit_response(), apply, discard, picked, hovered, held, snapped } } } @@ -957,6 +1004,8 @@ pub struct TransformBoxOutput { pub picked: Option, /// The part a press at the pointer would take; `None` where it is the tool's. pub hovered: Option, + /// The part the open press holds, from the press to its release or cancel, before and during its drag. + pub held: Option, /// The snap lines of the point being dragged, for the host's one drawing site. pub snapped: Vec, } diff --git a/crates/draw/tests/it/transform_box.rs b/crates/draw/tests/it/transform_box.rs index c55bb22db..ac6a72fe8 100644 --- a/crates/draw/tests/it/transform_box.rs +++ b/crates/draw/tests/it/transform_box.rs @@ -5,7 +5,9 @@ use craft_behavior::geometry::quad::{BoxPart, BoxRules, BoxState, ScaleRule, Tra use craft_behavior::geometry::{SnapKind, SnapOptions, SnapTarget, SnapTargets}; use craft_behavior::kurbo::{Point, Rect}; use craft_behavior::{CanvasView, EditEvent, RulerUnits, Ticks}; -use craft_draw::ui::transform::{LineLook, RotateZone, TransformBox, TransformEdit, TransformOverlay, TransformSession, TransformSettings}; +use craft_draw::ui::transform::{ + LineLook, RotateZone, TransformBox, TransformEdit, TransformOverlay, TransformReach, TransformSession, TransformSettings, +}; use craft_keymap::{Mods, Platform}; use craft_ui::canvas::{Canvas, RulerText, Snap}; use craft_ui::canvas::{Guide, GuideKey}; @@ -483,58 +485,73 @@ fn the_whole_enlarged_overflow_handle_grabs_in_a_turned_view_and_a_short_box_kee assert_eq!(h.host.borrow().hovered, Some(BoxPart::Edge(2)), "the bottom middle handle of a short box"); } +/// An overlay that offers only its move reach (the type tool's, 38 points): a press in the strip outside the frame is +/// held from the press and its drag moves the box; a press inside the frame, beside a handle or past the reach is +/// not the box's, so nothing is held and the box stays where it is. #[trial] -fn a_press_in_the_move_reach_moves_the_box_and_a_handle_still_scales() { - // The overlay's own zone is `RotateZone::Outside`; the band's order against the ring is not reachable from here - // (`TransformBox::move_reach` is crate-private). The reach is the type tool's (38 points): a side's segment takes a - // press up to half the hit target (at most 16 points) from it, and the ring the rest of the reach. +fn a_move_only_overlay_takes_a_press_only_in_its_reach_and_moves_the_box() { let session = Rc::new(RefCell::new(TransformSession::<()>::new(RECT))); let view = Rc::new(RefCell::new(CanvasView::new(egui::Rect::NOTHING, Point::ZERO, 1.0))); - let (shown_session, shown_view) = (Rc::clone(&session), Rc::clone(&view)); + let held = Rc::new(RefCell::new(Vec::new())); + let (shown_session, shown_view, shown_held) = (Rc::clone(&session), Rc::clone(&view), Rc::clone(&held)); let settings = TransformSettings::default(); let mut fixture = Fixture::new(move |ui| { let room = ui.available_rect_before_wrap(); let at = CanvasView::new(room, Point::new(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0); *shown_view.borrow_mut() = at; - let edits = Canvas::new("Document").view(at).show(ui, |ui, body| { + let shown = Canvas::new("Document").view(at).show(ui, |ui, body| { let session = shown_session.borrow(); - TransformOverlay::new("Free Transform", &session, &settings).move_reach(38.0).show_at(ui, body.rect).edits + TransformOverlay::new("Type transform", &session, &settings).reach(TransformReach::MoveOnly { points: 38.0 }).show_at(ui, body.rect) }); + let Some(out) = shown.inner else { return }; + shown_held.borrow_mut().push(out.held); let mut session = shown_session.borrow_mut(); - for edit in edits.inner.unwrap_or_default() { + for edit in out.edits { let edit: TransformEdit<()> = edit; session.apply(edit, 0.0); } }); fixture.settle(); let screen = |p: Point| view.borrow().to_screen(p); + // Each pass's held part, from the press through the drag to the frame after the release. let mut drag = |from: Point, to: Point| { let (start, end) = (screen(from), screen(to)); fixture.frame(vec![pointer::moved(start)]); + held.borrow_mut().clear(); fixture.frame(vec![pointer::press(start)]); + let at_press = held.borrow().last().copied().flatten(); fixture.frame(vec![pointer::moved(start + (end - start) * 0.5)]); fixture.frame(vec![pointer::moved(end)]); fixture.frame(vec![pointer::release(end)]); fixture.frame(Vec::new()); + let ever = held.borrow().iter().any(Option::is_some); + (at_press, ever) }; - let size = |quad: [Point; 4]| (quad[1] - quad[0]).hypot() * (quad[3] - quad[0]).hypot(); - // 24 points outside the right side, past the side's segment and away from its handles: the box moves and keeps its - // size. + let untaken = [ + (Point::new(200.0, 150.0), "the inside"), + (Point::new(RECT.x1 - 3.0, RECT.y1 - 3.0), "just inside a corner, where a handle would be"), + (Point::new(RECT.x1 - 3.0, 150.0), "just inside a side's middle"), + (Point::new(RECT.x1 + 60.0, 150.0), "past the reach"), + (Point::new(RECT.x1 + 60.0, RECT.y1 + 60.0), "past a corner, where the box would rotate"), + ]; + for (at, what) in untaken { + let before = session.borrow().quad(); + let (at_press, ever) = drag(at, Point::new(at.x + 30.0, at.y + 20.0)); + assert!(at_press.is_none() && !ever, "{what}: the box holds no press"); + assert_eq!(session.borrow().quad(), before, "{what}: the box stays"); + } + + // 24 points outside the right side: held from the press, and the drag translates the box. + let size = |quad: [Point; 4]| (quad[1] - quad[0]).hypot() * (quad[3] - quad[0]).hypot(); let before = session.borrow().quad(); - drag(Point::new(324.0, 100.0), Point::new(354.0, 120.0)); + let (at_press, _) = drag(Point::new(RECT.x1 + 24.0, 150.0), Point::new(RECT.x1 + 54.0, 170.0)); + assert_eq!(at_press, Some(BoxPart::Inside), "the press in reach is held as a move before any movement"); let after = session.borrow().quad(); assert!( - ((after[0].x - before[0].x) - 30.0).abs() < 1e-6 && ((after[0].y - before[0].y) - 20.0).abs() < 1e-6, - "the box translates: {before:?} -> {after:?}" + after[0].x > before[0].x + 1.0 && after[0].y > before[0].y + 1.0 && (size(after) - size(before)).abs() < 1e-6, + "the box moves and keeps its size: {before:?} -> {after:?}" ); - assert!((size(after) - size(before)).abs() < 1e-6, "the move keeps the size"); - - // A corner handle inside the same reach still scales. - let corner = after[2]; - drag(corner, Point::new(corner.x + 40.0, corner.y + 28.0)); - let scaled = session.borrow().quad(); - assert!(size(scaled) > size(after) + 1.0, "a corner drag scales: {after:?} -> {scaled:?}"); } /// The snap lines of a session box drag reach the tool host through `TransformTool`, as the crop frame's do: the host