From ea9a7c931f2021dd86974145a5696f2bea54fdcb Mon Sep 17 00:00:00 2001 From: Hamza Date: Thu, 5 Nov 2026 16:39:06 +0500 Subject: [PATCH] Marquee: with no selection, Shift and Alt held from the press constrain the shape (#11389) A Shift or Alt held at the press of a Rectangular or Elliptical Marquee drag picked the selection mode, and did not square or centre until it was released and pressed again, also when nothing was selected, where it picks nothing. The drag now records whether the document had a selection at the press (an empty one counts as none), and only then do held keys pick the mode. Without one, Shift held from the press squares to the drag's shorter side, as measured; Shift pressed during the drag keeps the longer side. --- crates/ui-egui/src/canvas/drag.rs | 36 +++- crates/ui-egui/src/canvas/tool_input.rs | 1 + crates/ui-egui/src/lib.rs | 2 + .../ui-egui/src/marquee_press_mods_tests.rs | 163 ++++++++++++++++++ 4 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 crates/ui-egui/src/marquee_press_mods_tests.rs diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index ac3da2eacb..24a20234e6 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -34,6 +34,10 @@ pub struct Drag { /// Freeform Pen and Path Selection gestures belong to their source document and target. pub(super) vector_owner: Option, pub(super) pen_owner: Option, + /// The document had a selection (one with content) when the drag was pressed. A modifier held + /// at the press picks the selection mode only then; without one it constrains the shape from + /// the first move (#11389). `true` until recorded, which keeps that rule for drags that don't. + pub selection_at_press: bool, } impl Drag { @@ -54,9 +58,16 @@ impl Drag { lasso: None, vector_owner: None, pen_owner: None, + selection_at_press: true, } } + /// Record whether the document has a selection at the press; an empty one counts as none, as + /// `tool_feedback::document_selection_mode` counts it (#11389). + pub(crate) fn capture_selection_at_press(&mut self, app: &PhotocraftApp) { + self.selection_at_press = app.session.active().and_then(|st| st.doc.selection.as_ref()).is_some_and(|s| !s.content_bounds().is_empty()); + } + /// Vector drags capture their owner at the press, the Freeform Pen also its destination, before /// later ⌥-clicks or a temporary Direct Selection can finish them. pub(crate) fn capture_vector_owners(&mut self, app: &PhotocraftApp) { @@ -92,13 +103,22 @@ impl Drag { self.released.alt |= !mods.alt; } - /// ⇧ (square / circle) and ⌥ (from the centre) for a marquee drag. A modifier held at the press - /// picks the selection mode instead (add / subtract, #188) until it is released and pressed - /// again, as in Photoshop. + /// ⇧ (square / circle) and ⌥ (from the centre) for a marquee drag. With a selection at the + /// press, a modifier held at the press picks the selection mode instead (add / subtract, #188) + /// until it is released and pressed again, as in Photoshop; without one it constrains the + /// shape from the first move (#11389). pub fn marquee_mods(&self) -> (bool, bool) { - let fresh = |now: bool, at_press: bool, released: bool| now && (!at_press || released); + let picks_mode = self.selection_at_press; + let fresh = |now: bool, at_press: bool, released: bool| now && (!at_press || released || !picks_mode); (fresh(self.live.shift, self.modifiers.shift, self.released.shift), fresh(self.live.alt, self.modifiers.alt, self.released.alt)) } + + /// ⇧ held since the press with no selection: the square takes the drag's shorter side, as the + /// reference application does (a 200 × 100 drag selects 100 × 100, #11389). ⇧ pressed during + /// the drag keeps the longer side (not measured, so as before). + fn square_from_press(&self) -> bool { + !self.selection_at_press && self.modifiers.shift && !self.released.shift && self.live.shift + } } /// The marquee dragged from `d.start` to `end` as two document-space corners: the options-bar @@ -113,7 +133,13 @@ pub fn marquee_corners(o: &crate::state::ToolOptions, d: &Drag, end: [f64; 2]) - let a = if alt { [end[0] - w / 2.0, end[1] - h / 2.0] } else { end }; return (a, [a[0] + w, a[1] + h]); } - let e = crate::chrome_ui::marquee_end(&o.marquee_style, o.marquee_width as f64, o.marquee_height as f64, shift, d.start, end); + let e = if shift && d.square_from_press() && !matches!(o.marquee_style.as_str(), "fixedRatio" | "fixedSize") { + let (dx, dy) = (end[0] - d.start[0], end[1] - d.start[1]); + let m = dx.abs().min(dy.abs()); + [d.start[0] + m.copysign(dx), d.start[1] + m.copysign(dy)] + } else { + crate::chrome_ui::marquee_end(&o.marquee_style, o.marquee_width as f64, o.marquee_height as f64, shift, d.start, end) + }; if !alt { return (d.start, e); } diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 671b507723..e832b6b4b9 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -509,6 +509,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) let tool = crate::retouch_ui::alt_flipped(tool, mods.alt); let mut drag = Drag { clear, ..Drag::new(tool, from.map_or([x, y], |p| [p[0], p[1]]), points, mods, erase) }; drag.capture_vector_owners(app); + drag.capture_selection_at_press(app); app.drag = Some(drag); app.trail = None; app.stylus.begin_stroke(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 05039b303b..531e335099 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -869,6 +869,8 @@ mod field_wheel_app_tests; #[cfg(test)] mod dropdown_press_tests; +#[cfg(test)] +mod marquee_press_mods_tests; #[cfg(test)] mod marquee_tests; diff --git a/crates/ui-egui/src/marquee_press_mods_tests.rs b/crates/ui-egui/src/marquee_press_mods_tests.rs new file mode 100644 index 0000000000..235dc2dfe4 --- /dev/null +++ b/crates/ui-egui/src/marquee_press_mods_tests.rs @@ -0,0 +1,163 @@ +//! A Shift or Alt held from the start of a Rectangular or Elliptical Marquee drag picks the +//! selection mode only when the document already has a selection (#11389). Without one the same +//! keys constrain the shape from the first move, as in the reference application: Shift squares to +//! the drag's shorter side, Alt draws from the centre. + +use egui::{Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ViewXform; +use crate::state::Tool; + +fn harness(tool: Tool) -> Harness<'static, PhotocraftApp> { + harness_with_step_dt(tool, 0.25) +} + +fn harness_with_step_dt(tool: Tool, step_dt: f32) -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = false; + app.ui.tool = tool; + let mut h = Harness::builder().with_size(vec2(1100.0, 800.0)).with_step_dt(step_dt).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::shortcuts::handle(app, &ctx); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + // 100 %: one document pixel per point, so drags land on whole pixels. + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.0; + v.center = [400.0, 300.0]; + v.fit_pending = false; + h.run_steps(2); + h +} + +fn view_xform(h: &Harness<'static, PhotocraftApp>) -> ViewXform { + let app = h.state(); + let v = &app.ui.views[0]; + ViewXform { + rect: crate::rulers::content_rect(app, app.last_canvas_rect), + zoom: v.zoom, + center: v.center, + flip: app.ui.view.flip_horizontal, + rotation: v.rotation, + aspect: app.ui.view.display_aspect(), + } +} + +fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 { + view_xform(h).to_screen(x, y) +} + +fn mods(h: &mut Harness<'static, PhotocraftApp>, m: Modifiers) { + h.event(egui::Event::ModifiersChanged(m)); + h.run_steps(1); +} + +fn button(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, down: bool, m: Modifiers) { + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: down, modifiers: m }); + h.run_steps(1); +} + +/// Move the pointer to document point `(x, y)` in a few steps. +fn move_to(h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32) { + let p = screen(h, x, y); + h.event(egui::Event::PointerMoved(p)); + h.run_steps(2); +} + +fn press_at(h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32, m: Modifiers) { + let p = screen(h, x, y); + h.event(egui::Event::PointerMoved(p)); + h.run_steps(1); + button(h, p, true, m); + // Past egui's click distance, so the drag starts at the press point. + move_to(h, x + 8.0, y + 6.0); +} + +fn release_at(h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32, m: Modifiers) { + move_to(h, x, y); + button(h, screen(h, x, y), false, m); + h.run_steps(2); +} + +fn selection(h: &Harness<'static, PhotocraftApp>) -> Rect { + h.state().session.active().unwrap().doc.selection.as_ref().map_or(Rect::EMPTY, |s| s.content_bounds()) +} + +/// The two-row readout shows `W: {w} px` and `H: {ht} px` (Photoshop's format). +fn readout(h: &Harness<'static, PhotocraftApp>, w: i32, ht: i32) -> bool { + let labels = h.query_by_label("W:").is_some() && h.query_by_label("H:").is_some(); + let count = |v: i32| h.query_all_by_label(&format!("{v} px")).count(); + let values = if w == ht { count(w) == 2 } else { count(w) == 1 && count(ht) == 1 }; + labels && values && h.query_all_by_label_contains(" px").count() == 2 +} + +/// Drag a marquee from `from` to `to` with `m` held from before the press to after the release. +fn drag_held(h: &mut Harness<'static, PhotocraftApp>, m: Modifiers, from: [f32; 2], to: [f32; 2]) { + mods(h, m); + press_at(h, from[0], from[1], m); + move_to(h, to[0], to[1]); + release_at(h, to[0], to[1], m); + mods(h, Modifiers::NONE); +} + +#[test] +fn without_a_selection_keys_held_from_the_press_constrain_the_shape() { + let shift_alt = Modifiers { shift: true, alt: true, ..Modifiers::NONE }; + let cases = [ + // ⇧ from the press: a 200 × 100 drag selects a 100 × 100 square (the shorter side). + (Modifiers::SHIFT, [100.0, 100.0], [300.0, 200.0], Rect::new(100, 100, 200, 200)), + // ⌥ from the press: drawn from the centre. + (Modifiers::ALT, [400.0, 300.0], [500.0, 350.0], Rect::new(300, 250, 500, 350)), + // Both: a square from the centre, half extent the shorter drag. + (shift_alt, [400.0, 300.0], [500.0, 350.0], Rect::new(350, 250, 450, 350)), + ]; + for tool in [Tool::RectMarquee, Tool::EllipseMarquee] { + for (m, from, to, want) in cases { + let mut h = harness(tool); + h.state_mut().ui.extras.snap = false; + drag_held(&mut h, m, from, to); + assert_eq!(selection(&h), want, "{tool:?} with {m:?} held from the press"); + } + } +} + +#[test] +fn the_live_readout_shows_the_constrained_shape_from_the_first_move() { + let mut h = harness(Tool::RectMarquee); + h.state_mut().ui.extras.snap = false; + mods(&mut h, Modifiers::SHIFT); + press_at(&mut h, 100.0, 100.0, Modifiers::SHIFT); + move_to(&mut h, 300.0, 200.0); + assert!(readout(&h, 100, 100), "the readout agrees with what the release selects"); + release_at(&mut h, 300.0, 200.0, Modifiers::SHIFT); + mods(&mut h, Modifiers::NONE); + assert_eq!(selection(&h), Rect::new(100, 100, 200, 200)); +} + +#[test] +fn with_a_selection_keys_held_from_the_press_still_pick_the_mode() { + let mut h = harness(Tool::RectMarquee); + h.state_mut().ui.extras.snap = false; + h.state_mut().run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 200})).unwrap(); + // ⇧ adds, and the added rectangle is as drawn, not squared: the union reaches 600. + drag_held(&mut h, Modifiers::SHIFT, [400.0, 100.0], [600.0, 150.0]); + assert_eq!(selection(&h), Rect::new(100, 100, 600, 300)); + let st = h.state().session.active().unwrap(); + let sel = st.doc.selection.as_ref().unwrap(); + assert!(sel.sample_channel(150, 150, 0) > 0.99 && sel.sample_channel(550, 125, 0) > 0.99 && sel.sample_channel(550, 250, 0) < 0.01); +}