diff --git a/crates/ui-egui/src/dialogs/char_style_options.rs b/crates/ui-egui/src/dialogs/char_style_options.rs index 9fcb532c33..c94169d659 100644 --- a/crates/ui-egui/src/dialogs/char_style_options.rs +++ b/crates/ui-egui/src/dialogs/char_style_options.rs @@ -4,9 +4,11 @@ //! Each field is unset (empty, absent, or a checkbox in its unset state) or set, and an unset field leaves //! its attribute out of the style. A field still as it opened keeps the style's stored value as it is. +use std::ops::RangeInclusive; use std::sync::Arc; use craft::behavior::Mixed; +use craft::text::ui::character::{AKI_PRESETS, DIACRITIC_PRESETS, TSUME_PRESETS}; use craft::ui::choice::Checkbox; use craft::ui::contract::{Bind, Entry, Outcome as _, Props as _, Show as _}; use craft::ui::dialog::{DialogResult, FieldRefusal}; @@ -14,7 +16,8 @@ use craft::ui::patterns::form_dialogs::{ DialogFields, FieldValue, HostedPropertiesDialog, Param, ParamKind, PropertiesDialog, PropertiesTab, PropertyRow, TabLayout, }; use craft::ui::pickers::Select; -use craft::ui::runtime::env::Label; +use craft::ui::runtime::env::{Env, Label}; +use craft::ui::text::{NumberField, NumberPick}; use serde_json::{Map, Value, json}; use vectorcraft_doc::setup::DocSetup; @@ -895,7 +898,39 @@ fn basic_rows() -> Vec { ]) } -// PLAN: the Aki, Tsume and Diacritic rows are plain text rows: `Param::text` has no preset menu to carry the Character panel's presets. +/// A number row over the text field `key` (empty for unset) whose menu offers `presets`, as the Character panel's field +/// of the same name does. A typed value stays within `range`; automation sets the field's text, as for a text row. +fn preset_number(key: &'static str, label: String, range: RangeInclusive, presets: &'static [f32]) -> Param { + let name = label.clone(); + Param::custom(key, label, move |ui, fields, _| { + let value = fields.text(key).and_then(|text| text.trim().parse::().ok()).filter(|n| n.is_finite()); + let units = Env::of(ui).units(); + let rows: Vec> = presets + .iter() + .map(|&p| { + let n = f64::from(p); + Entry::new(NumberPick::Number(n), units.number(n, 0..=2)) + }) + .collect(); + let out = NumberField::new(name.clone(), Bind::given(value)).range(range.clone()).presets(&rows).id_salt(key).show(ui); + if let Some(picked) = out.edit.value() { + let text = picked.map(|n| n.to_string()).unwrap_or_default(); + if fields.text(key) != Some(text.as_str()) { + fields.set(key, FieldValue::Text(text)); + } + } + out.edit.response.clone() + }) + // Set from outside: the field's text, as a text row takes it. + .on_set(|fields, key, value| match value { + Value::String(text) => { + fields.set(key, FieldValue::Text(text)); + Ok(()) + } + other => Err(FieldRefusal::Rejects(&other).message(key)), + }) +} + fn advanced_rows() -> Vec { let mut params: Vec = [ ("h_scale", tl!("Horizontal Scale"), "%"), @@ -904,15 +939,20 @@ fn advanced_rows() -> Vec { ("tcyUpDown", tl!("Tate-chu-yoko Up/Down"), "pt"), ("tcyLeftRight", tl!("Tate-chu-yoko Left/Right"), "pt"), ("rotation", tl!("Character Rotation"), "°"), - ("akiLeft", tl!("Insert Aki Left"), ""), - ("akiRight", tl!("Insert Aki Right"), ""), - ("tsume", tl!("Tsume"), "%"), - ("diacriticHorizontal", tl!("Adjust Horizontal Diacritic Position"), ""), - ("diacriticVertical", tl!("Adjust Vertical Diacritic Position"), ""), ] .into_iter() - .map(|(key, caption, unit)| text(key, if unit.is_empty() { caption.to_string() } else { format!("{caption} ({unit})") })) + .map(|(key, caption, unit)| text(key, format!("{caption} ({unit})"))) .collect(); + let aki = 0.0..=vectorcraft_doc::text::MAX_AKI; + let tsume = 0.0..=vectorcraft_doc::text::MAX_TSUME; + let diacritic = -vectorcraft_doc::text::MAX_DIACRITIC_POSITION..=vectorcraft_doc::text::MAX_DIACRITIC_POSITION; + params.extend([ + preset_number("akiLeft", tl!("Insert Aki Left").to_string(), aki.clone(), &AKI_PRESETS), + preset_number("akiRight", tl!("Insert Aki Right").to_string(), aki, &AKI_PRESETS), + preset_number("tsume", format!("{} (%)", tl!("Tsume")), tsume, &TSUME_PRESETS), + preset_number("diacriticHorizontal", tl!("Adjust Horizontal Diacritic Position").to_string(), diacritic.clone(), &DIACRITIC_PRESETS), + preset_number("diacriticVertical", tl!("Adjust Vertical Diacritic Position").to_string(), diacritic, &DIACRITIC_PRESETS), + ]); let languages: Vec<(&'static str, &'static str)> = vectorcraft_doc::setup::LANGUAGES.iter().map(|(name, _, _)| (*name, *name)).collect(); params.push(select("language", tl!("Language:"), &languages)); rows(params) diff --git a/crates/ui-egui/src/dialogs/char_style_options/tests.rs b/crates/ui-egui/src/dialogs/char_style_options/tests.rs index 79884992c4..a5af1854ec 100644 --- a/crates/ui-egui/src/dialogs/char_style_options/tests.rs +++ b/crates/ui-egui/src/dialogs/char_style_options/tests.rs @@ -547,6 +547,41 @@ fn tsume_field_shows_sets_resets_and_commits() { assert!(t.runs.iter().all(|r| r.style.tsume == 75.0)); } +/// The Insert Aki, Tsume and diacritic rows each have the Character panel's presets menu beside the field, and a +/// preset picked from it is the field's value and commits to the style, with each frame drawn once. +#[test] +fn aki_tsume_and_diacritic_rows_pick_from_the_character_panel_presets() { + use craft::ui::testing::Fixture; + let mut fx = Fixture::new(|ui| crate::dialogs::host::window_dialogs(ui.ctx()).with(|host| host.run(ui))); + fx.room(egui::vec2(900.0, 900.0)); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.ui_ctx = Some(fx.ctx().clone()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + styled(&mut app, json!({"size": 24.0}), "Text"); + open(&mut app, "Body"); + set::(&app, "tab", json!("advanced")); + fx.settle(); + + let presets_in_row = |fx: &Fixture, label: &str| { + let field = fx.by_label(label).unwrap_or_else(|| panic!("the {label} field")); + let tree = fx.tree(); + tree.keys().into_iter().filter(|key| tree.name_of(*key) == Some("Presets")).filter_map(|key| fx.target(key)).find(|chevron| { + chevron.rect.center().y > field.rect.top() && chevron.rect.center().y < field.rect.bottom() && chevron.rect.left() >= field.rect.left() + }) + }; + for label in ["Insert Aki Left", "Insert Aki Right", "Tsume (%)", "Adjust Horizontal Diacritic Position", "Adjust Vertical Diacritic Position"] { + assert!(presets_in_row(&fx, label).is_some(), "{label} has a presets menu"); + } + + let chevron = presets_in_row(&fx, "Insert Aki Left").unwrap(); + fx.click(&chevron).settle(); + let pick = fx.by_label("500").expect("the 500 preset"); + fx.click(&pick).settle(); + assert_eq!(fields::(&app)["akiLeft"], json!("500")); + assert!(ok::(&mut app)); + assert_eq!(app.session.doc().unwrap().doc.char_styles[0].attrs, attrs(json!({"size": 24.0, "akiLeft": 500.0}))); +} + /// The negated tags of the twenty stylistic sets: an explicit empty Stylistic Sets choice. fn no_sets() -> Vec { vectorcraft_text::EXTRA_FEATURES[..20].iter().map(|s| format!("-{s}")).collect()