PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Marquee: keep one-pixel rows and columns on release (#2391) (#2562)
Valentin Klein4d7e5c1ef07b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex f11a6cb41..3889385ae 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -198,14 +198,18 @@ pub fn marquee_corners(o: &crate::state::ToolOptions, d: &Drag, end: [f64; 2]) -}/// The pixel rectangle a Rectangular/Elliptical Marquee drag selects so far: shown live (ants-/// and readout) exactly as the release commits it, on whole pixel edges as in Photoshop.-pub(crate) fn marquee_preview_px(o: &crate::state::ToolOptions, d: &Drag) -> Option<[f64; 4]> {- if !matches!(d.tool, Tool::RectMarquee | Tool::EllipseMarquee) {+/// and readout) exactly as the release commits it, on whole pixel edges as in Photoshop. `None`+/// while the gesture is still a click (it never left the press point by more than a few screen+/// pixels at `zoom`, points per document pixel) or the rectangle has no area: a release then+/// deselects. A one-pixel row or column is a selection (#2391).+pub(crate) fn marquee_preview_px(o: &crate::state::ToolOptions, d: &Drag, zoom: f32) -> Option<[f64; 4]> {+ if !matches!(d.tool, Tool::RectMarquee | Tool::EllipseMarquee) || crate::shape_dialog::is_click(d.start, &d.points, zoom) {return None;}let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]);let (a, b) = marquee_corners(o, d, last);- Some(marquee_px(a, b))+ let r = marquee_px(a, b);+ (r[2] > r[0] && r[3] > r[1]).then_some(r)}/// The pixel rectangle `[x0, y0, x1, y1]` a marquee between two corners selects.@@ -3452,7 +3456,7 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &Viewreturn;}let last = d.points.last().map(|p| [p[0], p[1]]).unwrap_or(d.start);- let marquee = marquee_preview_px(&app.ui.tool_options, d);+ let marquee = marquee_preview_px(&app.ui.tool_options, d, app.point_zoom());if let Some(r) = marquee {draw_marquee_readout(painter.ctx(), xf.to_screen(last[0] as f32, last[1] as f32), marquee_readout(r));}@@ -3477,6 +3481,8 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &Viewtrail.draw(painter, doc_rect, xf.flip, col);}t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.live),+ // Nothing selected yet: no outline, as the release would deselect.+ Tool::RectMarquee | Tool::EllipseMarquee if marquee.is_none() => {}Tool::RectMarquee | Tool::EllipseMarquee | Tool::ObjectSelection => {// Marching ants, visible on any pixels (#172).let (a, b) = marquee.map_or((d.start, last), |[x0, y0, x1, y1]| ([x0, y0], [x1, y1]));@@ -4143,14 +4149,13 @@ pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) {}}Tool::RectMarquee | Tool::EllipseMarquee => {- let (a, b) = marquee_corners(&app.ui.tool_options, &d, [end[0], end[1]]);- let [x0, y0, x1, y1] = marquee_px(a, b);- if x1 - x0 < 2.0 || y1 - y0 < 2.0 {+ // Exactly what the drag showed (`marquee_preview_px`); a click deselects.+ let Some([x0, y0, x1, y1]) = marquee_preview_px(&app.ui.tool_options, &d, app.point_zoom()) else {if app.session.is_enabled("select.deselect") {let _ = app.run("select.deselect", json!({}));}return;- }+ };let mode = selection_mode(app, d.modifiers);let (aa, feather) = (app.ui.tool_options.anti_alias, app.ui.tool_options.feather);let _ = app.run("select.rect", json!({"x": x0, "y": y0, "width": x1 - x0, "height": y1 - y0, "mode": mode, "ellipse": d.tool == Tool::EllipseMarquee, "antiAlias": aa, "feather": feather}));diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rsindex 052702a12..22c8f118e 100644--- a/crates/ui-egui/src/marquee_tests.rs+++ b/crates/ui-egui/src/marquee_tests.rs@@ -177,13 +177,71 @@ fn the_live_outline_snaps_to_pixels_and_matches_the_committed_selection() {press_at(&mut h, 10.3, 10.3, Modifiers::NONE);move_to(&mut h, 20.6, 15.4);let app = h.state();- let live = app.drag.as_ref().and_then(|d| crate::canvas::marquee_preview_px(&app.ui.tool_options, d)).unwrap();+ let live = app.drag.as_ref().and_then(|d| crate::canvas::marquee_preview_px(&app.ui.tool_options, d, app.point_zoom())).unwrap();assert_eq!(live, [10.0, 10.0, 21.0, 16.0], "{tool:?}: whole pixels while dragging");release_at(&mut h, 20.6, 15.4, Modifiers::NONE);assert_eq!(selection(&h), Rect::new(10, 10, 21, 16), "{tool:?}: the commit is what was shown");}}+/// 800 %, snapping off, the view centred on (30, 30): the pointer lands between pixel edges.+fn zoomed(tool: Tool) -> Harness<'static, PhotocraftApp> {+ let mut h = harness(tool);+ h.state_mut().ui.extras.snap = false;+ let v = &mut h.state_mut().ui.views[0];+ v.zoom = 8.0;+ v.center = [30.0, 30.0];+ h.run_steps(2);+ h+}++/// Press at `from` and move to `to` in six ordinary pointer moves, the button still held.+fn drag_to(h: &mut Harness<'static, PhotocraftApp>, from: [f32; 2], to: [f32; 2]) {+ move_to(h, from[0], from[1]);+ button(h, screen(h, from[0], from[1]), true, Modifiers::NONE);+ for i in 1..=6 {+ let t = i as f32 / 6.0;+ move_to(h, from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t);+ }+}++/// #2391: a one-pixel column or row shown while dragging (ants and readout) is the selection the+/// release keeps, also when the drag starts outside a selection it replaces (Photoshop). A click+/// without a drag still deselects.+#[test]+fn a_one_pixel_column_or_row_is_kept_on_release() {+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {+ for (name, end, want) in [("column", [10.6, 50.4], Rect::new(10, 10, 11, 51)), ("row", [50.6, 10.8], Rect::new(10, 10, 51, 11))] {+ for existing in [false, true] {+ let mut h = zoomed(tool);+ if existing {+ h.state_mut().run("select.rect", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap();+ }+ drag_to(&mut h, [10.2, 10.4], end);+ let app = h.state();+ let live = app.drag.as_ref().and_then(|d| crate::canvas::marquee_preview_px(&app.ui.tool_options, d, app.point_zoom()));+ let shown = [want.x0, want.y0, want.x1, want.y1].map(f64::from);+ assert_eq!(live, Some(shown), "{tool:?} {name} (existing {existing}): the outline while dragging");+ assert!(readout(&h, want.width() as i32, want.height() as i32), "{tool:?} {name}: W/H readout");+ let p = screen(&h, end[0], end[1]);+ button(&mut h, p, false, Modifiers::NONE);+ h.run_steps(2);+ assert_eq!(selection(&h), want, "{tool:?} {name} (existing {existing}): the release keeps what was shown");+ assert!(h.query_by_label("W:").is_none(), "the readout goes away with the drag");+ }+ }+ // A plain click outside the selection deselects.+ let mut h = zoomed(tool);+ h.state_mut().run("select.rect", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap();+ move_to(&mut h, 10.2, 10.4);+ let p = screen(&h, 10.2, 10.4);+ button(&mut h, p, true, Modifiers::NONE);+ button(&mut h, p, false, Modifiers::NONE);+ h.run_steps(2);+ assert!(h.state().session.active().unwrap().doc.selection.is_none(), "{tool:?}: a click deselects");+ }+}+#[test]fn modifiers_held_before_the_drag_pick_the_mode_not_the_shape() {// #188: ⇧ held at the press adds (no square); released and pressed again it constrains.