From c06f404773bd4bebbd22be7dd8d36135e102ef9c Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Tue, 1 Dec 2026 16:32:29 +0530 Subject: [PATCH] ui-egui: keep an axis change at the caret over the chosen font's saved axes rebase put the chosen font's saved variations back after every change, so an axis change at the caret reverted. Axes the change sets now win; the others keep their chosen values. --- .../ui-egui/src/tools/type_tool/defaults.rs | 2 +- .../src/tools/type_tool/font_commit_tests.rs | 26 +++++++++++++++++++ .../src/tools/type_tool/insertion_style.rs | 12 ++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/tools/type_tool/defaults.rs b/crates/ui-egui/src/tools/type_tool/defaults.rs index 0c62fbd669..b5e483fb24 100644 --- a/crates/ui-egui/src/tools/type_tool/defaults.rs +++ b/crates/ui-egui/src/tools/type_tool/defaults.rs @@ -248,7 +248,7 @@ pub(super) fn apply(app: &mut PhotocraftApp, props: serde_json::Value, coalesce: if app.run("type.setStyle", p).is_ok() && let Some(kept) = kept { - insertion_style::rebase(app, kept); + insertion_style::rebase(app, kept, &props); } } 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 7451cf30ac..81fa78c371 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 @@ -408,3 +408,29 @@ fn a_digits_change_at_the_caret_keeps_the_chosen_typing_font() { }); assert_eq!(digits, Some(photocraft_doc::text::Digits::Devanagari), "the typed digit takes the picked digit style"); } + +/// An axis change at the caret after a font was chosen there wins over the chosen font's saved axes; the axes it does +/// not set keep their chosen values. +#[test] +fn an_axis_change_at_the_caret_keeps_its_value_with_the_chosen_typing_font() { + let (mut h, id) = fixture(0, "abcdef"); + h.state_mut().run("type.setStyle", json!({"layer": id.0, "variations": {"wght": 650, "wdth": 95}})).unwrap(); + select(&mut h, [3, 3]); + h.run(); + choose(&mut h, FONTS[1], "enter"); + super::apply(h.state_mut(), json!({"variations": {"wdth": 110}}), None); + super::host::type_text(h.state_mut(), "X"); + h.run(); + assert_eq!(families(&saved(&h, id))[3], FONTS[1], "the typed letter takes the chosen font"); + let mut offset = 0; + let axes = layer(&h.state().session.active().unwrap().doc, id) + .char_runs() + .into_iter() + .find_map(|r| { + offset += r.len; + (offset > 3).then_some(r.style.variations) + }) + .unwrap(); + assert!(axes.iter().any(|v| v.axis == "wdth" && v.value == 110.0), "the changed axis holds: {axes:?}"); + assert!(axes.iter().any(|v| v.axis == "wght" && v.value == 650.0), "an axis the change left keeps its value: {axes:?}"); +} 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 a8c33c2e2b..aa6e023bd0 100644 --- a/crates/ui-egui/src/tools/type_tool/insertion_style.rs +++ b/crates/ui-egui/src/tools/type_tool/insertion_style.rs @@ -52,7 +52,7 @@ pub(super) fn set_font(app: &mut PhotocraftApp, props: &serde_json::Value) -> bo /// 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) { +pub(super) fn rebase(app: &mut PhotocraftApp, mut kept: TypeInsertionStyle, props: &serde_json::Value) { let Some((mut style, _)) = styles_at(app) else { return }; let Some(st) = app.session.active() else { return }; let chosen = &kept.style; @@ -61,6 +61,16 @@ pub(super) fn rebase(app: &mut PhotocraftApp, mut kept: TypeInsertionStyle) { style.italic = chosen.italic; style.postscript_name.clone_from(&chosen.postscript_name); style.variations.clone_from(&chosen.variations); + // An axis the change set itself wins over the chosen font's saved value; the others stay as chosen. + if let Some(axes) = props.get("variations").and_then(serde_json::Value::as_object) { + for (axis, value) in axes { + let Some(value) = value.as_f64() else { continue }; + match style.variations.iter_mut().find(|v| v.axis == *axis) { + Some(v) => v.value = value as f32, + None => style.variations.push(photocraft_doc::text::FontVariation { axis: axis.clone(), value: value as f32 }), + } + } + } kept.style = style; kept.revision = st.revision; app.ui.type_insertion_style = Some(kept);