Skip to content

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

Marquee: Fixed Size selects exactly the entered width and height (#6015)

itsF4LCON4f3de938520b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 7b05bffce..e2332755d 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -244,14 +244,16 @@ pub fn marquee_corners(o: &crate::state::ToolOptions, d: &Drag, end: [f64; 2]) -
/// 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).
+/// deselects. A one-pixel row or column is a selection (#2391). With the Fixed Size style a
+/// click places the selection, as in Photoshop.
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) {
+ let fixed = o.marquee_style == "fixedSize";
+ if !matches!(d.tool, Tool::RectMarquee | Tool::EllipseMarquee) || (!fixed && 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);
- let mut r = marquee_px(a, b);
+ let mut r = if fixed { fixed_size_px(a, b, o.marquee_width, o.marquee_height) } else { marquee_px(a, b) };
if marquee_is_square(o, d) {
r = square_px(r, d.start, last);
}
@@ -263,6 +265,16 @@ 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()]
}
+/// The pixel rectangle of a Fixed Size marquee between the corners `a` and `b`: exactly `w` × `h`
+/// whole pixels. Snapping both edges outwards (`marquee_px`) added a pixel to each side whenever
+/// the press point sat between pixels, so 10 × 10 selected 11 × 11 (#6015). The near edge goes
+/// to the closest pixel edge and the far one is the size away.
+pub fn fixed_size_px(a: [f64; 2], b: [f64; 2], w: f32, h: f32) -> [f64; 4] {
+ let (w, h) = (f64::from(w).round().max(1.0), f64::from(h).round().max(1.0));
+ let (x0, y0) = (a[0].min(b[0]).round(), a[1].min(b[1]).round());
+ [x0, y0, x0 + w, y0 + h]
+}
+
/// Whether the marquee drag `d` is constrained to a square (or circle): ⇧ during the drag with
/// the Normal style, or a Fixed Ratio of 1:1. Mirrors the branches of `chrome_ui::marquee_end`.
fn marquee_is_square(o: &crate::state::ToolOptions, d: &Drag) -> bool {
@@ -6324,6 +6336,19 @@ mod tests {
assert_eq!(square_px([10.0, 20.0, 30.0, 40.0], [10.0, 20.0], [30.0, 40.0]), [10.0, 20.0, 30.0, 40.0]);
}
+ /// #6015: a Fixed Size marquee pressed between pixels selected one pixel more on each side.
+ #[test]
+ fn fixed_size_px_is_exactly_the_size() {
+ use super::fixed_size_px;
+ // Down-right from (10.4, 20.6): 10 × 10 from the closest pixel edges.
+ assert_eq!(fixed_size_px([10.4, 20.6], [20.4, 30.6], 10.0, 10.0), [10.0, 21.0, 20.0, 31.0]);
+ // Up-left: the far corner is the low one; still exactly the size.
+ assert_eq!(fixed_size_px([10.4, 20.6], [-53.6, -11.4], 64.0, 32.0), [-54.0, -11.0, 10.0, 21.0]);
+ // 1 × 1 is one pixel, and a fractional size rounds to whole pixels.
+ assert_eq!(fixed_size_px([3.5, 3.5], [4.5, 4.5], 1.0, 1.0), [4.0, 4.0, 5.0, 5.0]);
+ assert_eq!(fixed_size_px([0.0, 0.0], [2.6, 2.4], 2.6, 2.4), [0.0, 0.0, 3.0, 2.0]);
+ }
+
#[test]
fn display_image_matches_egui_unmultiplied_conversion() {
// Several parallel pieces and a ragged one; translucent, out-of-range and NaN values.
diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rs
index 321a31e73..400353d32 100644
--- a/crates/ui-egui/src/marquee_tests.rs
+++ b/crates/ui-egui/src/marquee_tests.rs
@@ -164,6 +164,35 @@ fn shift_square_pressed_between_pixels_has_equal_sides() {
}
}
+/// #6015: Fixed Size pressed between pixels selected one pixel more on each side (10 × 10 became
+/// 11 × 11, 1 × 1 became 2 × 2). The readout and the selection are the entered size, and a click
+/// without a drag places it too, as in Photoshop.
+#[test]
+fn fixed_size_selects_exactly_the_entered_size() {
+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {
+ for (w, ht) in [(10, 10), (1, 1), (64, 32)] {
+ let mut h = harness(tool);
+ let o = &mut h.state_mut().ui.tool_options;
+ o.marquee_style = "fixedSize".into();
+ (o.marquee_width, o.marquee_height) = (w as f32, ht as f32);
+ press_at(&mut h, 50.4, 60.6, Modifiers::NONE);
+ assert!(readout(&h, w, ht), "{tool:?} {w} × {ht}: the readout shows the entered size");
+ release_at(&mut h, 90.0, 90.0, Modifiers::NONE);
+ assert_eq!(selection(&h), Rect::new(50, 61, 50 + w, 61 + ht), "{tool:?} {w} × {ht}: dragged");
+
+ // A click: the button goes down and up at one point.
+ h.state_mut().run("select.deselect", json!({})).unwrap();
+ let p = screen(&h, 120.6, 100.4);
+ h.event(egui::Event::PointerMoved(p));
+ h.run_steps(1);
+ button(&mut h, p, true, Modifiers::NONE);
+ button(&mut h, p, false, Modifiers::NONE);
+ h.run_steps(2);
+ assert_eq!(selection(&h), Rect::new(121, 100, 121 + w, 100 + ht), "{tool:?} {w} × {ht}: clicked");
+ }
+ }
+}
+
/// #204: Preferences › Tools › Show Transformation Values off hides the readout, not the marquee.
#[test]
fn show_transformation_values_off_hides_the_readout() {