PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): Free Transform corners show the diagonal resize arrow on wide or tall boxes
steinluiz407cd229a41b
diff --git a/crates/ui-egui/src/cam_transform.rs b/crates/ui-egui/src/cam_transform.rsindex 995645940..1914dc883 100644--- a/crates/ui-egui/src/cam_transform.rs+++ b/crates/ui-egui/src/cam_transform.rs@@ -210,10 +210,7 @@ pub(crate) fn cursor(app: &PhotocraftApp, ctx: &egui::Context, xf: &ViewXform, pSome([(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0])};Some(match h {- Hit::Corner(i) => {- let (a, b) = (q.get(i)?, q.get((i + 2) % 4)?);- crate::tool_cursor::resize([a[0] - b[0], a[1] - b[1]], xf)- }+ Hit::Corner(i) => crate::tool_cursor::corner_resize(&q, i, xf),Hit::Edge(i) => {let (a, b) = (mid(i)?, mid(i + 2)?);crate::tool_cursor::resize([a[0] - b[0], a[1] - b[1]], xf)diff --git a/crates/ui-egui/src/tool_cursor.rs b/crates/ui-egui/src/tool_cursor.rsindex 8b67b96d6..31593500f 100644--- a/crates/ui-egui/src/tool_cursor.rs+++ b/crates/ui-egui/src/tool_cursor.rs@@ -127,7 +127,34 @@ pub(crate) fn native_scale(ctx: &egui::Context) -> f32 {/// and Warp splits), the type box, Content-Aware Move's box, the Artboard tool and guides pick/// their arrows here.pub(crate) fn resize(d: [f64; 2], xf: &ViewXform) -> CursorIcon {- let s = xf.map_vec(vec2(d[0] as f32, d[1] as f32));+ resize_on_screen(xf.map_vec(vec2(d[0] as f32, d[1] as f32)))+}++/// The resize arrow for corner `i` of a box's `quad` (corners in order round the box), as the view+/// shows it: along the corner's bisector, so an upright box's corners always show a diagonal arrow,+/// as in Photoshop, however wide or tall the box is. Aiming from the opposite corner instead gave a+/// box wider than about 2.4:1 the horizontal arrow at its corners (and a tall one the vertical).+pub(crate) fn corner_resize(quad: &[[f64; 2]; 4], i: usize, xf: &ViewXform) -> CursorIcon {+ let at = quad[i % 4];+ let edge = |k: usize| {+ let to = quad[k % 4];+ let v = xf.map_vec(vec2((at[0] - to[0]) as f32, (at[1] - to[1]) as f32));+ let l = v.length();+ (l.is_finite() && l > 1e-6).then(|| v / l)+ };+ let bisector = edge(i + 1).zip(edge(i + 3)).map(|(a, b)| a + b).filter(|s| s.length() > 1e-3);+ match bisector {+ Some(s) => resize_on_screen(s),+ // A collapsed or folded corner has no bisector: aim away from the opposite corner.+ None => {+ let opposite = quad[(i + 2) % 4];+ resize([at[0] - opposite[0], at[1] - opposite[1]], xf)+ }+ }+}++/// The nearest of the four resize arrows to screen direction `s`.+fn resize_on_screen(s: Vec2) -> CursorIcon {let angle = f64::from(s.y).atan2(f64::from(s.x)).rem_euclid(std::f64::consts::PI);match (angle / std::f64::consts::FRAC_PI_4).round() as u8 % 4 {0 => CursorIcon::ResizeHorizontal,diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 7ae223029..da59d002a 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -2122,7 +2122,7 @@ pub fn cursor(app: &PhotocraftApp, xf: &ViewXform, p: [f64; 2], quick_split: boo// A handle resizes the box away from the opposite handle.let resize = |from: [f64; 2], to: [f64; 2]| crate::tool_cursor::resize([to[0] - from[0], to[1] - from[1]], xf);Some(match h {- Hit::Corner(i) => resize(corner(i + 2), corner(i)),+ Hit::Corner(i) => crate::tool_cursor::corner_resize(&t.quad, i, xf),Hit::Edge(i) => resize(edge_mid(i + 2), edge_mid(i)),Hit::Pivot => CursorIcon::Crosshair,Hit::Inside => CursorIcon::Move,@@ -4616,6 +4616,21 @@ mod tests {}}+ /// The corners of a wide or tall box show the diagonal arrows, as in Photoshop, not the+ /// horizontal or vertical one its long diagonal is nearest to.+ #[test]+ fn corner_cursors_stay_diagonal_on_a_long_box() {+ use CursorIcon::{ResizeNeSw as NeSw, ResizeNwSe as NwSe};+ for rect in [photocraft_geom::Rect::new(8, 112, 248, 144), photocraft_geom::Rect::new(112, 8, 144, 248)] {+ let mut app = app_with_square(256, rect);+ crate::menus::invoke(&mut app, &egui::Context::default(), "edit.freeTransform", json!({})).unwrap();+ let t = app.ui.transform.clone().unwrap();+ let xf = ViewXform::active(&app).unwrap();+ let got: Vec<_> = (0..4).map(|i| cursor(&app, &xf, t.quad[i], false)).collect();+ assert_eq!(got, [NwSe, NeSw, NwSe, NeSw].map(Some), "{rect:?}");+ }+ }+/// A warp split's arrow crosses the split line as it looks on screen: it turns with a turned/// grid (Warp entered on a rotated box) and with a rotated view.#[test]diff --git a/crates/ui-egui/src/type_transform.rs b/crates/ui-egui/src/type_transform.rsindex 0c1b4837e..72c5c05d3 100644--- a/crates/ui-egui/src/type_transform.rs+++ b/crates/ui-egui/src/type_transform.rs@@ -382,24 +382,16 @@ pub(crate) fn cursor(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXforlet q = xf.to_doc(p);let handle = crate::type_tool::box_handle_at(app, id, q[0], q[1])?;let t = frame(app)?;- let direction = if handle < 4 {- let a = *t.quad.get(usize::from(handle))?;- let b = *t.quad.get((usize::from(handle) + 2) % 4)?;- [a[0] - b[0], a[1] - b[1]]- } else {- let b = if handle.is_multiple_of(2) { t.quad[3] } else { t.quad[1] };- [b[0] - t.quad[0][0], b[1] - t.quad[0][1]]- };- return Some(crate::tool_cursor::resize(direction, xf));+ if handle < 4 {+ return Some(crate::tool_cursor::corner_resize(&t.quad, usize::from(handle), xf));+ }+ let b = if handle.is_multiple_of(2) { t.quad[3] } else { t.quad[1] };+ return Some(crate::tool_cursor::resize([b[0] - t.quad[0][0], b[1] - t.quad[0][1]], xf));}let Some(t) = frame(app) else { return Some(CursorIcon::Default) };let h = app.ui.type_transform.as_ref().map(|t| t.gesture.hit).or_else(|| hit(app, &t, xf.to_doc(p)));Some(match h {- Some(Hit::Corner(i)) => {- let q = *t.quad.get(i)?;- let opposite = *t.quad.get((i + 2) % 4)?;- crate::tool_cursor::resize([q[0] - opposite[0], q[1] - opposite[1]], xf)- }+ Some(Hit::Corner(i)) => crate::tool_cursor::corner_resize(&t.quad, i, xf),Some(Hit::Edge(_)) => {// Original arrowhead indicator, above the canvas, without a proprietary cursor asset.let tokens = crate::theme::Tokens::get(ctx);