diff --git a/crates/ui-egui/src/datetime_tests.rs b/crates/ui-egui/src/datetime_tests.rs index aef25046a..de6c84b74 100644 --- a/crates/ui-egui/src/datetime_tests.rs +++ b/crates/ui-egui/src/datetime_tests.rs @@ -1,5 +1,4 @@ -//! Insert › Date and Time, driven like a person: the ribbon opens the dialog, a format is -//! chosen, OK inserts it in one undo, Cancel leaves the slide alone. +//! Date and Time inserts the picked field as one undoable edit through the hosted form. use deckcraft_engine::Session; use deckcraft_model::text::RunKind; @@ -10,16 +9,8 @@ use serde_json::json; use crate::{Services, SlideApp}; fn harness() -> Harness<'static, SlideApp> { - // P3(dialogs-w10): sh.A2 - let mut app = SlideApp::new_base(Session::with_new(), Services::default()); - app.ui.tab = "Insert".into(); - let mut h = Harness::builder().with_size(egui::vec2(1800.0, 1100.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); + let builder = Harness::builder().with_size(egui::vec2(1800.0, 1100.0)).with_step_dt(1.0 / 60.0); + let mut h = crate::test_harness::harness_with(Session::with_new(), Services::default(), builder, |_| {}); h.run_steps(3); h } @@ -40,41 +31,44 @@ fn date_run(h: &Harness<'_, SlideApp>) -> Option { .cloned() } -/// Insert opens date/time settings, and OK inserts exactly one field. +/// Accepting the hosted form inserts the chosen field in one undo step. #[test] fn insert_opens_the_dialog_and_ok_inserts_one_field() { let mut h = harness(); - h.get_by_label("Date & Time").click(); + assert!(date_run(&h).is_none()); + let before = h.state().session.active().unwrap().history.undo.len(); + h.state_mut().run("insert.dateTime", json!({})).unwrap(); h.run_steps(2); - assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("dateTime")); - let _ = h.get_by_label("Update automatically"); - assert_eq!(h.get_by_role_and_label(egui::accesskit::Role::ComboBox, "Language").value().as_deref(), Some("English (United States)")); + assert!(crate::dialogs::open_keys(&h.ctx).iter().any(|key| key == "dateTime")); + assert_eq!(h.state().session.active().unwrap().history.undo.len(), before); let now = deckcraft_text::datetime::DateTime::now_local().unwrap(); let sample = deckcraft_text::datetime::field_text("datetime2", Some("en-US"), now).unwrap(); - h.get_by_label(&sample).click(); + h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Date and Time").get_by_label(&sample).click(); h.run_steps(1); - h.get_by_label("OK").click(); + h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Date and Time").get_by_label("OK").click(); h.run_steps(2); - assert!(h.state().dialog.is_none()); + assert!(crate::dialogs::open_keys(&h.ctx).is_empty()); let run = date_run(&h).unwrap(); assert!(matches!(run.kind, RunKind::Field { ref field } if field == "datetime2"), "{run:?}"); - assert_eq!(run.text, sample); - let undo = &h.state().session.active().unwrap().history.undo; - assert_eq!(undo.len(), 1); - assert_eq!(undo[0].label, "Date & Time"); + assert_eq!(h.state().session.active().unwrap().history.undo.len(), before + 1); + // A double insert within one undo step would leave an extra field. + let fields = h + .state() + .session + .active() + .unwrap() + .current_slide() + .unwrap() + .shapes + .iter() + .filter_map(|shape| shape.text.as_ref()) + .flat_map(|text| &text.paragraphs) + .flat_map(|paragraph| ¶graph.runs) + .filter(|run| matches!(run.kind, RunKind::Field { .. })) + .count(); + assert_eq!(fields, 1); h.state_mut().session.execute("edit.undo", &json!({})).unwrap(); h.run_steps(1); assert!(date_run(&h).is_none()); -} - -#[test] -fn cancel_closes_the_dialog_without_a_date() { - let mut h = harness(); - h.get_by_label("Date & Time").click(); - h.run_steps(2); - h.get_by_label("Cancel").click(); - h.run_steps(2); - assert!(h.state().dialog.is_none()); - assert!(date_run(&h).is_none()); - assert!(h.state().session.active().unwrap().history.undo.is_empty()); + assert_eq!(h.state().session.active().unwrap().history.undo.len(), before); } diff --git a/crates/ui-egui/src/dialogs/bullets.rs b/crates/ui-egui/src/dialogs/bullets.rs index 0c8bcf644..8f520ca72 100644 --- a/crates/ui-egui/src/dialogs/bullets.rs +++ b/crates/ui-egui/src/dialogs/bullets.rs @@ -1,17 +1,145 @@ -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; +use craft::ui::contract::Bind; +use craft::ui::dialog::DialogHost; +use craft::ui::patterns::form_dialogs::{ + DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult, ParamTab, SharedRows, +}; +use craft::ui::pickers::{IconPicker, IconPickerGroup, IconPickerItem, IconPickerSearch}; +use craft::ui::runtime::env::UnitKind; +use craft::ui::sections::{DialogRole, FooterButton}; +use egui::Context; +use serde_json::{Value, json}; -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") +use crate::SlideApp; + +const BULLETED_TAB: usize = 0; +const NUMBERED_TAB: usize = 1; +const MIN_SIZE: f64 = 25.0; +const MAX_SIZE: f64 = 400.0; +const MIN_START: i64 = 1; +const MAX_START: i64 = 32767; + +fn styles( + key: &'static str, + label: &'static str, + cells: &'static [(&'static str, &'static str)], + default: &'static str, + wide: bool, +) -> Param { + let groups = vec![IconPickerGroup::new( + "", + cells + .iter() + .map(|(key, glyph)| { + // PLAN: assumed IconPickerItem::wide_glyph(self, wide: bool) -> Self (dialogs-w2) + IconPickerItem::new((*key).to_owned(), *glyph).glyph((*glyph).to_owned()).wide_glyph(wide) + }) + .collect(), + )]; + Param::custom(key, label, move |ui, fields, _| { + let selected = fields.text(key).map(str::to_owned); + let out = IconPicker::new(label, Bind::given(selected), &groups).search(IconPickerSearch::None).show(ui); + if let Some(picked) = out.picked { + fields.set(key, picked); + } + out.response + }) + .opens_with(default) + .full_width() } -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w10): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") +/// Opens the paragraph list styles on the requested tab. +pub(crate) fn open(_app: &SlideApp, ctx: &Context, params: &Value) -> HostedParamDialog { + let tab = match params.get("tab").and_then(Value::as_str) { + Some("numbered") => NUMBERED_TAB, + _ => BULLETED_TAB, + }; + HostedParamDialog::new(ctx, "bullets", DialogFields::new(), |fields| { + let shared = vec![ + Param::new("size", "Size (% of text)", ParamKind::number(Some(MIN_SIZE), Some(MAX_SIZE), 100.0, 1.0, 0)).unit(UnitKind::Percent), + Param::new( + "color", + "Color", + ParamKind::choice( + vec![ + ("".into(), "Automatic".into()), + ("tx1".into(), "Text".into()), + ("accent1".into(), "Accent 1".into()), + ("accent2".into(), "Accent 2".into()), + ("accent3".into(), "Accent 3".into()), + ("accent4".into(), "Accent 4".into()), + ("accent5".into(), "Accent 5".into()), + ("accent6".into(), "Accent 6".into()), + ], + Some(String::new()), + ), + ), + ]; + let tabs = vec![ + ParamTab::new( + "Bulleted", + vec![ + styles( + "char", + "Bullets", + &[("•", "•"), ("○", "○"), ("■", "■"), ("□", "□"), ("◆", "◆"), ("➢", "➢"), ("✓", "✓"), ("–", "–"), ("★", "★")], + "•", + false, + ), + Param::new("custom", "Custom character", ParamKind::text("")), + ], + ), + ParamTab::new( + "Numbered", + vec![ + styles( + "scheme", + "Numbering", + &[ + ("arabicPeriod", "1. 2. 3."), + ("arabicParenR", "1) 2) 3)"), + ("romanUcPeriod", "I. II. III."), + ("romanLcPeriod", "i. ii. iii."), + ("alphaUcPeriod", "A. B. C."), + ("alphaLcParenR", "a) b) c)"), + ("alphaLcPeriod", "a. b. c."), + ], + "arabicPeriod", + true, + ), + Param::new("start", "Start at", ParamKind::int(Some(MIN_START), Some(MAX_START), MIN_START)), + ], + ), + ]; + ParamDialog::new("bullets", "Bullets and Numbering", shared) + .tabs(tabs) + .shared_rows(SharedRows::Below) + .layout(ParamLayout::Document) + .caption_column(true) + .extra_buttons(vec![("none".into(), FooterButton::new("None", DialogRole::Alternative))]) + .values(Bind::bound(fields)) + }) + .tab(tab) +} + +/// Applies the accepted tab's list style or removes that kind of list. +pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> { + let numbered = result.tab == NUMBERED_TAB; + let command = if numbered { "format.numbering" } else { "format.bullets" }; + if result.button.as_deref() == Some("none") { + return app.run(command, json!({"on": false})).map(|_| ()); + } + let fields = &result.fields; + let size = fields.number("size").filter(|value| value.is_finite()).unwrap_or(100.0).clamp(MIN_SIZE, MAX_SIZE); + let mut params = json!({"on": true, "size": size}); + if let Some(color) = fields.text("color").filter(|value| !value.is_empty()) { + params["color"] = json!(color); + } + if numbered { + params["scheme"] = json!(fields.text("scheme").unwrap_or("arabicPeriod")); + params["start"] = json!(fields.int("start").unwrap_or(MIN_START).clamp(MIN_START, MAX_START)); + } else { + let custom = fields.text("custom").unwrap_or("").chars().find(|character| !character.is_whitespace()); + params["char"] = json!(custom.map(String::from).unwrap_or_else(|| fields.text("char").unwrap_or("•").to_owned())); + } + app.run(command, params).map(|_| ()) } diff --git a/crates/ui-egui/src/dialogs/date_time.rs b/crates/ui-egui/src/dialogs/date_time.rs index e8b12b12c..7e321e698 100644 --- a/crates/ui-egui/src/dialogs/date_time.rs +++ b/crates/ui-egui/src/dialogs/date_time.rs @@ -1,4 +1,13 @@ -use super::{DATE_KINDS, Dialog, SlideApp, Tokens, Ui, buttons, check, json}; +use craft::behavior::{Selection, keyed}; +use craft::ui::contract::{Bind, SelectMode, ShowCollection as _}; +use craft::ui::dialog::DialogHost; +use craft::ui::list::{List, ListRow}; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult}; +use deckcraft_text::datetime::DATE_KINDS; +use egui::Context; +use serde_json::{Value, json}; + +use super::SlideApp; const DATE_LANGS: &[(&str, &str)] = &[ ("en-US", "English (United States)"), @@ -26,77 +35,63 @@ pub(super) fn date_rows(lang: &str) -> Vec<(&'static str, String)> { rows } -/// Render language and format controls; OK applies `insert.dateTime`, and the return value says -/// whether the dialog should close. -pub(super) fn date_time_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bool { - ui.set_min_width(440.0); - ui.label("Available formats:"); - let lang = d.get("language", "en-US"); - let rows = date_rows(&lang); - if !rows.iter().any(|(k, _)| *k == d.get("format", "datetime1")) - && let Some((k, _)) = rows.first() - { - d.fields.insert("format".into(), (*k).to_string()); - } - let format = d.get("format", "datetime1"); - egui::ScrollArea::vertical().max_height(280.0).min_scrolled_height(220.0).auto_shrink([false, false]).show(ui, |ui| { - for (kind, sample) in &rows { - if ui.selectable_label(format == *kind, sample).clicked() { - d.fields.insert("format".into(), (*kind).to_string()); - } - } - }); - let lang_label = DATE_LANGS.iter().find(|(k, _)| *k == lang).map(|(_, l)| *l).unwrap_or("English (United States)"); - ui.horizontal(|ui| { - ui.label("Language:"); - // P3(dialogs-w10): earlier framework ComboBox removal (no contract id) - let opened = egui::ComboBox::from_id_salt("date-lang").selected_text(lang_label).width(240.0).show_ui(ui, |ui| { - for (k, l) in DATE_LANGS { - if ui.selectable_label(lang == *k, *l).clicked() { - d.fields.insert("language".into(), (*k).to_string()); +/// Opens date format, language and automatic updating settings. +pub(crate) fn open(_app: &SlideApp, ctx: &Context, _params: &Value) -> HostedParamDialog { + HostedParamDialog::new(ctx, "dateTime", DialogFields::new(), |fields| { + let params = vec![ + Param::custom("format", "Available formats", |ui, fields, _| { + let language = fields.text("language").unwrap_or("en-US"); + let rows = date_rows(language); + if !rows.iter().any(|(kind, _)| Some(*kind) == fields.text("format")) + && let Some((kind, _)) = rows.first() + { + fields.set("format", *kind); } - } - }); - let shown = lang_label.to_string(); - opened.response.widget_info(move || { - let mut info = egui::WidgetInfo::labeled(egui::Role::ComboBox, true, "Language"); - info.current_text_value = Some(shown.clone()); - info - }); - }); - ui.label( - egui::RichText::new("Month names and AM/PM are listed for English. Other languages list the numeric date and 24-hour time.") - .size(11.0) - .color(Tokens::get(ui.ctx()).text_dim), - ); - let update = check(ui, d, "Update automatically", "update", true); - let (ok, cancel) = buttons(ui, "OK"); - if ok { - let lang = d.get("language", "en-US"); - let mut format = d.get("format", "datetime1"); - let probe = deckcraft_text::datetime::DateTime::new(2026, 10, 9, 20, 5, 7); - if deckcraft_text::datetime::field_text(&format, Some(&lang), probe).is_none() { - format = "datetime1".into(); - } - return app.run("insert.dateTime", json!({"format": format, "update": update, "language": lang})).is_ok(); + let current = fields.text("format").map(str::to_owned); + let mut selection = current.clone().map_or_else(Selection::default, Selection::single); + let source = keyed(&rows, |row| row.0.to_owned()).with_text(|row| std::borrow::Cow::Borrowed(row.1.as_str())); + let out = List::new("Available formats").select(SelectMode::Single).max_rows(10).selection(Bind::bound(&mut selection)).show_items( + ui, + &source, + &mut |kind, _| ListRow::new(rows.iter().find(|row| row.0 == kind.as_str()).map_or_else(|| kind.clone(), |row| row.1.clone())), + ); + if selection.items.first() != current.as_ref() { + if let Some(kind) = selection.items.first() { + fields.set("format", kind.clone()); + } else { + fields.remove("format"); + } + } + out.response + }) + .opens_with("datetime1") + .full_width(), + Param::new( + "language", + "Language", + ParamKind::choice(DATE_LANGS.iter().map(|(tag, name)| ((*tag).into(), (*name).into())).collect(), Some("en-US".into())), + ), + // PLAN: plan error: no secondary-text note row in ParamKind + Param::read_only_text( + "language_note", + "", + "Month names and AM/PM are listed for English. Other languages list the numeric date and 24-hour time.", + ) + .full_width(), + Param::new("update", "Update automatically", ParamKind::bool(true)), + ]; + ParamDialog::new("dateTime", "Date and Time", params).layout(ParamLayout::Document).caption_column(true).values(Bind::bound(fields)) + }) +} + +/// Inserts the accepted date format, falling back when the language cannot draw it. +pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> { + let fields = &result.fields; + let language = fields.text("language").unwrap_or("en-US"); + let mut format = fields.text("format").unwrap_or("datetime1"); + let probe = deckcraft_text::datetime::DateTime::new(2026, 10, 9, 20, 5, 7); + if deckcraft_text::datetime::field_text(format, Some(language), probe).is_none() { + format = "datetime1"; } - cancel -} - -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; - -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") -} - -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w10): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") + app.run("insert.dateTime", json!({"format": format, "update": fields.bool("update").unwrap_or(true), "language": language})).map(|_| ()) } diff --git a/crates/ui-egui/src/dialogs/language.rs b/crates/ui-egui/src/dialogs/language.rs index ed6c869c9..ab3c7638b 100644 --- a/crates/ui-egui/src/dialogs/language.rs +++ b/crates/ui-egui/src/dialogs/language.rs @@ -27,20 +27,86 @@ pub fn selected_language(app: &SlideApp) -> (String, bool) { (props.lang.unwrap_or_else(|| "en-US".into()), props.no_proof == Some(true)) } -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; +use craft::behavior::{Selection, keyed}; +use craft::ui::contract::{Bind, SelectMode, Show as _, ShowCollection as _}; +use craft::ui::dialog::DialogHost; +use craft::ui::display::Text; +use craft::ui::list::{List, ListRow}; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind, ParamLayout, ParamResult}; +use egui::Context; +use serde_json::{Value, json}; -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") +/// Opens proofing language and scope settings for the current selection. +pub(crate) fn open(app: &SlideApp, ctx: &Context, _params: &Value) -> HostedParamDialog { + let (language, no_proof) = selected_language(app); + let has_selection = deckcraft_engine::cmd::has_text_or_shapes(&app.session).is_ok(); + let own = (!crate::squiggles::LANGS.iter().any(|(_, tag)| tag.eq_ignore_ascii_case(&language))).then(|| language.clone()); + let rows: Vec<_> = own + .into_iter() + .chain(crate::squiggles::LANGS.iter().map(|(_, tag)| (*tag).to_owned())) + .map(|tag| { + let name = crate::squiggles::display_lang_name(ctx, &tag); + let dictionary = deckcraft_engine::spell::dictionary(&tag).is_some(); + (name, tag, dictionary) + }) + .collect(); + let fields = DialogFields::from_pairs([("lang", language), ("scope", if has_selection { "sel" } else { "all" }.to_owned())]); + HostedParamDialog::new(ctx, "language", fields, move |fields| { + let mut scope = Param::new( + "scope", + "Apply to", + ParamKind::radio( + vec![("sel".into(), "Selected text".into()), ("all".into(), "Whole presentation".into())], + Some(if has_selection { "sel" } else { "all" }.into()), + ), + ) + .options_in_row(); + if !has_selection { + scope = scope.disabled_option("sel", "Select text or a shape to apply a language to selected text."); + } + let rows = rows.clone(); + let params = vec![ + scope, + Param::custom("lang", "Language", move |ui, fields, _| { + let heading = + Text::new(if fields.text("scope") == Some("all") { "Mark all text in the presentation as:" } else { "Mark selected text as:" }) + .show(ui); + let current = fields.text("lang").and_then(|tag| rows.iter().find(|row| row.1.eq_ignore_ascii_case(tag))).map(|row| row.1.clone()); + let mut selection = current.clone().map_or_else(Selection::default, Selection::single); + let source = keyed(&rows, |row| row.1.clone()).with_text(|row| std::borrow::Cow::Borrowed(row.0.as_str())); + let out = List::new("Language").select(SelectMode::Single).max_rows(10).selection(Bind::bound(&mut selection)).show_items( + ui, + &source, + &mut |tag, _| { + let row = rows.iter().find(|row| &row.1 == tag); + let mut item = ListRow::new(row.map_or_else(|| tag.clone(), |row| row.0.clone())); + if row.is_some_and(|row| row.2) { + item = item.detail("(dictionary)"); + } + item + }, + ); + if selection.items.first() != current.as_ref() { + if let Some(tag) = selection.items.first() { + fields.set("lang", tag.clone()); + } else { + fields.remove("lang"); + } + } + heading.union(out.response) + }) + .full_width(), + // PLAN: plan error: no secondary-text note row in ParamKind + Param::read_only_text("dictionary_note", "", "Spelling is checked for languages marked (dictionary).").full_width(), + Param::new("noProof", "Do not check spelling or grammar", ParamKind::bool(no_proof)), + ]; + ParamDialog::new("language", "Language", params).layout(ParamLayout::Document).caption_column(true).values(Bind::bound(fields)) + }) } -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w10): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") +/// Applies the accepted proofing language to the chosen scope. +pub(crate) fn finish(app: &mut SlideApp, result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> { + let fields = &result.fields; + let command = if fields.text("scope") == Some("all") { "format.languageAll" } else { "format.language" }; + app.run(command, json!({"lang": fields.text("lang").unwrap_or("en-US"), "noProof": fields.bool("noProof").unwrap_or(false)})).map(|_| ()) } diff --git a/crates/ui-egui/src/dialogs/symbol.rs b/crates/ui-egui/src/dialogs/symbol.rs index 06566e897..ac5e77aee 100644 --- a/crates/ui-egui/src/dialogs/symbol.rs +++ b/crates/ui-egui/src/dialogs/symbol.rs @@ -1,24 +1,70 @@ -use craft::ui::patterns::form_dialogs::{HostedParamDialog, ParamResult}; +use craft::ui::contract::Bind; +use craft::ui::dialog::DialogHost; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamLayout, ParamResult}; +use craft::ui::pickers::{IconPicker, IconPickerGroup, IconPickerGroups, IconPickerItem, IconPickerSearch}; +use egui::Context; +use serde_json::{Value, json}; -/// Opens this hosted form with the supplied parameters. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn open(app: &crate::SlideApp, ctx: &egui::Context, params: &serde_json::Value) -> HostedParamDialog { - let _ = (app, ctx, params); - todo!("dl.A9") +use crate::SlideApp; + +/// Opens a modeless symbol palette that inserts at the text caret. +pub(crate) fn open(_app: &SlideApp, ctx: &Context, _params: &Value) -> HostedParamDialog { + let groups: Vec<_> = [ + ("Punctuation", "–—‘’“”…•·§¶†‡‰′″‹›«»¡¿"), + ("Currency", "$€£¥¢₹₩₽₺₿"), + ("Math", "±×÷≠≈≤≥∞√∑∏∫∂∆∇∈∉∩∪⊂⊃∀∃¬∧∨⇒⇔°′"), + ("Greek", "αβγδεζηθικλμνξοπρστυφχψωΓΔΘΛΞΠΣΦΨΩ"), + ("Arrows", "←↑→↓↔↕⇐⇑⇒⇓↖↗↘↙⟵⟶"), + ("Shapes", "■□▪▫▲△▶▷▼▽◀◁◆◇○●◎★☆✓✔✗✘♠♣♥♦"), + ] + .into_iter() + .map(|(name, characters)| { + IconPickerGroup::new( + name, + characters + .chars() + .map(|character| { + let glyph = character.to_string(); + IconPickerItem::new(glyph.clone(), glyph.clone()).glyph(glyph) + }) + .collect(), + ) + }) + .collect(); + HostedParamDialog::new(ctx, "symbol", DialogFields::new(), move |fields| { + let groups = groups.clone(); + let params = vec![ + Param::read_only_text("instructions", "", "Click a symbol to insert it at the insertion point.").full_width(), + Param::custom("symbol", "Symbols", move |ui, _, cx| { + let out = IconPicker::new("Symbols", Bind::given(None::), &groups) + .group_display(IconPickerGroups::Headings) + .search(IconPickerSearch::None) + .show(ui); + if let Some(symbol) = out.picked { + cx.request_action(symbol); + } + out.response + }) + .full_width(), + ]; + ParamDialog::new("symbol", "Symbol", params) + .layout(ParamLayout::Document) + .caption_column(true) + .close_unchanged(true) + .values(Bind::bound(fields)) + }) + .modeless() } -/// Applies an accepted form result. -#[allow(dead_code)] // P3(dialogs-w10): dl.A9 -pub(crate) fn finish(app: &mut crate::SlideApp, result: &ParamResult, host: &mut craft::ui::dialog::DialogHost) -> Result<(), String> { - // P3(dialogs-w10): dl.A9 - let _ = host; - let _ = (app, result); - todo!("dl.A9") +/// Completes the palette's Close action without editing text. +pub(crate) fn finish(_app: &mut SlideApp, _result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> { + Ok(()) } -/// Runs an action delivered by the Symbol form. -#[allow(dead_code)] // P3(dialogs-w12): dl.A9 -pub(crate) fn action(app: &mut crate::SlideApp, id: &str) -> Result<(), String> { - let _ = (app, id); - todo!("dl.A9") +/// Inserts a symbol requested by the palette at the text caret. +pub(crate) fn action(app: &mut SlideApp, id: &str) -> Result<(), String> { + if app.session.active().is_some_and(|state| state.selection.text.is_none()) { + app.run("text.edit", json!({}))?; + } + app.run("insert.symbol", json!({"text": id})).map(|_| ()) }