BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / theme / mono_font.rs

mono_font.rs

mono_font.rs 4.4 KiB Download Raw
1//! Keeps the theme's default monospace family on a font the machine has.
2//!
3//! GPUI panics on the first line it lays out in a family it cannot find, and
4//! `Font::fallbacks` does not help there: it is a cascade list for missing
5//! glyphs, consulted only after the family itself has loaded. The theme's
6//! platform default is a bare name, so a machine without that font would crash
7//! on its first code block. This probe swaps the default for an installed
8//! alternative before any text is laid out.
9//!
10//! Only the platform default is probed. A family the application or a theme
11//! file chose explicitly is used as-is: it is usually embedded through
12//! `add_fonts`, and Windows lists families under their localized names, so an
13//! English name for a CJK font would look missing when it is not.
14
15use gpui::{App, SharedString};
16use gpui_base::TypographyTokens;
17use std::sync::OnceLock;
18
19/// Monospace families tried, in order, when the platform default is missing.
20const MONO_FONT_ALTERNATES: &[&str] = if cfg!(target_os = "macos") {
21 &["Monaco", "Courier New"]
22} else if cfg!(target_os = "windows") {
23 &["Cascadia Mono", "Courier New"]
24} else {
25 &["Noto Sans Mono", "Liberation Mono", "Ubuntu Mono"]
26};
27
28/// The virtual family GPUI resolves on every platform; the last resort when
29/// neither the default nor an alternate is installed.
30const SYSTEM_UI_FONT: &str = ".SystemUIFont";
31
32/// The monospace family the theme picks when none is configured.
33pub(super) fn default_mono_font_family() -> SharedString {
34 TypographyTokens::default().mono
35}
36
37/// Replaces the platform-default monospace family on the global theme with one
38/// that is installed. A family chosen explicitly is left alone.
39pub(super) fn resolve_default_mono_font(cx: &mut App) {
40 if cx.global::<super::Theme>().mono_font_family != default_mono_font_family() {
41 return;
42 }
43 let family = installed_default_mono_font_family(cx);
44 cx.global_mut::<super::Theme>().mono_font_family = family;
45}
46
47/// The installed stand-in for the platform default, resolved once per process.
48fn installed_default_mono_font_family(cx: &App) -> SharedString {
49 static RESOLVED: OnceLock<SharedString> = OnceLock::new();
50 RESOLVED
51 .get_or_init(|| {
52 let default = default_mono_font_family();
53 let family = first_installed(&default, MONO_FONT_ALTERNATES, installed_font_names(cx));
54 if family != default {
55 tracing::warn!(
56 "Monospace font {default:?} is not installed, using {family:?} instead."
57 );
58 }
59 family
60 })
61 .clone()
62}
63
64/// The families installed on the machine, listed once per process.
65///
66/// Enumerating fonts costs around a hundred milliseconds on macOS, and the
67/// answer only depends on the system fonts, which do not change while the
68/// process runs.
69pub(super) fn installed_font_names(cx: &App) -> &'static [String] {
70 static NAMES: OnceLock<Vec<String>> = OnceLock::new();
71 NAMES.get_or_init(|| cx.text_system().all_font_names())
72}
73
74/// `default` when it is installed, else the first installed alternate, else
75/// `.SystemUIFont`.
76fn first_installed(default: &str, alternates: &[&str], installed: &[String]) -> SharedString {
77 std::iter::once(default)
78 .chain(alternates.iter().copied())
79 .find(|candidate| installed.iter().any(|name| name == candidate))
80 .unwrap_or(SYSTEM_UI_FONT)
81 .to_string()
82 .into()
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 fn names(names: &[&str]) -> Vec<String> {
90 names.iter().map(|name| name.to_string()).collect()
91 }
92
93 #[test]
94 fn keeps_the_default_when_it_is_installed() {
95 let installed = names(&["Monaco", "Menlo", ".SystemUIFont"]);
96 assert_eq!(
97 first_installed("Menlo", &["Monaco"], &installed),
98 SharedString::from("Menlo")
99 );
100 }
101
102 #[test]
103 fn falls_through_the_alternates_in_order() {
104 let installed = names(&["Courier New", "Monaco", ".SystemUIFont"]);
105 assert_eq!(
106 first_installed("Menlo", &["Monaco", "Courier New"], &installed),
107 SharedString::from("Monaco")
108 );
109 }
110
111 #[test]
112 fn falls_back_to_the_system_font_when_nothing_is_installed() {
113 let installed = names(&["Helvetica", ".SystemUIFont"]);
114 assert_eq!(
115 first_installed("Menlo", &["Monaco"], &installed),
116 SharedString::from(".SystemUIFont")
117 );
118 }
119}