PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Free Transform: a typed W/H stops at the largest document side
Claudec5d50b439068
diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 0fafb4a19..5f45ed0c5 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -1307,10 +1307,20 @@ const ACTIONS_W: f32 = 140.0;/// Reach of the reference point X/Y fields: the largest document side (`image.canvasSize`'s limit).const POSITION_LIMIT_PX: f32 = 300_000.0;-/// Reach of the W/H fields. The engine takes any scale; this lets a 300 px layer span the largest+/// Reach of the W/H fields. The engine takes any scale; this lets a 30 px layer span the largest/// document (#1267).const SCALE_LIMIT_PCT: f32 = 1_000_000.0;+/// The W or H field's limit for a box side of `side` px: as far as the result stays within the+/// largest document side, so a typed scale can't ask for an image too large to allocate.+fn scale_limit_pct(side: f64) -> f32 {+ let side = side.abs();+ if !side.is_finite() || side < 1e-6 {+ return SCALE_LIMIT_PCT;+ }+ ((f64::from(POSITION_LIMIT_PX) / side * 100.0) as f32).clamp(100.0, SCALE_LIMIT_PCT)+}+/// Options bar while transforming: reference point X/Y, W/H %, angle, interpolation, and, pinned/// to the right, the warp switch, cancel and commit.pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {@@ -1354,8 +1364,9 @@ fn transform_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, t: &TransformSes}crate::widgets::vline(ui, 22.0);let (mut w, mut h) = (sx as f32, sy as f32);+ let (wlim, hlim) = (scale_limit_pct(t.rect[2] - t.rect[0]), scale_limit_pct(t.rect[3] - t.rect[1]));lbl(ui, "W:");- let rw = crate::widgets::value_field(ui, &mut w, -SCALE_LIMIT_PCT..=SCALE_LIMIT_PCT, "%", 66.0);+ let rw = crate::widgets::value_field(ui, &mut w, -wlim..=wlim, "%", 66.0);let link_id = egui::Id::new("transform-link");let mut link: bool = ui.data(|d| d.get_temp(link_id)).unwrap_or(true);if crate::icons::button(ui, if link { "link" } else { "unlink" }, 22.0, link, tl!("Maintain aspect ratio")).clicked() {@@ -1363,10 +1374,12 @@ fn transform_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, t: &TransformSesui.data_mut(|d| d.insert_temp(link_id, link));}lbl(ui, "H:");- let rh = crate::widgets::value_field(ui, &mut h, -SCALE_LIMIT_PCT..=SCALE_LIMIT_PCT, "%", 66.0);+ let rh = crate::widgets::value_field(ui, &mut h, -hlim..=hlim, "%", 66.0);if rw.changed() || rh.changed() {let (kx, ky) = if link {let k = if rw.changed() { w as f64 / sx.max(1e-9) } else { h as f64 / sy.max(1e-9) };+ // Linked, the other side follows: keep it within its own limit too.+ let k = k.min(f64::from(wlim) / sx.abs().max(1e-9)).min(f64::from(hlim) / sy.abs().max(1e-9));(k, k)} else {(w as f64 / sx.max(1e-9), h as f64 / sy.max(1e-9))@@ -1676,6 +1689,21 @@ mod tests {assert!((t.pivot[0] - 250_000.0).abs() < 1.0, "{:?}", t.pivot);}+ /// A typed scale stops where the box reaches the largest document side, not past what can be+ /// allocated: the 100 × 50 px test box tops out at 300000 % wide.+ #[test]+ fn typed_scales_stay_within_the_largest_document() {+ assert_eq!(scale_limit_pct(20.0), SCALE_LIMIT_PCT);+ assert_eq!(scale_limit_pct(3000.0), 10_000.0);+ assert_eq!(scale_limit_pct(1e9), 100.0);+ assert_eq!(scale_limit_pct(0.0), SCALE_LIMIT_PCT);+ assert_eq!(scale_limit_pct(f64::NAN), SCALE_LIMIT_PCT);+ let t = type_into_bar_field(2, "99999999");+ let (sx, sy, _, _) = readout(&t);+ let side = t.rect[2] - t.rect[0];+ assert!(sx <= f64::from(scale_limit_pct(side)) + 0.5 && sy <= f64::from(scale_limit_pct(t.rect[3] - t.rect[1])) + 0.5, "{sx} {sy}");+ }+fn drag(s: &mut TransformSession, from: [f64; 2], to: [f64; 2], mods: egui::Modifiers) {let g = Gesture { hit: hit(s, from, 8.0), start: from, quad0: s.quad, pivot0: s.pivot };apply_drag(s, g, to, mods);