From 92a603642a719de8608dea034f3b9ba0f45b14ca Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Fri, 13 Nov 2026 22:04:23 -0800 Subject: [PATCH 01/14] feat(ui-egui): font family box filters as you type Typing in the family box lists the families that contain the text, Up and Down move the highlight, and Enter applies it. The size box is unchanged. One new UI test is ignored until its focus problem is found. Co-Authored-By: Claude Haiku 5.5 --- crates/ui-egui/src/combo.rs | 103 ++++++++++++++++++++++++++------- crates/ui-egui/src/floating.rs | 2 + crates/ui-egui/src/ribbon.rs | 2 + crates/ui-egui/tests/ui.rs | 23 ++++++++ 4 files changed, 110 insertions(+), 20 deletions(-) diff --git a/crates/ui-egui/src/combo.rs b/crates/ui-egui/src/combo.rs index f96ca897..3268a75d 100644 --- a/crates/ui-egui/src/combo.rs +++ b/crates/ui-egui/src/combo.rs @@ -5,8 +5,12 @@ //! The arrow is a generous hit region with a tooltip and an accessible label. The list highlights //! the current value and scrolls to it once each time it opens. A typed value is applied when the //! box loses focus, if the caller accepts it. +//! +//! With `filter` on (the font name box), typing lists only the items that contain the text, Up and +//! Down move the highlight through them, and Enter applies the highlighted item (an item that +//! equals the typed text wins). Escape restores the shown value. -use egui::{Align, Area, Color32, Frame, Id, Order, ScrollArea, Shape, Stroke, Ui, WidgetInfo, WidgetType, vec2}; +use egui::{Align, Area, Color32, Frame, Id, Key, Modifiers, Order, Rect, ScrollArea, Shape, Stroke, Ui, WidgetInfo, WidgetType, vec2}; /// What the user chose this frame: a value picked from the list, or typed and accepted. #[derive(Debug, Default, PartialEq, Eq)] @@ -33,6 +37,14 @@ pub struct Spec<'a> { pub empty_note: Option<&'a str>, /// Whether a typed value is accepted when the box loses focus. pub accept: &'a dyn Fn(&str) -> bool, + /// Whether typing lists only the items that contain the text, with Up/Down/Enter to pick. + pub filter: bool, +} + +/// The items that contain `query` (ignoring case), in list order. Every item when `query` is empty. +fn matching<'a>(items: &'a [String], query: &str) -> Vec<&'a String> { + let query = query.trim().to_lowercase(); + items.iter().filter(|item| query.is_empty() || item.to_lowercase().contains(&query)).collect() } /// Shows the combo box and returns what was chosen this frame. @@ -41,29 +53,69 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { let typed_id = spec.id.with("typed"); let open_id = spec.id.with("open"); let scroll_id = spec.id.with("scroll"); + let highlight_id = spec.id.with("highlight"); + let list_rect_id = spec.id.with("list-rect"); let mut typed = ui.data(|d| d.get_temp::(typed_id)).unwrap_or_else(|| spec.shown.to_owned()); - let cancelled = ui.input(|i| i.key_pressed(egui::Key::Escape)); - let entered = ui.input(|i| i.key_pressed(egui::Key::Enter)); + let mut highlight = ui.data(|d| d.get_temp::(highlight_id)).unwrap_or(0); + let was_focused = ui.memory(|m| m.has_focus(typed_id)); + // Up and Down are read before the box sees them, so the highlight moves and the caret does not. + let (down, up) = if spec.filter && was_focused { + ui.input_mut(|i| (i.consume_key(Modifiers::NONE, Key::ArrowDown), i.consume_key(Modifiers::NONE, Key::ArrowUp))) + } else { + (false, false) + }; + let cancelled = ui.input(|i| i.key_pressed(Key::Escape)); + let entered = ui.input(|i| i.key_pressed(Key::Enter)); let value_box = ui.add(egui::TextEdit::singleline(&mut typed).id(typed_id).desired_width(spec.width)); value_box.widget_info(|| WidgetInfo::labeled(WidgetType::TextInput, true, spec.box_label)); - // Read the entry before resetting an unfocused box. Enter also loses focus; - // Escape cancels rather than applying the unfinished entry. + if value_box.changed() { + highlight = 0; + } + // Typing lists the matches while the box has focus and its text is not the shown value. + let typing = spec.filter && typed.trim() != spec.shown && (was_focused || value_box.has_focus()); + let hits = if typing { matching(spec.items, &typed) } else { Vec::new() }; + if down { + highlight = (highlight + 1).min(hits.len().saturating_sub(1)); + } + if up { + highlight = highlight.saturating_sub(1); + } + highlight = highlight.min(hits.len().saturating_sub(1)); + + // Read the entry before resetting an unfocused box. Enter also loses focus; Escape cancels + // rather than applying the unfinished entry. if value_box.lost_focus() { - // Formatting may return focus to the page in this same frame. The key - // belongs to this field, not to the editor which happens to run next. + // Formatting may return focus to the page in this same frame. The key belongs to this + // field, not to the editor which happens to run next. if entered || cancelled { - ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, if cancelled { egui::Key::Escape } else { egui::Key::Enter })); + ui.input_mut(|i| i.consume_key(Modifiers::NONE, if cancelled { Key::Escape } else { Key::Enter })); } - let value = typed.trim(); - if !cancelled && value != spec.shown && (spec.accept)(value) { - outcome.chosen = Some(value.to_owned()); + // A press on the list is not a finished entry: the list's own click applies its row. + let list_rect: Option = ui.data(|d| d.get_temp(list_rect_id)); + let on_list = match (ui.input(|i| i.pointer.hover_pos()), list_rect) { + (Some(p), Some(r)) => r.contains(p), + _ => false, + }; + if !cancelled && !on_list { + let exact = spec.items.iter().find(|item| item.eq_ignore_ascii_case(typed.trim())); + let value = match (spec.filter && entered, exact, hits.get(highlight)) { + (true, Some(item), _) => item.clone(), + (true, None, Some(hit)) => (*hit).clone(), + _ => typed.trim().to_owned(), + }; + if value != spec.shown && (spec.accept)(&value) { + outcome.chosen = Some(value); + } } } if !value_box.has_focus() { typed = spec.shown.to_owned(); } - ui.data_mut(|d| d.insert_temp(typed_id, typed)); + ui.data_mut(|d| { + d.insert_temp(typed_id, typed); + d.insert_temp(highlight_id, highlight); + }); let mut open = ui.data(|d| d.get_temp::(open_id)).unwrap_or(false); let mut scroll = ui.data(|d| d.get_temp::(scroll_id)).unwrap_or(false); @@ -84,32 +136,38 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { scroll = open; } - if open { + // The list: the matches while typing, otherwise all items when the arrow has opened it. + let show_list = typing || open; + let mut list_rect = None; + if show_list { + let rows: Vec<&String> = if typing { hits } else { spec.items.iter().collect() }; let list = Area::new(spec.id.with("list")).order(Order::Foreground).fixed_pos(value_box.rect.left_bottom()).show(ui.ctx(), |ui| { Frame::popup(ui.style()).show(ui, |ui| { ui.set_min_width(value_box.rect.width()); - if spec.items.is_empty() { + if rows.is_empty() { if let Some(note) = spec.empty_note { ui.label(note); } return; } ScrollArea::vertical().max_height(280.0).show(ui, |ui| { - for item in spec.items { - let current = item == spec.shown; - let row = ui.selectable_label(current, item); - if current && scroll { + for (index, item) in rows.iter().enumerate() { + let current = if typing { index == highlight } else { item.as_str() == spec.shown }; + let row = ui.selectable_label(current, item.as_str()); + if current && (scroll || typing) { row.scroll_to_me(Some(Align::Center)); } if row.clicked() { - outcome.chosen = Some(item.clone()); + outcome.chosen = Some((*item).clone()); open = false; + ui.memory_mut(|m| m.surrender_focus(typed_id)); } } }); }); }); - if ui.input(|i| i.pointer.any_click()) && !list.response.hovered() && !arrow.hovered() { + list_rect = Some(list.response.rect); + if !typing && ui.input(|i| i.pointer.any_click()) && !list.response.hovered() && !arrow.hovered() { open = false; } } @@ -117,6 +175,11 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { ui.data_mut(|d| { d.insert_temp(scroll_id, scroll); d.insert_temp(open_id, open); + if let Some(r) = list_rect { + d.insert_temp(list_rect_id, r); + } else { + d.remove::(list_rect_id); + } }); outcome } diff --git a/crates/ui-egui/src/floating.rs b/crates/ui-egui/src/floating.rs index ddc3e273..13fb69fe 100644 --- a/crates/ui-egui/src/floating.rs +++ b/crates/ui-egui/src/floating.rs @@ -207,6 +207,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts items: &fonts.families, empty_note: fonts.families_note.as_deref(), accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), + filter: true, }, ); if let Some(name) = families.chosen { @@ -225,6 +226,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts items: &fonts.sizes, empty_note: None, accept: &|typed| typed.parse::().is_ok_and(|size| crate::ribbon::FONT_SIZES.contains(&size)), + filter: false, }, ); if let Some(chosen) = size.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 152baafa..4dd8a990 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -1214,6 +1214,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { items: &families, empty_note: families_note.as_deref(), accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), + filter: true, }, ); if let Some(name) = family_out.chosen { @@ -1241,6 +1242,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { items: &sizes, empty_note: None, accept: &|typed| typed.parse::().is_ok_and(|size| FONT_SIZES.contains(&size)), + filter: false, }, ); let boxes = crate::floating::FontBoxes { diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index 30ddf5f3..fcdc82a3 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -9146,6 +9146,29 @@ fn choosing_a_font_changes_the_font_box() { assert_eq!(h.get_by_label("Font Family").value().as_deref(), Some(name.as_str()), "the box shows the chosen font"); } +/// Typing in the font box lists the families that contain the text, so a family can be found by +/// its middle letters rather than scrolled to. +#[test] +#[ignore = "exploration: after typing, the harness shows the shown value again; focus or delivery of ui.text to the font box is unresolved"] +fn typing_a_font_name_lists_the_families_that_contain_it() { + let Ok(catalog) = notecraft_ui_egui::fonts::system_catalog() else { return }; + let Some(name) = catalog.families().iter().find(|n| notecraft_formats::markdown::valid_font_family(n) && n.len() > 4).cloned() else { return }; + let query: String = name.chars().skip(1).take(3).collect(); + let (_t, app, s) = sample_app("ui-font-filter"); + let mut h = sized_harness(app, LIGHT, egui::vec2(2000.0, 900.0)); + h.state_mut().open_path(&s.md_page.to_string_lossy()); + settle(&mut h); + ask(&mut h, "ui.caret", json!({"block": 0})).expect("caret"); + settle(&mut h); + h.get_by_label("Font Family").click(); + h.run(); + ask(&mut h, "ui.key", json!({"key": "a", "ctrl": true})).expect("select the box's text"); + ask(&mut h, "ui.text", json!({"text": query})).expect("type a part of the name"); + settle(&mut h); + h.get_by_label(&name); + assert_eq!(h.get_by_label("Font Family").value().as_deref(), Some(query.as_str()), "the box shows what was typed"); +} + /// Dragging across plain text selects it, and the page reports the selection. #[test] fn dragging_across_text_selects_it() { From d4eaa1137bce65f9c58dc317cfb7aa71a51eee1d Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Tue, 17 Nov 2026 11:53:24 -0800 Subject: [PATCH 02/14] test(ui-egui): the font name box takes typed text in the filter test The filter test clicked the Font Family arrow, which opens the list and never focuses the text box, so typed text went nowhere. It now clicks the Font name box. The test runs again and passes. Co-Authored-By: Claude Haiku 5.5 --- crates/ui-egui/tests/ui.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index fcdc82a3..83fe9d38 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -9149,7 +9149,6 @@ fn choosing_a_font_changes_the_font_box() { /// Typing in the font box lists the families that contain the text, so a family can be found by /// its middle letters rather than scrolled to. #[test] -#[ignore = "exploration: after typing, the harness shows the shown value again; focus or delivery of ui.text to the font box is unresolved"] fn typing_a_font_name_lists_the_families_that_contain_it() { let Ok(catalog) = notecraft_ui_egui::fonts::system_catalog() else { return }; let Some(name) = catalog.families().iter().find(|n| notecraft_formats::markdown::valid_font_family(n) && n.len() > 4).cloned() else { return }; @@ -9160,13 +9159,14 @@ fn typing_a_font_name_lists_the_families_that_contain_it() { settle(&mut h); ask(&mut h, "ui.caret", json!({"block": 0})).expect("caret"); settle(&mut h); - h.get_by_label("Font Family").click(); + // "Font name" is the text box; "Font Family" is the arrow beside it, which opens the list. + h.get_by_label("Font name").click(); h.run(); ask(&mut h, "ui.key", json!({"key": "a", "ctrl": true})).expect("select the box's text"); ask(&mut h, "ui.text", json!({"text": query})).expect("type a part of the name"); settle(&mut h); h.get_by_label(&name); - assert_eq!(h.get_by_label("Font Family").value().as_deref(), Some(query.as_str()), "the box shows what was typed"); + assert_eq!(h.get_by_label("Font name").value().as_deref(), Some(query.as_str()), "the box shows what was typed"); } /// Dragging across plain text selects it, and the page reports the selection. From c8cc4a55a5a17d7b4e0f29371e9d35bb3440d975 Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Tue, 17 Nov 2026 13:18:31 -0800 Subject: [PATCH 03/14] feat(ui-egui): font boxes list recently used fonts first A font family box lists the families used lately above All Fonts, and the ribbon and the floating bar share the list. The matching, recents and size check live in a font_picker module with no egui in it, so it can move to a shared library later. Ported from WordCraft's ranking. Co-Authored-By: Claude Haiku 5.5 --- crates/ui-egui/src/combo.rs | 31 ++++++-- crates/ui-egui/src/floating.rs | 6 +- crates/ui-egui/src/font_picker.rs | 123 ++++++++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/ribbon.rs | 11 ++- crates/ui-egui/tests/ui.rs | 4 +- i18n/ar-XB.tsv | 2 + i18n/en-XA.tsv | 2 + i18n/en.tsv | 2 + 9 files changed, 172 insertions(+), 10 deletions(-) create mode 100644 crates/ui-egui/src/font_picker.rs diff --git a/crates/ui-egui/src/combo.rs b/crates/ui-egui/src/combo.rs index 3268a75d..277aa023 100644 --- a/crates/ui-egui/src/combo.rs +++ b/crates/ui-egui/src/combo.rs @@ -39,12 +39,9 @@ pub struct Spec<'a> { pub accept: &'a dyn Fn(&str) -> bool, /// Whether typing lists only the items that contain the text, with Up/Down/Enter to pick. pub filter: bool, -} - -/// The items that contain `query` (ignoring case), in list order. Every item when `query` is empty. -fn matching<'a>(items: &'a [String], query: &str) -> Vec<&'a String> { - let query = query.trim().to_lowercase(); - items.iter().filter(|item| query.is_empty() || item.to_lowercase().contains(&query)).collect() + /// Families used lately, most recent first. Shown under Recently Used Fonts above All Fonts + /// when the box is not being typed in. Empty for the size box. + pub recent: &'a [String], } /// Shows the combo box and returns what was chosen this frame. @@ -74,7 +71,8 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { } // Typing lists the matches while the box has focus and its text is not the shown value. let typing = spec.filter && typed.trim() != spec.shown && (was_focused || value_box.has_focus()); - let hits = if typing { matching(spec.items, &typed) } else { Vec::new() }; + let hits: Vec<&String> = + if typing { crate::font_picker::matches(&typed, spec.items).into_iter().map(|i| &spec.items[i]).collect() } else { Vec::new() }; if down { highlight = (highlight + 1).min(hits.len().saturating_sub(1)); } @@ -141,6 +139,8 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { let mut list_rect = None; if show_list { let rows: Vec<&String> = if typing { hits } else { spec.items.iter().collect() }; + // Recently used families go above the full list, when the box is not being typed in. + let recent_rows: Vec<&String> = if typing { Vec::new() } else { spec.recent.iter().collect() }; let list = Area::new(spec.id.with("list")).order(Order::Foreground).fixed_pos(value_box.rect.left_bottom()).show(ui.ctx(), |ui| { Frame::popup(ui.style()).show(ui, |ui| { ui.set_min_width(value_box.rect.width()); @@ -151,6 +151,23 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { return; } ScrollArea::vertical().max_height(280.0).show(ui, |ui| { + if !recent_rows.is_empty() { + ui.label(egui::RichText::new(crate::tl_id!("ui.recent_fonts")).small().weak()); + for item in &recent_rows { + let current = item.as_str() == spec.shown; + let row = ui.selectable_label(current, item.as_str()); + if current && scroll { + row.scroll_to_me(Some(Align::Center)); + } + if row.clicked() { + outcome.chosen = Some((*item).clone()); + open = false; + ui.memory_mut(|m| m.surrender_focus(typed_id)); + } + } + ui.separator(); + ui.label(egui::RichText::new(crate::tl_id!("ui.all_fonts")).small().weak()); + } for (index, item) in rows.iter().enumerate() { let current = if typing { index == highlight } else { item.as_str() == spec.shown }; let row = ui.selectable_label(current, item.as_str()); diff --git a/crates/ui-egui/src/floating.rs b/crates/ui-egui/src/floating.rs index 13fb69fe..68d481b6 100644 --- a/crates/ui-egui/src/floating.rs +++ b/crates/ui-egui/src/floating.rs @@ -115,6 +115,8 @@ pub struct FontBoxes { pub families: Vec, pub families_note: Option, pub sizes: Vec, + /// Recently used families, most recent first, shared with the ribbon's family box. + pub recent: Vec, } /// The egui memory key under which the ribbon stores the [`FontBoxes`] for this frame. @@ -208,6 +210,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts empty_note: fonts.families_note.as_deref(), accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), filter: true, + recent: &fonts.recent, }, ); if let Some(name) = families.chosen { @@ -225,8 +228,9 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts width: 38.0, items: &fonts.sizes, empty_note: None, - accept: &|typed| typed.parse::().is_ok_and(|size| crate::ribbon::FONT_SIZES.contains(&size)), + accept: &|typed| crate::font_picker::size_is_usable(typed, &crate::ribbon::FONT_SIZES), filter: false, + recent: &[], }, ); if let Some(chosen) = size.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/src/font_picker.rs b/crates/ui-egui/src/font_picker.rs new file mode 100644 index 00000000..aee44f10 --- /dev/null +++ b/crates/ui-egui/src/font_picker.rs @@ -0,0 +1,123 @@ +//! Logic for the font name and size boxes, with no egui in it, so it can move to a shared library +//! later without changes. Ported from WordCraft's `widgets/combo.rs` (font matching and recents) +//! and its font dialog (size check). Keep this file free of UI types. + +/// The most recent families kept in the Recently Used list. +pub const RECENT_CAP: usize = 8; + +/// Indices of the `items` matching `query`, ignoring case: exact matches, then names starting with +/// it, then names containing it, each group in list order. A blank query matches everything. +/// (WordCraft's ranking; NoteCraft used list order only before this.) +pub fn matches(query: &str, items: &[String]) -> Vec { + let q = query.trim().to_lowercase(); + if q.is_empty() { + return (0..items.len()).collect(); + } + let (mut exact, mut prefix, mut inner) = (Vec::new(), Vec::new(), Vec::new()); + for (i, item) in items.iter().enumerate() { + let name = item.to_lowercase(); + if name == q { + exact.push(i); + } else if name.starts_with(&q) { + prefix.push(i); + } else if name.contains(&q) { + inner.push(i); + } + } + exact.extend(prefix); + exact.extend(inner); + exact +} + +/// Records `name` as the most recently used family: it moves to the front, appears once, and the +/// list keeps at most [`RECENT_CAP`] names. +pub fn remember(recent: &mut Vec, name: &str) { + recent.retain(|n| !n.eq_ignore_ascii_case(name)); + recent.insert(0, name.to_owned()); + recent.truncate(RECENT_CAP); +} + +/// True when `name` is not among the installed families, so the page will show a fallback font. +/// An empty name is not missing; it means "the default". +pub fn is_missing(name: &str, installed: &[String]) -> bool { + let name = name.trim(); + !name.is_empty() && !installed.iter().any(|f| f.eq_ignore_ascii_case(name)) +} + +/// A typed size the page can hold: a finite whole number of points from `sizes`. Text such as +/// "NaN", "inf" or "1.5" is refused, so a non-finite size never reaches the layout. +pub fn size_is_usable(text: &str, sizes: &[u8]) -> bool { + text.trim().parse::().is_ok_and(|size| sizes.contains(&size)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn names(list: &[&str]) -> Vec { + list.iter().map(|s| (*s).to_owned()).collect() + } + + #[test] + fn a_blank_query_matches_every_family_in_order() { + let items = names(&["Arial", "Georgia", "Verdana"]); + assert_eq!(matches(" ", &items), vec![0, 1, 2]); + } + + #[test] + fn exact_matches_come_first_then_prefixes_then_the_rest() { + let items = names(&["Courier New", "Arial Black", "Arial", "Arial Narrow", "Carial"]); + // "arial": exact "Arial" (2), prefixes "Arial Black" (1) and "Arial Narrow" (3), then + // "Carial" (4) because it only contains the query. + assert_eq!(matches("arial", &items), vec![2, 1, 3, 4]); + } + + #[test] + fn matching_ignores_case_and_surrounding_spaces() { + let items = names(&["Georgia", "Verdana"]); + assert_eq!(matches(" GEOR ", &items), vec![0]); + } + + #[test] + fn a_query_with_no_match_lists_nothing() { + let items = names(&["Arial"]); + assert!(matches("zzz", &items).is_empty()); + } + + #[test] + fn the_last_family_used_comes_first_and_appears_once() { + let mut recent = Vec::new(); + remember(&mut recent, "Arial"); + remember(&mut recent, "Georgia"); + remember(&mut recent, "arial"); + assert_eq!(recent, names(&["arial", "Georgia"])); + } + + #[test] + fn the_recent_list_keeps_only_the_latest_names() { + let mut recent = Vec::new(); + for i in 0..(RECENT_CAP + 3) { + remember(&mut recent, &format!("Font {i}")); + } + assert_eq!(recent.len(), RECENT_CAP); + assert_eq!(recent[0], format!("Font {}", RECENT_CAP + 2)); + } + + #[test] + fn a_family_that_is_not_installed_is_missing_but_the_default_is_not() { + let installed = names(&["Arial", "Georgia"]); + assert!(is_missing("Aptos", &installed)); + assert!(!is_missing("arial", &installed)); + assert!(!is_missing("", &installed)); + } + + #[test] + fn only_whole_sizes_from_the_list_are_usable() { + let sizes = [8u8, 11, 12, 72]; + assert!(size_is_usable(" 12 ", &sizes)); + assert!(!size_is_usable("NaN", &sizes)); + assert!(!size_is_usable("inf", &sizes)); + assert!(!size_is_usable("10.5", &sizes)); + assert!(!size_is_usable("13", &sizes)); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index db3bce66..4d37e69e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -29,6 +29,7 @@ pub mod diagramview; #[path = "filepicker.android.rs"] pub(crate) mod filepicker; pub mod floating; +pub mod font_picker; pub mod fonts; pub mod gridcraft; pub mod highlight; diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 4dd8a990..b42528b4 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -1202,6 +1202,9 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { (Vec::new(), Some(crate::tl_id!("ui.font_catalog_unavailable").to_string())) } }; + // Recently used families, most recent first. Kept for this session in egui memory. + let recent_key = egui::Id::new("notecraft-recent-fonts"); + let recent_families: Vec = ui.data(|d| d.get_temp(recent_key)).unwrap_or_default(); let family_out = crate::combo::show( ui, t.text, @@ -1215,9 +1218,13 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { empty_note: families_note.as_deref(), accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), filter: true, + recent: &recent_families, }, ); if let Some(name) = family_out.chosen { + let mut recent = recent_families.clone(); + crate::font_picker::remember(&mut recent, &name); + ui.data_mut(|d| d.insert_temp(recent_key, recent)); acts.push(Act::FontFamily(name)); } // OneNote's size control: an editable box showing the size, and a drop-down arrow. @@ -1241,8 +1248,9 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { width: 38.0, items: &sizes, empty_note: None, - accept: &|typed| typed.parse::().is_ok_and(|size| FONT_SIZES.contains(&size)), + accept: &|typed| crate::font_picker::size_is_usable(typed, &FONT_SIZES), filter: false, + recent: &[], }, ); let boxes = crate::floating::FontBoxes { @@ -1251,6 +1259,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { families: families.clone(), families_note: families_note.clone(), sizes: sizes.clone(), + recent: recent_families.clone(), }; ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new(crate::floating::FONT_BOXES_KEY), boxes)); if let Some(size) = size_out.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index 83fe9d38..40f61b5e 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -9274,7 +9274,9 @@ fn installed_font_family_can_be_applied_and_undone_in_both_formats() { pump(&mut h); h.get_by_label("Font Family").click(); h.run(); - h.get_by_label(&family).click(); + // After the first page, this family is also under Recently Used Fonts. The All Fonts + // entry comes last in the list, so pick that one. + h.query_all_by_label(&family).last().expect("the family is listed").click(); settle(&mut h); let after = h.state().body().unwrap_or_default().to_string(); let (blocks, _) = notecraft_formats::boxes::read(format, &after); diff --git a/i18n/ar-XB.tsv b/i18n/ar-XB.tsv index 1ea9a670..1403ed99 100644 --- a/i18n/ar-XB.tsv +++ b/i18n/ar-XB.tsv @@ -82,6 +82,8 @@ @id ui.font_family [[RLI]]$$$Font Family@@@[[PDI]] @id ui.loading_fonts [[RLI]]$$$Loading fonts...@@@[[PDI]] @id ui.font_family_box [[RLI]]$$$Font name@@@[[PDI]] +@id ui.recent_fonts [[RLI]]$$$Recently Used Fonts@@@[[PDI]] +@id ui.all_fonts [[RLI]]$$$All Fonts@@@[[PDI]] @id ui.pick_date [[RLI]]$$$Pick a date@@@[[PDI]] @id ui.pick_time [[RLI]]$$$Pick a time@@@[[PDI]] @id ui.am [[RLI]]$$$AM@@@[[PDI]] diff --git a/i18n/en-XA.tsv b/i18n/en-XA.tsv index d491d6bd..0a8ef6c4 100644 --- a/i18n/en-XA.tsv +++ b/i18n/en-XA.tsv @@ -82,6 +82,8 @@ @id ui.font_family $$$Font Family@@@ @id ui.loading_fonts $$$Loading fonts...@@@ @id ui.font_family_box $$$Font name@@@ +@id ui.recent_fonts $$$Recently Used Fonts@@@ +@id ui.all_fonts $$$All Fonts@@@ @id ui.pick_date $$$Pick a date@@@ @id ui.pick_time $$$Pick a time@@@ @id ui.am $$$AM@@@ diff --git a/i18n/en.tsv b/i18n/en.tsv index 2a2dc377..c5033e30 100644 --- a/i18n/en.tsv +++ b/i18n/en.tsv @@ -82,6 +82,8 @@ @id ui.font_size_box Font size @id ui.font_family Font Family @id ui.font_family_box Font name +@id ui.recent_fonts Recently Used Fonts +@id ui.all_fonts All Fonts @id ui.pick_date Pick a date @id ui.pick_time Pick a time @id ui.am AM From f2016b51e6fd6496fdef454e10ea8bf66b5edd7b Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Tue, 17 Nov 2026 19:15:29 -0800 Subject: [PATCH 04/14] feat(ui-egui): font box warns when the font is not installed A family box shows an amber mark, with a tooltip, when the family on the page is not among the installed fonts. The warning waits until the installed list is known, so it does not flash while fonts load. Co-Authored-By: Claude Haiku 5.5 --- crates/ui-egui/src/combo.rs | 11 +++++++++++ crates/ui-egui/src/floating.rs | 4 ++++ crates/ui-egui/src/ribbon.rs | 5 +++++ i18n/ar-XB.tsv | 1 + i18n/en-XA.tsv | 1 + i18n/en.tsv | 1 + 6 files changed, 23 insertions(+) diff --git a/crates/ui-egui/src/combo.rs b/crates/ui-egui/src/combo.rs index 277aa023..49ba301f 100644 --- a/crates/ui-egui/src/combo.rs +++ b/crates/ui-egui/src/combo.rs @@ -42,6 +42,9 @@ pub struct Spec<'a> { /// Families used lately, most recent first. Shown under Recently Used Fonts above All Fonts /// when the box is not being typed in. Empty for the size box. pub recent: &'a [String], + /// The shown value is not an installed font: a warning mark shows beside the box, with the + /// tooltip `ui.font_missing`. + pub missing: bool, } /// Shows the combo box and returns what was chosen this frame. @@ -65,6 +68,14 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { let cancelled = ui.input(|i| i.key_pressed(Key::Escape)); let entered = ui.input(|i| i.key_pressed(Key::Enter)); let value_box = ui.add(egui::TextEdit::singleline(&mut typed).id(typed_id).desired_width(spec.width)); + if spec.missing { + // An amber triangle inside the box's right edge, as WordCraft draws it. + let c = egui::pos2(value_box.rect.right() - 8.0, value_box.rect.center().y); + let points = vec![c + vec2(-5.0, 4.0), c + vec2(5.0, 4.0), c + vec2(0.0, -5.0)]; + ui.painter().add(Shape::convex_polygon(points, Color32::from_rgb(0xE0, 0x8A, 0x00), Stroke::NONE)); + let warning = value_box.rect.with_min_x(value_box.rect.right() - 18.0); + ui.interact(warning, value_box.id.with("missing"), egui::Sense::hover()).on_hover_text(crate::tl_id!("ui.font_missing")); + } value_box.widget_info(|| WidgetInfo::labeled(WidgetType::TextInput, true, spec.box_label)); if value_box.changed() { highlight = 0; diff --git a/crates/ui-egui/src/floating.rs b/crates/ui-egui/src/floating.rs index 68d481b6..fb58e192 100644 --- a/crates/ui-egui/src/floating.rs +++ b/crates/ui-egui/src/floating.rs @@ -117,6 +117,8 @@ pub struct FontBoxes { pub sizes: Vec, /// Recently used families, most recent first, shared with the ribbon's family box. pub recent: Vec, + /// The family shown is not installed, so the box shows its warning mark. + pub family_missing: bool, } /// The egui memory key under which the ribbon stores the [`FontBoxes`] for this frame. @@ -211,6 +213,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), filter: true, recent: &fonts.recent, + missing: fonts.family_missing, }, ); if let Some(name) = families.chosen { @@ -231,6 +234,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts accept: &|typed| crate::font_picker::size_is_usable(typed, &crate::ribbon::FONT_SIZES), filter: false, recent: &[], + missing: false, }, ); if let Some(chosen) = size.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index b42528b4..649c109c 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -1202,6 +1202,8 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { (Vec::new(), Some(crate::tl_id!("ui.font_catalog_unavailable").to_string())) } }; + // Warn only once the installed list is known; while it loads, nothing is missing yet. + let family_missing = !families.is_empty() && crate::font_picker::is_missing(view.font_family.unwrap_or(""), &families); // Recently used families, most recent first. Kept for this session in egui memory. let recent_key = egui::Id::new("notecraft-recent-fonts"); let recent_families: Vec = ui.data(|d| d.get_temp(recent_key)).unwrap_or_default(); @@ -1219,6 +1221,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { accept: &|typed| notecraft_formats::markdown::valid_font_family(typed), filter: true, recent: &recent_families, + missing: family_missing, }, ); if let Some(name) = family_out.chosen { @@ -1251,6 +1254,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { accept: &|typed| crate::font_picker::size_is_usable(typed, &FONT_SIZES), filter: false, recent: &[], + missing: false, }, ); let boxes = crate::floating::FontBoxes { @@ -1260,6 +1264,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { families_note: families_note.clone(), sizes: sizes.clone(), recent: recent_families.clone(), + family_missing, }; ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new(crate::floating::FONT_BOXES_KEY), boxes)); if let Some(size) = size_out.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/i18n/ar-XB.tsv b/i18n/ar-XB.tsv index 1403ed99..1bc2cd6b 100644 --- a/i18n/ar-XB.tsv +++ b/i18n/ar-XB.tsv @@ -84,6 +84,7 @@ @id ui.font_family_box [[RLI]]$$$Font name@@@[[PDI]] @id ui.recent_fonts [[RLI]]$$$Recently Used Fonts@@@[[PDI]] @id ui.all_fonts [[RLI]]$$$All Fonts@@@[[PDI]] +@id ui.font_missing [[RLI]]$$$This font is not installed. The page shows a fallback font.@@@[[PDI]] @id ui.pick_date [[RLI]]$$$Pick a date@@@[[PDI]] @id ui.pick_time [[RLI]]$$$Pick a time@@@[[PDI]] @id ui.am [[RLI]]$$$AM@@@[[PDI]] diff --git a/i18n/en-XA.tsv b/i18n/en-XA.tsv index 0a8ef6c4..faafd59d 100644 --- a/i18n/en-XA.tsv +++ b/i18n/en-XA.tsv @@ -84,6 +84,7 @@ @id ui.font_family_box $$$Font name@@@ @id ui.recent_fonts $$$Recently Used Fonts@@@ @id ui.all_fonts $$$All Fonts@@@ +@id ui.font_missing $$$This font is not installed. The page shows a fallback font.@@@ @id ui.pick_date $$$Pick a date@@@ @id ui.pick_time $$$Pick a time@@@ @id ui.am $$$AM@@@ diff --git a/i18n/en.tsv b/i18n/en.tsv index c5033e30..0cb86f9d 100644 --- a/i18n/en.tsv +++ b/i18n/en.tsv @@ -84,6 +84,7 @@ @id ui.font_family_box Font name @id ui.recent_fonts Recently Used Fonts @id ui.all_fonts All Fonts +@id ui.font_missing This font is not installed. The page shows a fallback font. @id ui.pick_date Pick a date @id ui.pick_time Pick a time @id ui.am AM From c2159509e587be644b414a720cc4c70f17f6a925 Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Wed, 18 Nov 2026 08:08:24 -0800 Subject: [PATCH 05/14] feat(ui-egui): font list draws each family in its own font The family list shows every name in that font, as a sample, once the font is loaded. Fonts load as their rows come into view, and a row falls back to the interface font until then. The list keeps NoteCraft's drop-down position and look. The size list is unchanged. Co-Authored-By: Claude Haiku 5.5 --- crates/ui-egui/src/combo.rs | 33 +++++++++++++++++++++++++++++++-- crates/ui-egui/src/floating.rs | 2 ++ crates/ui-egui/src/ribbon.rs | 2 ++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/combo.rs b/crates/ui-egui/src/combo.rs index 49ba301f..2b27192d 100644 --- a/crates/ui-egui/src/combo.rs +++ b/crates/ui-egui/src/combo.rs @@ -45,6 +45,22 @@ pub struct Spec<'a> { /// The shown value is not an installed font: a warning mark shows beside the box, with the /// tooltip `ui.font_missing`. pub missing: bool, + /// Each family name is drawn in its own font, as a sample, once that font is loaded. The + /// fonts load as the rows come into view. + pub preview: bool, +} + +/// The text of one list row: the name, drawn in its own font when `preview` is on and that font +/// is already registered. A font that is not registered yet draws in the interface font. +fn name_text(ui: &Ui, name: &str, preview: bool) -> egui::RichText { + let text = egui::RichText::new(name); + if preview { + let family = crate::fonts::named_family(name); + if ui.ctx().fonts(|fonts| fonts.definitions().families.contains_key(&family)) { + return text.family(family); + } + } + text } /// Shows the combo box and returns what was chosen this frame. @@ -152,6 +168,8 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { let rows: Vec<&String> = if typing { hits } else { spec.items.iter().collect() }; // Recently used families go above the full list, when the box is not being typed in. let recent_rows: Vec<&String> = if typing { Vec::new() } else { spec.recent.iter().collect() }; + // Names whose rows are on screen this frame; their fonts load after the list is drawn. + let mut wanted: Vec = Vec::new(); let list = Area::new(spec.id.with("list")).order(Order::Foreground).fixed_pos(value_box.rect.left_bottom()).show(ui.ctx(), |ui| { Frame::popup(ui.style()).show(ui, |ui| { ui.set_min_width(value_box.rect.width()); @@ -166,7 +184,10 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { ui.label(egui::RichText::new(crate::tl_id!("ui.recent_fonts")).small().weak()); for item in &recent_rows { let current = item.as_str() == spec.shown; - let row = ui.selectable_label(current, item.as_str()); + let row = ui.selectable_label(current, name_text(ui, item, spec.preview)); + if spec.preview && ui.is_rect_visible(row.rect) { + wanted.push(item.to_string()); + } if current && scroll { row.scroll_to_me(Some(Align::Center)); } @@ -181,7 +202,10 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { } for (index, item) in rows.iter().enumerate() { let current = if typing { index == highlight } else { item.as_str() == spec.shown }; - let row = ui.selectable_label(current, item.as_str()); + let row = ui.selectable_label(current, name_text(ui, item, spec.preview)); + if spec.preview && ui.is_rect_visible(row.rect) { + wanted.push(item.to_string()); + } if current && (scroll || typing) { row.scroll_to_me(Some(Align::Center)); } @@ -194,6 +218,11 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { }); }); }); + if !wanted.is_empty() { + // Registers the fonts of the visible rows. Drawn in them from the next frame. + let names: std::collections::BTreeSet = wanted.into_iter().collect(); + crate::fonts::ensure_page_fonts(ui.ctx(), &names); + } list_rect = Some(list.response.rect); if !typing && ui.input(|i| i.pointer.any_click()) && !list.response.hovered() && !arrow.hovered() { open = false; diff --git a/crates/ui-egui/src/floating.rs b/crates/ui-egui/src/floating.rs index fb58e192..bcbeae47 100644 --- a/crates/ui-egui/src/floating.rs +++ b/crates/ui-egui/src/floating.rs @@ -214,6 +214,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts filter: true, recent: &fonts.recent, missing: fonts.family_missing, + preview: true, }, ); if let Some(name) = families.chosen { @@ -235,6 +236,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts filter: false, recent: &[], missing: false, + preview: false, }, ); if let Some(chosen) = size.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 649c109c..be596c37 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -1222,6 +1222,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { filter: true, recent: &recent_families, missing: family_missing, + preview: true, }, ); if let Some(name) = family_out.chosen { @@ -1255,6 +1256,7 @@ fn home_tab(ui: &mut Ui, view: &View<'_>, t: &Tokens, acts: &mut Vec) { filter: false, recent: &[], missing: false, + preview: false, }, ); let boxes = crate::floating::FontBoxes { From 100211b7624a520ef91c7a20f0d905c2308475f8 Mon Sep 17 00:00:00 2001 From: AlienTech Software Date: Wed, 18 Nov 2026 09:16:30 -0800 Subject: [PATCH 06/14] feat(ui-egui): font list groups families by script The family list has a heading for each script a font covers, so the list scrolls from language to language. Fonts covering none of the listed scripts go under Other. The catalogue reads each font's characters once. Fixes a press on a typed match: the list stayed on the matches until the release, so the click lands on the row that was pressed. The font tests now choose a font by typing its name, as a user does. Co-Authored-By: Claude Haiku 5.5 --- Cargo.lock | 1 + crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/combo.rs | 84 +++++++++++++++++++------- crates/ui-egui/src/floating.rs | 4 ++ crates/ui-egui/src/font_picker.rs | 95 ++++++++++++++++++++++++++++++ crates/ui-egui/src/fonts/system.rs | 37 +++++++++--- crates/ui-egui/src/ribbon.rs | 37 ++++++++++++ crates/ui-egui/tests/ui.rs | 41 +++++++++---- i18n/ar-XB.tsv | 11 ++++ i18n/en-XA.tsv | 11 ++++ i18n/en.tsv | 11 ++++ 11 files changed, 293 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6081eaa4..a75b21fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3381,6 +3381,7 @@ dependencies = [ "semver", "serde_json", "sha2", + "ttf-parser", "ureq", "web-time", "zip", diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 3023115d..28ebbe22 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -51,6 +51,7 @@ arboard = { workspace = true } [target.'cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))'.dependencies] fontdb = "0.23" +ttf-parser = "0.25.1" # File > About > "Check for updates": asks GitHub for the newest release, downloads the file for # this computer and checks it against the release's SHA-256 before running it. Nothing is sent # until the button is pressed. diff --git a/crates/ui-egui/src/combo.rs b/crates/ui-egui/src/combo.rs index 2b27192d..53e5770a 100644 --- a/crates/ui-egui/src/combo.rs +++ b/crates/ui-egui/src/combo.rs @@ -48,6 +48,25 @@ pub struct Spec<'a> { /// Each family name is drawn in its own font, as a sample, once that font is loaded. The /// fonts load as the rows come into view. pub preview: bool, + /// The list grouped by script: each heading with its families. Empty when there is no + /// grouping (the size box, or while the catalog loads). + pub sections: &'a [(String, Vec)], +} + +/// One row of a font list. Records a visible name so its font loads, and scrolls to the row when +/// asked to. +fn font_row(ui: &mut Ui, name: &str, current: bool, preview: bool, wanted: &mut Vec, scroll_to: bool) -> egui::Response { + // A fixed row height: a row drawn in its own font would otherwise change size as its font loads, + // and move under the pointer. + let height = ui.spacing().interact_size.y; + let row = ui.add_sized(egui::vec2(ui.available_width(), height), egui::Button::selectable(current, name_text(ui, name, preview)).truncate()); + if preview && ui.is_rect_visible(row.rect) { + wanted.push(name.to_owned()); + } + if current && scroll_to { + row.scroll_to_me(Some(Align::Center)); + } + row } /// The text of one list row: the name, drawn in its own font when `preview` is on and that font @@ -97,7 +116,20 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { highlight = 0; } // Typing lists the matches while the box has focus and its text is not the shown value. - let typing = spec.filter && typed.trim() != spec.shown && (was_focused || value_box.has_focus()); + // A press on a row of the typed list takes focus from the box. The list must keep its matches + // until the button is released, or the row under the pointer changes before the click lands. + let last_typing: bool = ui.data(|d| d.get_temp(spec.id.with("last-typing"))).unwrap_or(false); + let list_under_pointer: bool = match (ui.input(|i| i.pointer.hover_pos()), ui.data(|d| d.get_temp::(list_rect_id))) { + (Some(p), Some(r)) => r.contains(p), + _ => false, + }; + let pressing_list = list_under_pointer && ui.input(|i| i.pointer.primary_down()); + // Armed by a press on the typed list; it stays armed through the release, so the release + // lands on the row that was pressed. + let armed: bool = ui.data(|d| d.get_temp(spec.id.with("press-armed"))).unwrap_or(false); + let released = ui.input(|i| i.pointer.primary_released()); + let keep_typing = (pressing_list && last_typing) || armed; + let typing = spec.filter && ((typed.trim() != spec.shown && (was_focused || value_box.has_focus())) || keep_typing); let hits: Vec<&String> = if typing { crate::font_picker::matches(&typed, spec.items).into_iter().map(|i| &spec.items[i]).collect() } else { Vec::new() }; if down { @@ -134,7 +166,7 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { } } } - if !value_box.has_focus() { + if !value_box.has_focus() && !keep_typing { typed = spec.shown.to_owned(); } ui.data_mut(|d| { @@ -168,6 +200,8 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { let rows: Vec<&String> = if typing { hits } else { spec.items.iter().collect() }; // Recently used families go above the full list, when the box is not being typed in. let recent_rows: Vec<&String> = if typing { Vec::new() } else { spec.recent.iter().collect() }; + // The script sections replace the one full list when the box is not being typed in. + let by_script = !typing && !spec.sections.is_empty(); // Names whose rows are on screen this frame; their fonts load after the list is drawn. let mut wanted: Vec = Vec::new(); let list = Area::new(spec.id.with("list")).order(Order::Foreground).fixed_pos(value_box.rect.left_bottom()).show(ui.ctx(), |ui| { @@ -184,13 +218,7 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { ui.label(egui::RichText::new(crate::tl_id!("ui.recent_fonts")).small().weak()); for item in &recent_rows { let current = item.as_str() == spec.shown; - let row = ui.selectable_label(current, name_text(ui, item, spec.preview)); - if spec.preview && ui.is_rect_visible(row.rect) { - wanted.push(item.to_string()); - } - if current && scroll { - row.scroll_to_me(Some(Align::Center)); - } + let row = font_row(ui, item, current, spec.preview, &mut wanted, scroll); if row.clicked() { outcome.chosen = Some((*item).clone()); open = false; @@ -198,21 +226,33 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { } } ui.separator(); - ui.label(egui::RichText::new(crate::tl_id!("ui.all_fonts")).small().weak()); } - for (index, item) in rows.iter().enumerate() { - let current = if typing { index == highlight } else { item.as_str() == spec.shown }; - let row = ui.selectable_label(current, name_text(ui, item, spec.preview)); - if spec.preview && ui.is_rect_visible(row.rect) { - wanted.push(item.to_string()); + if by_script { + // One heading per script, so the list scrolls from language to language. + for (heading, names) in spec.sections { + ui.label(egui::RichText::new(heading).small().weak()); + for item in names { + let current = item.as_str() == spec.shown; + let row = font_row(ui, item, current, spec.preview, &mut wanted, scroll); + if row.clicked() { + outcome.chosen = Some(item.clone()); + open = false; + ui.memory_mut(|m| m.surrender_focus(typed_id)); + } + } } - if current && (scroll || typing) { - row.scroll_to_me(Some(Align::Center)); + } else { + if !recent_rows.is_empty() { + ui.label(egui::RichText::new(crate::tl_id!("ui.all_fonts")).small().weak()); } - if row.clicked() { - outcome.chosen = Some((*item).clone()); - open = false; - ui.memory_mut(|m| m.surrender_focus(typed_id)); + for (index, item) in rows.iter().enumerate() { + let current = if typing { index == highlight } else { item.as_str() == spec.shown }; + let row = font_row(ui, item, current, spec.preview, &mut wanted, scroll || typing); + if row.clicked() { + outcome.chosen = Some((*item).clone()); + open = false; + ui.memory_mut(|m| m.surrender_focus(typed_id)); + } } } }); @@ -232,6 +272,8 @@ pub fn show(ui: &mut Ui, text: Color32, spec: Spec<'_>) -> Outcome { ui.data_mut(|d| { d.insert_temp(scroll_id, scroll); d.insert_temp(open_id, open); + d.insert_temp(spec.id.with("last-typing"), typing); + d.insert_temp(spec.id.with("press-armed"), keep_typing && !released); if let Some(r) = list_rect { d.insert_temp(list_rect_id, r); } else { diff --git a/crates/ui-egui/src/floating.rs b/crates/ui-egui/src/floating.rs index bcbeae47..bce2443e 100644 --- a/crates/ui-egui/src/floating.rs +++ b/crates/ui-egui/src/floating.rs @@ -119,6 +119,8 @@ pub struct FontBoxes { pub recent: Vec, /// The family shown is not installed, so the box shows its warning mark. pub family_missing: bool, + /// The installed families grouped by script, with a heading for each. + pub sections: Vec<(String, Vec)>, } /// The egui memory key under which the ribbon stores the [`FontBoxes`] for this frame. @@ -215,6 +217,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts recent: &fonts.recent, missing: fonts.family_missing, preview: true, + sections: &fonts.sections, }, ); if let Some(name) = families.chosen { @@ -237,6 +240,7 @@ pub fn show_bar(ctx: &Context, id: Id, anchor: Rect, bar: &Bar, t: &Tokens, acts recent: &[], missing: false, preview: false, + sections: &[], }, ); if let Some(chosen) = size.chosen.and_then(|typed| typed.parse::().ok()) { diff --git a/crates/ui-egui/src/font_picker.rs b/crates/ui-egui/src/font_picker.rs index aee44f10..fd9c2054 100644 --- a/crates/ui-egui/src/font_picker.rs +++ b/crates/ui-egui/src/font_picker.rs @@ -121,3 +121,98 @@ mod tests { assert!(!size_is_usable("13", &sizes)); } } + +/// A script the family list is grouped by. A font belongs under each script whose sample +/// character it draws. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Script { + Latin, + Greek, + Cyrillic, + Arabic, + Hebrew, + Devanagari, + Thai, + Han, + Kana, + Hangul, +} + +impl Script { + /// Every script, in the order the list shows them. + pub const ALL: [Script; 10] = [ + Script::Latin, + Script::Greek, + Script::Cyrillic, + Script::Arabic, + Script::Hebrew, + Script::Devanagari, + Script::Thai, + Script::Han, + Script::Kana, + Script::Hangul, + ]; + + /// One letter a font of this script must draw. + pub fn sample(self) -> char { + match self { + Script::Latin => 'A', + Script::Greek => '\u{03A9}', + Script::Cyrillic => '\u{0416}', + Script::Arabic => '\u{0627}', + Script::Hebrew => '\u{05D0}', + Script::Devanagari => '\u{0915}', + Script::Thai => '\u{0E01}', + Script::Han => '\u{4E2D}', + Script::Kana => '\u{3042}', + Script::Hangul => '\u{D55C}', + } + } +} + +/// The scripts a font covers, judged by whether it draws each script's sample character. +pub fn scripts_of(has_glyph: impl Fn(char) -> bool) -> Vec