2bc41aeb8602
font_menu.rs
//! The font family menu of the Character, Properties and Glyphs panels and Find Font's Replace
//! With: a search field over every family available, each row the family's name and a sample of
//! the selected text set in it. In the Character and Properties panels the row under the pointer
//! or reached with โ/โ previews its font on the selected text (one live interaction, nothing in
//! the history); a click or Enter applies it as one step, Escape or closing the menu puts the text
//! back. The Glyphs panel and Find Font only pick a font ([`picked`]): nothing is previewed on the
//! document. A star marks a favourite family, and the โ
filter lists only those. The Document
//! filter lists only the families that type in the current document is set in
//! ([`document_families`]), and is unavailable while no document is open. The Recently Added
//! filter lists only the installed families with a font file created in the last 30 days, the
//! newest first and without the Recent Fonts section ([`vectorcraft_text::FontDb::recently_added`]).
//! The โ button before a family's star (Show Similar Fonts) orders the listed families by how alike
//! they look to it ([`vectorcraft_text::FontDb::similar_families`]), that family first and without the Recent
//! Fonts section, until the โ chip under the filters clears it or the menu closes. The Font
//! Technology filter's Variable lists only variable font families. Preferences โบ Type โบ Enable
//! in-menu font previews and Font Preview Size turn the samples off or size the rows. While the
//! samples are shown, a row under the filters sets Font Preview Size to Small, Medium or Large
//! ([`apply_preview_size`]), which sizes the rows of every font menu, and the list stays on the
//! rows it showed. The menu lays out its filters on the same five rows wherever it opens, with the
//! size row under them while the samples are shown. However narrow its field, it opens at least
//! wide enough for those rows and for a family name up to 220 points wide with its sample
//! beside it, up to 60% of the window; each sample fills its row's height. Recently used families
//! appear first, in the order they were last applied, within the same search and filters as the
//! remaining families. In the Character and Properties panels and the Control bar,
//! the mouse wheel turned over the closed field applies the next family (or previous one) to the
//! selected text, one undo step per notch. The menu's order at the first turn stays fixed until the
//! pointer leaves the field or the text selection changes; steps stop at its first and last family.
//! The field takes the wheel only when the pointer was moved onto it after the field last moved or
//! was covered by a window or overlay, and the wheel stream began there.
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use egui::{Color32, Sense, Ui, vec2};
use serde_json::json;
use vectorcraft_text::FontClass;
use crate::VectorcraftApp;
use crate::theme::Tokens;
use crate::widgets;
use crate::widgets::{ArmedWheelField, WheelStream, WheelStreamOwner};
/// What happened in the menu this frame.
#[derive(Clone, Debug, PartialEq)]
pub enum FontPick {
/// A family (and one of its styles, when picked from the family's expanded list) was chosen.
Chosen(String, Option<String>),
/// The highlighted row changed: preview its family (and style).
Preview(String, Option<String>),
/// The menu closed (or came back to the current family) without a choice: undo the preview.
EndPreview,
/// The star of a family was clicked: add it to the favourites, or take it out.
Favorite(String),
/// The mouse wheel turned over the closed field: apply each of these families in turn, one
/// undo step each.
Wheel(Vec<String>),
}
/// The UI that contains a font family field.
#[derive(Clone, Copy, Debug)]
pub enum WheelOwner {
Character,
Properties,
Control,
/// The Character popover attached to this Control bar link.
ControlPopover(egui::Id),
/// The Contextual Task Bar's type controls.
TaskBar,
}
impl WheelOwner {
fn visible(self, app: &VectorcraftApp, ctx: &egui::Context) -> bool {
if crate::panels::character::text_style(app).is_none() {
return false;
}
let panel = match self {
Self::Control | Self::ControlPopover(_) | Self::Properties if app.session.tool_id() == "artboard" => return false,
Self::Control => return app.ui.control_bar && app.ui.screen_mode < 2,
Self::ControlPopover(anchor) => {
return app.ui.control_bar && app.ui.screen_mode < 2 && widgets::popover_is_open(ctx, anchor);
}
Self::TaskBar => {
return app.ui.task_bar
&& app.ui.screen_mode < 3
&& crate::canvas::task_bar_shows_type(app)
&& crate::canvas::task_bar_shows_fonts(ctx);
}
Self::Character => "character",
Self::Properties => {
let selected = app.session.active().and_then(|document| document.selection.objects.first().and_then(|id| document.doc.node(*id)));
if !selected.is_some_and(|n| matches!(&n.kind, vectorcraft_doc::NodeKind::Text(_))) {
return false;
}
"properties"
}
};
app.ui.dock
&& app.ui.screen_mode < 3
&& (crate::panel_docking::showing(&app.ui, panel)
|| (app.ui.transient_panel.as_deref() == Some(panel) && crate::panel_docking::showing(&app.ui, "appearance")))
}
}
/// How the menu looks (from the preferences and the favourites) and whether its closed field
/// takes the mouse wheel.
#[derive(Clone, Copy, Debug)]
pub struct MenuLook<'a> {
/// Show each family's sample (Preferences โบ Type โบ Enable in-menu font previews).
pub samples: bool,
/// Preferences โบ Type โบ Show samples on white: each sample is drawn in dark type on a white
/// area, whatever the interface's brightness (off by default).
pub on_white: bool,
/// Preferences โบ Type โบ Show Font Names in English (on by default).
pub english_names: bool,
/// Row height in points (Font Preview Size).
pub row: f32,
/// The starred families.
pub favorites: &'a [String],
/// Recently applied families, newest first, limited by Number of Recent Fonts.
pub recent: &'a [String],
/// The mouse wheel turned over the closed field steps through the families in the menu's
/// order ([`FontPick::Wheel`]): the Character and Properties panels and the Control bar.
pub wheel: bool,
/// Its owner, so a field hidden before the next layout cannot capture wheel input.
pub wheel_owner: Option<WheelOwner>,
/// The name screen readers give the closed field when no label stands beside it.
pub name: Option<&'a str>,
/// The current document, which the Document filter lists the families of. `None`: no document
/// is open, and the filter is unavailable.
pub document: Option<MenuDocument<'a>>,
/// The time the Recently Added filter counts back from. `None`: the system clock.
pub clock: Option<std::time::SystemTime>,
}
/// The current document as the Document filter reads it.
#[derive(Clone, Copy, Debug)]
pub struct MenuDocument<'a> {
/// The document's identity.
pub uid: u64,
/// Its content: while an interaction is open (a font previewed from the menu), the content it
/// started from, so a preview does not change the families listed. Its families are listed
/// again when this is another version of the document.
pub doc: &'a Arc<vectorcraft_doc::Document>,
}
impl MenuDocument<'_> {
/// Which document and which version of it the families were read from.
fn key(&self) -> DocumentKey {
DocumentKey { uid: self.uid, doc: Arc::downgrade(self.doc) }
}
}
/// A document and a version of its content, compared by identity. The weak reference keeps the
/// version's allocation, so a later version is never placed at the same address while the key is
/// kept.
#[derive(Clone, Debug)]
struct DocumentKey {
uid: u64,
doc: std::sync::Weak<vectorcraft_doc::Document>,
}
impl PartialEq for DocumentKey {
fn eq(&self, other: &Self) -> bool {
self.uid == other.uid && std::sync::Weak::ptr_eq(&self.doc, &other.doc)
}
}
impl Default for MenuLook<'static> {
/// Samples on in the theme's colors, English names, medium rows, no favorites, no wheel, no
/// document, the system clock.
fn default() -> Self {
Self {
samples: true,
on_white: false,
english_names: true,
row: ROW,
favorites: &[],
recent: &[],
wheel: false,
wheel_owner: None,
name: None,
document: None,
clock: None,
}
}
}
impl<'a> MenuLook<'a> {
pub fn of(app: &'a VectorcraftApp) -> Self {
let row = preview_row(&app.session.prefs.font_preview_size);
Self {
samples: app.session.prefs.font_preview,
on_white: app.session.prefs.font_samples_on_white,
english_names: app.session.prefs.font_names_in_english,
row,
favorites: &app.ui.favorite_fonts,
recent: app.recent_fonts(),
wheel: false,
wheel_owner: None,
name: None,
document: app
.session
.active()
.map(|state| MenuDocument { uid: state.uid, doc: state.interaction.as_ref().map_or(&state.doc, |interaction| &interaction.doc) }),
clock: None,
}
}
}
/// Height of a row, in points (Font Preview Size: Medium).
const ROW: f32 = 24.0;
/// Font Preview Size's values (Preferences โบ Type), each with the row height it gives.
const PREVIEW_SIZES: [(&str, f32); 3] = [("small", 20.0), ("medium", ROW), ("large", 32.0)];
/// The row height of Font Preview Size `size`: Medium's for a value it does not have.
fn preview_row(size: &str) -> f32 {
PREVIEW_SIZES.iter().find(|(key, _)| *key == size).map_or(ROW, |(_, row)| *row)
}
/// The label of Font Preview Size `size`, as Preferences names it.
fn preview_size_label(size: &str) -> &'static str {
match size {
"small" => tl!("Small"),
"large" => tl!("Large"),
_ => tl!("Medium"),
}
}
/// Width of the star at the end of a family's row.
const STAR: f32 = 18.0;
/// Width of the Show Similar Fonts button before the star.
const SIMILAR: f32 = 18.0;
/// The least share of a row the family's name takes when the row is too narrow for the widest
/// name and [`SAMPLE_W`]; the sample fills the rest.
const NAME_SHARE: f32 = 0.5;
/// Size of a family's name in its row.
const NAME_SIZE: f32 = 12.0;
/// The widest name the menu widens itself for. A longer name takes more of a row only in a menu
/// wider than that.
pub(crate) const NAME_MAX: f32 = 220.0;
/// Space between a family's name and its sample.
const NAME_GAP: f32 = 8.0;
/// How much wider than the sum of its characters' advances a name may lay out once shaped
/// (kerning, ligatures, contextual forms), as a share of that sum.
const SHAPING: f32 = 0.1;
/// The room the menu keeps for a sample beside the widest name.
const SAMPLE_W: f32 = 150.0;
/// The greatest share of the window's width the menu takes.
const WINDOW_SHARE: f32 = 0.6;
/// Size of the filters' labels.
const FILTER_SIZE: f32 = 11.0;
/// Width of the disclosure triangle of a family with several styles.
const DISCLOSURE: f32 = 14.0;
/// How far a family's style rows are indented past the triangle.
const STYLE_INDENT: f32 = 12.0;
/// The list's greatest height, under the search field and filters.
const LIST_HEIGHT: f32 = 600.0;
/// Samples rendered (or asked for) per frame, across every menu, so a long list doesn't stall the UI.
const SAMPLES_PER_FRAME: usize = 4;
/// Samples rendering at once (native).
#[cfg(not(target_arch = "wasm32"))]
const MAX_PENDING: usize = 8;
/// What a sample shows when the font lacks the selected text's characters (or there is none).
const FALLBACK_SAMPLE: &str = "Sample";
/// The script filter's choices: all, Japanese, Latin, Thai.
const SCRIPTS: usize = 4;
fn script_label(i: usize) -> &'static str {
match i {
// CJK families use the `japanese` font trait; Chinese UI calls this ๆฑๅญ (#2206).
1 => crate::i18n::tr_ctx(crate::i18n::current(), "font-script", "Japanese"),
2 => tl!("Latin"),
3 => tl!("Thai"),
_ => tl!("All Scripts"),
}
}
/// The Font Technology filter's one choice: variable fonts. An adjective for fonts, not the
/// Variables panel's noun (`ๅคๆฐ`), hence its context.
fn variable_label() -> &'static str {
crate::i18n::tr_ctx(crate::i18n::current(), "font-technology", "Variable")
}
/// Does a family of `traits` pass script filter `script` ([`SCRIPTS`])? Latin: neither Japanese
/// nor Thai.
fn script_matches(script: usize, traits: vectorcraft_text::FontTraits) -> bool {
match script {
1 => traits.japanese,
2 => !traits.japanese && !traits.thai,
3 => traits.thai,
_ => true,
}
}
fn class_label(c: FontClass) -> &'static str {
match c {
FontClass::Serif => tl!("Serif / Mincho"),
FontClass::Sans => tl!("Sans Serif / Gothic"),
FontClass::Rounded => tl!("Rounded"),
FontClass::Script => tl!("Script / Brush"),
FontClass::Monospaced => tl!("Monospaced"),
FontClass::Decorative => tl!("Decorative"),
FontClass::Other => tl!("Other"),
}
}
/// A row of the list: a family, or one of the styles of an expanded family.
#[derive(Clone, Debug, PartialEq)]
enum Row {
Heading(&'static str),
Family(String),
Style(String, String),
}
impl Row {
fn family(&self) -> Option<&str> {
match self {
Row::Heading(_) => None,
Row::Family(f) | Row::Style(f, _) => Some(f),
}
}
fn style(&self) -> Option<&str> {
match self {
Row::Heading(_) | Row::Family(_) => None,
Row::Style(_, s) => Some(s),
}
}
fn pick(&self) -> Option<(String, Option<String>)> {
Some((self.family()?.to_string(), self.style().map(str::to_string)))
}
}
/// Recent families keep their application order, once each, and only while still available and
/// passing the filters. The other families keep the database's order. Headings share the virtual
/// list's row height, but never act as fonts or take keyboard focus.
fn grouped_rows(families: &[String], recent: &[String], expanded: &[String], styles: impl Fn(&str) -> Vec<String>) -> Vec<Row> {
let mut first: Vec<&String> = vec![];
for name in recent {
if let Some(f) = families.iter().find(|f| f.eq_ignore_ascii_case(name))
&& !first.iter().any(|other| other.eq_ignore_ascii_case(f))
{
first.push(f);
}
}
let mut rows = vec![];
let append = |rows: &mut Vec<Row>, f: &String| {
rows.push(Row::Family(f.clone()));
if expanded.iter().any(|e| e.eq_ignore_ascii_case(f)) {
rows.extend(styles(f).into_iter().map(|s| Row::Style(f.clone(), s)));
}
};
// Build the family rows before adding headings, so the same expansion logic serves both groups.
for f in &first {
append(&mut rows, f);
}
let boundary = rows.len();
for f in families.iter().filter(|f| !first.contains(f)) {
append(&mut rows, f);
}
if boundary > 0 {
if boundary < rows.len() {
rows.insert(boundary, Row::Heading("Fonts"));
}
rows.insert(0, Row::Heading("Recent Fonts"));
}
rows
}
/// Keep keyboard/pointer focus on the same font after filtering, loading new fonts or
/// expanding a family's styles. A row index on its own can now name a different font.
fn remap_highlight(before: &[Row], selected: Option<usize>, after: &[Row]) -> Option<usize> {
let row = before.get(selected?)?;
after.iter().position(|candidate| candidate == row)
}
/// What the rows were listed for: the query, the script, kind, font technology (variable only) and
/// favourites filters, Show Font Names in English, the expanded families, favourites, recent
/// history, font list generation, the family Show Similar Fonts orders by, the document version
/// while the Document filter lists its families, and the recently added families while the
/// Recently Added filter lists them.
type RowsKey = (
String,
usize,
Option<FontClass>,
bool,
bool,
bool,
Vec<String>,
Vec<String>,
Vec<String>,
u64,
Option<String>,
(Option<DocumentKey>, Option<Arc<AddedFonts>>),
);
/// The installed families the Recently Added filter lists: each one's place in the list
/// (lowercase name to place, newest first), and the instant from which they are to be found again
/// ([`vectorcraft_text::RecentlyAdded::recheck_at`]).
#[derive(Debug, Default, PartialEq, Eq)]
struct AddedFonts {
place: HashMap<String, usize>,
recheck_at: Option<std::time::SystemTime>,
}
impl AddedFonts {
fn of(list: &vectorcraft_text::RecentlyAdded) -> Self {
Self { place: list.families.iter().enumerate().map(|(i, f)| (f.to_lowercase(), i)).collect(), recheck_at: list.recheck_at }
}
/// Orders `families` (the listed ones) newest first.
fn order(&self, families: &mut [String]) {
families.sort_by_cached_key(|f| self.place.get(&f.to_lowercase()).copied().unwrap_or(usize::MAX));
}
}
/// The recently added families a [`MenuState`] found, the database and font list generation they
/// were found in ([`vectorcraft_text::FontDb::id`], [`vectorcraft_text::FontDb::generation`]), and
/// the time they were found for.
type RecentlyAddedFonts = ((u64, u64), std::time::SystemTime, Arc<AddedFonts>);
/// The families a [`MenuState`] found in a document (lowercase), and the document version and font
/// list generation they were found for.
type DocumentFonts = ((DocumentKey, u64), Arc<HashSet<String>>);
/// The menu's state between frames.
#[derive(Clone, Default)]
struct MenuState {
/// The pass the list was last drawn in, and the pass it opened in.
last: u64,
opened: u64,
/// The family the text had as the menu opened. A preview shows another one in the document
/// (and the menu's caller), but this one stays the current family: hovering it ends the
/// preview (#563).
current: String,
/// The highlighted row and the query and filters it belongs to.
highlight: Option<usize>,
query: String,
/// The filters (kept from one opening to the next, and the same in every font menu:
/// [`Filters`]): script (0..[`SCRIPTS`]), kind, font technology (variable fonts only),
/// favorites only, the current document's families only and recently added families only.
script: usize,
class: Option<FontClass>,
variable_only: bool,
favorites_only: bool,
document_only: bool,
added_only: bool,
/// The recently added families the Recently Added filter last listed.
added: Option<RecentlyAddedFonts>,
/// The families of the current document the Document filter last listed.
document_fonts: Option<DocumentFonts>,
/// The Document filter lists nothing because the document's type uses none of the menu's
/// families (none at all, or only missing ones), whatever the other filters.
none_used: bool,
/// Families whose styles are listed (lowercase).
expanded: Vec<String>,
/// Show Similar Fonts: the family the list is ordered by likeness to, until it is cleared or
/// the menu closes.
similar: Option<String>,
/// Show Similar Fonts was chosen or cleared: the family it ordered by, which is highlighted
/// and scrolled to once (to the top while the list is ordered by it).
similar_moved: Option<String>,
/// What was previewed last (None: nothing is previewed).
previewed: Option<(String, Option<String>)>,
/// The rows listed, and what for: built again only when that changes, not every frame.
rows: Arc<[Row]>,
rows_key: Option<RowsKey>,
/// The height of the search field and filters last frame.
header: f32,
/// The width of the widest row of filters without wrapping, last frame.
filters: f32,
/// The rows' height last frame, and how far down the list its view began, in rows. At a new
/// height (Font Preview Size) the list scrolls back to the same row.
scrolled: Option<(f32, f32)>,
/// The families the wheel steps through, listed at its first notch over the closed field and
/// kept for that text selection while the pointer stays on it: each step makes its family a recent one, which would
/// otherwise move it in the order. The Document filter's families are kept the same way: a
/// step that leaves a family unused keeps it in the order, so turning the wheel back returns
/// to it. They are listed again at the first notch after the pointer leaves the field or the
/// selection changes.
wheel_order: Option<Arc<[String]>>,
wheel_target: Option<WheelTarget>,
}
/// The filters of [`MenuState`], one set for every font menu (the Character panel's, the Control
/// bar's, the Properties panel'sโฆ): a filter chosen in one menu is on in all of them. They last
/// for the session.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Filters {
script: usize,
class: Option<FontClass>,
variable_only: bool,
favorites_only: bool,
document_only: bool,
added_only: bool,
}
impl Filters {
fn of(st: &MenuState) -> Self {
Self {
script: st.script,
class: st.class,
variable_only: st.variable_only,
favorites_only: st.favorites_only,
document_only: st.document_only,
added_only: st.added_only,
}
}
fn apply(self, st: &mut MenuState) {
st.script = self.script;
st.class = self.class;
st.variable_only = self.variable_only;
st.favorites_only = self.favorites_only;
st.document_only = self.document_only;
st.added_only = self.added_only;
}
}
/// The families the type in `doc` is set in, lowercase and by the name the menu lists them under
/// ([`vectorcraft_text::FontDb::canonical`]): the runs of every text object in the layers, symbols,
/// patterns, graph designs and stored scatter brushes, hidden, locked and masked ones included, with
/// a composite font named as such. A pattern being edited is read from its editing layer only. Each
/// run's family is looked up with its style, so a family and style split from a PostScript name
/// (`Honoka`, `Mincho`) find the installed font of that name. Styles no type uses are left out.
fn document_families(db: &vectorcraft_text::FontDb, doc: &vectorcraft_doc::Document) -> HashSet<String> {
// Each family and style once, so a document with many runs in a few fonts looks each one up
// once.
let mut named: HashMap<String, HashSet<String>> = HashMap::new();
let mut add = |n: &vectorcraft_doc::Node| {
if let vectorcraft_doc::NodeKind::Text(t) = &n.kind {
for r in &t.runs {
let (family, style) = (&r.style.font_family, &r.style.font_style);
match named.get_mut(family) {
Some(styles) => {
if !styles.contains(style) {
styles.insert(style.clone());
}
}
None => {
named.insert(family.clone(), HashSet::from([style.clone()]));
}
}
}
}
};
let editing = doc.pattern_edit.as_ref().map(|e| e.pattern.as_str());
let patterns = doc.patterns.iter().filter(|p| Some(p.name.as_str()) != editing).flat_map(|p| p.art.iter().map(|a| &**a));
let designs = doc.graph_designs.iter().map(|g| &*g.art);
for n in doc.layers.iter().map(|l| &**l).chain(doc.symbols.iter().map(|s| &*s.art)).chain(patterns).chain(designs) {
vectorcraft_doc::swatches::walk_with_masks(n, &mut add);
}
// A scatter brush draws copies of its art's type along a stroke; art and pattern brushes drop
// type when they bend their art. Malformed library entries are skipped.
let brushes = doc.unknown.get(vectorcraft_brush::DOC_KEY).map(vectorcraft_brush::parse_library).unwrap_or_default();
for b in &brushes {
if let vectorcraft_brush::BrushKind::Scatter(s) = &b.kind {
vectorcraft_doc::swatches::walk_with_masks(&s.art, &mut add);
}
}
named.iter().flat_map(|(f, styles)| styles.iter().map(move |s| db.canonical(f, s).0.to_lowercase())).collect()
}
/// The families the Document filter lists, when it is on and a document is open: those of
/// `look`'s document, found again only when the document, its version or the font list changed.
fn used_families(db: &vectorcraft_text::FontDb, st: &mut MenuState, look: MenuLook) -> Option<Arc<HashSet<String>>> {
let document = look.document.filter(|_| st.document_only)?;
let key = (document.key(), db.generation());
if let Some((k, families)) = &st.document_fonts
&& *k == key
{
return Some(families.clone());
}
let families = Arc::new(document_families(db, document.doc));
st.document_fonts = Some((key, families.clone()));
Some(families)
}
/// The time the Recently Added filter counts back from: `look`'s clock, else the system clock. On
/// wasm, which has no installed fonts to date, the Unix epoch.
fn added_clock(look: MenuLook) -> std::time::SystemTime {
look.clock.unwrap_or_else(|| {
#[cfg(not(target_arch = "wasm32"))]
{
std::time::SystemTime::now()
}
#[cfg(target_arch = "wasm32")]
{
std::time::SystemTime::UNIX_EPOCH
}
})
}
/// The families the Recently Added filter lists, when it is on: those `db` dates within
/// [`vectorcraft_text::RECENTLY_ADDED_WITHIN`] before `look`'s clock, found again only when the
/// font list changed, the clock reached the instant the list may change at, or the clock went back
/// before the time the list was found for.
fn added_families(db: &vectorcraft_text::FontDb, st: &mut MenuState, look: MenuLook) -> Option<Arc<AddedFonts>> {
if !st.added_only {
return None;
}
let now = added_clock(look);
let key = (db.id(), db.generation());
if let Some((k, asked, added)) = &st.added
&& *k == key
&& *asked <= now
&& added.recheck_at.is_none_or(|t| now < t)
{
return Some(added.clone());
}
let added = Arc::new(AddedFonts::of(&db.recently_added(now)));
st.added = Some((key, now, added.clone()));
Some(added)
}
/// Where the filters every font menu shares are kept ([`Filters`]).
fn filters_id() -> egui::Id {
egui::Id::unique("font-menu-filters")
}
/// The font family menu showing `current`, `width` points wide, rows sampling `sample` (the
/// selected text; `None`: a generic sample).
pub fn font_menu(
ui: &mut Ui,
id: impl std::hash::Hash + std::fmt::Debug,
current: &str,
width: f32,
sample: Option<&str>,
look: MenuLook,
) -> Option<FontPick> {
let state_id = ui.scope_id().with(&id).with("font-menu");
let mut st: MenuState = ui.data(|d| d.get_temp(state_id)).unwrap_or_default();
ui.data(|d| d.get_temp::<Filters>(filters_id())).unwrap_or_default().apply(&mut st);
// The viewport is read outside `ui.data`. The closure of `ui.data` runs with the context
// locked, and a second lock taken inside it blocks once a font-sample thread is waiting to
// request a repaint.
let wheel_fields = wheel_fields_id(ui.ctx().viewport_id());
let target = ui.data(|d| d.get_temp(wheel_fields.with("target")));
let target_changed = st.wheel_target != target;
if target_changed {
st.wheel_order = None;
st.wheel_target = target;
}
let height = st.header + LIST_HEIGHT;
let state = &mut st;
// The search field and filters stay in place over the rows, which scroll. The rows are all
// one height (a family's styles too), so only those in view are laid out: there are thousands
// of families.
let (field, shown) = widgets::combo_menu_field_named(ui, &id, look.name, current, width, |menu| {
menu.height(height).show_rows_with_header(
|ui| {
// Wider than a narrow field, so the filters keep their rows and long names show.
ui.set_min_width(menu_width(ui, state, look));
header(ui, vectorcraft_text::FontDb::global(), state_id, state, current, look)
},
look.row,
|h| h.rows.len(),
|ui, range, h| rows(ui, range, h, sample, look),
)
});
let drawn = shown.is_some();
if target_changed {
ui.data_mut(|d| d.remove::<f32>(field.id.with("wheel")));
}
let mut out = shown.flatten();
if !drawn && out.is_none() && st.previewed.take().is_some() {
// Closed (Escape, a click outside) with a preview showing.
out = Some(FontPick::EndPreview);
}
let turns = remember_owned_wheel_field(ui, &field, look.wheel && !drawn && field.enabled(), state_id, look.wheel_owner);
if !look.wheel || !field.enabled() {
st.wheel_order = None;
ui.data_mut(|d| d.remove::<f32>(field.id.with("wheel")));
} else if out.is_none() || drawn {
if !turns.is_empty() {
let mut own = vectorcraft_text::FontDb::global().canonical(current, "").0;
let mut steps = Vec::new();
for turn in turns {
match turn {
WheelInput::Leave => st.wheel_order = None,
WheelInput::Notches(notches) => {
if st.wheel_order.is_none() {
st.wheel_order = Some(wheel_order(&mut st, look).into());
}
if let Some(order) = st.wheel_order.as_deref() {
let next = wheel_steps(order, &own, notches);
if let Some(last) = next.last() {
own.clone_from(last);
}
steps.extend(next);
}
}
}
}
if !steps.is_empty() {
if drawn {
// These turns preceded the opener. The popup cancels back to the newly
// applied family, and any preview drawn this pass has not been applied.
st.current.clone_from(&own);
st.highlight = st.rows.iter().position(|row| matches!(row, Row::Family(f) if f.eq_ignore_ascii_case(&own)));
st.previewed = None;
}
out = Some(FontPick::Wheel(steps));
}
}
let queued =
ui.data(|d| d.get_temp::<WheelFields>(wheel_fields).is_some_and(|fields| fields.queued.iter().any(|event| event.field == field.id)));
if drawn || (!field.contains_pointer() && !queued) {
st.wheel_order = None;
ui.data_mut(|d| d.remove::<f32>(field.id.with("wheel")));
}
}
if matches!(out, Some(FontPick::Chosen(..))) {
st.previewed = None;
}
ui.data_mut(|d| {
d.insert_temp(filters_id(), Filters::of(&st));
d.insert_temp(state_id, st);
});
out
}
/// Does the menu list family `f` for search `query` (lowercase) under the filters of `st`, among
/// `used` (lowercase) while the Document filter lists the document's families, and among `added`
/// while the Recently Added filter lists the recently added ones?
fn listed(
db: &vectorcraft_text::FontDb,
f: &str,
query: &str,
st: &MenuState,
look: MenuLook,
used: Option<&HashSet<String>>,
added: Option<&AddedFonts>,
) -> bool {
if used.is_some_and(|used| !used.contains(&f.to_lowercase())) {
return false;
}
if added.is_some_and(|added| !added.place.contains_key(&f.to_lowercase())) {
return false;
}
let label = db.family_display_name(f, look.english_names);
if !(query.is_empty() || f.to_lowercase().contains(query) || label.to_lowercase().contains(query)) {
return false;
}
if st.favorites_only && !look.favorites.iter().any(|x| x.eq_ignore_ascii_case(f)) {
return false;
}
if st.script == 0 && st.class.is_none() && !st.variable_only {
return true;
}
let traits = db.family_traits(f);
script_matches(st.script, traits) && st.class.is_none_or(|c| c == traits.class) && (!st.variable_only || traits.variable)
}
/// The families the closed field's wheel steps through, in the open menu's order with no search:
/// the recent families first, then the others, under the menu's filters; with Recently Added, the
/// newest first. Listed once per wheel gesture ([`MenuState`]'s `wheel_order`).
fn wheel_order(st: &mut MenuState, look: MenuLook) -> Vec<String> {
wheel_order_in(vectorcraft_text::FontDb::global(), st, look)
}
/// [`wheel_order`] of the families in `db`.
fn wheel_order_in(db: &vectorcraft_text::FontDb, st: &mut MenuState, look: MenuLook) -> Vec<String> {
let used = used_families(db, st, look);
let added = added_families(db, st, look);
let mut families: Vec<_> =
db.menu_family_list().iter().filter(|f| listed(db, f, "", st, look, used.as_deref(), added.as_deref())).cloned().collect();
if let Some(added) = &added {
added.order(&mut families);
}
grouped_rows(&families, if added.is_some() { &[] } else { look.recent }, &[], |_| vec![])
.into_iter()
.filter_map(|r| match r {
Row::Family(f) => Some(f),
Row::Heading(_) | Row::Style(..) => None,
})
.collect()
}
/// The families `notches` turns of the wheel step to from family `current` in `order`, one per
/// notch: forward (down the menu) when positive, back when negative. The steps stop at either end
/// of the order, and there are none when `current` is not in it.
fn wheel_steps(order: &[String], current: &str, notches: i32) -> Vec<String> {
let Some(at) = order.iter().position(|f| f.eq_ignore_ascii_case(current)) else { return vec![] };
let last = order.len().saturating_sub(1);
let n = notches.unsigned_abs() as usize;
let to = if notches >= 0 { at.saturating_add(n).min(last) } else { at.saturating_sub(n) };
if to >= at { order.iter().skip(at + 1).take(to - at).cloned().collect() } else { order.iter().take(at).skip(to).rev().cloned().collect() }
}
#[derive(Clone)]
enum WheelInput {
Leave,
Notches(i32),
}
#[derive(Clone, Copy)]
struct WheelField {
id: egui::Id,
menu: egui::Id,
owner: Option<WheelOwner>,
rect: egui::Rect,
layer: egui::LayerId,
}
#[derive(Clone)]
struct QueuedWheel {
#[cfg(not(target_arch = "wasm32"))]
frame: u64,
field: egui::Id,
#[cfg(not(target_arch = "wasm32"))]
menu: egui::Id,
#[cfg(not(target_arch = "wasm32"))]
position: egui::Pos2,
rect: egui::Rect,
#[cfg(not(target_arch = "wasm32"))]
event: egui::Event,
#[cfg(not(target_arch = "wasm32"))]
amount: f32,
reset_after: bool,
positions_after: Vec<egui::Pos2>,
}
#[derive(Clone)]
struct ReplayWheel {
#[cfg(not(target_arch = "wasm32"))]
target: egui::LayerId,
position: egui::Pos2,
event: egui::Event,
}
#[derive(Clone, Default)]
struct WheelFields {
pass: u64,
pending_frame: u64,
activation_pass: u64,
activating: Vec<egui::Id>,
pointer: Option<egui::Pos2>,
fields: Vec<WheelField>,
pending: HashMap<egui::Id, Vec<WheelInput>>,
queued: Vec<QueuedWheel>,
replay: Vec<ReplayWheel>,
armed: Option<ArmedWheelField>,
stream: WheelStream,
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone)]
struct WheelLayers(Arc<dyn Fn(egui::Pos2) -> Option<egui::LayerId> + Send + Sync>);
#[cfg(not(target_arch = "wasm32"))]
#[derive(Default)]
struct WheelLayerSnapshot;
#[cfg(not(target_arch = "wasm32"))]
impl egui::Plugin for WheelLayerSnapshot {
fn debug_name(&self) -> &'static str {
"font wheel viewport layers"
}
fn on_begin_pass(&mut self, ui: &mut Ui) {
let ctx = ui.ctx();
let viewport = ctx.viewport_id();
if viewport == egui::ViewportId::ROOT {
return;
}
let key = wheel_fields_id(viewport);
let Some(mut fields) = ctx.data(|d| d.get_temp::<WheelFields>(key)) else { return };
let frame = ctx.cumulative_frame_nr_for(viewport);
let mut ready = Vec::new();
fields.queued.retain(|event| {
if event.frame < frame {
ready.push(event.clone());
false
} else {
true
}
});
let replayed = fields.replay.len();
for event in ready {
let current = fields.fields.iter().find(|field| field.id == event.field);
let layer = current.map(|field| field.layer);
let opened_after_wheel = current.is_none()
&& ctx.data(|d| d.get_temp::<MenuState>(event.menu)).is_some_and(|menu| menu.opened > 0 && menu.opened == fields.pass);
let active = opened_after_wheel
|| current.is_some_and(|field| field.rect.contains(event.position) && ctx.layer_id_at(event.position) == Some(field.layer));
let left = !active || event.reset_after || event.positions_after.iter().any(|position| ctx.layer_id_at(*position) != layer);
let id = event.field.with("wheel");
if !active {
ctx.data_mut(|d| d.remove::<f32>(id));
fields.stream.release(event.field);
if let Some(target) = ctx.layer_id_at(event.position) {
fields.replay.push(ReplayWheel { target, position: event.position, event: event.event });
}
} else {
let notches = ctx.data(|d| d.get_temp::<f32>(id)).unwrap_or(0.0) + event.amount;
let whole = notches.trunc();
ctx.data_mut(|d| d.insert_temp(id, notches - whole));
if whole != 0.0 {
fields.pending.entry(event.field).or_default().push(WheelInput::Notches(whole as i32));
}
}
if left {
ctx.data_mut(|d| d.remove::<f32>(id));
fields.pending.entry(event.field).or_default().push(WheelInput::Leave);
}
}
if fields.replay.len() > replayed {
ctx.request_repaint();
}
ctx.data_mut(|d| d.insert_temp(key, fields));
}
fn on_end_pass(&mut self, ui: &mut Ui) {
let ctx = ui.ctx();
let viewport = ctx.viewport_id();
if viewport == egui::ViewportId::ROOT || ctx.data(|d| d.get_temp::<WheelFields>(wheel_fields_id(viewport))).is_none() {
return;
}
let current_layers = ctx.viewport(|viewport| {
viewport.this_pass.widgets.layers().filter_map(|(layer, widgets)| (!widgets.is_empty()).then_some(*layer)).collect::<Vec<_>>()
});
let layers = ctx.memory(|m| {
m.layer_ids()
.filter(|layer| current_layers.contains(layer))
.filter(|layer| m.areas().is_visible(layer))
.filter_map(|layer| m.area_rect(layer.id).map(|rect| (layer, rect)))
.collect::<Vec<_>>()
});
let layers = layers
.into_iter()
.filter(|(layer, _)| egui::AreaState::load(ctx, layer.id).is_some_and(|state| state.interactable))
.map(|(layer, rect)| (layer, ctx.layer_transform_to_global(layer).map_or(rect, |transform| transform * rect)))
.collect::<Vec<_>>();
ctx.data_mut(|d| {
d.insert_temp(
wheel_fields_id(viewport).with("layers"),
WheelLayers(Arc::new(move |p| layers.iter().rev().find(|(_, rect)| rect.contains(p)).map(|(layer, _)| *layer))),
)
});
}
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn ensure_native_wheel_layers(ctx: &egui::Context) {
if ctx.with_plugin(|_: &mut WheelLayerSnapshot| ()).is_none() {
ctx.add_plugin(WheelLayerSnapshot);
}
}
fn wheel_fields_id(viewport: egui::ViewportId) -> egui::Id {
egui::Id::unique("font-wheel-fields").with(viewport)
}
fn mark_queued_wheel_leaves(fields: &mut WheelFields, point: Option<egui::Pos2>) {
let mut marked = HashSet::new();
for event in fields.queued.iter_mut().rev() {
if marked.insert(event.field) {
match point {
Some(position) if event.rect.contains(position) => event.positions_after.push(position),
_ => event.reset_after = true,
}
}
}
}
#[cfg(test)]
pub(crate) fn wheel_field_layer_for_test(ctx: &egui::Context, viewport: egui::ViewportId, point: egui::Pos2) -> Option<egui::LayerId> {
ctx.data(|data| data.get_temp::<WheelFields>(wheel_fields_id(viewport)))
.and_then(|fields| fields.fields.iter().find(|field| field.rect.contains(point)).map(|field| field.layer))
}
#[cfg(test)]
pub(crate) fn offset_wheel_fields_for_test(ctx: &egui::Context, viewport: egui::ViewportId, offset: egui::Vec2) {
let key = wheel_fields_id(viewport);
ctx.data_mut(|data| {
let mut fields = data.get_temp::<WheelFields>(key).expect("registered wheel fields");
for field in &mut fields.fields {
field.rect = field.rect.translate(offset);
}
data.insert_temp(key, fields);
});
}
#[derive(Clone, PartialEq)]
struct WheelTarget {
input: crate::state::InputGeneration,
workspace: u64,
selection: Vec<vectorcraft_doc::NodeId>,
range: Option<(vectorcraft_doc::NodeId, usize, usize)>,
}
/// Pending turns and field rectangles belong to their input target and visible workspace.
pub(crate) fn sync_wheel_target(app: &VectorcraftApp, ctx: &egui::Context, viewport: egui::ViewportId) {
let target = WheelTarget {
input: app.input_generation(),
workspace: app.ui.docking_generation,
selection: app.session.active().map_or_else(Vec::new, |document| document.selection.objects.clone()),
range: crate::panels::character::text_editing(app),
};
let key = wheel_fields_id(viewport);
let (previous, mut fields) = ctx.data(|d| (d.get_temp::<WheelTarget>(key.with("target")), d.get_temp::<WheelFields>(key)));
if previous.is_some_and(|old| old != target) {
// A new target keeps the armed field and the running wheel stream.
fields = fields.map(|old| WheelFields { pass: old.pass, armed: old.armed, stream: old.stream, ..Default::default() });
}
let mut hidden = Vec::new();
if let Some(fields) = &mut fields {
fields.fields.retain(|field| {
if field.owner.is_none_or(|owner| owner.visible(app, ctx)) {
true
} else {
hidden.push(*field);
false
}
});
for field in &hidden {
fields.pending.remove(&field.id);
}
}
ctx.data_mut(|d| {
if let Some(fields) = fields {
d.insert_temp(key, fields);
} else {
d.remove::<WheelFields>(key);
}
for field in hidden {
d.remove::<f32>(field.id.with("wheel"));
if let Some(mut state) = d.get_temp::<MenuState>(field.menu) {
state.wheel_order = None;
d.insert_temp(field.menu, state);
}
}
d.insert_temp(key.with("target"), target);
});
}
/// Remember the visible closed fields for the next input batch and take this field's turns.
#[cfg(test)]
fn remember_wheel_field(ui: &Ui, resp: &egui::Response, enabled: bool) -> Vec<WheelInput> {
remember_owned_wheel_field(ui, resp, enabled, resp.id, None)
}
fn remember_owned_wheel_field(ui: &Ui, resp: &egui::Response, enabled: bool, menu: egui::Id, owner: Option<WheelOwner>) -> Vec<WheelInput> {
let key = wheel_fields_id(ui.ctx().viewport_id());
let pass = ui.ctx().cumulative_pass_nr();
// A window or overlay over the pointer covers the field even when no input arrives.
let pointer = ui.data_mut(|d| d.get_temp_mut_or_default::<WheelFields>(key).pointer).or_else(|| ui.ctx().input(|i| i.pointer.latest_pos()));
let covered = pointer.is_some_and(|p| resp.interact_rect.contains(p) && ui.ctx().layer_id_at(p) != Some(resp.layer_id));
ui.data_mut(|d| {
let mut fields: WheelFields = d.get_temp(key).unwrap_or_default();
if fields.pass != pass {
fields.pass = pass;
fields.fields.clear();
}
fields.fields.retain(|f| f.id != resp.id);
if let Some(armed) = &mut fields.armed
&& armed.id == resp.id
{
if armed.rect == resp.interact_rect && pass.saturating_sub(armed.pass) <= 1 && !covered {
armed.pass = pass;
} else {
fields.armed = None;
fields.stream.release(resp.id);
}
}
if enabled {
fields.fields.push(WheelField { id: resp.id, menu, owner, rect: resp.interact_rect, layer: resp.layer_id });
}
let turns = fields.pending.remove(&resp.id).unwrap_or_default();
d.insert_temp(key, fields);
turns
})
}
/// Take the closed font fields' wheel events before egui smooths and combines them. Other
/// events stay in the input, so scrolling elsewhere keeps its own delta and event order. A field
/// takes a wheel stream only when the stream begins with the pointer over it and the pointer was
/// moved onto it after the field last moved or was covered. A stream that began anywhere else
/// stays there until it ends, and a stream that leaves its field does not return to it.
pub(crate) fn take_raw_wheel(ctx: &egui::Context, raw: &mut egui::RawInput) {
let key = wheel_fields_id(raw.viewport_id);
let Some(mut fields) = ctx.data(|d| d.get_temp::<WheelFields>(key)) else { return };
if !ctx.input(|i| i.raw.viewports.contains_key(&raw.viewport_id)) {
ctx.data_mut(|d| d.remove::<WheelFields>(key));
#[cfg(not(target_arch = "wasm32"))]
ctx.data_mut(|d| {
d.remove::<WheelLayers>(key.with("layers"));
});
return;
}
let frame = ctx.cumulative_frame_nr_for(raw.viewport_id);
let pass = ctx.cumulative_pass_nr_for(raw.viewport_id);
if pass < fields.pass || pass.saturating_sub(fields.pass) > 1 {
fields.fields.clear();
fields.pointer = None;
fields.armed = None;
fields.stream.release_any();
}
let now = raw.time.unwrap_or_else(|| ctx.input_for(raw.viewport_id, |i| i.time) + f64::from(raw.predicted_dt));
if fields.pending_frame != frame {
fields.pending_frame = frame;
fields.pending.clear();
}
let line = ctx.options(|o| o.input_options.line_scroll_speed).max(1.0);
let mut pointer = fields.pointer.or_else(|| ctx.input_for(raw.viewport_id, |i| i.pointer.latest_pos()));
#[cfg(not(target_arch = "wasm32"))]
let native_layers = ctx.data(|d| d.get_temp::<WheelLayers>(key.with("layers")));
let over = |field: &WheelField, point: Option<egui::Pos2>| {
point.is_some_and(|p| {
if !field.rect.contains(p) {
return false;
}
if ctx.viewport_id() == raw.viewport_id {
ctx.layer_id_at(p) == Some(field.layer)
} else {
#[cfg(not(target_arch = "wasm32"))]
{
native_layers.as_ref().is_some_and(|layers| (layers.0)(p) == Some(field.layer))
}
#[cfg(target_arch = "wasm32")]
{
false
}
}
})
};
// An activation batch belongs to the popup, including a release after a prior press.
if fields.activation_pass != pass {
fields.activation_pass = pass;
fields.activating.clear();
let pressed = ctx.input(|i| if i.pointer.button_down(egui::PointerButton::Primary) { i.pointer.press_origin() } else { None });
if let Some(field) = fields.fields.iter().find(|f| over(f, pressed)) {
fields.activating.push(field.id);
}
}
let focused = ctx.memory(|m| m.focused());
let mut captured = false;
raw.events.retain(|event| {
let opening = match event {
egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, .. } => {
fields.fields.iter().rev().find(|f| over(f, Some(*pos))).map(|f| f.id)
}
egui::Event::Key { key: egui::Key::Enter | egui::Key::Space, pressed: true, .. } if raw.focused => {
focused.filter(|id| fields.fields.iter().any(|f| f.id == *id))
}
_ => None,
};
if let Some(id) = opening
&& !fields.activating.contains(&id)
{
fields.activating.push(id);
}
if matches!(event, egui::Event::MouseWheel { .. }) {
fields.stream.note_input(now);
}
match event {
egui::Event::PointerMoved(p) | egui::Event::PointerButton { pos: p, .. } => {
if pointer != Some(*p) {
let pass = fields.pass;
fields.armed = fields.fields.iter().rev().find(|f| over(f, Some(*p))).map(|f| ArmedWheelField { id: f.id, rect: f.rect, pass });
if let Some(WheelStreamOwner::Field(id)) = fields.stream.owner
&& !fields.fields.iter().any(|f| f.id == id && over(f, Some(*p)))
{
fields.stream.release(id);
}
}
mark_queued_wheel_leaves(&mut fields, Some(*p));
for field in &fields.fields {
if pointer.is_some_and(|point| field.rect.contains(point))
&& !field.rect.contains(*p)
&& !fields.queued.iter().any(|event| event.field == field.id)
{
ctx.data_mut(|d| d.remove::<f32>(field.id.with("wheel")));
fields.pending.entry(field.id).or_default().push(WheelInput::Leave);
}
}
pointer = Some(*p);
}
egui::Event::PointerGone => {
pointer = None;
fields.armed = None;
fields.stream.release_any();
mark_queued_wheel_leaves(&mut fields, None);
for field in &fields.fields {
if !fields.queued.iter().any(|event| event.field == field.id) {
ctx.data_mut(|d| d.remove::<f32>(field.id.with("wheel")));
fields.pending.entry(field.id).or_default().push(WheelInput::Leave);
}
}
}
egui::Event::MouseWheel { unit, delta, modifiers, source, .. } if modifiers.is_none() && delta.y != 0.0 => {
let under = fields.fields.iter().rev().find(|f| !fields.activating.contains(&f.id) && over(f, pointer)).copied();
let starts_on_field = under
.filter(|f| fields.armed.is_some_and(|armed| armed.id == f.id) && !matches!(source, egui::MouseWheelSource::Momentum))
.map(|f| f.id);
let owner = *fields.stream.owner.get_or_insert(starts_on_field.map_or(WheelStreamOwner::Elsewhere, WheelStreamOwner::Field));
if let Some(field) = under.filter(|f| owner == WheelStreamOwner::Field(f.id)) {
let amount = -match unit {
egui::MouseWheelUnit::Point => delta.y / line,
egui::MouseWheelUnit::Line | egui::MouseWheelUnit::Page => delta.y,
};
if cfg!(not(target_arch = "wasm32")) && ctx.viewport_id() != raw.viewport_id {
#[cfg(not(target_arch = "wasm32"))]
if let Some(position) = pointer {
fields.queued.push(QueuedWheel {
frame,
field: field.id,
menu: field.menu,
position,
rect: field.rect,
event: event.clone(),
amount,
reset_after: false,
positions_after: Vec::new(),
});
}
} else {
let id = field.id.with("wheel");
let notches = ctx.data(|d| d.get_temp::<f32>(id)).unwrap_or(0.0) + amount;
let whole = notches.trunc();
ctx.data_mut(|d| d.insert_temp(id, notches - whole));
if whole != 0.0 {
fields.pending.entry(field.id).or_default().push(WheelInput::Notches(whole as i32));
}
}
captured = true;
return false;
}
fields.stream.owner = Some(WheelStreamOwner::Elsewhere);
}
egui::Event::MouseWheel { delta, .. } if *delta != egui::Vec2::ZERO => {
fields.stream.owner.get_or_insert(WheelStreamOwner::Elsewhere);
}
_ => {}
}
true
});
if !fields.replay.is_empty() {
let mut replay = Vec::new();
for wheel in std::mem::take(&mut fields.replay) {
#[cfg(not(target_arch = "wasm32"))]
let target_still_visible = native_layers.as_ref().is_some_and(|layers| (layers.0)(wheel.position) == Some(wheel.target));
#[cfg(target_arch = "wasm32")]
let target_still_visible = false;
if target_still_visible {
replay.push(egui::Event::PointerMoved(wheel.position));
replay.push(wheel.event);
}
}
if !replay.is_empty() {
replay.push(pointer.map_or(egui::Event::PointerGone, egui::Event::PointerMoved));
raw.events.splice(0..0, replay);
}
}
fields.pointer = pointer;
ctx.data_mut(|d| d.insert_temp(key, fields));
if captured {
ctx.data_mut(|d| d.insert_temp(widgets::taken_from_raw_id(raw.viewport_id), now));
ctx.request_repaint();
}
}
/// What the menu's header hands its rows.
struct Listed<'s> {
/// The fonts listed: [`vectorcraft_text::FontDb::global`] outside the unit tests.
db: &'s vectorcraft_text::FontDb,
st: &'s mut MenuState,
/// The rows listed.
rows: Arc<[Row]>,
/// The current family by its own name.
current: String,
/// Enter was pressed; โ/โ/โ/โ moved the highlight; the menu is opening.
enter: bool,
keyed: bool,
opening: bool,
}
/// The menu's contents outside a menu, for the unit tests: the header over its rows, as
/// [`craft_ui::combo::Menu::show_rows_with_header`] lays them out.
#[cfg(test)]
fn list(ui: &mut Ui, state_id: egui::Id, st: &mut MenuState, current: &str, sample: Option<&str>, look: MenuLook) -> Option<FontPick> {
list_of(ui, vectorcraft_text::FontDb::global(), state_id, st, current, sample, look).inner
}
/// [`list`] of the families in `db`, with the rows' scroll area as laid out.
#[cfg(test)]
fn list_of(
ui: &mut Ui,
db: &vectorcraft_text::FontDb,
state_id: egui::Id,
st: &mut MenuState,
current: &str,
sample: Option<&str>,
look: MenuLook,
) -> egui::scroll_area::ScrollAreaOutput<Option<FontPick>> {
let h = header(ui, db, state_id, st, current, look);
let total = h.rows.len();
egui::ScrollArea::vertical().max_height(ui.available_height()).show_rows(ui, look.row, total, |ui, range| rows(ui, range, h, sample, look))
}
/// The top of the open menu, which stays in place: the search field, the filters, and the rows
/// of `db` they list.
fn header<'s>(ui: &mut Ui, db: &'s vectorcraft_text::FontDb, state_id: egui::Id, st: &'s mut MenuState, current: &str, look: MenuLook) -> Listed<'s> {
let t = Tokens::get(ui.ctx());
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
// Each time the menu opens: no query, the current family highlighted and in view (the
// filters stay as they were).
let pass = ui.ctx().cumulative_pass_nr();
let opening_now = st.last + 1 < pass || st.last == 0;
if opening_now {
st.opened = pass;
st.highlight = None;
st.query.clear();
st.current = current.to_string();
st.similar = None;
st.similar_moved = None;
}
st.last = pass;
// For [`end_stale_preview`]: the menu is open, in this layer.
ui.ctx().data_mut(|d| d.insert_temp(open_id(), (pass, ui.layer_id())));
let field = state_id.with("query");
if opening_now {
ui.data_mut(|d| d.insert_temp(field, String::new()));
}
// A popup sizes itself invisibly in its first pass.
let opening = pass <= st.opened + 1;
// Arrow keys move the highlight and open or close a family's styles (taken before the
// search field sees them).
let key = |i: &mut egui::InputState, k| i.consume_key(egui::Modifiers::NONE, k);
let (up, down, left, right) =
ui.input_mut(|i| (key(i, egui::Key::ArrowUp), key(i, egui::Key::ArrowDown), key(i, egui::Key::ArrowLeft), key(i, egui::Key::ArrowRight)));
if ui.memory(|m| m.focused().is_none()) {
ui.memory_mut(|m| m.request_focus(field.with("edit")));
}
let query = widgets::search_field(ui, field, tl!("Search")).to_lowercase();
let refilter = filters(ui, st, &t, look.document.is_some(), look.samples.then_some(look.row));
let added = added_families(db, st, look);
// On the system clock, the list is drawn again when a family's 30 days end.
if let (Some(at), None) = (added.as_ref().and_then(|a| a.recheck_at), look.clock)
&& let Ok(wait) = at.duration_since(added_clock(look))
{
ui.ctx().request_repaint_after(wait);
}
let enter = ui.input(|i| i.key_pressed(egui::Key::Enter));
// The current family by its own name (a document may name it in Japanese: ใใฉใฎใ่งใดใทใใฏ),
// as it was before any preview.
let current = db.canonical(&st.current, "").0;
let current = current.as_str();
// The generation is read first: fonts that load meanwhile make the next frame list them.
let key: RowsKey = (
query.clone(),
st.script,
st.class,
st.variable_only,
st.favorites_only,
look.english_names,
st.expanded.clone(),
look.favorites.to_vec(),
look.recent.to_vec(),
db.generation(),
st.similar.clone(),
(look.document.filter(|_| st.document_only).map(|d| d.key()), added.clone()),
);
if st.rows_key.as_ref() != Some(&key) {
let used = used_families(db, st, look);
let mut families: Vec<_> =
db.menu_family_list().iter().filter(|f| listed(db, f, &query, st, look, used.as_deref(), added.as_deref())).cloned().collect();
st.none_used = families.is_empty() && used.is_some_and(|used| !db.menu_family_list().iter().any(|f| used.contains(&f.to_lowercase())));
// Recently Added lists every family it finds once, newest first: the recent ones among
// them, without the Recent Fonts section.
let recent = match &added {
Some(added) => {
added.order(&mut families);
&[]
}
None => look.recent,
};
// Show Similar Fonts orders every listed family by likeness, the recent ones among them.
let rows = match &st.similar {
Some(base) => grouped_rows(&db.similar_families(base, &families), &[], &st.expanded, |f| db.styles(f)),
None => grouped_rows(&families, recent, &st.expanded, |f| db.styles(f)),
};
// Filter changes (including un-starring a favourite) may remove rows; expanding
// another family or a background font scan can insert rows before the highlight.
// Never preview an unrelated family because the old numeric index now points to it.
st.highlight = remap_highlight(&st.rows, st.highlight, &rows);
st.rows = rows.into();
st.rows_key = Some(key);
}
let rows = st.rows.clone();
if query != st.query || refilter {
st.query = query.clone();
st.highlight = None;
}
if let Some(base) = &st.similar_moved {
st.highlight = rows.iter().position(|r| matches!(r, Row::Family(f) if f.eq_ignore_ascii_case(base)));
}
if st.highlight.is_none() && query.is_empty() {
st.highlight = rows.iter().position(|r| matches!(r, Row::Family(f) if f.eq_ignore_ascii_case(current)));
}
let mut keyed = false;
if (up || down) && !rows.is_empty() {
let selectable = |(_, r): &(usize, &Row)| r.family().is_some();
let next = match (st.highlight, down) {
(None, _) => rows.iter().enumerate().find(selectable),
(Some(i), true) => rows.iter().enumerate().skip(i.saturating_add(1)).find(selectable),
(Some(i), false) => rows.iter().enumerate().take(i).rev().find(selectable),
};
st.highlight = next.map(|(i, _)| i).or(st.highlight);
keyed = true;
}
// โ opens the highlighted family's styles, โ closes them (from the family or one of them).
if let Some(family) = st.highlight.and_then(|i| rows.get(i)).and_then(Row::family).filter(|_| left || right) {
let f = family.to_lowercase();
let open = st.expanded.contains(&f);
if right && !open && db.styles(family).len() > 1 {
st.expanded.push(f);
} else if left && open {
st.expanded.retain(|e| *e != f);
st.highlight = rows.iter().position(|r| *r == Row::Family(family.to_string()));
}
keyed = true;
}
st.header = ui.min_rect().height();
if rows.is_empty() {
// The Document filter on a document whose type uses none of the listed families (none at
// all, or only missing ones) says so, whatever the other filters.
widgets::dim_label(ui, if st.none_used { tl!("This document uses no available fonts") } else { tl!("No matching fonts") });
}
ui.spacing_mut().item_spacing.y = 0.0;
// The scroll bar beside the rows, not over them: a floating one covered the stars at their
// right ends, so a favourite was hard to click (#2277).
ui.spacing_mut().scroll = widgets::list_scroll_style();
Listed { db, st, rows, current: current.to_string(), enter, keyed, opening }
}
/// One of the filters' chips.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Chip {
/// A script ([`SCRIPTS`]).
Script(usize),
Favorites,
/// The families the current document's type uses.
Document,
/// The families installed in the last 30 days.
Added,
AnyKind,
Class(FontClass),
/// The Font Technology label, which filters nothing.
Technology,
Variable,
}
/// The filters, row by row, the same wherever the menu opens: the scripts; favorites, the
/// document's fonts, any kind and the first three kinds; the other kinds; font technology; the
/// recently added fonts.
fn filter_rows() -> [Vec<Chip>; 5] {
let (first, rest) = FontClass::ALL.split_at(3);
[
(0..SCRIPTS).map(Chip::Script).collect(),
[Chip::Favorites, Chip::Document, Chip::AnyKind].into_iter().chain(first.iter().copied().map(Chip::Class)).collect(),
rest.iter().copied().map(Chip::Class).collect(),
vec![Chip::Technology, Chip::Variable],
vec![Chip::Added],
]
}
/// The filters on their rows ([`filter_rows`]): a row wraps only where the menu is narrower than
/// it. The Document filter is unavailable without a `document`. With `row` (the rows' height while
/// samples are shown), a row under them offers Font Preview Size's values, the one of that height
/// selected; a click on one asks for it ([`apply_preview_size`]). Records in `st` the width of the
/// widest row unwrapped, the size row included; whether a filter changed.
fn filters(ui: &mut Ui, st: &mut MenuState, t: &Tokens, document: bool, row: Option<f32>) -> bool {
let mut refilter = false;
let mut widest = 0.0_f32;
for row in filter_rows() {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = vec2(4.0, 2.0);
let gap = ui.spacing().item_spacing.x;
let text = |s: &str| egui::RichText::new(s).size(FILTER_SIZE);
let mut width = -gap;
for chip in row {
let resp = match chip {
Chip::Script(i) => ui.selectable_label(st.script == i, text(script_label(i))),
Chip::Favorites => ui.selectable_label(st.favorites_only, text("โ
")).on_hover_text(tl!("Favorites")),
Chip::Document => {
let on = st.document_only && document;
let tip = tl!("Fonts used in this document");
let resp = ui
.add_enabled(document, egui::Button::selectable(on, text(tl!("Document"))))
.on_hover_text(tip)
.on_disabled_hover_text(tip);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, document, on, tip));
resp
}
Chip::Added => {
ui.selectable_label(st.added_only, text(tl!("Recently Added"))).on_hover_text(tl!("Fonts added in the last 30 days"))
}
Chip::AnyKind => ui.selectable_label(st.class.is_none(), text(tl!("Any Kind"))),
Chip::Class(c) => ui.selectable_label(st.class == Some(c), text(class_label(c))),
Chip::Technology => ui.label(text(tl!("Font Technology")).color(t.text_dim)),
Chip::Variable => ui.selectable_label(st.variable_only, text(variable_label())),
};
width += resp.rect.width() + gap;
if !resp.clicked() {
continue;
}
match chip {
Chip::Script(i) => st.script = i,
Chip::Favorites => st.favorites_only = !st.favorites_only,
Chip::Document => st.document_only = !st.document_only,
Chip::Added => st.added_only = !st.added_only,
Chip::AnyKind => st.class = None,
Chip::Class(c) => st.class = if st.class == Some(c) { None } else { Some(c) },
Chip::Technology => continue,
Chip::Variable => st.variable_only = !st.variable_only,
}
refilter = true;
}
widest = widest.max(width);
});
}
if let Some(row) = row {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = vec2(4.0, 2.0);
let gap = ui.spacing().item_spacing.x;
let text = |s: &str| egui::RichText::new(s).size(FILTER_SIZE);
let mut width = ui.label(text(tl!("Font Preview Size")).color(t.text_dim)).rect.width();
// Screen readers find the three sizes in a radio group named after the row.
let group = egui::UiBuilder::new().accessibility_role(egui::accesskit::Role::RadioGroup).accessibility_label(tl!("Font Preview Size"));
ui.scope_builder(group, |ui| {
for (size, height) in PREVIEW_SIZES {
let on = (height - row).abs() < 0.5;
let label = preview_size_label(size);
let resp = ui.selectable_label(on, text(label));
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::RadioButton, true, on, label));
width += gap + resp.rect.width();
if resp.clicked() {
ui.data_mut(|d| d.insert_temp(preview_size_id(), size));
// A focused button is activated by Enter; the list must not read it as a pick.
ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
}
}
});
widest = widest.max(width);
});
}
st.filters = widest;
// While Show Similar Fonts orders the list: the family it orders by, which clears it. Its
// width is left out of the widest row: a long name wraps instead of widening the menu.
if let Some(base) = st.similar.clone() {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = vec2(4.0, 2.0);
let chip = egui::RichText::new(format!("โ {base} โ")).size(FILTER_SIZE);
if ui.selectable_label(true, chip).on_hover_text(tl!("Clear Similar Fonts")).clicked() {
st.similar_moved = st.similar.take();
refilter = true;
}
});
}
refilter
}
/// The width the open menu takes at least: the widest row of filters last frame, and the widest
/// name up to [`NAME_MAX`] with room for its sample, the Show Similar Fonts button, the star and the
/// scroll bar beside it, within [`WINDOW_SHARE`] of the window. The menu is never narrower than its
/// field.
fn menu_width(ui: &Ui, st: &MenuState, look: MenuLook) -> f32 {
let sample = if look.samples { SAMPLE_W } else { 0.0 };
let names = name_width(ui, vectorcraft_text::FontDb::global(), look.english_names, &st.rows).min(NAME_MAX);
let row = DISCLOSURE + names + NAME_GAP + sample + SIMILAR + STAR + 2.0 + widgets::list_scroll_style().allocated_width();
// Rounding in the chips' widths must not wrap the widest row.
let filters = if st.filters > 0.0 { st.filters.ceil() + 1.0 } else { 0.0 };
filters.max(row).min(ui.ctx().content_rect().width() * WINDOW_SHARE)
}
/// The width of the widest name the menu lists after the disclosure triangle: the family names of
/// `db` as Show Font Names in English shows them, and the style names among `rows` after their
/// indent.
fn name_width(ui: &Ui, db: &vectorcraft_text::FontDb, english: bool, rows: &[Row]) -> f32 {
let font = egui::FontId::proportional(NAME_SIZE);
let measure = |name: &str| ui.painter().layout_no_wrap(name.to_string(), font.clone(), Color32::WHITE).size().x;
let styles = rows.iter().filter_map(Row::style).map(|s| STYLE_INDENT + measure(s)).fold(0.0, f32::max);
family_name_width(ui, db, english).max(styles)
}
/// What the widest family name was measured for: the font database and its generation, Show Font
/// Names in English, and the number of fonts the interface draws with, which grows as faces for
/// other scripts are added to it.
fn family_name_key(ui: &Ui, db: &vectorcraft_text::FontDb, english: bool) -> (u64, u64, bool, usize) {
let faces = ui.ctx().fonts(|f| f.definitions().font_data.len());
(db.id(), db.generation(), english, faces)
}
/// The id under which the widest family name's width is kept with its [`family_name_key`].
fn family_name_id() -> egui::Id {
egui::Id::unique("font-menu-name-width")
}
/// The width of the widest family name the menu lists from `db`, labeled as Show Font Names in
/// English (`english`) labels it; worked out again only when its [`family_name_key`] changes.
fn family_name_width(ui: &Ui, db: &vectorcraft_text::FontDb, english: bool) -> f32 {
let key = family_name_key(ui, db, english);
if let Some((k, w)) = ui.data(|d| d.get_temp::<((u64, u64, bool, usize), f32)>(family_name_id()))
&& k == key
{
return w;
}
let w = widest_label(ui, db.menu_family_list().iter().map(|f| db.family_display_name(f, english)).collect());
ui.data_mut(|d| d.insert_temp(family_name_id(), (key, w)));
w
}
/// The width of the widest of `labels` laid out at the names' size. Each label's characters'
/// advances are added up, and the labels are laid out widest sum first, until no sum left could
/// reach the widest layout, allowing [`SHAPING`] for shaping.
fn widest_label(ui: &Ui, labels: Vec<String>) -> f32 {
let font = egui::FontId::proportional(NAME_SIZE);
let mut sums: Vec<(f32, String)> =
ui.ctx().fonts_mut(|f| labels.into_iter().map(|l| (l.chars().map(|c| f.glyph_width(&font, c)).sum(), l)).collect());
sums.sort_by(|a, b| b.0.total_cmp(&a.0));
let mut w = 0.0_f32;
for (sum, label) in sums {
if sum * (1.0 + SHAPING) + 1.0 <= w {
break;
}
w = w.max(ui.painter().layout_no_wrap(label, font.clone(), Color32::WHITE).size().x);
}
w
}
/// Where a row `width` wide puts its sample, after the name column: room for the widest name
/// (`widest`) and a gap, as far as that leaves [`SAMPLE_W`], the Show Similar Fonts button and the
/// star for the sample or [`NAME_SHARE`] of the row for the name. Without samples the name runs to
/// the Show Similar Fonts button.
fn name_column(width: f32, widest: f32, samples: bool) -> f32 {
if !samples {
return width - SIMILAR - STAR;
}
(DISCLOSURE + widest + NAME_GAP).min((width - SIMILAR - STAR - 2.0 - SAMPLE_W).max(width * NAME_SHARE))
}
/// The disclosure triangle of a family with several styles; whether the pointer is on it.
fn disclosure(p: &egui::Painter, resp: &egui::Response, tri: egui::Rect, open: bool, t: &Tokens) -> bool {
p.text(tri.center(), egui::Align2::CENTER_CENTER, if open { "โพ" } else { "โธ" }, egui::FontId::proportional(11.0), t.text_dim);
resp.interact_pointer_pos().is_some_and(|pos| tri.contains(pos))
}
/// The star at the end of a family's row, shown when it is a favourite, hovered or highlighted; whether the pointer is
/// on it.
fn star_cell(p: &egui::Painter, resp: &egui::Response, star: egui::Rect, (on, highlighted): (bool, bool), t: &Tokens) -> bool {
let hot = resp.hover_pos().is_some_and(|pos| star.contains(pos));
if on || hot || highlighted {
let color = if on { t.accent } else { t.text_dim };
p.text(star.center(), egui::Align2::CENTER_CENTER, if on { "โ
" } else { "โ" }, egui::FontId::proportional(12.0), color);
}
resp.interact_pointer_pos().is_some_and(|pos| star.contains(pos))
}
/// The Show Similar Fonts button before a family's star, shown when the row is hovered or
/// highlighted, or is the family the list is ordered by; whether the pointer hovers it, and whether
/// it presses it.
fn similar_cell(p: &egui::Painter, resp: &egui::Response, cell: egui::Rect, (on, highlighted): (bool, bool), t: &Tokens) -> (bool, bool) {
let hot = resp.hover_pos().is_some_and(|pos| cell.contains(pos));
if on || highlighted || resp.hovered() {
let color = if on || hot { t.accent } else { t.text_dim };
p.text(cell.center(), egui::Align2::CENTER_CENTER, "โ", egui::FontId::proportional(13.0), color);
}
(hot, resp.interact_pointer_pos().is_some_and(|pos| cell.contains(pos)))
}
/// A heading row (Recent Fonts, Fonts), which cannot preview or apply a font.
fn heading_row(ui: &mut Ui, row_h: f32, label: &str, t: &Tokens) {
let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), row_h), Sense::hover());
// Existing translated menu labels; the heading cannot preview or apply a font.
let label = match label {
"Recent Fonts" => tl!("Recent Fonts"),
_ => tl!("Fonts"),
};
ui.painter_at(rect).text(rect.left_center() + vec2(4.0, 0.0), egui::Align2::LEFT_CENTER, label, egui::FontId::proportional(11.0), t.text_dim);
}
/// The row the list scrolls to this frame, and how it aligns it. Opening, the list shows the recent
/// section even when the current family is farther down the list, else centres the current font;
/// key presses keep the highlight in view. Show Similar Fonts goes to the first row, the family it
/// orders by, and clearing it goes back to that family, highlighted.
fn scroll_target(st: &mut MenuState, rows: &[Row], opening: bool, keyed: bool) -> Option<(usize, Option<egui::Align>)> {
if st.similar_moved.take().is_some() {
return match st.similar {
Some(_) => Some((0, Some(egui::Align::TOP))),
None => st.highlight.map(|i| (i, Some(egui::Align::Center))),
};
}
if opening && matches!(rows.first(), Some(Row::Heading("Recent Fonts"))) {
return Some((0, Some(egui::Align::TOP)));
}
st.highlight.filter(|_| opening || keyed).map(|i| (i, opening.then_some(egui::Align::Center)))
}
/// The Show Similar Fonts button of family `f`'s row `i` before its star (`star`); whether the
/// pointer presses it. A click on it asks for the list ordered by likeness to `f`.
fn similar_button(p: &egui::Painter, resp: &egui::Response, star: egui::Rect, f: &str, (st, i): (&MenuState, usize), t: &Tokens) -> (bool, bool) {
let cell = egui::Rect::from_min_max(egui::pos2(star.min.x - SIMILAR, star.min.y), egui::pos2(star.min.x, star.max.y));
let ordering = st.similar.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(f));
let (hot, pressed) = similar_cell(p, resp, cell, (ordering, st.highlight == Some(i)), t);
if hot {
resp.clone().on_hover_text(tl!("Show Similar Fonts"));
}
(pressed, resp.clicked() && pressed)
}
/// The rows of the open menu in `range`, the ones in view: the menu's one scrolling part, under
/// the search field and filters (#555).
fn rows(ui: &mut Ui, range: std::ops::Range<usize>, h: Listed<'_>, sample: Option<&str>, look: MenuLook) -> Option<FontPick> {
let Listed { db, st, rows, current, enter, keyed, opening } = h;
let current = current.as_str();
let row_h = look.row;
let t = Tokens::get(ui.ctx());
let ink = sample_ink(look.on_white, t.text);
let favorite = |f: &str| look.favorites.iter().any(|x| x.eq_ignore_ascii_case(f));
let widest = name_width(ui, db, look.english_names, &rows);
if rows.is_empty() {
// A filter or a font rescan can remove the row whose font was previewed.
// Do not leave that font applied while the list has no candidate to highlight.
return st.previewed.take().map(|_| FontPick::EndPreview);
}
// Scrolls the list to row `i`'s place, laid out or not: the rows in view start at
// `range.start`.
let (left, width) = (ui.max_rect().left(), ui.max_rect().width());
let top = ui.max_rect().top() - range.start as f32 * row_h;
let scroll_to = |ui: &Ui, i: usize, align| {
let place = egui::Rect::from_min_size(egui::pos2(left, top + i as f32 * row_h), vec2(width, row_h));
ui.scroll_to_rect_animation(place, align, egui::style::ScrollAnimation::none());
};
// At a new row height (Font Preview Size, set from the menu), the view stays on the rows it
// showed: the scroll offset is in points.
let at = (ui.clip_rect().top() - top) / row_h;
let kept = st.scrolled.filter(|&(h, _)| !opening && (h - row_h).abs() > 0.5).map(|(_, at)| at);
st.scrolled = Some((row_h, kept.unwrap_or(at)));
if let Some((i, align)) = scroll_target(st, &rows, opening, keyed) {
scroll_to(ui, i, align);
} else if let Some(at) = kept {
let place = egui::Rect::from_min_size(egui::pos2(left, top + at * row_h), vec2(width, row_h));
ui.scroll_to_rect_animation(place, Some(egui::Align::TOP), egui::style::ScrollAnimation::none());
}
let mut chosen: Option<(String, Option<String>)> = None;
let mut hovered: Option<usize> = None;
let mut toggled: Option<String> = None;
let mut starred: Option<String> = None;
let mut similar: Option<String> = None;
for i in range {
let Some(row) = rows.get(i) else { continue };
if let Row::Heading(label) = row {
heading_row(ui, row_h, label, &t);
continue;
}
let Some(family) = row.family() else { continue };
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), row_h), Sense::click());
if resp.hovered() && ui.input(|inp| inp.pointer.delta() != egui::Vec2::ZERO || inp.smooth_scroll_delta != egui::Vec2::ZERO) {
hovered = Some(i);
}
let p = ui.painter_at(rect);
if st.highlight == Some(i) {
p.rect_filled(rect, 2.0, t.selection);
} else if matches!(row, Row::Family(f) if f.eq_ignore_ascii_case(current)) {
p.rect_filled(rect, 2.0, t.hover);
}
// The disclosure triangle of a family with several styles.
let tri = egui::Rect::from_min_size(rect.min, vec2(DISCLOSURE, row_h));
let mut on_triangle = false;
if let Row::Family(f) = row
&& db.styles(f).len() > 1
{
let open = st.expanded.iter().any(|e| e.eq_ignore_ascii_case(f));
on_triangle = disclosure(&p, &resp, tri, open, &t);
if resp.clicked() && on_triangle {
toggled = Some(f.to_lowercase());
}
}
// The star at the end of a family's row.
let star = egui::Rect::from_min_max(egui::pos2(rect.max.x - STAR, rect.min.y), rect.max);
let mut on_star = false;
if let Row::Family(f) = row {
let on = favorite(f);
on_star = star_cell(&p, &resp, star, (on, st.highlight == Some(i)), &t);
if resp.clicked() && on_star {
starred = Some(f.clone());
}
}
let mut on_similar = false;
if let Row::Family(f) = row {
let (pressed, clicked) = similar_button(&p, &resp, star, f, (&*st, i), &t);
on_similar = pressed;
similar = similar.or(clicked.then(|| f.clone()));
}
let indent = if row.style().is_some() { DISCLOSURE + STYLE_INDENT } else { DISCLOSURE };
let name_w = name_column(rect.width(), widest, look.samples);
let name_rect = egui::Rect::from_min_max(rect.min + vec2(indent, 0.0), egui::pos2(rect.min.x + name_w - 4.0, rect.max.y));
// Family rows follow Show Font Names in English; style rows keep the style name (#394).
let name = match row {
Row::Style(_, style) => style.clone(),
Row::Family(f) => db.family_display_name(f, look.english_names),
Row::Heading(_) => continue,
};
ui.painter_at(name_rect).text(name_rect.left_center(), egui::Align2::LEFT_CENTER, name, egui::FontId::proportional(NAME_SIZE), t.text);
// The sample takes the row's whole height.
let sample_rect = egui::Rect::from_min_max(rect.min + vec2(name_w, 0.0), rect.max - vec2(SIMILAR + STAR + 2.0, 0.0));
if look.samples {
// The paper is drawn while the sample is still rendering, and for a family whose sample fails.
let area = sample_area(ui, sample_rect, ink, PAPER_INSET);
if let Some(tex) = sample_texture(ui.ctx(), family, row.style(), sample, area.height()) {
paint_sample(ui, area, &tex, ink);
}
}
if resp.clicked() && !on_triangle && !on_star && !on_similar {
chosen = row.pick();
}
}
if let Some(f) = similar {
// The rows are ordered again next frame, which highlights `f` and scrolls to it.
st.similar_moved = Some(f.clone());
st.similar = Some(f);
return None;
}
if let Some(f) = toggled {
if st.expanded.contains(&f) {
st.expanded.retain(|e| *e != f);
} else {
st.expanded.push(f);
}
// The rows change next frame: what the highlight points at is decided then.
return None;
}
if let Some(i) = hovered {
st.highlight = Some(i);
}
if let Some(f) = starred {
return Some(FontPick::Favorite(f));
}
if enter && chosen.is_none() {
chosen = st.highlight.and_then(|i| rows.get(i)).and_then(Row::pick).or_else(|| rows.iter().find_map(Row::pick));
}
if let Some((f, s)) = chosen {
ui.close();
return Some(FontPick::Chosen(f, s));
}
// The highlighted row is previewed; back on the current family, the preview goes.
let lit = st.highlight.and_then(|i| rows.get(i)).and_then(Row::pick);
match lit {
Some((f, None)) if f == current => st.previewed.take().map(|_| FontPick::EndPreview),
Some(pick) if st.previewed.as_ref() != Some(&pick) => {
st.previewed = Some(pick.clone());
Some(FontPick::Preview(pick.0, pick.1))
}
None => st.previewed.take().map(|_| FontPick::EndPreview),
_ => None,
}
}
// ---------- samples ----------
/// Width of the sample drawn at the end of a Type โ Font menu item.
/// The room a Type โ Font menu item keeps for its sample, after the family's name.
pub(crate) const MENU_SAMPLE_SIZE: egui::Vec2 = vec2(90.0, 16.0);
/// A generic sample of `family` drawn in `slot` (the room a Type โ Font item of the in-window
/// menu bar keeps for it; the native macOS menu shows names only).
/// With `on_white` (Show samples on white), the sample is drawn in dark type on a white slot.
pub(crate) fn menu_item_sample(ui: &Ui, slot: egui::Rect, family: &str, on_white: bool) {
let ink = sample_ink(on_white, Tokens::get(ui.ctx()).text_dim);
let area = sample_area(ui, slot, ink, 0.0);
if let Some(tex) = sample_texture(ui.ctx(), family, None, None, area.height()) {
paint_sample(ui, area, &tex, ink);
}
}
/// A sample of `family` in `style` drawn in `slot`: `text` set in it (a generic sample when it is
/// `None` or the font lacks its characters), filling the slot's height (the Retype panel's rows).
/// With `on_white` (Show samples on white), the sample is drawn in dark type on a white slot.
pub(crate) fn styled_sample(ui: &Ui, slot: egui::Rect, family: &str, style: &str, text: Option<&str>, on_white: bool) {
let ink = sample_ink(on_white, Tokens::get(ui.ctx()).text);
let area = sample_area(ui, slot, ink, PAPER_INSET);
if let Some(tex) = sample_texture(ui.ctx(), family, Some(style), text, area.height()) {
paint_sample(ui, area, &tex, ink);
}
}
/// What a sample is drawn on with Show samples on white.
pub(crate) const SAMPLE_PAPER: Color32 = Color32::WHITE;
/// How a sample is drawn: in `theme` (an interface text color) on the menu's own background, or,
/// with Show samples on white, in the Light brightness's text color on [`SAMPLE_PAPER`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct SampleInk {
pub ink: Color32,
pub paper: Option<Color32>,
}
pub(crate) fn sample_ink(on_white: bool, theme: Color32) -> SampleInk {
if on_white {
SampleInk { ink: Tokens::for_brightness(crate::theme::Brightness::Light).text, paper: Some(SAMPLE_PAPER) }
} else {
SampleInk { ink: theme, paper: None }
}
}
/// Space between the left edge of a sample's paper and its type.
const PAPER_PAD: f32 = 3.0;
/// Space above and below a menu row's paper, where the row's highlight color shows.
pub(crate) const PAPER_INSET: f32 = 2.0;
/// The part of `slot` a sample is drawn in: `slot` itself, or, when `ink` has paper, the paper
/// painted in `slot` with `inset` points left above and below it.
fn sample_area(ui: &Ui, slot: egui::Rect, ink: SampleInk, inset: f32) -> egui::Rect {
let Some(paper) = ink.paper else { return slot };
let area = slot.shrink2(vec2(0.0, inset));
if area.is_positive() {
ui.painter_at(slot).rect_filled(area, 2.0, paper);
}
area
}
/// Draw sample `tex` at the left of `area`, centered vertically, cut at its right; on paper,
/// [`PAPER_PAD`] in from its left edge.
fn paint_sample(ui: &Ui, area: egui::Rect, tex: &egui::TextureHandle, ink: SampleInk) {
let painter = ui.painter_at(area);
let mut start = area.left_center();
if ink.paper.is_some() {
start.x += PAPER_PAD;
}
let size = tex.size_vec2() / ui.ctx().pixels_per_point();
let at = egui::Rect::from_min_size(start - vec2(0.0, size.y * 0.5), size);
let shown = at.intersect(area);
if !shown.is_positive() {
return;
}
let uv = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2((shown.width() / size.x).clamp(0.0, 1.0), 1.0));
painter.image(tex.id(), shown, uv, ink.ink);
}
type Key = (String, String, String, u32);
/// Samples kept before the cache starts over.
const MAX_SAMPLES: usize = 256;
#[derive(Default)]
struct Samples {
ready: HashMap<Key, Option<egui::TextureHandle>>,
#[cfg(not(target_arch = "wasm32"))]
pending: HashMap<Key, std::sync::mpsc::Receiver<Option<egui::ColorImage>>>,
/// The pass the budget is for, and the samples started in it.
pass: u64,
started: usize,
}
impl Samples {
/// Take one sample from this frame's budget ([`SAMPLES_PER_FRAME`]); `false` when it is spent
/// (another frame is asked for, to go on).
fn take_budget(&mut self, ctx: &egui::Context) -> bool {
let pass = ctx.cumulative_pass_nr();
if self.pass != pass {
(self.pass, self.started) = (pass, 0);
}
if self.started >= SAMPLES_PER_FRAME {
ctx.request_repaint();
return false;
}
self.started += 1;
true
}
}
thread_local! {
static SAMPLES: crate::graphics::TexCache<Samples> = crate::graphics::TexCache::default();
}
/// The sample of `family` for a row `height` points high: the selected text set in it (else a
/// generic sample), white with coverage as alpha (tinted when drawn). Rendered off the UI thread
/// on native (the worker asks for a frame when it is done), a few per frame; `None` until it is
/// ready.
fn sample_texture(ctx: &egui::Context, family: &str, style: Option<&str>, sample: Option<&str>, height: f32) -> Option<egui::TextureHandle> {
let px = (height * ctx.pixels_per_point()).round().clamp(8.0, 96.0) as u32;
let key: Key = (family.to_string(), style.unwrap_or("Regular").to_string(), sample.unwrap_or_default().to_string(), px);
let texture = |img: egui::ColorImage| ctx.load_texture(format!("font-sample-{family}-{px}"), img, egui::TextureOptions::LINEAR);
SAMPLES.with(|s| {
let mut s = s.borrow_mut();
if let Some(t) = s.ready.get(&key) {
return t.clone();
}
// Up to ~370 KB of texture each (96 px high, 10:1): keep a few screens' worth.
if s.ready.len() > MAX_SAMPLES {
s.ready.clear();
}
#[cfg(not(target_arch = "wasm32"))]
{
if let Some(rx) = s.pending.get(&key) {
let img = match rx.try_recv() {
Ok(img) => img,
Err(std::sync::mpsc::TryRecvError::Empty) => return None,
Err(std::sync::mpsc::TryRecvError::Disconnected) => None,
};
s.pending.remove(&key);
let tex = img.map(texture);
s.ready.insert(key, tex.clone());
return tex;
}
if s.pending.len() >= MAX_PENDING {
// Finished samples join the ready ones, so samples nobody asks for again (their
// text or size changed) do not keep new ones from starting.
let mut done = vec![];
for (k, rx) in &s.pending {
match rx.try_recv() {
Err(std::sync::mpsc::TryRecvError::Empty) => {}
Ok(img) => done.push((k.clone(), img)),
Err(std::sync::mpsc::TryRecvError::Disconnected) => done.push((k.clone(), None)),
}
}
for (k, img) in done {
s.pending.remove(&k);
let tex = img.map(|img| ctx.load_texture(format!("font-sample-{}-{}", k.0, k.3), img, egui::TextureOptions::LINEAR));
s.ready.insert(k, tex);
}
}
if s.pending.len() >= MAX_PENDING || !s.take_budget(ctx) {
return None;
}
let (tx, rx) = std::sync::mpsc::channel();
let (f, style, text) = (key.0.clone(), key.1.clone(), key.2.clone());
let repaint = ctx.clone();
let spawned = std::thread::Builder::new().name("font-sample".into()).spawn(move || {
// The receiver may be gone (the cache was cleared): nothing to do then.
let _ = tx.send(render_sample(&f, &style, &text, px));
repaint.request_repaint();
});
if spawned.is_ok() {
s.pending.insert(key, rx);
} else {
s.ready.insert(key, None);
}
None
}
// The web has no threads: a few samples per frame, on this one.
#[cfg(target_arch = "wasm32")]
{
if !s.take_budget(ctx) {
return None;
}
let tex = render_sample(&key.0, &key.1, &key.2, px).map(texture);
s.ready.insert(key, tex.clone());
tex
}
})
}
/// Longest sample, in pixels per pixel of height (wider samples are cut).
const MAX_ASPECT: f32 = 10.0;
/// `text` (or [`FALLBACK_SAMPLE`] when the font lacks some of its characters) set in `family`,
/// `px` pixels high: a coverage mask as white with alpha.
fn render_sample(family: &str, style: &str, text: &str, px: u32) -> Option<egui::ColorImage> {
let db = vectorcraft_text::FontDb::global();
let face: Arc<vectorcraft_text::FontFace> = db.face(family, style)?;
let covers = |s: &str| !s.trim().is_empty() && s.chars().filter(|c| !c.is_whitespace()).all(|c| face.covers(c));
let text = if covers(text) { text } else { FALLBACK_SAMPLE };
if !covers(text) && !face.family.eq_ignore_ascii_case(family) {
return None;
}
let (asc, desc) = face.vertical_metrics();
let em = (asc + desc).max(1.0) / face.units_per_em();
// The em box fills the height with a little air.
let size = px as f64 * 0.8 / em;
let style = vectorcraft_doc::CharStyle { font_family: face.family.clone(), font_style: face.style.clone(), size, ..Default::default() };
let t = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::ZERO, text, style);
let layout = vectorcraft_text::layout(db, &t);
let mut path = layout.to_bezpath();
let b = layout.bounds;
let w = (b.width().ceil() as u32 + 2).clamp(1, (px as f32 * MAX_ASPECT) as u32) as usize;
let h = px as usize;
// Baseline where the ascent fits the top (with the air above it).
let baseline = px as f64 * 0.1 + asc / face.units_per_em() * size;
path.apply_affine(vectorcraft_geom::Affine::translate((1.0 - b.x0.min(0.0), baseline)));
let mask = crate::panels::glyphs::rasterize(&path, w, h);
Some(egui::ColorImage::new([w, h], mask.iter().map(|&a| Color32::from_rgba_premultiplied(a, a, a, a)).collect()))
}
#[cfg(test)]
pub(crate) fn render_sample_for_test(family: &str, text: &str, px: u32) -> Option<egui::ColorImage> {
render_sample(family, "Regular", text, px)
}
/// Waits for the samples rendering on this thread's behalf and keeps them, so the next frame
/// draws them. Returns `false` when no sample was rendering.
#[cfg(all(test, not(target_arch = "wasm32")))]
pub(crate) fn wait_for_samples(ctx: &egui::Context) -> bool {
SAMPLES.with(|s| {
let mut s = s.borrow_mut();
let pending: Vec<_> = s.pending.drain().collect();
let waited = !pending.is_empty();
for (key, rx) in pending {
let tex =
rx.recv().ok().flatten().map(|img| ctx.load_texture(format!("font-sample-{}-{}", key.0, key.3), img, egui::TextureOptions::LINEAR));
s.ready.insert(key, tex);
}
waited
})
}
// ---------- applying a pick ----------
/// Is a font previewed on the text (an interaction is open for it)?
const PREVIEWING: &str = "font-previewing";
/// Where the open font menu was drawn last: (pass, its popup's layer).
fn open_id() -> egui::Id {
egui::Id::unique("font-menu-open")
}
/// The id under which a font menu keeps the Font Preview Size its buttons asked for, until
/// [`apply_preview_size`] sets it.
fn preview_size_id() -> egui::Id {
egui::Id::unique("font-menu-preview-size")
}
/// Set the Font Preview Size (Preferences โบ Type) that a font menu's Small, Medium or Large button
/// asked for in the last pass, which sizes the rows of every font menu, and have the host write the
/// preferences file as it does when Preferences is confirmed. Nothing runs when it is already that
/// size; a saved value that is none of the sizes is replaced. The shell calls it at the start of
/// each frame.
pub(crate) fn apply_preview_size(app: &mut VectorcraftApp, ctx: &egui::Context) {
let Some(size) = ctx.data_mut(|d| d.remove_temp::<&'static str>(preview_size_id())) else { return };
if app.session.prefs.font_preview_size == size {
return;
}
match app.run("prefs.set", json!({"key": "fontPreviewSize", "value": size})) {
Ok(_) => {
crate::prefs_dialog::snapshot(app);
app.prefs_changed = true;
}
Err(e) => app.ui.status = e,
}
}
/// Undo a preview whose menu is gone (its panel wasn't drawn: the text was deselected, the panel
/// closed) or is closing (Escape, a press outside the menu), before anything else sees this
/// frame's input: the canvas must not act inside the preview's interaction (a drag would be
/// rolled back with the preview). The shell calls it at the start of each frame.
pub(crate) fn end_stale_preview(app: &mut VectorcraftApp, ctx: &egui::Context) {
if !crate::panels::pstate::<bool>(ctx, PREVIEWING) {
return;
}
let drawn = ctx.data(|d| d.get_temp::<(u64, egui::LayerId)>(open_id()));
// Drawn in the last pass: still open.
let Some((_, layer)) = drawn.filter(|(pass, _)| pass + 1 >= ctx.cumulative_pass_nr()) else {
apply(app, ctx, FontPick::EndPreview);
return;
};
// (`layer_id_at` outside `input`: egui's context lock isn't re-entrant.)
let (escape, press) = ctx.input(|i| (i.key_pressed(egui::Key::Escape), i.pointer.any_pressed().then(|| i.pointer.interact_pos())));
let leaving = escape || press.is_some_and(|at| at.is_none_or(|p| ctx.layer_id_at(p) != Some(layer)));
if leaving {
apply(app, ctx, FontPick::EndPreview);
}
}
/// What the font menus preview on and apply to: the Type tool's selected range while it edits
/// text (the whole text when nothing is selected), else every frame of each selected story.
pub(crate) fn apply(app: &mut VectorcraftApp, ctx: &egui::Context, pick: FontPick) {
let previewing = crate::panels::pstate::<bool>(ctx, PREVIEWING);
match pick {
FontPick::Preview(f, style) => {
if !previewing {
crate::panels::character::end_typing(app);
if let Err(e) = app.begin_interaction("Font") {
app.status(e);
return;
}
crate::panels::set_pstate(ctx, PREVIEWING, true);
}
let (cmd, params) = font_command(app, &f, style.as_deref());
if let Err(e) = app.session.preview(cmd, ¶ms) {
app.ui.status = e.to_string();
}
app.sync_views();
}
FontPick::EndPreview => {
if previewing {
app.session.cancel_interaction().ok();
crate::panels::set_pstate(ctx, PREVIEWING, false);
app.sync_views();
}
}
FontPick::Favorite(f) => toggle_favorite(app, f),
FontPick::Wheel(families) => {
if previewing {
app.session.cancel_interaction().ok();
crate::panels::set_pstate(ctx, PREVIEWING, false);
}
crate::panels::character::end_typing(app);
// Each family is its own step, keeping the style when the family has it, else the
// closest it has.
for f in families {
let (cmd, params) = font_command(app, &f, None);
if app.run(cmd, params).is_err() {
break;
}
}
}
FontPick::Chosen(f, style) => {
if previewing {
app.session.cancel_interaction().ok();
crate::panels::set_pstate(ctx, PREVIEWING, false);
}
crate::panels::character::end_typing(app);
let (cmd, params) = font_command(app, &f, style.as_deref());
app.run(cmd, params).ok();
}
}
}
/// Star `family`, or take its star off (any case).
fn toggle_favorite(app: &mut VectorcraftApp, family: String) {
let favs = &mut app.ui.favorite_fonts;
match favs.iter().position(|x| x.eq_ignore_ascii_case(&family)) {
Some(i) => {
favs.remove(i);
}
None => favs.push(family),
}
}
/// For a font menu that only picks a font and never changes the document (Find Font's Replace
/// With, the Glyphs panel): the family (and style) chosen, if any. A star is toggled here; the
/// highlighted row is not previewed.
pub(crate) fn picked(app: &mut VectorcraftApp, pick: Option<FontPick>) -> Option<(String, Option<String>)> {
match pick? {
FontPick::Chosen(f, style) => Some((f, style)),
FontPick::Favorite(f) => {
toggle_favorite(app, f);
None
}
FontPick::Preview(..) | FontPick::EndPreview | FontPick::Wheel(_) => None,
}
}
/// The command that sets font `family` (and `style`, else the closest the family has) on what the
/// menus act on.
fn font_command(app: &VectorcraftApp, family: &str, style: Option<&str>) -> (&'static str, serde_json::Value) {
let mut p = json!({"font": family});
if let Some(s) = style {
p["style"] = json!(s);
}
// After Select All in threaded text: every frame of the story.
if let Some(first) = crate::panels::character::text_story(app).and_then(|story| story.first().copied()) {
p["id"] = json!(first.0);
p["story"] = json!(true);
return ("text.setRangeStyle", p);
}
match crate::panels::character::text_editing(app) {
Some((id, a, b)) => {
p["id"] = json!(id.0);
if b > a {
p["start"] = json!(a);
p["end"] = json!(b);
}
("text.setRangeStyle", p)
}
None => {
// With the Selection tool: every frame of each selected story.
crate::panels::character::target_selected_stories(app, &mut p);
("text.setStyle", p)
}
}
}
/// The text the menus sample: the Type tool's selected characters, else the first line of the
/// first selected text object, at most a dozen characters.
pub(crate) fn sample_text(app: &VectorcraftApp) -> Option<String> {
let doc = &app.session.active()?.doc;
let text = match crate::panels::character::text_editing(app) {
Some((id, a, b)) if b > a => match &doc.node(id)?.kind {
vectorcraft_doc::NodeKind::Text(t) => t.plain_text().get(a..b).map(str::to_string),
_ => None,
},
_ => app.session.active()?.selection.objects.iter().find_map(|id| match &doc.node(*id)?.kind {
vectorcraft_doc::NodeKind::Text(t) => Some(t.plain_text()),
_ => None,
}),
}?;
let line = text.lines().map(str::trim).find(|l| !l.is_empty())?;
Some(line.chars().take(12).collect())
}
#[cfg(test)]
mod tests;
No file is open. Choose one in the tree, or press Ctrl+O (โO on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.