diff --git a/crates/ui-egui/src/dialogs/paragraph_text_size.rs b/crates/ui-egui/src/dialogs/paragraph_text_size.rs index 7e29c0908d..96ec34b10d 100644 --- a/crates/ui-egui/src/dialogs/paragraph_text_size.rs +++ b/crates/ui-egui/src/dialogs/paragraph_text_size.rs @@ -10,12 +10,17 @@ //! cannot make. A drag upwards or to the left of its start keeps today's rule (the box is the //! rectangle between the two points), and Cancel beyond closing the dialog is not specified. //! +//! The Vertical Type Mask tool's rule (#12090) reuses this dialog: a drag with no selection is +//! drawn from the centre, as with the marquee, so it opens through [`open_centred`] prefilled +//! with twice the dragged size, and OK centres the box on the drag start. +//! //! The dialog is a hosted parameter dialog with a key of its own, so `ui.inspect`, `ui.dialog.set` -//! and `ui.dialog.confirm` drive it like the other parameter dialogs. The drag's top-left, the -//! document it was made in and the tool, type options and placeholder preference the new layer is -//! built from are not fields: they are the family's captured value, read by [`run`]. The last of -//! those three is taken when the dialog opens rather than at OK, because the reference application's -//! dialog is modal — the layer keeps what was active at the drag (#12044). +//! and `ui.dialog.confirm` drive it like the other parameter dialogs. The drag's anchor — the +//! top-left, or the centre for [`open_centred`] —, the document it was made in and the tool, type +//! options and placeholder preference the new layer is built from are not fields: they are the +//! family's captured value, read by [`run`]. The last of those is taken when the dialog opens rather +//! than at OK, because the reference application's dialog is modal — the layer keeps what was active +//! at the drag (#12044). use craft::ui::dialog::DialogId; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamDialog, ParamResult}; @@ -56,8 +61,21 @@ fn build(w: f64, h: f64) -> impl for<'b> Fn(&'b mut DialogFields) -> ParamDialog /// Alt+drag and OK, so the box always uses what was active at the drag. A control that changes them /// while the dialog happens to be open therefore cannot leak into the new layer (#12044). pub fn open(app: &mut PhotocraftApp, at: [f64; 2], w: f64, h: f64) -> DialogId { + open_with(app, at, w, h, false) +} + +/// Open the dialog centred on `(x, y)` — the Vertical Type Mask tool's Alt+drag start (#12090): +/// the fields prefill the size to place, `(w, h)` is twice the dragged size there, and OK centres +/// the box on `(x, y)`, so fields edited in the dialog keep the box centred on the drag start. +pub fn open_centred(app: &mut PhotocraftApp, at: [f64; 2], w: f64, h: f64) -> DialogId { + open_with(app, at, w, h, true) +} + +/// The shared open: `centred` goes into the captured state next to the anchor point. +fn open_with(app: &mut PhotocraftApp, at: [f64; 2], w: f64, h: f64, centred: bool) -> DialogId { let captured = json!({ "at": [at[0], at[1]], + "centred": centred, "document": app.session.active().map(|st| json!(st.doc.id)).unwrap_or(Value::Null), "settings": crate::type_tool::TypeSnapshot::live(app), }); @@ -83,6 +101,11 @@ pub fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamResult) { crate::dialogs::alerts::report_error(app, "the Paragraph Text Size dialog has no size"); return; }; + // `open_with` always captures the anchor rule; the default keeps the plain top-left rule. + let centred = captured.get("centred").and_then(Value::as_bool).unwrap_or(false); + // #12090: a centred drag places the box by its centre on the captured point, so fields + // edited in the dialog keep it centred there; the plain rule puts the top-left at it. + let (x, y) = if centred { (x - w / 2.0, y - h / 2.0) } else { (x, y) }; // The settings are the ones the drag had, not the ones the app has now: the reference dialog is // modal, so a control that switched the tool or edited the options while it was open has no say // in this layer (#12044). diff --git a/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs b/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs index 475d1716d9..7a5b52400d 100644 --- a/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs +++ b/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs @@ -321,7 +321,7 @@ fn a_name_the_dialog_does_not_show_as_a_field_is_refused() { drag(&mut app, [10.0, 10.0], [109.5, 60.0], Modifiers::ALT, Modifiers::ALT); let (id, _) = only_dialog(&app); let ctx = app.ctx().clone(); - for field in ["at", "document", "settings", "box", "text"] { + for field in ["at", "document", "settings", "centred", "box", "text"] { assert!(app.framework.dialogs.set_json_field(&ctx, id, field, json!(1.0)).is_err(), "`{field}` is not a field of this dialog"); } assert_eq!(only_dialog(&app).1["width"].as_f64(), Some(99.5), "and the two real fields keep the dragged size"); diff --git a/crates/ui-egui/src/tools/type_tool.rs b/crates/ui-egui/src/tools/type_tool.rs index 1f39f6da3b..b7f51ba9ce 100644 --- a/crates/ui-egui/src/tools/type_tool.rs +++ b/crates/ui-egui/src/tools/type_tool.rs @@ -137,3 +137,6 @@ mod mask_escape_tests; #[cfg(test)] mod unchanged_edit_history_tests; + +#[cfg(test)] +mod vertical_mask_alt_drag_tests; diff --git a/crates/ui-egui/src/tools/type_tool/pointer.rs b/crates/ui-egui/src/tools/type_tool/pointer.rs index 304ad523ec..57119f97d6 100644 --- a/crates/ui-egui/src/tools/type_tool/pointer.rs +++ b/crates/ui-egui/src/tools/type_tool/pointer.rs @@ -317,12 +317,25 @@ fn create_text(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2], alt_at_p let is_box = makes_box(app, start, end); // Photoshop (#12044): Alt+drag with the Type tool sizes the new paragraph box in the // Paragraph Text Size dialog instead of committing the drag, and its OK creates the text. - // Measured for the horizontal Type tool alone, so the vertical and Type Mask tools keep the - // plain drag; Alt+click (a drag too small for a box) stays point text. + // Measured for the horizontal Type tool, so the vertical Type tool and the Horizontal Type + // Mask tool keep the plain drag; Alt+click (a drag too small for a box) stays point text. if alt_at_press && is_box && app.ui.tool == crate::state::Tool::Type { crate::paragraph_text_size::open(app, [start[0].min(end[0]), start[1].min(end[1])], w, h); return; } + // Photoshop (#12090): Alt+drag with the Vertical Type Mask tool opens the same dialog. With + // no selection the box is drawn from the centre, as with the marquee: the dialog prefills + // twice the dragged size and OK centres the box on the drag start. With a selection it + // prefills the dragged size and OK puts the box's top-left at the drag start. A drag up or + // to the left of its start is not measured, so it keeps today's plain box. + if alt_at_press && is_box && app.ui.tool == crate::state::Tool::VerticalTypeMask && end[0] >= start[0] && end[1] >= start[1] { + if crate::canvas::tool_feedback::has_selection(app) { + crate::paragraph_text_size::open(app, [start[0], start[1]], w, h); + } else { + crate::paragraph_text_size::open_centred(app, [start[0], start[1]], 2.0 * w, 2.0 * h); + } + return; + } let mask = app.ui.tool.is_type_mask(); // A click (not a drag) with the Type tool on a path: area type inside a closed path, type // on the path on its outline. The Type Mask tools ignore paths. diff --git a/crates/ui-egui/src/tools/type_tool/vertical_mask_alt_drag_tests.rs b/crates/ui-egui/src/tools/type_tool/vertical_mask_alt_drag_tests.rs new file mode 100644 index 0000000000..039ddfd264 --- /dev/null +++ b/crates/ui-egui/src/tools/type_tool/vertical_mask_alt_drag_tests.rs @@ -0,0 +1,287 @@ +//! #12090: Alt+drag with the Vertical Type Mask tool opens the Paragraph Text Size dialog instead +//! of committing the box. Without a selection the dialog prefills twice the dragged size (the box +//! is drawn from the centre, as with the marquee) and OK centres the box on the drag start; with a +//! selection it prefills the dragged size and OK puts the box's top-left at the drag start; Cancel +//! creates nothing and adds no History state. The neighbours of that route keep today's behaviour, +//! pinned below: a plain drag still makes the mask box at once, Alt+click without a drag is still +//! a point mask, the Horizontal Type Mask tool keeps the plain drag (not measured), a drag up, to +//! the left, or up and to the right of its start keeps today's box (not measured), ⌥ is read at the +//! press, and on the centred route fields edited in the dialog keep the box centred on the drag +//! start. +//! +//! The gestures go through the canvas pass (`canvas::testing`) the way the released Type tool does +//! on the hosted framework, and the dialog is driven the way a control answer drives a hosted +//! parameter dialog (as in the Paragraph Text Size tests of #12044, the dialog this reuses). + +use craft::ui::dialog::DialogId; +use egui::Modifiers; +use photocraft_doc::LayerContent; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::canvas::testing::{ToolEvent, frame, tool_event}; +use crate::state::Tool; + +/// A 400 × 200 document with the Vertical Type Mask tool ready. +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 200})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::VerticalTypeMask; + app +} + +/// The measured "with a selection" state: a rectangle away from both drags below. +fn with_selection(app: &mut PhotocraftApp) { + app.run("select.rect", json!({"x": 10.0, "y": 150.0, "width": 40.0, "height": 20.0})).unwrap(); + app.sync_views(); +} + +/// Press at `from` with `down` held, drag to `to` and release with `up` held, as the canvas pass +/// sees it. ⌥ can change mid-gesture: the Alt+drag rule reads it from the press. +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], down: Modifiers, up: Modifiers) { + tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, down); + if from != to { + tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, up); + } + tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, up); +} + +fn layers(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().doc.layers.len() +} + +/// The active document's History entries: "Cancel adds no history state" is measured here. +fn history_len(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.entries().len() +} + +/// Asserts that the gesture opened exactly one dialog and that it is the Paragraph Text Size +/// family's (#12044's dialog, reused here); returns its id and fields. +fn only_dialog(app: &PhotocraftApp) -> (DialogId, Value) { + let open = app.framework.dialogs.open_dialogs(); + assert_eq!(open.len(), 1, "the Alt+drag opens one dialog"); + let d = &open[0]; + assert!(d.key.as_deref().is_some_and(crate::paragraph_text_size::owns), "it is the Paragraph Text Size dialog, got key {:?}", d.key); + (d.id, d.fields.clone().unwrap_or_default()) +} + +/// Set dialog fields without confirming them, the way a control `ui.dialog.set` does. +fn set(app: &mut PhotocraftApp, id: DialogId, fields: [(&str, Value); 2]) { + let ctx = app.ctx().clone(); + for (field, value) in fields { + for (field, value) in crate::control::dialogs::map_request(app, id, field, value) { + app.framework.dialogs.set_json_field(&ctx, id, &field, value).unwrap(); + } + } +} + +fn text(app: &PhotocraftApp, id: u64) -> photocraft_doc::TextLayer { + let st = app.session.active().unwrap(); + let layer = st.doc.layer(photocraft_doc::LayerId(id)).unwrap(); + let LayerContent::Text(t) = &layer.content else { panic!("not a type layer") }; + t.clone() +} + +/// The paragraph box of `id` in document pixels: its top-left through the layer transform, then +/// its width and height. +fn box_rect(app: &PhotocraftApp, id: u64) -> [f64; 4] { + let t = text(app, id); + let photocraft_doc::text::TextShape::Box { x, y, width, height } = t.shape else { panic!("not paragraph text") }; + [t.transform.m[4] + f64::from(x), t.transform.m[5] + f64::from(y), f64::from(width), f64::from(height)] +} + +/// #12090 measured, no selection: Alt+drag from (200, 100) to (250, 125) opens the Paragraph Text +/// Size dialog prefilled twice the dragged size; the drag itself creates nothing. +#[test] +fn alt_drag_without_a_selection_opens_the_dialog_at_twice_the_dragged_size() { + let mut app = app(); + let before = layers(&app); + drag(&mut app, [200.0, 100.0], [250.0, 125.0], Modifiers::ALT, Modifiers::ALT); + let (_id, f) = only_dialog(&app); + assert_eq!((f["width"].as_f64(), f["height"].as_f64()), (Some(100.0), Some(50.0)), "twice the 50 × 25 drag"); + assert_eq!(layers(&app), before, "the drag itself creates nothing"); + assert!(app.ui.text_edit.is_none(), "and starts no edit session"); +} + +/// #12090 measured, no selection: OK makes a vertical paragraph mask whose box is centred on the +/// drag start, in one History state, with the working layer hidden as a plain mask drag leaves it. +#[test] +fn confirming_centres_the_box_on_the_drag_start() { + let mut app = app(); + drag(&mut app, [200.0, 100.0], [250.0, 125.0], Modifiers::ALT, Modifiers::ALT); + let (d, _) = only_dialog(&app); + let steps = history_len(&app); + let ctx = app.ctx().clone(); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "OK closes the dialog"); + assert_eq!(history_len(&app), steps + 1, "OK adds one History state, as the drag's create does"); + let ed = app.ui.text_edit.as_ref().expect("OK starts the mask session, as the drag does"); + assert!(ed.mask, "the session is a Type Mask"); + assert_eq!(box_rect(&app, ed.layer), [150.0, 75.0, 100.0, 50.0], "centred on the drag start (200, 100)"); + let t = text(&app, ed.layer); + assert_eq!(t.orientation, photocraft_doc::text::Orientation::Vertical, "vertical type"); + assert!( + !app.session.active().unwrap().doc.layer(photocraft_doc::LayerId(ed.layer)).unwrap().visible, + "the mask's working layer is hidden, as a plain mask drag leaves it" + ); +} + +/// #12090 centred route, field EDIT (the module's claim, `paragraph_text_size.rs`: "fields edited +/// in the dialog keep the box centred on the drag start"): entering 80 × 40 in the prefilled dialog +/// moves the top-left so the box stays centred on the drag start. +#[test] +fn editing_the_fields_keeps_the_box_centred_on_the_drag_start() { + let mut app = app(); + drag(&mut app, [200.0, 100.0], [250.0, 125.0], Modifiers::ALT, Modifiers::ALT); + let (d, f) = only_dialog(&app); + assert_eq!((f["width"].as_f64(), f["height"].as_f64()), (Some(100.0), Some(50.0)), "the prefilled size, twice the drag"); + set(&mut app, d, [("width", json!(80.0)), ("height", json!(40.0))]); + let ctx = app.ctx().clone(); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + let ed = app.ui.text_edit.as_ref().expect("OK starts the mask session"); + assert!(ed.mask, "the session is a Type Mask"); + assert_eq!(box_rect(&app, ed.layer), [160.0, 80.0, 80.0, 40.0], "the typed 80 × 40, still centred on the drag start (200, 100)"); +} + +/// #12090 measured, with a selection: the dialog opens prefilled with the plain drag size. +#[test] +fn alt_drag_with_a_selection_opens_the_dialog_at_the_dragged_size() { + let mut app = app(); + with_selection(&mut app); + let before = layers(&app); + drag(&mut app, [100.0, 50.0], [200.0, 150.0], Modifiers::ALT, Modifiers::ALT); + let (_id, f) = only_dialog(&app); + assert_eq!((f["width"].as_f64(), f["height"].as_f64()), (Some(100.0), Some(100.0)), "the plain drag size, not twice it"); + assert_eq!(layers(&app), before, "the drag itself creates nothing"); +} + +/// #12090 measured, with a selection: entering 120 / 80 and OK gives a 120 × 80 box whose +/// top-left is the drag start. +#[test] +fn confirming_with_a_selection_puts_the_top_left_at_the_drag_start() { + let mut app = app(); + with_selection(&mut app); + drag(&mut app, [100.0, 50.0], [200.0, 150.0], Modifiers::ALT, Modifiers::ALT); + let (d, _) = only_dialog(&app); + set(&mut app, d, [("width", json!(120.0)), ("height", json!(80.0))]); + let steps = history_len(&app); + let ctx = app.ctx().clone(); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + assert_eq!(history_len(&app), steps + 1, "OK adds one History state"); + let ed = app.ui.text_edit.as_ref().expect("OK starts the mask session"); + assert!(ed.mask, "the session is a Type Mask"); + assert_eq!(box_rect(&app, ed.layer), [100.0, 50.0, 120.0, 80.0], "top-left at the drag start, the typed size"); +} + +/// #12090 Done when: Cancel makes nothing and adds no History state. +#[test] +fn cancelling_creates_nothing_and_adds_no_history_state() { + let mut app = app(); + drag(&mut app, [200.0, 100.0], [250.0, 125.0], Modifiers::ALT, Modifiers::ALT); + let (id, _) = only_dialog(&app); + let before = layers(&app); + let steps = history_len(&app); + let ctx = app.ctx().clone(); + app.framework.dialogs.cancel_by_id(&ctx, id); + frame(&mut app); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "Cancel closes the dialog"); + assert_eq!(layers(&app), before, "Cancel creates nothing"); + assert_eq!(history_len(&app), steps, "and adds no History state"); + assert!(app.ui.text_edit.is_none(), "and starts no edit session"); +} + +/// Unchanged (guard test, passes before and after): a plain drag with the Vertical Type Mask tool +/// still makes the box at once, without a dialog. +#[test] +fn a_plain_drag_still_makes_the_mask_box_at_once() { + let mut app = app(); + drag(&mut app, [100.0, 50.0], [200.0, 150.0], Modifiers::NONE, Modifiers::NONE); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "a plain drag opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert!(ed.mask, "the session is a Type Mask"); + assert_eq!(box_rect(&app, ed.layer), [100.0, 50.0, 100.0, 100.0]); +} + +/// Not measured (Alt+click without a drag), so today's behaviour, pinned: a point mask. +#[test] +fn an_alt_click_without_a_drag_still_starts_a_point_mask() { + let mut app = app(); + drag(&mut app, [50.0, 100.0], [50.0, 100.0], Modifiers::ALT, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "a click opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the click starts a point mask"); + assert!(ed.mask, "the session is a Type Mask"); + let t = text(&app, ed.layer); + assert!(matches!(t.shape, photocraft_doc::text::TextShape::Point), "point text"); + assert_eq!([t.transform.m[4], t.transform.m[5]], [50.0, 100.0]); +} + +/// Guard: pins that the ticket measured the Vertical Type Mask tool only, so the Horizontal +/// Type Mask tool keeps today's gesture — an Alt+drag there commits the plain box at once. +#[test] +fn an_alt_drag_with_the_horizontal_type_mask_tool_still_makes_the_box_directly() { + let mut app = app(); + app.ui.tool = Tool::HorizontalTypeMask; + drag(&mut app, [100.0, 50.0], [200.0, 150.0], Modifiers::ALT, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "the Horizontal Type Mask tool opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert_eq!(text(&app, ed.layer).orientation, photocraft_doc::text::Orientation::Horizontal, "horizontal, as today"); + assert_eq!(box_rect(&app, ed.layer), [100.0, 50.0, 100.0, 100.0], "the plain drag's box"); +} + +/// Not measured (a drag up or to the left of the start), so today's rule whole: the box is the +/// rectangle between the two points, committed at once, with no dialog. Up and left in two tests, +/// as the ticket names both. +#[test] +fn an_upward_alt_drag_keeps_todays_plain_box() { + let mut app = app(); + drag(&mut app, [200.0, 100.0], [150.0, 50.0], Modifiers::ALT, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "an up-and-left ⌥-drag opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert_eq!(box_rect(&app, ed.layer), [150.0, 50.0, 50.0, 50.0], "the rectangle between the two points"); +} + +#[test] +fn a_leftward_alt_drag_keeps_todays_plain_box() { + let mut app = app(); + drag(&mut app, [200.0, 50.0], [150.0, 100.0], Modifiers::ALT, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "a leftward ⌥-drag opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert_eq!(box_rect(&app, ed.layer), [150.0, 50.0, 50.0, 50.0], "the rectangle between the two points"); +} + +/// Guard for the SECOND condition of the gate (`pointer.rs`: `end[0] >= start[0] && end[1] >= start[1]`): +/// a drag up and to the right passes the first condition and fails the second, so it is not the +/// measured down-right configuration and keeps today's plain box, with no dialog. Green before and +/// after this PR on purpose: it pins the axis the gate turns on. +#[test] +fn an_up_right_alt_drag_keeps_todays_plain_box() { + let mut app = app(); + drag(&mut app, [200.0, 100.0], [250.0, 50.0], Modifiers::ALT, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "an up-right ⌥-drag opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert_eq!(box_rect(&app, ed.layer), [200.0, 50.0, 50.0, 50.0], "the rectangle between the two points"); +} + +/// Guard: the ⌥ latch the dialog route already has (#12044) — ⌥ pressed only as the button comes +/// up opens nothing, the mask drag stays today's plain box. +#[test] +fn alt_pressed_mid_drag_makes_the_plain_mask_box_instead() { + let mut app = app(); + drag(&mut app, [100.0, 50.0], [200.0, 150.0], Modifiers::NONE, Modifiers::ALT); + assert!(app.framework.dialogs.open_dialogs().is_empty(), "⌥ pressed mid-drag opens no dialog"); + let ed = app.ui.text_edit.as_ref().expect("the drag starts the mask at once"); + assert_eq!(box_rect(&app, ed.layer), [100.0, 50.0, 100.0, 100.0], "the plain drag's box"); +} + +/// The latch the other way: ⌥ held at the press and let go mid-drag still opens the dialog (the +/// rule reads the press, as the Type tool's route does). +#[test] +fn alt_released_mid_drag_still_opens_the_dialog() { + let mut app = app(); + let before = layers(&app); + drag(&mut app, [200.0, 100.0], [250.0, 125.0], Modifiers::ALT, Modifiers::NONE); + let (_id, f) = only_dialog(&app); + assert_eq!((f["width"].as_f64(), f["height"].as_f64()), (Some(100.0), Some(50.0)), "twice the 50 × 25 drag"); + assert_eq!(layers(&app), before, "the drag itself still creates nothing"); +}