diff --git a/crates/ui-egui/src/dialogs/paragraph_text_size.rs b/crates/ui-egui/src/dialogs/paragraph_text_size.rs index 43dc174947c..7e29c0908da 100644 --- a/crates/ui-egui/src/dialogs/paragraph_text_size.rs +++ b/crates/ui-egui/src/dialogs/paragraph_text_size.rs @@ -11,8 +11,11 @@ //! rectangle between the two points), and Cancel beyond closing the dialog is not specified. //! //! 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 and the -//! document it was made in are not fields: they are the family's captured value, read by [`run`]. +//! 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). use craft::ui::dialog::DialogId; use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamDialog, ParamResult}; @@ -43,13 +46,20 @@ fn build(w: f64, h: f64) -> impl for<'b> Fn(&'b mut DialogFields) -> ParamDialog } /// Open the dialog on the box an Alt+drag drew: top-left `(x, y)`, size `w` × `h`, in document -/// pixels and exactly as dragged (the fields keep the decimals). The top-left and the document the -/// drag was made in are captured for [`run`]: OK creates the text only while that document is -/// still the active one. +/// pixels and exactly as dragged (the fields keep the decimals). The top-left, the document the drag +/// was made in and the settings the new layer is built from are captured for [`run`]: OK creates the +/// text only while that document is still the active one, and with the tool and options that were +/// active at the drag. +/// +/// The settings are snapshotted here rather than read at OK because the reference application's +/// dialog is modal: nothing can change the tool (horizontal or vertical) or its options between the +/// 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 { let captured = json!({ "at": [at[0], at[1]], "document": app.session.active().map(|st| json!(st.doc.id)).unwrap_or(Value::Null), + "settings": crate::type_tool::TypeSnapshot::live(app), }); let fields = DialogFields::from_pairs([(WIDTH, FieldValue::Number(w)), (HEIGHT, FieldValue::Number(h))]); params::open_param_unsaved(app, KEY, fields, captured, build(w, h)) @@ -73,10 +83,17 @@ 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; }; - let mut p = crate::type_tool::create_params(app); + // 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). + let Ok(settings) = serde_json::from_value(captured.get("settings").cloned().unwrap_or(Value::Null)) else { + crate::dialogs::alerts::report_error(app, "the Paragraph Text Size dialog has no settings snapshot"); + return; + }; + let mut p = crate::type_tool::create_params(app, true, &settings); p["box"] = json!([x, y, w, h]); // A refused create already said why in the status bar, as the gesture's own create does. - if crate::type_tool::create_layer(app, p, None).is_ok() { + if crate::type_tool::create_layer(app, p, None, &settings).is_ok() { crate::type_tool::focus_editor(ctx); } } 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 5b2fed9b02d..475d1716d9c 100644 --- a/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs +++ b/crates/ui-egui/src/dialogs/paragraph_text_size/tests.rs @@ -4,7 +4,8 @@ //! route are checked to keep today's behaviour: a plain drag still makes the box directly, Alt+click //! is still point text, the vertical Type tool keeps its plain drag, ⌥ is read at the press so it can //! come up or go down mid-drag, a second drag while the dialog is open is swallowed by that dialog, -//! and a name the dialog does not show as a field is refused. +//! the layer keeps the tool, the type options and the placeholder preference that were active when +//! the dialog opened, and a name the dialog does not show as a field is refused. //! //! The gestures go through the canvas pass (`canvas::testing`), the way the released Type tool does //! on the hosted framework: a press with ⌥ held, a move, a release. The dialog is driven the way a @@ -232,16 +233,95 @@ fn an_alt_drag_with_the_vertical_type_tool_still_makes_the_box_directly() { assert_eq!(box_rect(&app, ed.layer), [10.0, 10.0, 100.0, 50.0], "the plain drag's box"); } -/// Guard (pattern 9, no guessing from a name): the drag's top-left and the document it was made in -/// are the family's captured value, not fields - a control `set_json_field` for a name the dialog -/// does not show is refused, so the geometry cannot be pushed in through the field map. +/// The point size of the new layer's first run, as `type.create` stored it. +fn size_pt(app: &PhotocraftApp, id: u64) -> f32 { + text(app, id).char_runs().into_iter().next().expect("the new layer has one run").style.size_pt +} + +/// #12044 review: the reference dialog is modal, so nothing can change the tool or its options +/// between the Alt+drag and OK. The dialog therefore snapshots the tool, the type options and the +/// placeholder preference when it opens, and OK creates from that snapshot: a control that switches +/// to the vertical Type tool and changes the size while the dialog is open does not leak into the +/// new layer, which stays horizontal at the size the drag had. +#[test] +fn the_layer_keeps_the_tool_and_options_active_at_the_drag() { + let mut app = app(); + app.ui.tool_options.type_font = "Drag Font".into(); + app.ui.tool_options.type_style = "Regular".into(); + app.ui.tool_options.type_size = 24.0; + drag(&mut app, [10.0, 10.0], [109.5, 60.0], Modifiers::ALT, Modifiers::ALT); + let (d, _) = only_dialog(&app); + // Change the settings the dialog is about to read, while it is open. + app.ui.tool = Tool::VerticalType; + app.ui.tool_options.type_font = "Later Font".into(); + app.ui.tool_options.type_size = 72.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 edit session, as the drag does"); + let t = text(&app, ed.layer); + assert_eq!(t.orientation, photocraft_doc::text::Orientation::Horizontal, "the tool held at the drag, not the one at OK"); + assert_eq!(size_pt(&app, ed.layer), 24.0, "the size held at the drag, not the one at OK"); + assert_eq!(t.char_runs()[0].style.font_family, "Drag Font", "and the family held at the drag"); + assert_eq!(box_rect(&app, ed.layer), [10.0, 10.0, 99.5, 50.0], "still the box the drag drew"); +} + +/// The same snapshot covers the placeholder preference: turning "Fill new type layers with +/// placeholder text" off while the dialog is open does not leave the new layer empty. +#[test] +fn the_layer_keeps_the_placeholder_preference_active_at_the_drag() { + let mut app = app(); + drag(&mut app, [10.0, 10.0], [109.5, 60.0], Modifiers::ALT, Modifiers::ALT); + let (d, _) = only_dialog(&app); + app.session.edit_prefs(|p| p.type_.fill_new_type_layers_with_placeholder = false); + 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 edit session, as the drag does"); + assert_eq!(text(&app, ed.layer).text, PLACEHOLDER, "the preference held at the drag, not the one at OK"); + assert_eq!((ed.anchor, ed.caret), (0, PLACEHOLDER.chars().count()), "and its placeholder stays selected"); +} + +/// Not changed by the snapshot: a plain drag reads the tool and its options at the moment it ends. +/// Pinned so the snapshot stays inside the dialog's route. +#[test] +fn a_plain_drag_reads_the_settings_at_its_own_release() { + let mut app = app(); + app.ui.tool = Tool::VerticalType; + app.ui.tool_options.type_font = "Drag Font".into(); + app.ui.tool_options.type_size = 24.0; + drag(&mut app, [10.0, 10.0], [110.0, 60.0], Modifiers::NONE, Modifiers::NONE); + let ed = app.ui.text_edit.as_ref().expect("the drag creates the text at once"); + let t = text(&app, ed.layer); + assert_eq!(t.orientation, photocraft_doc::text::Orientation::Vertical, "the tool in hand at the release"); + assert_eq!(size_pt(&app, ed.layer), 24.0, "and the options in hand at the release"); + assert_eq!(t.char_runs()[0].style.font_family, "Drag Font"); +} + +/// The other unchanged neighbour: a click reads the live settings too, including the placeholder +/// preference as it is at that moment. +#[test] +fn a_click_reads_the_settings_at_its_own_release() { + let mut app = app(); + app.session.edit_prefs(|p| p.type_.fill_new_type_layers_with_placeholder = false); + app.ui.tool_options.type_size = 30.0; + drag(&mut app, [50.0, 100.0], [50.0, 100.0], Modifiers::NONE, Modifiers::NONE); + let ed = app.ui.text_edit.as_ref().expect("the click creates point text"); + let t = text(&app, ed.layer); + assert!(matches!(t.shape, photocraft_doc::text::TextShape::Point), "point text"); + assert_eq!(t.text, "", "the preference as it stands at the click"); + assert_eq!(size_pt(&app, ed.layer), 30.0, "and the options as they stand at the click"); +} + +/// Guard (pattern 9, no guessing from a name): the drag's top-left, the document it was made in and +/// the settings snapshot are the family's captured value, not fields - a control `set_json_field` +/// for a name the dialog does not show is refused, so neither the geometry nor the tool and its +/// options can be pushed in through the field map. #[test] fn a_name_the_dialog_does_not_show_as_a_field_is_refused() { let mut app = app(); 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", "box", "text"] { + for field in ["at", "document", "settings", "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 c26c8401020..1f39f6da3bd 100644 --- a/crates/ui-egui/src/tools/type_tool.rs +++ b/crates/ui-egui/src/tools/type_tool.rs @@ -54,7 +54,7 @@ pub(crate) use panel::toggle_opentype; pub use panel::{character_panel, type_properties}; #[cfg(test)] pub(crate) use pointer::{PathClick, path_target}; -pub(crate) use pointer::{TypeState, box_shape, create_layer, create_params, drain, tool}; +pub(crate) use pointer::{TypeSnapshot, TypeState, box_shape, create_layer, create_params, drain, tool}; pub(crate) use values::doc_unit; pub const PLACEHOLDER: &str = "Lorem Ipsum"; diff --git a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs index d88505cb7a9..0cd9005cf71 100644 --- a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs @@ -417,7 +417,7 @@ mod panel_defaults { let mut app = new_app(); app.ui.tool_options.type_style_defaults = json!({"tracking": 20, "text": "Injected", "box": [0, 0, 2, 2], "coalesce": "unrelated"}).as_object().unwrap().clone(); - let p = super::super::default_style_params(&app); + let p = super::super::default_style_params(&app, &app.ui.tool_options); assert_eq!(p["tracking"], 20); assert!(p.get("text").is_none() && p.get("box").is_none() && p.get("coalesce").is_none()); app.ui.tool_options.type_style_defaults = json!({ @@ -427,7 +427,7 @@ mod panel_defaults { .as_object() .unwrap() .clone(); - let p = super::super::default_style_params(&app); + let p = super::super::default_style_params(&app, &app.ui.tool_options); for key in ["tracking", "leading", "spaceAfter", "variations", "features"] { assert!(p.get(key).is_none(), "hostile {key} is ignored"); } diff --git a/crates/ui-egui/src/tools/type_tool/defaults.rs b/crates/ui-egui/src/tools/type_tool/defaults.rs index 9477b2e60b0..48b9408096d 100644 --- a/crates/ui-egui/src/tools/type_tool/defaults.rs +++ b/crates/ui-egui/src/tools/type_tool/defaults.rs @@ -59,9 +59,10 @@ pub(super) fn shown_scale(app: &PhotocraftApp) -> f32 { /// The styles a new Type-tool layer will receive. Use the same command parameters for the /// panels and creation so defaults also survive action recording/replay. The older options-bar -/// fields remain authoritative, and text colour continues to follow the foreground colour. -pub(super) fn default_style_params(app: &PhotocraftApp) -> serde_json::Value { - let o = &app.ui.tool_options; +/// fields remain authoritative, and text colour continues to follow the foreground colour. `o` is +/// the options-bar state to read: today's live one for a panel or a plain gesture, a snapshot taken +/// when the Paragraph Text Size dialog opened for its OK (#12044). +pub(super) fn default_style_params(app: &PhotocraftApp, o: &crate::state::ToolOptions) -> serde_json::Value { let mut p = json!({}); // Settings are editable JSON: only recognized formatting keys may reach type.create. // In particular a stray `text`, `box`, or `coalesce` key must not become a create parameter. @@ -149,7 +150,7 @@ fn finite_style_value(value: &serde_json::Value) -> bool { pub(super) fn default_styles(app: &PhotocraftApp) -> (photocraft_doc::text::CharStyle, photocraft_doc::text::ParagraphStyle) { let (mut c, mut para) = app.session.type_defaults.clone().unwrap_or_default(); - let mut p = default_style_params(app); + let mut p = default_style_params(app, &app.ui.tool_options); // The saved panels can appear on the very first frame. Style parsing resolves the named // font through shared(), which waits for the font scan; display the saved names meanwhile, // and let fonts_ready/axis_sliders expose the font controls when try_shared is ready. diff --git a/crates/ui-egui/src/tools/type_tool/pointer.rs b/crates/ui-egui/src/tools/type_tool/pointer.rs index 0875c48b3fa..304ad523eca 100644 --- a/crates/ui-egui/src/tools/type_tool/pointer.rs +++ b/crates/ui-egui/src/tools/type_tool/pointer.rs @@ -215,43 +215,67 @@ fn press_at(app: &mut PhotocraftApp, ctx: &egui::Context, x: f64, y: f64, mods: ToolAction::Done } +/// The settings a `type.create` is built from: the tool in hand (horizontal or vertical, and which +/// Type Mask), the options bar's type settings and the placeholder preference. A plain drag or a +/// click reads them live at its release, through [`TypeSnapshot::live`]. The Paragraph Text Size +/// dialog instead takes the snapshot when it opens and hands it to its OK (#12044): that dialog is +/// modal in the reference application, so nothing can change the tool or its options between the +/// Alt+drag and OK, and the box it makes uses what was active at the drag even when a control +/// changes them while the dialog is open. +#[derive(Clone, serde::Serialize, serde::Deserialize)] +pub(crate) struct TypeSnapshot { + pub tool: crate::state::Tool, + pub options: crate::state::ToolOptions, + pub placeholder: bool, +} + +impl TypeSnapshot { + /// Read from the app as it stands at this moment: today's live reading, what the plain drag and + /// the click create with. + pub(crate) fn live(app: &PhotocraftApp) -> Self { + Self { tool: app.ui.tool, options: app.ui.tool_options.clone(), placeholder: app.session.prefs().type_.fill_new_type_layers_with_placeholder } + } +} + /// The `type.create` parameters a Type-tool gesture ending now starts from: the placeholder or /// empty text, the tool's orientation and the options bar's styles. The caller adds the geometry /// (`box` for a dragged paragraph, `x`/`y` for point text), and [`create_layer`] adds the session's /// `coalesce` key. `is_box` says whether the gesture that ends was a paragraph drag, which #12077's -/// mask style applies only to a click. -pub(crate) fn create_params(app: &PhotocraftApp, is_box: bool) -> serde_json::Value { - let mask = app.ui.tool.is_type_mask(); +/// mask style applies only to a click; `s` names the tool and settings to create with. +pub(crate) fn create_params(app: &PhotocraftApp, is_box: bool, s: &TypeSnapshot) -> serde_json::Value { + let mask = s.tool.is_type_mask(); // Preferences ▸ Type: "Fill new type layers with placeholder text" (on by default). The // Horizontal Type Mask gets the Type tool's placeholder, as Photoshop's does (#12072); the // vertical mask's placeholder is not measured yet, so it keeps starting empty. - let placeholder = app.ui.tool != crate::state::Tool::VerticalTypeMask && app.session.prefs().type_.fill_new_type_layers_with_placeholder; + let placeholder = s.tool != crate::state::Tool::VerticalTypeMask && s.placeholder; let text = if placeholder { PLACEHOLDER } else { "" }; - let mut p = default_style_params(app); + let mut p = default_style_params(app, &s.options); // #12077: the measured case - a click, not a paragraph box, with the Horizontal Type Mask over // a horizontal text layer - starts the mask in that layer's font, style and size, as the // options bar shows them for the layer. The vertical mask and a box drag are not measured in // the issue, so they keep today's tool options. if mask && !is_box - && app.ui.tool == crate::state::Tool::HorizontalTypeMask + && s.tool == crate::state::Tool::HorizontalTypeMask && let Some(style) = mask_style_from_active_layer(app) && let (Some(params), Some(style)) = (p.as_object_mut(), style.as_object()) { params.extend(style.clone()); } p["text"] = json!(text); - p["orientation"] = json!(if app.ui.tool.is_vertical_type() { "vertical" } else { "horizontal" }); + p["orientation"] = json!(if s.tool.is_vertical_type() { "vertical" } else { "horizontal" }); p } /// Create the type layer `p` describes, and finish what a released Type-tool gesture does with it: /// the path target of a click, the options bar's antialiasing, the Type Mask tools' hidden working -/// layer, and the edit session with the placeholder selected. The id of the new layer, or why -/// creating it failed (the status bar already says so, since [`PhotocraftApp::run`] reported it). -pub(crate) fn create_layer(app: &mut PhotocraftApp, mut p: serde_json::Value, target: Option) -> Result { - let o = app.ui.tool_options.clone(); - let mask = app.ui.tool.is_type_mask(); +/// layer, and the edit session with the placeholder selected. `s` is the snapshot `p` was built +/// from, so the antialiasing and the Type Mask handling follow those settings and not the live ones. +/// The id of the new layer, or why creating it failed (the status bar already says so, since +/// [`PhotocraftApp::run`] reported it). +pub(crate) fn create_layer(app: &mut PhotocraftApp, mut p: serde_json::Value, target: Option, s: &TypeSnapshot) -> Result { + let o = &s.options; + let mask = s.tool.is_type_mask(); let n = p.get("text").and_then(serde_json::Value::as_str).unwrap_or("").chars().count(); let key = session_key(app); p["coalesce"] = json!(key); @@ -303,14 +327,17 @@ fn create_text(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2], alt_at_p // 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. let target = if !mask && !is_box { path_target(app, start[0], start[1]) } else { None }; - let mut p = create_params(app, is_box); + // A plain drag or click reads the tool, its options and the placeholder preference as they + // stand at its release, as they always have (#12044). + let snapshot = TypeSnapshot::live(app); + let mut p = create_params(app, is_box, &snapshot); if is_box { p["box"] = json!([start[0].min(end[0]).round(), start[1].min(end[1]).round(), w.round(), h.round()]); } else { p["x"] = json!(start[0].round()); p["y"] = json!(start[1].round()); } - let _ = create_layer(app, p, target); + let _ = create_layer(app, p, target, &snapshot); } impl CanvasTool for TypeTool<'_> { diff --git a/crates/ui-egui/src/tools/type_tool/tests.rs b/crates/ui-egui/src/tools/type_tool/tests.rs index 35adeb9e3da..f0a254b9507 100644 --- a/crates/ui-egui/src/tools/type_tool/tests.rs +++ b/crates/ui-egui/src/tools/type_tool/tests.rs @@ -56,7 +56,7 @@ fn layer_text(app: &PhotocraftApp) -> String { fn stacked_variant(app: &mut PhotocraftApp, s: &str) -> LayerId { // A click on the stack's spot would edit the text under it, so the layer is made where the click would. app.ui.text_edit = None; - let mut p = default_style_params(app); + let mut p = default_style_params(app, &app.ui.tool_options); p["text"] = json!(s); p["orientation"] = json!("horizontal"); p["x"] = json!(50.0);