| 1 | //! Keeps the theme's UI family on the font GPUI actually draws it with. |
| 2 | //! |
| 3 | //! The default UI family is the virtual `.SystemUIFont`, which GPUI maps to a |
| 4 | //! platform name and, when that name is not installed, resolves through its |
| 5 | //! own fallback stack. The fallback works, but it is paid for on every text |
| 6 | //! run of every frame: the failed lookup is cached as an error that is |
| 7 | //! formatted and allocated again each time it is hit, once per entry of the |
| 8 | //! stack that is missing too. On Linux the mapped name is one most desktops |
| 9 | //! do not ship, so this is the common case there. |
| 10 | //! |
| 11 | //! This probe asks GPUI once which family `.SystemUIFont` lands on and, when |
| 12 | //! that is an installed family rather than the system font itself, names it |
| 13 | //! on the theme directly, so every later lookup hits the cache and the text |
| 14 | //! is drawn exactly as before. A family the application or a theme file |
| 15 | //! chose explicitly is used as-is. |
| 16 | //! |
| 17 | //! GPUI panics when neither `.SystemUIFont` nor any family of its fallback |
| 18 | //! stack is installed. On the web the text system starts with no fonts at |
| 19 | //! all and only sees the ones the application adds, usually after `init`, so |
| 20 | //! the probe does nothing while no font is installed: there would be nothing |
| 21 | //! to name anyway. |
| 22 | |
| 23 | use gpui::{App, SharedString, font}; |
| 24 | |
| 25 | use super::mono_font::installed_font_names; |
| 26 | |
| 27 | /// The virtual family GPUI resolves on every platform. |
| 28 | const SYSTEM_UI_FONT: &str = ".SystemUIFont"; |
| 29 | |
| 30 | /// Replaces `.SystemUIFont` on the global theme with the installed family GPUI |
| 31 | /// resolves it to, when that differs. Any other family is left alone. |
| 32 | pub(super) fn resolve_default_font(cx: &mut App) { |
| 33 | if cx.global::<super::Theme>().font_family != SYSTEM_UI_FONT { |
| 34 | return; |
| 35 | } |
| 36 | let installed = installed_font_names(cx); |
| 37 | if installed.is_empty() { |
| 38 | return; |
| 39 | } |
| 40 | let text_system = cx.text_system(); |
| 41 | let resolved = text_system |
| 42 | .get_font_for_id(text_system.resolve_font(&font(SYSTEM_UI_FONT))) |
| 43 | .map(|font| font.family); |
| 44 | let Some(family) = substitute(SYSTEM_UI_FONT, resolved.as_deref(), installed) else { |
| 45 | return; |
| 46 | }; |
| 47 | tracing::info!("UI font {SYSTEM_UI_FONT:?} resolves to {family:?}, naming it on the theme."); |
| 48 | cx.global_mut::<super::Theme>().font_family = family; |
| 49 | } |
| 50 | |
| 51 | /// The family to name instead of `requested`: the one GPUI resolved it to, |
| 52 | /// when that is a different, installed family. |
| 53 | fn substitute( |
| 54 | requested: &str, |
| 55 | resolved: Option<&str>, |
| 56 | installed: &[String], |
| 57 | ) -> Option<SharedString> { |
| 58 | let resolved = resolved.filter(|resolved| *resolved != requested)?; |
| 59 | installed |
| 60 | .iter() |
| 61 | .any(|name| name == resolved) |
| 62 | .then(|| resolved.to_string().into()) |
| 63 | } |
| 64 | |
| 65 | #[cfg(test)] |
| 66 | mod tests { |
| 67 | use super::*; |
| 68 | |
| 69 | fn names(names: &[&str]) -> Vec<String> { |
| 70 | names.iter().map(|name| name.to_string()).collect() |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn keeps_the_system_font_when_it_resolves_to_itself() { |
| 75 | let installed = names(&["Noto Sans", ".SystemUIFont"]); |
| 76 | assert_eq!( |
| 77 | substitute(".SystemUIFont", Some(".SystemUIFont"), &installed), |
| 78 | None |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn names_the_installed_family_the_system_font_fell_back_to() { |
| 84 | let installed = names(&["Noto Sans", "DejaVu Sans"]); |
| 85 | assert_eq!( |
| 86 | substitute(".SystemUIFont", Some("Noto Sans"), &installed), |
| 87 | Some(SharedString::from("Noto Sans")) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn keeps_the_system_font_when_the_resolved_family_is_not_installed() { |
| 93 | let installed = Vec::new(); |
| 94 | assert_eq!( |
| 95 | substitute(".SystemUIFont", Some("Noto Sans"), &installed), |
| 96 | None |
| 97 | ); |
| 98 | assert_eq!(substitute(".SystemUIFont", None, &installed), None); |
| 99 | } |
| 100 | } |