diff --git a/crates/ui-egui/src/dialogs/symbol.rs b/crates/ui-egui/src/dialogs/symbol.rs index 2e123774f..374c4ef05 100644 --- a/crates/ui-egui/src/dialogs/symbol.rs +++ b/crates/ui-egui/src/dialogs/symbol.rs @@ -1,33 +1,50 @@ -use craft::behavior::keyed; -use craft::ui::contract::{ActivateOn, Bind, CollectionEvent, SelectMode, ShowCollection as _}; +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::tile_grid::{CaptionPlace, Tile, TileGrid, TileGroup}; +use craft::ui::pickers::{IconPicker, IconPickerGroup, IconPickerGroups, IconPickerItem, IconPickerSearch}; use egui::Context; use serde_json::{Value, json}; use crate::SlideApp; -const GROUPS: [(&str, &str); 6] = [ - ("Punctuation", "–—‘’“”…•·§¶†‡‰′″‹›«»¡¿"), - ("Currency", "$€£¥¢₹₩₽₺₿"), - ("Math", "±×÷≠≈≤≥∞√∑∏∫∂∆∇∈∉∩∪⊂⊃∀∃¬∧∨⇒⇔°′"), - ("Greek", "αβγδεζηθικλμνξοπρστυφχψωΓΔΘΛΞΠΣΦΨΩ"), - ("Arrows", "←↑→↓↔↕⇐⇑⇒⇓↖↗↘↙⟵⟶"), - ("Shapes", "■□▪▫▲△▶▷▼▽◀◁◆◇○●◎★☆✓✔✗✘♠♣♥♦"), -]; - /// 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 (response, picked) = palette(ui); - if let Some(symbol) = picked { + let out = IconPicker::new("Symbols", Bind::given(None::), &groups) + .group_display(IconPickerGroups::Headings) + .natural_height(true) + .search(IconPickerSearch::None) + .show(ui); + if let Some(symbol) = out.picked { cx.request_action(symbol); } - response + out.response }) .full_width(), ]; @@ -40,37 +57,6 @@ pub(crate) fn open(_app: &SlideApp, ctx: &Context, _params: &Value) -> HostedPar .modeless() } -/// Shows every group's symbols under its heading, at the grid's full height: the dialog's body scrolls them, as the -/// palette is a document row of that body. Returns the grid's response and the symbol clicked this pass. -fn palette(ui: &mut egui::Ui) -> (egui::Response, Option) { - let mut symbols = Vec::new(); - let mut named = Vec::new(); - for (name, characters) in GROUPS { - let start = symbols.len(); - symbols.extend(characters.chars().map(String::from)); - named.push((start, symbols.len() - start, name)); - } - // The heading rows are keyed after the cells, whose keys are their positions. - let groups: Vec> = - named.iter().enumerate().map(|(at, (start, count, name))| TileGroup::new(symbols.len() + at, *start, *count, *name)).collect(); - let cells: Vec = (0..symbols.len()).collect(); - let source = keyed(&cells, |at| *at).with_text(|at| symbols[*at].clone().into()); - let out = TileGrid::::new("Symbols") - .natural_height(true) - .select(SelectMode::Single) - .activate(ActivateOn::Click) - .caption(CaptionPlace::None) - .content_cells() - .slow_click_rename(false) - .groups(&groups) - .show_items(ui, &source, &mut |at, _| Tile::new(symbols[*at].clone()).glyph(symbols[*at].clone())); - let picked = out.response.events.iter().find_map(|event| match event { - CollectionEvent::Activate(at) => symbols.get(*at).cloned(), - _ => None, - }); - (out.response.response, picked) -} - /// Completes the palette's Close action without editing text. pub(crate) fn finish(_app: &mut SlideApp, _result: &ParamResult, _host: &mut DialogHost) -> Result<(), String> { Ok(())