Unify theme sizing and spacing on Base UI metrics

- Read only mode, accent, accessibility, and native window material from the OS.
- Replace platform palettes, metrics, type ramps, borders, and elevations with unified tokens.
- Align controls, overlays, and data rows with Base UI sizing.
This commit is contained in:
2026-11-16 18:27:44 +07:00
parent 745a01d7af
commit b08516211c
32 changed files with 518 additions and 1761 deletions
+65 -387
View File
@@ -1,224 +1,29 @@
//! 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.
//! One type ladder shared by every platform, scaled by the theme's root font
//! size.
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::SEMIBOLD),
// 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
/// The shared ladder at a 16px root: size, line height, then weight.
const LADDER: [Rung; 11] = [
(24.0, 32.0, FontWeight::SEMIBOLD),
(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),
(18.0, 24.0, FontWeight::SEMIBOLD),
(16.0, 24.0, FontWeight::BOLD),
(14.0, 20.0, FontWeight::BOLD),
(16.0, 24.0, FontWeight::NORMAL),
(15.0, 22.0, FontWeight::NORMAL),
(14.0, 20.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.
/// One rung: size and line height at a 16px root, then the weight.
type Rung = (f32, f32, FontWeight);
/// A role in the platform's type ladder.
/// A role in the unified type ladder.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TextRole {
/// The largest heading — hero panels, greeters, welcome screens.
@@ -253,7 +58,7 @@ pub struct TypeMetrics {
pub weight: FontWeight,
}
/// The platform's type ladder, resolved at the theme's root font size.
/// The unified type ladder, resolved at the theme's root font size.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Typography {
pub large_title: TypeMetrics,
@@ -270,19 +75,12 @@ pub struct Typography {
}
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);
pub(crate) fn resolve(root_font_size: Pixels) -> Self {
let scale = root_font_size / px(16.);
let metrics = |(size, line_height, weight): Rung| TypeMetrics {
size: px(size * scale),
line_height: px(line_height * scale),
size: px(size) * scale,
line_height: px(line_height) * scale,
weight,
};
@@ -298,7 +96,7 @@ impl Typography {
footnote,
caption,
caption2,
] = ladder.map(metrics);
] = LADDER.map(metrics);
Self {
large_title,
@@ -335,7 +133,7 @@ impl Typography {
/// 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`.
/// Sizes, leads, and weights the element as `role`.
fn typeset(self, role: TextRole, cx: &App) -> Self {
let theme = cx.theme();
let metrics = theme.typography.get(role);
@@ -351,181 +149,61 @@ impl<E: Styled> Typeset for E {}
mod tests {
use super::*;
fn typography(source: SourceKind, root_font_size: f32) -> Typography {
Typography::resolve(source, px(root_font_size))
fn typography(root_font_size: f32) -> Typography {
Typography::resolve(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::SEMIBOLD
}
);
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),
fn the_unified_ladder_matches_the_base_contract() {
let typography = typography(16.0);
let expected = [
(TextRole::LargeTitle, 24.0, 32.0, FontWeight::SEMIBOLD),
(TextRole::Title, 20.0, 28.0, FontWeight::SEMIBOLD),
(TextRole::Title2, 18.0, 24.0, FontWeight::SEMIBOLD),
(TextRole::Title3, 16.0, 24.0, FontWeight::BOLD),
(TextRole::Headline, 14.0, 20.0, FontWeight::BOLD),
(TextRole::Body, 16.0, 24.0, FontWeight::NORMAL),
(TextRole::Callout, 14.0, 20.0, FontWeight::NORMAL),
(TextRole::Subheadline, 14.0, 20.0, FontWeight::NORMAL),
(TextRole::Footnote, 13.0, 18.0, FontWeight::NORMAL),
(TextRole::Caption, 12.0, 16.0, FontWeight::NORMAL),
(TextRole::Caption2, 12.0, 16.0, FontWeight::MEDIUM),
];
for (source, base) in native {
let typography = typography(source, base);
assert_eq!(typography.body.size, px(base), "{source:?}");
for (role, size, line_height, weight) in expected {
assert_eq!(
typography.get(role),
TypeMetrics {
size: px(size),
line_height: px(line_height),
weight,
},
"{role:?}"
);
}
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:?}");
}
fn the_ladder_scales_from_a_16px_root() {
let base = typography(16.0);
let raised = typography(20.0);
for role in [
TextRole::LargeTitle,
TextRole::Title,
TextRole::Title2,
TextRole::Title3,
TextRole::Headline,
TextRole::Body,
TextRole::Callout,
TextRole::Subheadline,
TextRole::Footnote,
TextRole::Caption,
TextRole::Caption2,
] {
let base = base.get(role);
let raised = raised.get(role);
assert!((raised.size - base.size * 1.25).abs() < px(0.01));
assert!((raised.line_height - base.line_height * 1.25).abs() < px(0.01));
assert_eq!(raised.weight, base.weight);
}
}
}