diff --git a/crates/ui-egui/src/dialogs/shape_dialog.rs b/crates/ui-egui/src/dialogs/shape_dialog.rs index c165703895..bb87d0a351 100644 --- a/crates/ui-egui/src/dialogs/shape_dialog.rs +++ b/crates/ui-egui/src/dialogs/shape_dialog.rs @@ -28,10 +28,31 @@ pub(crate) fn open(app: &mut PhotocraftApp, kind: ShapeKind, at: Point) -> Optio let sides = o.polygon_sides.clamp(3, 100); let units = GuideUnits::new(ruler, size[0], size[1], dpi).points_per_inch(points); // The options bar has no star ratio: a polygon opens as a plain polygon. - let options = CreateShapeOptions::initial(kind, at).units(units).tool(radius, sides, 1.0); + let mut options = CreateShapeOptions::initial(kind, at).units(units).tool(radius, sides, 1.0); + if kind == ShapeKind::Ellipse { + options = ellipse_prefill(app, options); + } Some(app.framework.dialogs.open(CreateShapeDialog::new(options).alert_title(tl!("PhotoCraft")))) } +/// Create Ellipse opens as Photoshop's does (#13229): with the active ellipse layer's size, or 100 × 100 with a pixel +/// layer active or none, whatever the last OK was, and From Center unchecked (the tool has no From Center option yet). +/// Another shape layer active wasn't measured and keeps the remembered values. +fn ellipse_prefill(app: &PhotocraftApp, mut options: CreateShapeOptions) -> CreateShapeOptions { + use photocraft_doc::{LayerContent, LiveShape}; + + let active = app.session.active().and_then(|st| st.active_layer.and_then(|id| st.doc.layer(id))); + match active.map(|l| &l.content) { + Some(LayerContent::Shape(shape)) => match shape.live { + Some(LiveShape::Ellipse { rect: [_, _, w, h] }) if w.abs() >= 1.0 && h.abs() >= 1.0 => options.size = (w.abs(), h.abs()), + _ => return options, + }, + _ => options.size = (100.0, 100.0), + } + options.from_center = false; + options.recalled() +} + /// Whether a click at `at` (document px) with the Rectangle tool in Shape mode lies inside the active live /// rectangle's outline: the click leaves the shape alone and opens no dialog (#13340). Rounded corners use /// the outline, not the box. @@ -73,3 +94,7 @@ pub(crate) fn take(app: &mut PhotocraftApp, _ctx: &egui::Context) { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "shape_dialog/create_ellipse_prefill_tests.rs"] +mod create_ellipse_prefill_tests; diff --git a/crates/ui-egui/src/dialogs/shape_dialog/create_ellipse_prefill_tests.rs b/crates/ui-egui/src/dialogs/shape_dialog/create_ellipse_prefill_tests.rs new file mode 100644 index 0000000000..f2baa26abb --- /dev/null +++ b/crates/ui-egui/src/dialogs/shape_dialog/create_ellipse_prefill_tests.rs @@ -0,0 +1,90 @@ +//! #13229: Create Ellipse (a click with the Ellipse tool) opens with the active ellipse layer's +//! size, or 100 × 100 with a pixel layer active or in a new document, whatever the last OK was. From +//! Center starts unchecked (the tool has no From Center option yet). Another shape layer active +//! keeps today's remembered values, and the other tools' dialogs are unchanged. + +use craft::ui::dialog::{DialogId, StackDialog as _}; +use serde_json::{Value, json}; + +use super::*; +use crate::canvas::testing; +use crate::state::Tool; + +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({"name": "Paint"})).unwrap(); + app +} + +/// A click with `tool` at `at`; the Create dialog it opens. +fn click(app: &mut PhotocraftApp, tool: Tool, at: [f64; 2]) -> DialogId { + app.ui.tool = tool; + testing::click(app, at, egui::Modifiers::NONE); + crate::dialogs::testing::show(app); + app.framework.dialogs.get::().expect("a Create Shape dialog").id() +} + +/// The dialog's width, height and From Center. +fn shown(app: &PhotocraftApp) -> (f64, f64, bool) { + let f: Value = app.framework.dialogs.get::().and_then(|d| d.json_fields()).expect("fields"); + (f["width"].as_f64().unwrap(), f["height"].as_f64().unwrap(), f["fromCenter"].as_bool().unwrap()) +} + +/// OK with `width` × `height` and From Center on, so the dialog remembers them. +fn ok_with(app: &mut PhotocraftApp, id: DialogId, width: f64, height: f64) { + let ctx = app.ctx().clone(); + for (k, v) in [("width", json!(width)), ("height", json!(height)), ("fromCenter", json!(true))] { + app.framework.dialogs.set_json_field(&ctx, id, k, v).unwrap(); + } + crate::dialogs::testing::confirm(app, id).unwrap(); +} + +#[test] +fn the_active_ellipse_layers_size() { + let mut app = app(); + app.run("shape.create", json!({"kind": "ellipse", "rect": [40, 40, 200, 100], "fill": "#3366aa"})).unwrap(); + app.sync_views(); + click(&mut app, Tool::EllipseShape, [330.0, 250.0]); + assert_eq!(shown(&app), (200.0, 100.0, false)); +} + +#[test] +fn a_pixel_layer_or_a_new_document_opens_100_by_100_whatever_the_last_ok() { + let mut app = app(); + let id = click(&mut app, Tool::EllipseShape, [20.0, 20.0]); + ok_with(&mut app, id, 150.0, 80.0); + // The ellipse it drew is the active layer now: back to a pixel layer. + let paint = app.session.active().unwrap().doc.walk().into_iter().map(|(_, _, l)| l).find(|l| l.name == "Paint").unwrap().id.0; + app.run("layer.select", json!({"layer": paint})).unwrap(); + app.sync_views(); + click(&mut app, Tool::EllipseShape, [300.0, 200.0]); + assert_eq!(shown(&app), (100.0, 100.0, false), "a pixel layer"); + app.run("file.new", json!({"width": 300, "height": 200, "background": "white"})).unwrap(); + app.sync_views(); + click(&mut app, Tool::EllipseShape, [50.0, 50.0]); + assert_eq!(shown(&app), (100.0, 100.0, false), "a new document"); +} + +#[test] +fn another_shape_layer_keeps_the_remembered_values() { + let mut app = app(); + let id = click(&mut app, Tool::EllipseShape, [20.0, 20.0]); + ok_with(&mut app, id, 150.0, 80.0); + app.run("shape.create", json!({"kind": "rect", "rect": [200, 150, 60, 40], "fill": "#3366aa"})).unwrap(); + app.sync_views(); + click(&mut app, Tool::EllipseShape, [350.0, 280.0]); + assert_eq!(shown(&app), (150.0, 80.0, true), "not measured: today's remembered values"); +} + +#[test] +fn the_rectangle_dialog_is_unchanged() { + let mut app = app(); + let id = click(&mut app, Tool::Rectangle, [20.0, 20.0]); + ok_with(&mut app, id, 150.0, 80.0); + let paint = app.session.active().unwrap().doc.walk().into_iter().map(|(_, _, l)| l).find(|l| l.name == "Paint").unwrap().id.0; + app.run("layer.select", json!({"layer": paint})).unwrap(); + app.sync_views(); + click(&mut app, Tool::Rectangle, [300.0, 200.0]); + assert_eq!(shown(&app), (150.0, 80.0, true), "the Rectangle keeps its remembered values"); +}