diff --git a/crates/ui-egui/src/tools/type_tool/defaults.rs b/crates/ui-egui/src/tools/type_tool/defaults.rs index 48b9408096d..0c62fbd6698 100644 --- a/crates/ui-egui/src/tools/type_tool/defaults.rs +++ b/crates/ui-egui/src/tools/type_tool/defaults.rs @@ -230,6 +230,8 @@ pub(super) fn apply(app: &mut PhotocraftApp, props: serde_json::Value, coalesce: if insertion_style::set_font(app, &props) { return; } + // A font chosen at the caret and not yet typed outlives another character change there (#10996). + let kept = insertion_style::pending(app).cloned(); app.ui.type_insertion_style = None; let Some(mut p) = formatting_params(app) else { apply_defaults(app, &props); @@ -243,7 +245,11 @@ pub(super) fn apply(app: &mut PhotocraftApp, props: serde_json::Value, coalesce: if let Some(key) = app.ui.text_edit.as_ref().map(|ed| ed.session.clone()).or(coalesce) { p["coalesce"] = json!(key); } - let _ = app.run("type.setStyle", p); + if app.run("type.setStyle", p).is_ok() + && let Some(kept) = kept + { + insertion_style::rebase(app, kept); + } } /// Open the shared Color Picker with a snapshot of the text target. Sampling or cancelling diff --git a/crates/ui-egui/src/tools/type_tool/font_commit_tests.rs b/crates/ui-egui/src/tools/type_tool/font_commit_tests.rs index 9d1c96e054d..7451cf30acf 100644 --- a/crates/ui-egui/src/tools/type_tool/font_commit_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/font_commit_tests.rs @@ -385,3 +385,26 @@ fn an_existing_empty_layer_keeps_its_font_when_committed() { assert_eq!(families(&saved(&h, id)), [FONTS[1]]); } } + +/// A font chosen at the caret survives a Digits change there before typing (#10996): the typed digit takes the +/// chosen font and the picked digit style. +#[test] +fn a_digits_change_at_the_caret_keeps_the_chosen_typing_font() { + let (mut h, id) = fixture(0, "abcdef"); + select(&mut h, [3, 3]); + h.run(); + choose(&mut h, FONTS[1], "enter"); + assert!(super::insertion_style::pending(h.state()).is_some(), "a pending font at the caret"); + super::apply(h.state_mut(), json!({"digits": "devanagari"}), None); + assert!(super::insertion_style::pending(h.state()).is_some(), "the Digits change keeps the pending font"); + super::host::type_text(h.state_mut(), "5"); + h.run(); + assert_eq!(families(&saved(&h, id))[3], FONTS[1], "the typed digit takes the chosen font"); + let t = layer(&h.state().session.active().unwrap().doc, id); + let mut offset = 0; + let digits = t.char_runs().into_iter().find_map(|r| { + offset += r.len; + (offset > 3).then_some(r.style.digits) + }); + assert_eq!(digits, Some(photocraft_doc::text::Digits::Devanagari), "the typed digit takes the picked digit style"); +} diff --git a/crates/ui-egui/src/tools/type_tool/insertion_style.rs b/crates/ui-egui/src/tools/type_tool/insertion_style.rs index 05a2624c99a..a8c33c2e2b0 100644 --- a/crates/ui-egui/src/tools/type_tool/insertion_style.rs +++ b/crates/ui-egui/src/tools/type_tool/insertion_style.rs @@ -49,6 +49,23 @@ pub(super) fn set_font(app: &mut PhotocraftApp, props: &serde_json::Value) -> bo true } +/// Keeps the caret's pending font `kept` after another character change at the same caret: the style the command +/// stored there (in the layer's own units) with the chosen font on it, for the new revision, so the next typed text +/// still takes the chosen font and the change. +pub(super) fn rebase(app: &mut PhotocraftApp, mut kept: TypeInsertionStyle) { + let Some((mut style, _)) = styles_at(app) else { return }; + let Some(st) = app.session.active() else { return }; + let chosen = &kept.style; + style.font_family.clone_from(&chosen.font_family); + style.weight = chosen.weight; + style.italic = chosen.italic; + style.postscript_name.clone_from(&chosen.postscript_name); + style.variations.clone_from(&chosen.variations); + kept.style = style; + kept.revision = st.revision; + app.ui.type_insertion_style = Some(kept); +} + /// How many characters `text` adds once the engine stores it: a CRLF pair becomes one `\n` (a lone CR is one /// character either way), so a pasted Windows line break moves the caret by one, not two. pub(super) fn stored_chars(text: &str) -> usize {