From f5931605373d9cd0ab3ec9d290cf9959fcfb03ea Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 3 Nov 2026 16:25:13 +0500 Subject: [PATCH] Crop: a press that hasn't moved is not a change, Show Cropped Area off keeps its fill, and the dashed outline is cut to the view --- crates/ui-egui/src/canvas/paint.rs | 80 +++++++++++++++++++++- crates/ui-egui/src/tools/crop_overlay.rs | 17 ++++- crates/ui-egui/src/tools/crop_shield.rs | 6 +- crates/ui-egui/tests/it/crop_frame_look.rs | 21 ++++++ 4 files changed, 117 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/canvas/paint.rs b/crates/ui-egui/src/canvas/paint.rs index ce91a609f4..a817bcfae6 100644 --- a/crates/ui-egui/src/canvas/paint.rs +++ b/crates/ui-egui/src/canvas/paint.rs @@ -280,9 +280,16 @@ fn crop_frame_outline(painter: &egui::Painter, q: [Pos2; 4], look: crate::crop_o FrameLook::Untouched => { // Dashed like a selection outline: 4 px black, then 4 px light grey. Static rather // than marching, so an untouched frame doesn't keep the canvas repainting. - let ring: Vec = q.iter().copied().chain(q.first().copied()).collect(); - painter.add(egui::Shape::line(ring.clone(), Stroke::new(1.0, Color32::from_gray(210)))); - painter.extend(egui::Shape::dashed_line(&ring, Stroke::new(1.0, Color32::BLACK), 4.0, 4.0)); + // Each edge is cut to the visible area first: zoomed far in, a frame's edges run + // thousands of points off screen, and dashing them all would make a shape per dash. + let view = painter.clip_rect().expand(4.0); + for i in 0..4 { + let (a, b) = (q[i], q[(i + 1) % 4]); + if let Some([a, b]) = clip_segment(a, b, view) { + painter.line_segment([a, b], Stroke::new(1.0, Color32::from_gray(210))); + painter.extend(egui::Shape::dashed_line(&[a, b], Stroke::new(1.0, Color32::BLACK), 4.0, 4.0)); + } + } grey } FrameLook::Started => { @@ -292,6 +299,30 @@ fn crop_frame_outline(painter: &egui::Painter, q: [Pos2; 4], look: crate::crop_o } } +/// The part of the segment `a`–`b` inside `r` (Liang–Barsky), or None when none of it is. +fn clip_segment(a: Pos2, b: Pos2, r: Rect) -> Option<[Pos2; 2]> { + let d = b - a; + let (mut t0, mut t1) = (0.0f32, 1.0f32); + for (p, q) in [(-d.x, a.x - r.min.x), (d.x, r.max.x - a.x), (-d.y, a.y - r.min.y), (d.y, r.max.y - a.y)] { + if p == 0.0 { + if q < 0.0 { + return None; + } + continue; + } + let t = q / p; + if p < 0.0 { + t0 = t0.max(t); + } else { + t1 = t1.min(t); + } + if t0 > t1 { + return None; + } + } + (t0.is_finite() && t1.is_finite()).then(|| [a + d * t0, a + d * t1]) +} + /// The crop overlay's guides, each unit-square point placed on screen by `at`. fn draw_crop_guides(painter: &egui::Painter, guides: &[crate::crop_overlay::Polyline], at: impl Fn(f32, f32) -> Pos2) { let thin = Stroke::new(1.0, Color32::from_white_alpha(90)); @@ -661,3 +692,46 @@ pub(super) fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter _ => {} } } + +#[cfg(test)] +mod clip_segment_tests { + use super::{Pos2, Rect, clip_segment, pos2}; + + fn view() -> Rect { + Rect::from_min_max(pos2(0.0, 0.0), pos2(100.0, 50.0)) + } + + /// Within a hundredth of a point, far below a device pixel: f32 on edges tens of thousands of + /// points long can't do better, and the screen doesn't need it to. + fn near(a: Pos2, b: Pos2) -> bool { + (a - b).length() < 1e-2 + } + + #[test] + fn a_segment_inside_is_kept_whole() { + let [a, b] = clip_segment(pos2(10.0, 10.0), pos2(90.0, 40.0), view()).unwrap(); + assert!(near(a, pos2(10.0, 10.0)) && near(b, pos2(90.0, 40.0))); + } + + #[test] + fn a_segment_running_far_off_screen_is_cut_to_the_view() { + // A frame edge at 3200 % runs tens of thousands of points past the view. + let [a, b] = clip_segment(pos2(-40_000.0, 20.0), pos2(40_000.0, 20.0), view()).unwrap(); + assert!(near(a, pos2(0.0, 20.0)) && near(b, pos2(100.0, 20.0)), "{a:?} {b:?}"); + let [a, b] = clip_segment(pos2(50.0, -1000.0), pos2(50.0, 1000.0), view()).unwrap(); + assert!(near(a, pos2(50.0, 0.0)) && near(b, pos2(50.0, 50.0))); + } + + #[test] + fn a_slanted_segment_through_a_corner_is_cut_on_both_sides() { + let [a, b] = clip_segment(pos2(-10.0, -10.0), pos2(60.0, 60.0), view()).unwrap(); + assert!(near(a, pos2(0.0, 0.0)) && near(b, pos2(50.0, 50.0)), "{a:?} {b:?}"); + } + + #[test] + fn a_segment_outside_the_view_draws_nothing() { + assert!(clip_segment(pos2(-50.0, 10.0), pos2(-5.0, 40.0), view()).is_none(), "left of it"); + assert!(clip_segment(pos2(10.0, 60.0), pos2(90.0, 90.0), view()).is_none(), "below it"); + assert!(clip_segment(pos2(-10.0, 70.0), pos2(70.0, 200.0), view()).is_none(), "past a corner"); + } +} diff --git a/crates/ui-egui/src/tools/crop_overlay.rs b/crates/ui-egui/src/tools/crop_overlay.rs index d5a862baa6..a1b0e67d77 100644 --- a/crates/ui-egui/src/tools/crop_overlay.rs +++ b/crates/ui-egui/src/tools/crop_overlay.rs @@ -246,12 +246,25 @@ pub fn current_guides(app: &PhotocraftApp, w: f32, h: f32) -> Vec { } /// Whether the crop has started: its frame was changed since it was made (it is no longer the -/// untouched default frame), or is being changed now. Until then the reference application shows +/// untouched default frame), or a drag is changing it now. Until then the reference application shows /// only the frame, with no shield and no overlay in any show mode; both start with the first change /// and stay until the crop is committed or cancelled (#10930). Classic Mode wasn't measured and /// keeps the behaviour it had. pub fn crop_started(app: &PhotocraftApp) -> bool { - app.ui.tool_options.crop_shield.classic_mode || app.crop.drag.is_some() || !app.crop.default_frame + app.ui.tool_options.crop_shield.classic_mode || !app.crop.default_frame || drag_changed(app) +} + +/// Whether the drag in progress has changed the frame since its press. A press that hasn't moved +/// anything yet is not the first change, so it leaves the untouched look (#10930). The tests mirror +/// the ones `crop_ui` uses when a drag ends. +fn drag_changed(app: &PhotocraftApp) -> bool { + use crate::crop_ui::CropDrag; + match &app.crop.drag { + None => false, + Some(CropDrag::Draw { anchor, cur, .. }) => anchor != cur, + Some(CropDrag::Move { rect, .. } | CropDrag::Resize { rect, .. } | CropDrag::MoveImage { rect, .. }) => app.ui.crop_rect != Some(*rect), + Some(CropDrag::Rotate { from, .. } | CropDrag::TurnImage { from, .. }) => crate::crop_ui::angle(app) != *from, + } } /// How the crop frame's outline and handles are drawn (#10930). diff --git a/crates/ui-egui/src/tools/crop_shield.rs b/crates/ui-egui/src/tools/crop_shield.rs index b215b1a0a1..eda1a7cd7d 100644 --- a/crates/ui-egui/src/tools/crop_shield.rs +++ b/crates/ui-egui/src/tools/crop_shield.rs @@ -112,11 +112,13 @@ pub fn canvas_color(app: &PhotocraftApp, ctx: &egui::Context) -> Color32 { /// The shield over the pending crop box now ([`fill`]). pub fn current(app: &PhotocraftApp, ctx: &egui::Context) -> Option { + let s = &app.ui.tool_options.crop_shield; // No shield before the crop has started: an untouched frame shows only itself (#10930). - if !crate::crop_overlay::crop_started(app) { + // Show Cropped Area off still hides everything outside the frame, as it does once started. + if s.shows_cropped_area() && !crate::crop_overlay::crop_started(app) { return None; } - fill(&app.ui.tool_options.crop_shield, canvas_color(app, ctx), app.crop.drag.is_some()) + fill(s, canvas_color(app, ctx), app.crop.drag.is_some()) } /// Show Cropped Area is off with the Crop tool showing a box: nothing outside it shows. diff --git a/crates/ui-egui/tests/it/crop_frame_look.rs b/crates/ui-egui/tests/it/crop_frame_look.rs index 8301fc101f..29f05fbf11 100644 --- a/crates/ui-egui/tests/it/crop_frame_look.rs +++ b/crates/ui-egui/tests/it/crop_frame_look.rs @@ -96,3 +96,24 @@ fn classic_mode_keeps_the_look_it_had() { assert_eq!(look, FrameLook::Classic); assert!(guides && shield, "Classic Mode shows its overlay and shield as before, even untouched"); } + +#[test] +fn a_press_that_has_not_moved_yet_is_not_a_change() { + // The first change is a drag; a press held still leaves the untouched look in every mode. + for show in MODES { + let mut app = crop_app(show); + control(&mut app, "ui.pointer", json!({"tool": "Crop", "events": [{"kind": "down", "x": 50, "y": 40}]})); + assert_eq!(drawn(&app), (false, false, FrameLook::Untouched), "{show:?}: pressed, not moved"); + control(&mut app, "ui.pointer", json!({"tool": "Crop", "events": [{"kind": "move", "x": 150, "y": 90}]})); + assert_eq!(drawn(&app).2, FrameLook::Started, "{show:?}: the move is the first change"); + } +} + +#[test] +fn show_cropped_area_off_still_hides_what_is_outside_an_untouched_frame() { + let mut app = crop_app(OverlayShow::Always); + app.ui.tool_options.crop_shield.show_cropped_area = false; + let ctx = egui::Context::default(); + assert!(photocraft_ui_egui::crop_shield::current(&app, &ctx).is_some(), "the opaque outside fill stays"); + assert!(current_guides(&app, 300.0, 200.0).is_empty(), "but no overlay before the first change"); +}