LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LR-CROP-STRAIGHTEN: keep the angle readout on the canvas, not the photo
Nathaniel Ledford676e8c01ce0e
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rsindex 7c14a2bc2..c98aeda12 100644--- a/crates/ui-egui/src/panels/detail.rs+++ b/crates/ui-egui/src/panels/detail.rs@@ -1323,7 +1323,8 @@ fn crop_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Responslet _ = app.run("develop.endInteraction", json!({}));}if let Some(at) = readout_at {- angle_readout(ui, at, shown_angle, map.rect);+ // the canvas (the loupe's own rect), not the photo: rotating happens in the margin around it+ angle_readout(ui, at, shown_angle, resp.rect);}let _ = id;}@@ -1339,7 +1340,14 @@ pub(crate) fn crop_angle_label(angle: f64) -> String {/// Where the angle readout of `size` goes for a pointer `at`: below right of it, flipped to the/// left / above where that would leave `bounds`, then kept inside them.-fn readout_rect(at: Pos2, size: egui::Vec2, bounds: Rect) -> Rect {+pub(crate) fn readout_rect(at: Pos2, size: egui::Vec2, bounds: Rect) -> Rect {+ // without a real pointer or bounds (NaN, an empty rect) there is nothing to keep it in; and+ // f32::clamp panics on NaN, so only finite numbers get there+ if !at.is_finite() || !bounds.is_finite() || !bounds.is_positive() {+ let at = if at.is_finite() { at } else { bounds.min.max(Pos2::ZERO) };+ let at = if at.is_finite() { at } else { Pos2::ZERO };+ return Rect::from_min_size(at + vec2(18.0, 14.0), size);+ }let x = if at.x + 18.0 + size.x > bounds.right() { at.x - 18.0 - size.x } else { at.x + 18.0 };let y = if at.y + 14.0 + size.y > bounds.bottom() { at.y - 14.0 - size.y } else { at.y + 14.0 };let x = x.clamp(bounds.left(), (bounds.right() - size.x).max(bounds.left()));@@ -1361,8 +1369,8 @@ fn rotate_cursor(ui: &egui::Ui, at: Pos2) {register(ui.ctx(), "cropRotateCursor", r);}-/// While rotating: the angle, next to the pointer, kept on the canvas (`bounds`): on the pointer's-/// other side when it would run past an edge.+/// While rotating: the angle, next to the pointer, kept inside `bounds` (the canvas): on the+/// pointer's other side when it would run past an edge.fn angle_readout(ui: &egui::Ui, at: Pos2, angle: f64, bounds: Rect) {let t = Tokens::get(ui.ctx());let p = top_painter(ui, "crop-angle-readout");diff --git a/crates/ui-egui/src/tests_crop_rotate.rs b/crates/ui-egui/src/tests_crop_rotate.rsindex 5654b66af..9cd69cc2b 100644--- a/crates/ui-egui/src/tests_crop_rotate.rs+++ b/crates/ui-egui/src/tests_crop_rotate.rs@@ -178,3 +178,37 @@ fn dragging_the_angle_field_is_one_undo_step() {assert!(angle(&h) > 0.0, "dragged right: {}", angle(&h));assert_eq!(h.app.session.undo.len(), undo + 1, "one step for the whole drag");}++/// Rotating in the margin beside a narrow photo keeps the readout next to the pointer: it is kept+/// on the canvas, not pinned to the photo's edge.+#[test]+fn the_readout_follows_the_pointer_in_the_margin() {+ let mut h = crop_tool();+ let img = h.app.image_rect.expect("the photo on screen");+ let canvas = h.app.canvas_rect.expect("the canvas");+ assert!(img.left() - canvas.left() > 150.0, "this test needs a margin beside the photo");+ let at = egui::pos2(canvas.left() + 40.0, img.center().y);+ let x = (at.x - img.left()) / img.width();+ let r = h.request(+ "ui.pointer",+ json!({"events": [{"kind": "down", "x": x, "y": 0.4}, {"kind": "drag", "x": x, "y": 0.45}, {"kind": "drag", "x": x, "y": 0.5}]}),+ T,+ );+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ let readout = h.app.widgets.iter().find(|(w, _)| w == "cropAngleReadout").map(|(_, r)| *r).expect("the readout while rotating");+ assert!(readout.distance_to_pos(at) < 60.0, "{readout:?} near {at:?}");+}++/// Placing the readout never panics, whatever the bounds (a NaN or an empty rect).+#[test]+fn placing_the_readout_never_panics() {+ use crate::panels::detail::readout_rect;+ let size = egui::vec2(50.0, 20.0);+ for bounds in [egui::Rect::NAN, egui::Rect::NOTHING, egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(10.0, 10.0))] {+ let r = readout_rect(egui::pos2(5.0, 5.0), size, bounds);+ assert_eq!(r.size(), size, "{bounds:?}");+ }+ let r = readout_rect(egui::pos2(f32::NAN, 5.0), size, egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 100.0)));+ assert_eq!(r.size(), size);+}