diff --git a/crates/theme/src/lib.rs b/crates/theme/src/lib.rs index a1e0e0d..18e6cd5 100644 --- a/crates/theme/src/lib.rs +++ b/crates/theme/src/lib.rs @@ -13,12 +13,14 @@ mod ramps; mod resolve; mod source; mod system; +mod typography; pub use colors::{ColorTokens, MaterialTokens}; pub use definition::ThemeDefinition; pub use ramps::{AccentShades, Oklch}; pub use resolve::{Ramps, ResolvedTheme}; pub use system::{AccessibilityPreferences, PartialPalette, SourceKind, SystemTheme}; +pub use typography::{TextRole, TypeMetrics, Typeset, Typography}; use gpui::{ App, Global, Hsla, Pixels, SpringConfig, WindowAppearance, WindowBackgroundAppearance, hsla, px, @@ -163,6 +165,8 @@ pub struct Theme { pub root_font_size: Pixels, /// The platform's monospace family, for code surfaces. pub mono_font: &'static str, + /// The platform's type ladder, resolved at the root font size. + pub typography: Typography, pub sizes: SizeTable, pub accessibility: AccessibilityPreferences, /// The material a window should open with, after accessibility. @@ -192,6 +196,7 @@ impl Theme { titlebar_height: resolved.titlebar_height, root_font_size: resolved.root_font_size, mono_font: resolved.mono_font, + typography: resolved.typography, sizes: resolved.sizes, accessibility: resolved.accessibility, window_background: resolved.window_background, diff --git a/crates/theme/src/resolve.rs b/crates/theme/src/resolve.rs index 5cf164c..291df40 100644 --- a/crates/theme/src/resolve.rs +++ b/crates/theme/src/resolve.rs @@ -3,7 +3,7 @@ use crate::ramps::{AccentShades, Oklch, contrast_foreground, derive_shades, mix} use crate::system::SystemTheme; use crate::{ Borders, ColorTokens, CustomTheme, Elevations, MaterialTokens, Motion, Radius, Scrollbar, - SizeTable, Spacing, ThemeVariant, + SizeTable, Spacing, ThemeVariant, Typography, }; use gpui::{WindowBackgroundAppearance, px}; @@ -42,6 +42,8 @@ pub struct ResolvedTheme { pub root_font_size: gpui::Pixels, /// The platform's monospace family, for code surfaces. pub mono_font: &'static str, + /// The platform's type ladder, resolved at the root font size. + pub typography: Typography, pub sizes: SizeTable, pub accessibility: crate::system::AccessibilityPreferences, /// The material a window should open with, after accessibility. @@ -335,6 +337,7 @@ pub(crate) fn resolve( titlebar_height, root_font_size, mono_font: mono_font(), + typography: Typography::resolve(system.source, root_font_size), sizes, accessibility, window_background, @@ -357,7 +360,7 @@ mod tests { use super::*; use crate::system::{AccessibilityPreferences, PartialPalette, SourceKind}; use crate::{Elevation, Shadow}; - use gpui::{Rgba, WindowBackgroundAppearance, rgb}; + use gpui::{FontWeight, Rgba, WindowBackgroundAppearance, rgb}; fn hsla(hex: u32) -> gpui::Hsla { gpui::Hsla::from(rgb(hex)) @@ -963,4 +966,24 @@ mod tests { assert_eq!(resolved.sizes.medium.text_size, px(20.)); assert_eq!(resolved.titlebar_height, px(35.)); } + + #[test] + fn the_platforms_ladder_resolves_at_the_root_font_size_and_rescales() { + // macOS states 13pt, so its HIG sizes paint exactly. + let mut stated = system(ThemeVariant::Light, PartialPalette::default(), None); + stated.root_font_size = Some(px(13.)); + let native = resolve(&stated, None, None, None); + assert_eq!(native.typography.body.size, px(13.)); + assert_eq!(native.typography.headline.weight, FontWeight::BOLD); + + // A custom root rescales the whole ladder off the platform's base. + let custom = CustomTheme { + root_font_size: Some(px(26.)), + ..CustomTheme::default() + }; + let raised = resolve(&stated, None, None, Some(custom)); + assert_eq!(raised.root_font_size, px(26.)); + assert_eq!(raised.typography.body.size, px(26.)); + assert_eq!(raised.typography.large_title.size, px(52.)); + } } diff --git a/crates/theme/src/typography.rs b/crates/theme/src/typography.rs new file mode 100644 index 0000000..5901bc4 --- /dev/null +++ b/crates/theme/src/typography.rs @@ -0,0 +1,531 @@ +//! The platform's type ladder: eleven roles resolved from the OS's own text +//! styles. +//! +//! Each [`SourceKind`] paints its stated ramp — Apple's macOS built-in text +//! styles, WinUI's type ramp, GTK 4's typography classes, Kirigami's heading +//! levels — and every rung scales with the theme's root font size, the way +//! XAML text scaling and GTK's relative font sizes work. Platforms that state +//! no styles paint the neutral ladder. + +use gpui::{App, FontWeight, Pixels, Styled, px}; + +use crate::ActiveTheme; +use crate::system::SourceKind; + +const MACOS_BASE_PX: f32 = 13.0; +const WINDOWS_BASE_PX: f32 = 14.0; +const GNOME_BASE_PX: f32 = 44.0 / 3.0; +const KDE_BASE_PX: f32 = 40.0 / 3.0; +const NEUTRAL_BASE_PX: f32 = 16.0; +const GNOME_LEADING: f32 = 1.33; +const KDE_LEADING: f32 = 1.36; + +/// Apple's macOS built-in text styles (HIG, Typography specifications). +const MACOS: [Rung; 11] = [ + // Large Title + (26.0, 32.0, FontWeight::NORMAL), + // Title 1 + (22.0, 26.0, FontWeight::NORMAL), + // Title 2 + (17.0, 22.0, FontWeight::NORMAL), + // Title 3 + (15.0, 20.0, FontWeight::NORMAL), + // Headline + (13.0, 16.0, FontWeight::BOLD), + // Body + (13.0, 16.0, FontWeight::NORMAL), + // Callout + (12.0, 15.0, FontWeight::NORMAL), + // Subheadline + (11.0, 14.0, FontWeight::NORMAL), + // Footnote + (10.0, 13.0, FontWeight::NORMAL), + // Caption 1 + (10.0, 13.0, FontWeight::NORMAL), + // Caption 2 + (10.0, 13.0, FontWeight::MEDIUM), +]; + +/// WinUI's type ramp (Segoe UI Variable, Windows 11). +const WINDOWS: [Rung; 11] = [ + // TitleLarge + (40.0, 52.0, FontWeight::SEMIBOLD), + // Title + (28.0, 36.0, FontWeight::SEMIBOLD), + // Subtitle + (20.0, 28.0, FontWeight::SEMIBOLD), + // BodyLarge + (18.0, 24.0, FontWeight::NORMAL), + // BodyStrong + (14.0, 20.0, FontWeight::SEMIBOLD), + // Body + (14.0, 20.0, FontWeight::NORMAL), + // Body + (14.0, 20.0, FontWeight::NORMAL), + // Caption + (12.0, 16.0, FontWeight::NORMAL), + // Caption + (12.0, 16.0, FontWeight::NORMAL), + // Caption + (12.0, 16.0, FontWeight::NORMAL), + // Caption + (12.0, 16.0, FontWeight::NORMAL), +]; + +/// GTK 4's typography classes (Adwaita, "General Typography"), stated as +/// percentages of the default font. +const GNOME: [Rung; 11] = [ + // large-title: 240%, light + ( + GNOME_BASE_PX * 2.4, + GNOME_BASE_PX * 2.4 * GNOME_LEADING, + FontWeight::LIGHT, + ), + // title-1: 200% + ( + GNOME_BASE_PX * 2.0, + GNOME_BASE_PX * 2.0 * GNOME_LEADING, + FontWeight::EXTRA_BOLD, + ), + // title-2: 150% + ( + GNOME_BASE_PX * 1.5, + GNOME_BASE_PX * 1.5 * GNOME_LEADING, + FontWeight::EXTRA_BOLD, + ), + // title-3: 150% + ( + GNOME_BASE_PX * 1.5, + GNOME_BASE_PX * 1.5 * GNOME_LEADING, + FontWeight::BOLD, + ), + // heading: 110% + ( + GNOME_BASE_PX * 1.1, + GNOME_BASE_PX * 1.1 * GNOME_LEADING, + FontWeight::BOLD, + ), + // body: the default font; the HIG's body is control-label size, so the + // theme's `.body` class adds a further 10% only where apps ask for it. + ( + GNOME_BASE_PX, + GNOME_BASE_PX * GNOME_LEADING, + FontWeight::NORMAL, + ), + // body + ( + GNOME_BASE_PX, + GNOME_BASE_PX * GNOME_LEADING, + FontWeight::NORMAL, + ), + // caption: 90% + ( + GNOME_BASE_PX * 0.9, + GNOME_BASE_PX * 0.9 * GNOME_LEADING, + FontWeight::NORMAL, + ), + // caption + ( + GNOME_BASE_PX * 0.9, + GNOME_BASE_PX * 0.9 * GNOME_LEADING, + FontWeight::NORMAL, + ), + // caption + ( + GNOME_BASE_PX * 0.9, + GNOME_BASE_PX * 0.9 * GNOME_LEADING, + FontWeight::NORMAL, + ), + // caption-heading: 90% + ( + GNOME_BASE_PX * 0.9, + GNOME_BASE_PX * 0.9 * GNOME_LEADING, + FontWeight::BOLD, + ), +]; + +/// Kirigami's heading levels and Plasma's small font (Breeze). +const KDE: [Rung; 11] = [ + // Heading level 1: 1.35×, DemiBold + ( + KDE_BASE_PX * 1.35, + KDE_BASE_PX * 1.35 * KDE_LEADING, + FontWeight::SEMIBOLD, + ), + // level 2: 1.20× + ( + KDE_BASE_PX * 1.2, + KDE_BASE_PX * 1.2 * KDE_LEADING, + FontWeight::SEMIBOLD, + ), + // level 3: 1.15× + ( + KDE_BASE_PX * 1.15, + KDE_BASE_PX * 1.15 * KDE_LEADING, + FontWeight::SEMIBOLD, + ), + // level 4: 1.10× + ( + KDE_BASE_PX * 1.1, + KDE_BASE_PX * 1.1 * KDE_LEADING, + FontWeight::SEMIBOLD, + ), + // level 5: DemiBold at the default size + (KDE_BASE_PX, KDE_BASE_PX * KDE_LEADING, FontWeight::SEMIBOLD), + // the default font + (KDE_BASE_PX, KDE_BASE_PX * KDE_LEADING, FontWeight::NORMAL), + // the default font + (KDE_BASE_PX, KDE_BASE_PX * KDE_LEADING, FontWeight::NORMAL), + // Plasma's small font (8pt at the 10pt default) + ( + KDE_BASE_PX * 0.8, + KDE_BASE_PX * 0.8 * KDE_LEADING, + FontWeight::NORMAL, + ), + ( + KDE_BASE_PX * 0.8, + KDE_BASE_PX * 0.8 * KDE_LEADING, + FontWeight::NORMAL, + ), + ( + KDE_BASE_PX * 0.8, + KDE_BASE_PX * 0.8 * KDE_LEADING, + FontWeight::NORMAL, + ), + // DemiBold, the platform's only stated emphasis weight + ( + KDE_BASE_PX * 0.8, + KDE_BASE_PX * 0.8 * KDE_LEADING, + FontWeight::SEMIBOLD, + ), +]; + +/// The fallback ladder for platforms that state no text styles. +const NEUTRAL: [Rung; 11] = [ + (32.0, 40.0, FontWeight::NORMAL), + (24.0, 32.0, FontWeight::NORMAL), + (20.0, 26.0, FontWeight::NORMAL), + (18.0, 24.0, FontWeight::NORMAL), + (16.0, 24.0, FontWeight::SEMIBOLD), + (16.0, 24.0, FontWeight::NORMAL), + (15.0, 22.0, FontWeight::NORMAL), + (14.0, 20.0, FontWeight::NORMAL), + (13.0, 18.0, FontWeight::NORMAL), + (12.0, 16.0, FontWeight::NORMAL), + (12.0, 16.0, FontWeight::MEDIUM), +]; + +/// One rung: size and line height at the table's base size, then the weight. +type Rung = (f32, f32, FontWeight); + +/// A role in the platform's type ladder. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum TextRole { + /// The largest heading — hero panels, greeters, welcome screens. + LargeTitle, + /// Window and page titles. + Title, + /// Section titles. + Title2, + /// Sub-section and dialog headings. + Title3, + /// Body-sized emphasis — list row titles, group labels. + Headline, + /// Control labels and reading text. + Body, + /// Slightly smaller body text. + Callout, + /// Secondary lines under a body row. + Subheadline, + /// Hints and auxiliary notes. + Footnote, + /// Timestamps, badges, and dense metadata. + Caption, + /// Emphasized captions. + Caption2, +} + +/// One role as it paints: size, line height, and weight. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct TypeMetrics { + pub size: Pixels, + pub line_height: Pixels, + pub weight: FontWeight, +} + +/// The platform's type ladder, resolved at the theme's root font size. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Typography { + pub large_title: TypeMetrics, + pub title: TypeMetrics, + pub title2: TypeMetrics, + pub title3: TypeMetrics, + pub headline: TypeMetrics, + pub body: TypeMetrics, + pub callout: TypeMetrics, + pub subheadline: TypeMetrics, + pub footnote: TypeMetrics, + pub caption: TypeMetrics, + pub caption2: TypeMetrics, +} + +impl Typography { + pub(crate) fn resolve(source: SourceKind, root_font_size: Pixels) -> Self { + let (ladder, base_px) = match source { + SourceKind::Macos => (MACOS, MACOS_BASE_PX), + SourceKind::Windows => (WINDOWS, WINDOWS_BASE_PX), + SourceKind::Gnome => (GNOME, GNOME_BASE_PX), + SourceKind::Kde => (KDE, KDE_BASE_PX), + SourceKind::Fallback | SourceKind::Omarchy => (NEUTRAL, NEUTRAL_BASE_PX), + }; + let scale = root_font_size / px(base_px); + + let metrics = |(size, line_height, weight): Rung| TypeMetrics { + size: px(size * scale), + line_height: px(line_height * scale), + weight, + }; + + let [ + large_title, + title, + title2, + title3, + headline, + body, + callout, + subheadline, + footnote, + caption, + caption2, + ] = ladder.map(metrics); + + Self { + large_title, + title, + title2, + title3, + headline, + body, + callout, + subheadline, + footnote, + caption, + caption2, + } + } + + /// The metrics for `role`. + pub fn get(&self, role: TextRole) -> TypeMetrics { + match role { + TextRole::LargeTitle => self.large_title, + TextRole::Title => self.title, + TextRole::Title2 => self.title2, + TextRole::Title3 => self.title3, + TextRole::Headline => self.headline, + TextRole::Body => self.body, + TextRole::Callout => self.callout, + TextRole::Subheadline => self.subheadline, + TextRole::Footnote => self.footnote, + TextRole::Caption => self.caption, + TextRole::Caption2 => self.caption2, + } + } +} + +/// Paints a [`TextRole`] from the resolved theme, on anything styled. +pub trait Typeset: Styled + Sized { + /// Sizes, leads, and weights the element as the platform's `role`. + fn typeset(self, role: TextRole, cx: &App) -> Self { + let theme = cx.theme(); + let metrics = theme.typography.get(role); + self.text_size(metrics.size) + .line_height(metrics.line_height) + .font_weight(metrics.weight) + } +} + +impl Typeset for E {} + +#[cfg(test)] +mod tests { + use super::*; + + fn typography(source: SourceKind, root_font_size: f32) -> Typography { + Typography::resolve(source, px(root_font_size)) + } + + #[test] + fn the_platforms_paint_their_stated_styles() { + let macos = typography(SourceKind::Macos, 13.0); + assert_eq!( + macos.body, + TypeMetrics { + size: px(13.0), + line_height: px(16.0), + weight: FontWeight::NORMAL + } + ); + assert_eq!( + macos.headline, + TypeMetrics { + size: px(13.0), + line_height: px(16.0), + weight: FontWeight::BOLD + } + ); + assert_eq!( + macos.caption2, + TypeMetrics { + size: px(10.0), + line_height: px(13.0), + weight: FontWeight::MEDIUM + } + ); + assert_eq!(macos.large_title.size, px(26.0)); + + let windows = typography(SourceKind::Windows, 14.0); + assert_eq!( + windows.body, + TypeMetrics { + size: px(14.0), + line_height: px(20.0), + weight: FontWeight::NORMAL + } + ); + assert_eq!( + windows.headline, + TypeMetrics { + size: px(14.0), + line_height: px(20.0), + weight: FontWeight::SEMIBOLD + } + ); + assert_eq!( + windows.large_title, + TypeMetrics { + size: px(40.0), + line_height: px(52.0), + weight: FontWeight::SEMIBOLD + } + ); + + let gnome_base = 44.0 / 3.0; + let gnome = typography(SourceKind::Gnome, gnome_base); + assert_eq!( + gnome.body, + TypeMetrics { + size: px(gnome_base), + line_height: px(gnome_base * GNOME_LEADING), + weight: FontWeight::NORMAL + } + ); + assert_eq!( + gnome.large_title, + TypeMetrics { + size: px(gnome_base * 2.4), + line_height: px(gnome_base * 2.4 * GNOME_LEADING), + weight: FontWeight::LIGHT + } + ); + assert_eq!( + gnome.caption2, + TypeMetrics { + size: px(gnome_base * 0.9), + line_height: px(gnome_base * 0.9 * GNOME_LEADING), + weight: FontWeight::BOLD + } + ); + + let kde_base = 40.0 / 3.0; + let kde = typography(SourceKind::Kde, kde_base); + assert_eq!( + kde.body, + TypeMetrics { + size: px(kde_base), + line_height: px(kde_base * KDE_LEADING), + weight: FontWeight::NORMAL + } + ); + assert_eq!( + kde.large_title, + TypeMetrics { + size: px(kde_base * 1.35), + line_height: px(kde_base * 1.35 * KDE_LEADING), + weight: FontWeight::SEMIBOLD + } + ); + } + + #[test] + fn a_raised_root_font_size_scales_the_whole_ladder() { + // 26pt over macOS's 13pt base doubles every rung, weights carry over. + let doubled = typography(SourceKind::Macos, 26.0); + assert_eq!( + doubled.body, + TypeMetrics { + size: px(26.0), + line_height: px(32.0), + weight: FontWeight::NORMAL + } + ); + assert_eq!( + doubled.large_title, + TypeMetrics { + size: px(52.0), + line_height: px(64.0), + weight: FontWeight::NORMAL + } + ); + } + + #[test] + fn the_body_rung_equals_the_root_font_size_on_every_platform() { + let native = [ + (SourceKind::Macos, 13.0), + (SourceKind::Windows, 14.0), + (SourceKind::Gnome, 44.0 / 3.0), + (SourceKind::Kde, 40.0 / 3.0), + (SourceKind::Fallback, 16.0), + (SourceKind::Omarchy, 16.0), + ]; + for (source, base) in native { + let typography = typography(source, base); + assert_eq!(typography.body.size, px(base), "{source:?}"); + } + + let raised = typography(SourceKind::Windows, 18.9); + assert!((raised.body.size - px(18.9)).abs() < px(0.01)); + } + + #[test] + fn every_platform_ladder_descends() { + let sources = [ + SourceKind::Fallback, + SourceKind::Omarchy, + SourceKind::Macos, + SourceKind::Windows, + SourceKind::Gnome, + SourceKind::Kde, + ]; + for source in sources { + let typography = typography(source, 16.0); + let ladder = [ + typography.get(TextRole::LargeTitle).size, + typography.get(TextRole::Title).size, + typography.get(TextRole::Title2).size, + typography.get(TextRole::Title3).size, + typography.get(TextRole::Headline).size, + typography.get(TextRole::Body).size, + typography.get(TextRole::Callout).size, + typography.get(TextRole::Subheadline).size, + typography.get(TextRole::Footnote).size, + typography.get(TextRole::Caption).size, + typography.get(TextRole::Caption2).size, + ]; + for pair in ladder.windows(2) { + assert!(pair[0] >= pair[1], "{source:?}: {ladder:?}"); + } + } + } +}