Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Snap the Marquee drag outline to whole pixels (#1587)

eric829aca94839a
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 1de604572..40cac5430 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -195,6 +195,17 @@ pub fn marquee_corners(o: &crate::state::ToolOptions, d: &Drag, end: [f64; 2]) -
([d.start[0] - hx, d.start[1] - hy], [d.start[0] + hx, d.start[1] + hy])
}
+/// 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) {
+ 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))
+}
+
/// The pixel rectangle `[x0, y0, x1, y1]` a marquee between two corners selects.
pub fn marquee_px(a: [f64; 2], b: [f64; 2]) -> [f64; 4] {
[a[0].min(b[0]).floor(), a[1].min(b[1]).floor(), a[0].max(b[0]).ceil(), a[1].max(b[1]).ceil()]
@@ -3023,9 +3034,9 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
return;
}
let last = d.points.last().map(|p| [p[0], p[1]]).unwrap_or(d.start);
- let marquee = matches!(d.tool, Tool::RectMarquee | Tool::EllipseMarquee).then(|| marquee_corners(&app.ui.tool_options, d, last));
- if let Some((a, b)) = marquee {
- draw_marquee_readout(painter.ctx(), xf.to_screen(last[0] as f32, last[1] as f32), marquee_readout(marquee_px(a, b)));
+ let marquee = marquee_preview_px(&app.ui.tool_options, d);
+ if let Some(r) = marquee {
+ draw_marquee_readout(painter.ctx(), xf.to_screen(last[0] as f32, last[1] as f32), marquee_readout(r));
}
match d.tool {
// The canvas shows the live stroke itself (`LiveStroke`); a stroke that couldn't start one
@@ -3045,7 +3056,7 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.live),
Tool::RectMarquee | Tool::EllipseMarquee | Tool::ObjectSelection => {
// Marching ants, visible on any pixels (#172).
- let (a, b) = marquee.unwrap_or((d.start, last));
+ let (a, b) = marquee.map_or((d.start, last), |[x0, y0, x1, y1]| ([x0, y0], [x1, y1]));
let r = Rect::from_two_pos(xf.to_screen(a[0] as f32, a[1] as f32), xf.to_screen(b[0] as f32, b[1] as f32));
let r = Rect::from_min_max(r.min.round() + vec2(0.5, 0.5), r.max.round() + vec2(0.5, 0.5));
let pts = if d.tool == Tool::EllipseMarquee {
diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rs
index 0a0c4a003..ae5b754be 100644
--- a/crates/ui-egui/src/marquee_tests.rs
+++ b/crates/ui-egui/src/marquee_tests.rs
@@ -134,6 +134,25 @@ fn alt_draws_from_the_centre_and_shift_alt_a_centred_circle() {
assert!(r.x0.abs_diff(170) <= 1 && r.x1.abs_diff(230) <= 1 && r.y0.abs_diff(120) <= 1 && r.y1.abs_diff(180) <= 1, "{r:?}");
}
+#[test]
+fn the_live_outline_snaps_to_pixels_and_matches_the_committed_selection() {
+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {
+ let mut h = harness(tool);
+ // Zoomed in, the pointer lands between pixel edges.
+ let v = &mut h.state_mut().ui.views[0];
+ v.zoom = 8.0;
+ v.center = [20.0, 15.0];
+ h.run_steps(2);
+ 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();
+ 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");
+ }
+}
+
#[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.