Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions thinkterm-i18n/i18n/de-DE.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ settings-font-size-pane-header = Schriftgröße der Pane-Kopfzeile
settings-font-size-pane-header-description = Wird für die Beschriftungen geteilter Panes separat gespeichert.
settings-font-weight = Schriftstärke der Einstellungen
settings-font-weight-description = Steuert die Schriftstärke von Rahmen und Inhalt des Einstellungsfensters.
settings-ui-font = Schriftart der Oberfläche
settings-ui-font-description = Die Schrift für Tabs, Seitenleisten, Einstellungen und die Befehlspalette. Das Terminal behält seine eigene Schrift.
settings-ui-font-system = Systemstandard
settings-workspaces-heading = Remote-Dateien
settings-sftp-idle = Zeitlimit für inaktive Remote-Dateien-Verbindung
settings-sftp-idle-description = Trennt eine ungenutzte SFTP-Sitzung; Verzeichnis- und Vorschaudaten werden sofort freigegeben.
Expand Down
3 changes: 3 additions & 0 deletions thinkterm-i18n/i18n/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ settings-font-size-pane-header = Pane Header Font Size
settings-font-size-pane-header-description = Saved separately for split-pane header labels.
settings-font-weight = Settings UI Font Weight
settings-font-weight-description = Controls the Settings window chrome and content text weight.
settings-ui-font = Interface Font
settings-ui-font-description = The typeface for tabs, sidebars, Settings and the command palette. The terminal keeps its own font.
settings-ui-font-system = System Default
settings-workspaces-heading = Remote Files
settings-sftp-idle = Remote Files Connection Idle Timeout
settings-sftp-idle-description = Disconnects an unused SFTP session; directory and preview data are released immediately.
Expand Down
3 changes: 3 additions & 0 deletions thinkterm-i18n/i18n/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ settings-font-size-pane-header = Taille de police des en-têtes de volet
settings-font-size-pane-header-description = Enregistrée séparément pour les libellés des volets scindés.
settings-font-weight = Graisse de la police des réglages
settings-font-weight-description = Contrôle la graisse de l’habillage et du texte de la fenêtre Réglages.
settings-ui-font = Police de l’interface
settings-ui-font-description = La police des onglets, des barres latérales, des Réglages et de la palette de commandes. Le terminal garde sa propre police.
settings-ui-font-system = Police du système
settings-workspaces-heading = Fichiers distants
settings-sftp-idle = Délai d’inactivité de la connexion aux fichiers distants
settings-sftp-idle-description = Déconnecte une session SFTP inutilisée et libère immédiatement les données de répertoire et d’aperçu.
Expand Down
3 changes: 3 additions & 0 deletions thinkterm-i18n/i18n/ja-JP.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ settings-font-size-pane-header = ペインヘッダーのフォントサイズ
settings-font-size-pane-header-description = 分割ペインのヘッダーラベル用に個別保存します。
settings-font-weight = 設定 UI のフォントウェイト
settings-font-weight-description = 設定ウィンドウの外枠と本文の太さを調整します。
settings-ui-font = インターフェースのフォント
settings-ui-font-description = タブ、サイドバー、設定、コマンドパレットに使う書体です。ターミナルのフォントは変わりません。
settings-ui-font-system = システム標準
settings-workspaces-heading = リモートファイル
settings-sftp-idle = リモートファイル接続のアイドルタイムアウト
settings-sftp-idle-description = 未使用の SFTP セッションを切断し、ディレクトリとプレビューのデータをすぐ解放します。
Expand Down
3 changes: 3 additions & 0 deletions thinkterm-i18n/i18n/zh-CN.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ settings-font-size-pane-header = 窗格标题字号
settings-font-size-pane-header-description = 单独保存,用于分屏窗格标题。
settings-font-weight = 设置界面字重
settings-font-weight-description = 控制设置窗口框架和内容文字的粗细。
settings-ui-font = 界面字体
settings-ui-font-description = 标签页、侧边栏、设置和命令面板使用的字体。终端字体不受影响。
settings-ui-font-system = 系统默认
settings-workspaces-heading = 远程文件
settings-sftp-idle = 远程文件连接空闲超时
settings-sftp-idle-description = 断开未使用的 SFTP 会话,并立即释放目录和预览数据。
Expand Down
151 changes: 146 additions & 5 deletions wezterm-font/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use config::{
FontRasterizerSelection, FontStretch, FontStyle, FontWeight, TextStyle,
};
use rangeset::RangeSet;
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::rc::{Rc, Weak};
Expand Down Expand Up @@ -44,6 +44,36 @@ pub struct ClearShapeCache {}

static FONT_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type LoadedFontId = usize;
/// The family the interface is drawn with instead of the system's, and how
/// many times that has changed: every `FontConfiguration` holds interface
/// fonts it built for the family of the time, and the count is how each
/// finds out that they are stale.
static UI_FONT_FAMILY: Mutex<Option<String>> = Mutex::new(None);
static UI_FONT_GENERATION: ::std::sync::atomic::AtomicUsize =
::std::sync::atomic::AtomicUsize::new(0);

/// Draw the interface -- window titles, tabs, sidebars, the command palette
/// -- with `family`, in every window. `None` goes back to the system's
/// interface font. The terminal's own font is not this one's business.
/// True when that changed the family.
pub fn set_ui_font_family(family: Option<String>) -> bool {
let family = family
.map(|family| family.trim().to_string())
.filter(|family| !family.is_empty());
let mut current = UI_FONT_FAMILY.lock().unwrap();
if *current == family {
return false;
}
*current = family;
UI_FONT_GENERATION.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
true
}

/// What `set_ui_font_family` was last given.
pub fn ui_font_family() -> Option<String> {
UI_FONT_FAMILY.lock().unwrap().clone()
}

pub fn alloc_font_id() -> LoadedFontId {
FONT_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
Expand Down Expand Up @@ -578,6 +608,9 @@ struct FontConfigInner {
char_select_font: RefCell<Option<Rc<LoadedFont>>>,
command_palette_font: RefCell<Option<Rc<LoadedFont>>>,
entity_font_overrides: RefCell<HashMap<(Entity, u64, u16, bool), Rc<LoadedFont>>>,
/// `UI_FONT_GENERATION` as it stood when the interface fonts above were
/// built.
ui_font_generation: Cell<usize>,
fallback_channel: RefCell<Option<Sender<FallbackResolveInfo>>>,
}

Expand All @@ -600,6 +633,9 @@ impl FontConfigInner {
char_select_font: RefCell::new(None),
command_palette_font: RefCell::new(None),
entity_font_overrides: RefCell::new(HashMap::new()),
ui_font_generation: Cell::new(
UI_FONT_GENERATION.load(::std::sync::atomic::Ordering::Relaxed),
),
font_scale: RefCell::new(1.0),
dpi: RefCell::new(dpi),
config: RefCell::new(config.clone()),
Expand Down Expand Up @@ -672,7 +708,24 @@ impl FontConfigInner {
}
}

let mut fonts = vec![if cfg!(target_os = "macos") {
let mut fonts = vec![];

// Their own choice of family goes first, and the system's stays
// behind it for whatever theirs has no glyph for -- or for all of
// it, should theirs have been uninstalled since.
if let Some(family) = ui_font_family() {
fonts.push(weighted(
&family,
if make_bold {
FontWeight::MEDIUM
} else {
FontWeight::REGULAR
},
));
}
let chosen = !fonts.is_empty();

let mut system = if cfg!(target_os = "macos") {
weighted(
".AppleSystemUIFont",
if make_bold {
Expand All @@ -685,7 +738,9 @@ impl FontConfigInner {
weighted("Roboto", FontWeight::BOLD)
} else {
FontAttributes::new("Roboto")
}];
};
system.is_fallback = chosen;
fonts.push(system);

if cfg!(target_os = "macos") {
let mut fallback = weighted(
Expand Down Expand Up @@ -775,8 +830,15 @@ impl FontConfigInner {
};
let font_size = override_font_size.unwrap_or(configured_font_size);

let text_style =
text_style.unwrap_or(config.window_frame.font.as_ref().unwrap_or(&sys_font));
// A family chosen in Settings is the newer word on the interface's
// font, and outranks what the configuration file asks for.
let text_style = if ui_font_family().is_some()
&& matches!(entity, Entity::Title | Entity::CommandPalette)
{
&sys_font
} else {
text_style.unwrap_or(config.window_frame.font.as_ref().unwrap_or(&sys_font))
};
let override_style;
let text_style = if override_weight.is_some() || override_italic {
override_style = {
Expand Down Expand Up @@ -841,7 +903,21 @@ impl FontConfigInner {
Ok(loaded)
}

/// Drop the interface fonts built for a family that has since been
/// changed with `set_ui_font_family`.
fn forget_stale_ui_fonts(&self) {
let generation = UI_FONT_GENERATION.load(::std::sync::atomic::Ordering::Relaxed);
if self.ui_font_generation.replace(generation) != generation {
self.title_font.borrow_mut().take();
self.pane_select_font.borrow_mut().take();
self.char_select_font.borrow_mut().take();
self.command_palette_font.borrow_mut().take();
self.entity_font_overrides.borrow_mut().clear();
}
}

fn title_font(&self, myself: &Rc<Self>) -> anyhow::Result<Rc<LoadedFont>> {
self.forget_stale_ui_fonts();
let mut title_font = self.title_font.borrow_mut();

if let Some(entry) = title_font.as_ref() {
Expand All @@ -856,6 +932,7 @@ impl FontConfigInner {
}

fn command_palette_font(&self, myself: &Rc<Self>) -> anyhow::Result<Rc<LoadedFont>> {
self.forget_stale_ui_fonts();
let mut command_palette_font = self.command_palette_font.borrow_mut();

if let Some(entry) = command_palette_font.as_ref() {
Expand All @@ -871,6 +948,7 @@ impl FontConfigInner {
}

fn char_select_font(&self, myself: &Rc<Self>) -> anyhow::Result<Rc<LoadedFont>> {
self.forget_stale_ui_fonts();
let mut char_select_font = self.char_select_font.borrow_mut();

if let Some(entry) = char_select_font.as_ref() {
Expand All @@ -885,6 +963,7 @@ impl FontConfigInner {
}

fn pane_select_font(&self, myself: &Rc<Self>) -> anyhow::Result<Rc<LoadedFont>> {
self.forget_stale_ui_fonts();
let mut pane_select_font = self.pane_select_font.borrow_mut();

if let Some(entry) = pane_select_font.as_ref() {
Expand All @@ -906,6 +985,7 @@ impl FontConfigInner {
font_weight: Option<u16>,
italic: bool,
) -> anyhow::Result<Rc<LoadedFont>> {
self.forget_stale_ui_fonts();
let font_size = font_size.clamp(6.0, 72.0);
let font_weight = font_weight.unwrap_or(0);
let key = (entity, font_size.to_bits(), font_weight, italic);
Expand Down Expand Up @@ -1441,6 +1521,30 @@ impl FontConfiguration {
self.inner.locator.enumerate_all_fonts()
}

/// Every family there is to choose from -- the system's, those in the
/// configured font directories and the built-in ones -- by name, in
/// alphabetical order. Families the system keeps for itself, whose names
/// start with a dot, are left out.
pub fn list_font_families(&self) -> Vec<String> {
let mut families = self
.inner
.locator
.enumerate_family_names()
.unwrap_or_else(|err| {
log::warn!("listing the system's font families: {err:#}");
vec![]
});
families.extend(
self.list_fonts_in_font_dirs()
.into_iter()
.map(|font| font.names().family.clone()),
);
families.retain(|family| !family.is_empty() && !family.starts_with('.'));
families.sort_by_cached_key(|family| (family.to_lowercase(), family.clone()));
families.dedup();
families
}

/// Apply the defined font_rules from the user configuration to
/// produce the text style that best matches the supplied input
/// cell attributes.
Expand All @@ -1457,8 +1561,13 @@ impl FontConfiguration {
mod tests {
use super::*;

/// The interface's family is the whole process's, and a test that
/// changes it would drop the fonts another is holding to compare.
static UI_FONT_FAMILY_IN_USE: Mutex<()> = Mutex::new(());

#[test]
fn dpi_change_invalidates_sized_entity_font_cache() -> anyhow::Result<()> {
let _family = UI_FONT_FAMILY_IN_USE.lock().unwrap();
let fonts = FontConfiguration::new(Some(ConfigHandle::default_config()), 144)?;

let retina = fonts.title_font_with_size(15.0)?;
Expand All @@ -1479,6 +1588,38 @@ mod tests {

Ok(())
}

#[test]
fn the_interface_is_drawn_with_the_family_chosen_for_it() -> anyhow::Result<()> {
fn family(font: anyhow::Result<Rc<LoadedFont>>) -> anyhow::Result<String> {
Ok(font?.clone_handles()[0].names().family.clone())
}

let _family = UI_FONT_FAMILY_IN_USE.lock().unwrap();
let fonts = FontConfiguration::new(Some(ConfigHandle::default_config()), 144)?;

// Not Helvetica Neue, which was next in the list while the system's
// own font could not be found.
let system = family(fonts.title_font_with_size(15.0))?;
assert_ne!(system, "Helvetica Neue");
assert_eq!(family(fonts.command_palette_font_with_size(15.0))?, system);

// A window that already holds fonts takes the change up as well.
assert!(set_ui_font_family(Some(" Menlo ".to_string())));
assert!(!set_ui_font_family(Some("Menlo".to_string())));
let chosen = fonts.title_font_with_size(15.0)?;
assert_eq!(family(Ok(Rc::clone(&chosen)))?, "Menlo");
assert_eq!(family(fonts.title_font())?, "Menlo");
assert_eq!(family(fonts.command_palette_font_with_size(15.0))?, "Menlo");
assert!(Rc::ptr_eq(&chosen, &fonts.title_font_with_size(15.0)?));

// An empty name is no choice.
assert!(set_ui_font_family(Some(String::new())));
assert_eq!(ui_font_family(), None);
assert_eq!(family(fonts.title_font_with_size(15.0))?, system);

Ok(())
}
}

#[cfg(test)]
Expand Down
Loading