diff --git a/crates/ui-egui/src/star_tool_tests.rs b/crates/ui-egui/src/star_tool_tests.rs index b83bb15d83..8a476690c8 100644 --- a/crates/ui-egui/src/star_tool_tests.rs +++ b/crates/ui-egui/src/star_tool_tests.rs @@ -109,7 +109,7 @@ fn shift_and_alt_constrain_and_centre_the_star_like_the_polygon_tool() { app.ui.tool = Tool::Star; drag(&mut app, [100.0, 100.0], [140.0, 120.0], Modifiers::SHIFT | Modifiers::ALT); match live_shape(&app) { - Some(LiveShape::Polygon { rect, .. }) => assert_eq!(rect, [60.0, 60.0, 80.0, 80.0], "a square box drawn from the centre"), + Some(LiveShape::Polygon { rect, .. }) => assert_eq!(rect, [80.0, 80.0, 40.0, 40.0], "a square box of the shorter side drawn from the centre (#13222)"), other => panic!("a live star shape, got {other:?}"), } } diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index 01ff55fce0..75daabe713 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -238,6 +238,9 @@ mod pen_enter_deselects_tests; #[cfg(test)] mod line_shift_snap_tests; +#[cfg(test)] +mod ellipse_shift_circle_tests; + #[cfg(test)] mod pen_mode_menu_tests; diff --git a/crates/ui-egui/src/tools/vector_ui/ellipse_shift_circle_tests.rs b/crates/ui-egui/src/tools/vector_ui/ellipse_shift_circle_tests.rs new file mode 100644 index 0000000000..0d134ece1a --- /dev/null +++ b/crates/ui-egui/src/tools/vector_ui/ellipse_shift_circle_tests.rs @@ -0,0 +1,76 @@ +//! #13222: ⇧ while dragging with the Ellipse tool makes a circle from the shorter side of the +//! drag, as in the reference application, not the longer one; ⇧⌥ centres that circle on the +//! press. The drag preview (`shape_bounds`) shows the same box as the layer the drag makes. + +use egui::Modifiers; +use photocraft_doc::{LayerContent, LiveShape}; +use serde_json::json; + +use super::shape_bounds; +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::EllipseShape; + app +} + +/// A drag pressed without modifiers, with `mods` pressed after the start (so the press picks no +/// path operation or eyedropper). +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) { + tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: (from[0] + to[0]) / 2.0, y: (from[1] + to[1]) / 2.0, pressure: 1.0 }, mods); + tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, mods); + tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, mods); +} + +/// The active layer's live ellipse `[x, y, w, h]`. +fn ellipse_rect(app: &PhotocraftApp) -> [f64; 4] { + let st = app.session.active().unwrap(); + match &st.doc.layer(st.active_layer.unwrap()).unwrap().content { + LayerContent::Shape(s) => match &s.live { + Some(LiveShape::Ellipse { rect }) => *rect, + other => panic!("a live ellipse, got {other:?}"), + }, + _ => panic!("a shape layer"), + } +} + +fn preview(app: &PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) -> [f64; 4] { + shape_bounds(&app.ui.tool_options, Tool::EllipseShape, from, to, mods).expect("a box") +} + +#[test] +fn shift_makes_a_circle_from_the_shorter_side() { + let mut app = app(); + drag(&mut app, [400.0, 300.0], [600.0, 400.0], Modifiers::SHIFT); + assert_eq!(ellipse_rect(&app), [400.0, 300.0, 100.0, 100.0]); + assert_eq!(preview(&app, [400.0, 300.0], [600.0, 400.0], Modifiers::SHIFT), [400.0, 300.0, 500.0, 400.0]); +} + +#[test] +fn shift_uses_the_shorter_side_when_the_drag_is_taller_than_wide() { + let mut app = app(); + drag(&mut app, [400.0, 300.0], [450.0, 500.0], Modifiers::SHIFT); + assert_eq!(ellipse_rect(&app), [400.0, 300.0, 50.0, 50.0]); + assert_eq!(preview(&app, [400.0, 300.0], [450.0, 500.0], Modifiers::SHIFT), [400.0, 300.0, 450.0, 350.0]); +} + +#[test] +fn shift_alt_centres_the_shorter_side_circle_on_the_press() { + let mut app = app(); + let mods = Modifiers::SHIFT | Modifiers::ALT; + drag(&mut app, [400.0, 300.0], [600.0, 400.0], mods); + assert_eq!(ellipse_rect(&app), [300.0, 200.0, 200.0, 200.0]); + assert_eq!(preview(&app, [400.0, 300.0], [600.0, 400.0], mods), [300.0, 200.0, 500.0, 400.0]); +} + +#[test] +fn shift_keeps_the_drag_direction_up_and_left() { + let app = app(); + assert_eq!(preview(&app, [400.0, 300.0], [200.0, 200.0], Modifiers::SHIFT), [300.0, 200.0, 400.0, 300.0]); +} diff --git a/crates/ui-egui/src/tools/vector_ui/shapes.rs b/crates/ui-egui/src/tools/vector_ui/shapes.rs index 958c35fe07..cd05a26036 100644 --- a/crates/ui-egui/src/tools/vector_ui/shapes.rs +++ b/crates/ui-egui/src/tools/vector_ui/shapes.rs @@ -116,7 +116,8 @@ pub(super) fn shape_geometry(o: &ToolOptions, tool: Tool, start: [f64; 2], end: } else { let (mut w, mut h) = (end[0] - start[0], end[1] - start[1]); if mods.shift { - let m = w.abs().max(h.abs()); + // The drag's shorter side sets the square, as in the reference application (#13222). + let m = w.abs().min(h.abs()); (w, h) = (m * w.signum(), m * h.signum()); } let (x0, y0) = if mods.alt { (start[0] - w, start[1] - h) } else { (start[0], start[1]) }; diff --git a/crates/ui-egui/src/tools/vector_ui/tests.rs b/crates/ui-egui/src/tools/vector_ui/tests.rs index ecfdd7e72d..486ce2e6d0 100644 --- a/crates/ui-egui/src/tools/vector_ui/tests.rs +++ b/crates/ui-egui/src/tools/vector_ui/tests.rs @@ -491,7 +491,7 @@ fn shape_drag_creates_layers_with_modifiers() { finish_shape(&mut app, Tool::Rectangle, [10.0, 10.0], [60.0, 30.0], egui::Modifiers::SHIFT); let st = app.session.active().unwrap(); let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); - assert_eq!((b.width(), b.height()), (50, 50), "shift = square"); + assert_eq!((b.width(), b.height()), (20, 20), "shift = square of the shorter side (#13222)"); finish_shape(&mut app, Tool::EllipseShape, [100.0, 100.0], [120.0, 110.0], egui::Modifiers::ALT); let st = app.session.active().unwrap(); let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); @@ -694,9 +694,9 @@ fn shift_pressed_during_a_shape_drag_constrains_it() { let l = st.doc.layer(st.active_layer.unwrap()).unwrap(); assert!(matches!(l.content, LayerContent::Shape(_)), "{tool:?} ({sides} sides): a shape layer"); let b = l.surface().unwrap().content_bounds(); - // The drag is 60 × 30; constrained, the shape's box is 60 × 60 (a hexagon is narrower - // than its box, so the height tells). - assert!(b.height().abs_diff(60) <= 1 && b.width() <= 61, "{tool:?}: ⇧ mid-drag keeps proportions: {b:?}"); + // The drag is 60 × 30; constrained, the shape's box is 30 × 30, the shorter side (#13222) + // (a hexagon is narrower than its box, so the height tells). + assert!(b.height().abs_diff(30) <= 1 && b.width() <= 31, "{tool:?}: ⇧ mid-drag keeps proportions: {b:?}"); } }