use crate::model::border::ResolvedPadding;
use crate::resolve::ResolutionContext;
use crate::{ColorMode, ResolvedTheme, Theme, ThemeMode};
const PLATFORM_FACTS: &str = include_str!("../../../docs/platform-facts.md");
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NativePreset {
KdeBreeze,
Adwaita,
MacosMonterey,
MacosTahoe,
Windows11,
Material3,
}
impl NativePreset {
const ALL: [NativePreset; 6] = [
NativePreset::KdeBreeze,
NativePreset::Adwaita,
NativePreset::MacosMonterey,
NativePreset::MacosTahoe,
NativePreset::Windows11,
NativePreset::Material3,
];
fn key(self) -> &'static str {
match self {
NativePreset::KdeBreeze => "kde-breeze",
NativePreset::Adwaita => "adwaita",
NativePreset::MacosMonterey => "macos-monterey",
NativePreset::MacosTahoe => "macos-tahoe",
NativePreset::Windows11 => "windows-11",
NativePreset::Material3 => "material-3",
}
}
fn resolutions(self) -> &'static [bool] {
match self.reader_constants() {
Some(_) => &[false, true],
None => &[false],
}
}
fn reader_constants(self) -> Option<ThemeMode> {
match self {
NativePreset::KdeBreeze => {
let mut v = ThemeMode::default();
crate::kde_metrics::populate_widget_sizing(&mut v);
Some(v)
}
NativePreset::Adwaita
| NativePreset::Material3
| NativePreset::Windows11
| NativePreset::MacosMonterey
| NativePreset::MacosTahoe => None,
}
}
}
type Sides = [Option<f32>; 4];
const NONE: Sides = [None; 4];
const fn all(v: f32) -> Sides {
[Some(v); 4]
}
const fn axes(vertical: f32, horizontal: f32) -> Sides {
[
Some(vertical),
Some(horizontal),
Some(vertical),
Some(horizontal),
]
}
const fn trbl(t: f32, r: f32, b: f32, l: f32) -> Sides {
[Some(t), Some(r), Some(b), Some(l)]
}
struct Row {
preset: NativePreset,
widget: &'static str,
padding: Sides,
lines: &'static [usize],
note: &'static str,
extra: &'static [(&'static str, Option<f32>, usize)],
}
impl Row {
const fn with(self, extra: &'static [(&'static str, Option<f32>, usize)]) -> Row {
Row { extra, ..self }
}
}
const fn row(
preset: NativePreset,
widget: &'static str,
padding: Sides,
lines: &'static [usize],
note: &'static str,
) -> Row {
Row {
preset,
widget,
padding,
lines,
note,
extra: &[],
}
}
use NativePreset::{Adwaita, KdeBreeze, MacosMonterey, MacosTahoe, Material3, Windows11};
#[rustfmt::skip]
const ROWS: &[Row] = &[
row(KdeBreeze, "window", NONE, &[1170, 1171], "pointer to §2.20 layout margins"),
row(KdeBreeze, "button", all(6.0), &[1184, 1185], "Button_MarginWidth = 6, both axes"),
row(KdeBreeze, "input", axes(6.0, 7.0), &[1213, 1214], "inside the frame line: LineEdit_FrameWidth 6 + QLineEdit's 2 / 1 − 1"),
row(KdeBreeze, "text_area", all(5.0), &[1714, 1715], "inside the frame line: Frame_FrameWidth 2 + documentMargin 4 − 1"),
row(KdeBreeze, "checkbox", NONE, &[1237, 1238], "(none)"),
row(KdeBreeze, "menu", all(4.0), &[1261, 1262], "MenuItem_MarginWidth = 4; MenuItem_MarginHeight = 4; kept: :1261 gives a 16 px text inset in a menu without icons or checkable items and :1260 a floor of 29, which wait (docs/todo.md)")
.with(&[("row_height", None, 1260)]),
row(KdeBreeze, "tooltip", all(3.0), &[1283, 1284], "ToolTip_FrameWidth = 3"),
row(KdeBreeze, "progress_bar", all(0.0), &[1341, 1342], "0; 0, the contents span the groove"),
row(KdeBreeze, "tab", axes(4.0, 8.0), &[1352, 1353], "TabBar_TabMarginWidth = 8; TabBar_TabMarginHeight = 4")
.with(&[("item_gap", Some(0.0), 1360)]),
row(KdeBreeze, "sidebar", NONE, &[1380, 1381], "❓: the GlobalDrawer states no padding"),
Row {
preset: KdeBreeze,
widget: "toolbar",
padding: all(6.0),
lines: &[1392, 1393],
note: "ToolBar_ItemMargin = 6 on all four sides (qtoolbarlayout.cpp:87-89)",
extra: &[("bar_height", None, 1390), ("item_gap", Some(0.0), 1391)],
},
row(KdeBreeze, "status_bar", trbl(3.0, 14.0, 2.0, 2.0), &[1414, 1415], "QStatusBar: 2 left, 3 top / 2 bottom; 14 right, the 1 + 13px size grip Breeze paints as nothing (not maximized)"),
row(KdeBreeze, "list", axes(1.0, 2.0), &[1433, 1434], "2; 1")
.with(&[("row_height", None, 1431), ("header_height", None, 1432)]),
row(KdeBreeze, "popover", all(12.0), &[1461, 1462], "qqc2-desktop-style Popup: padding 12"),
row(KdeBreeze, "dialog", all(10.0), &[1556, 1557], "Layout_TopLevelMarginWidth = 10"),
row(KdeBreeze, "combo_box", trbl(6.0, 0.0, 6.0, 6.0), &[1618, 1623], "ComboBox_FrameWidth = 6 left, top and bottom; 0 right, to the 20px arrow column")
.with(&[("arrow_area_width", Some(20.0), 1620)]),
row(KdeBreeze, "segmented_control", NONE, &[1640, 1645], "tab bar as proxy, not the platform's value"),
row(KdeBreeze, "card", all(7.0), &[1662, 1663], "Kirigami Card: Units.largeSpacing 8 from the outer edge, 8 − 1 inside the frame line"),
row(KdeBreeze, "expander", NONE, &[1677, 1678], "(none), app-defined")
.with(&[("arrow_gap", Some(9.0), 1684), ("content_indent", Some(20.0), 1685)]),
row(Adwaita, "window", NONE, &[1170, 1171], "pointer to §2.20 layout margins"),
row(Adwaita, "button", axes(5.0, 17.0), &[1184, 1185], "label-only .text-button 17; 5"),
row(Adwaita, "input", axes(0.0, 9.0), &[1213, 1214], "9; 0"),
row(Adwaita, "text_area", all(0.0), &[1714, 1715], "textview: no padding, GtkTextView margins 0"),
row(Adwaita, "checkbox", all(3.0), &[1237, 1238], "check { padding: 3px }"),
row(Adwaita, "menu", axes(0.0, 12.0), &[1261, 1262], "12 ($menu_padding); 0")
.with(&[("row_height", Some(32.0), 1260)]),
row(Adwaita, "tooltip", axes(6.0, 10.0), &[1283, 1284], "10; 6"),
row(Adwaita, "progress_bar", all(0.0), &[1341, 1342], "0; 0, the progress fills the trough"),
row(Adwaita, "tab", axes(3.0, 12.0), &[1352, 1353], "12; 3")
.with(&[("item_gap", Some(0.0), 1360)]),
row(Adwaita, "sidebar", trbl(6.0, 6.0, 4.0, 6.0), &[1380, 1381], ".navigation-sidebar > row margin 0 $menu_margin, 6; padding-top 6, padding-bottom $menu_margin - 2px, 4"),
Row {
preset: Adwaita,
widget: "toolbar",
padding: all(6.0),
lines: &[1392, 1393],
note: ".toolbar { padding: 6px }",
extra: &[("bar_height", None, 1390), ("item_gap", Some(6.0), 1391)],
},
row(Adwaita, "status_bar", axes(6.0, 10.0), &[1414, 1415], "statusbar { padding: 6px 10px }"),
row(Adwaita, "list", all(2.0), &[1433, 1434], "plain list context: 2")
.with(&[("row_height", None, 1431), ("header_height", Some(30.0), 1432)]),
row(Adwaita, "popover", all(8.0), &[1461, 1462], "popover > contents { padding: 8px }"),
row(Adwaita, "dialog", trbl(32.0, 24.0, 24.0, 24.0), &[1556, 1557], "24; 32 top / 24 bottom"),
row(Adwaita, "combo_box", axes(5.0, 10.0), &[1618, 1623], "base button rule, the dropdown's button has a box child: 10; 5")
.with(&[("arrow_area_width", None, 1620)]),
row(Adwaita, "segmented_control", axes(0.0, 8.0), &[1640, 1645], "AdwToggleGroup text toggle 11 − 3 = 8; 0"),
row(Adwaita, "card", NONE, &[1662, 1663], "(none), app-defined"),
row(Adwaita, "expander", axes(8.0, 14.0), &[1677, 1678], "AdwExpanderRow header sums: 2 + 12 = 14; 2 + 6 = 8")
.with(&[("arrow_gap", Some(9.0), 1684), ("content_indent", Some(0.0), 1685)]),
row(MacosMonterey, "window", NONE, &[1170, 1171], "pointer to §2.20 layout margins"),
row(MacosMonterey, "button", trbl(1.0, 8.0, 3.0, 8.0), &[1184, 1185], "~8 (WebKit); 1 top / 3 bottom (API, the macOS 14.8.9 probe)"),
row(MacosMonterey, "input", all(2.0), &[1213, 1214], "2; 2 top / 2 bottom (API, the macOS 14.8.9 probe)"),
row(MacosMonterey, "text_area", NONE, &[1714, 1715], "(none): textContainerInset's default is not stated"),
row(MacosMonterey, "checkbox", NONE, &[1237, 1238], "(none)"),
row(MacosMonterey, "menu", axes(3.0, 12.0), &[1261, 1262], "12; 3 (measured)")
.with(&[("row_height", Some(22.0), 1260)]),
row(MacosMonterey, "tooltip", all(4.0), &[1283, 1284], "4; 4"),
row(MacosMonterey, "progress_bar", all(0.0), &[1341, 1342], "0; 0 (measured, macOS 14.8.9 and 15.7.9)"),
row(MacosMonterey, "tab", axes(4.0, 12.0), &[1352, 1353], "12; 4 (measured)")
.with(&[("item_gap", None, 1360)]),
row(MacosMonterey, "sidebar", NONE, &[1380, 1381], "❓"),
Row {
preset: MacosMonterey,
widget: "toolbar",
padding: axes(0.0, 8.0),
lines: &[1392, 1393],
note: "8 (measured); 0",
extra: &[("bar_height", Some(38.0), 1390), ("item_gap", Some(8.0), 1391)],
},
row(MacosMonterey, "status_bar", NONE, &[1414, 1415], "(none): no window status bar"),
row(MacosMonterey, "list", all(4.0), &[1433, 1434], "4; 4 (measured)")
.with(&[("row_height", Some(24.0), 1431), ("header_height", Some(28.0), 1432)]),
row(MacosMonterey, "popover", NONE, &[1461, 1462], "(none)"),
row(MacosMonterey, "dialog", all(20.0), &[1556, 1557], "~20 (measured)"),
row(MacosMonterey, "combo_box", [Some(1.0), None, Some(3.0), Some(8.0)], &[1618, 1623], "8 left, the arrow on the right; 1 top / 3 bottom (API, the macOS 14.8.9 probe)")
.with(&[("arrow_area_width", None, 1620)]),
row(MacosMonterey, "segmented_control", [Some(3.0), None, Some(3.0), None], &[1640, 1645], "horizontal is a range; ~3 (measured)"),
row(MacosMonterey, "card", all(5.0), &[1662, 1663], "NSBox.contentViewMargins 5 (API)"),
row(MacosMonterey, "expander", NONE, &[1677, 1678], "(none), app-defined")
.with(&[("arrow_gap", None, 1684), ("content_indent", None, 1685)]),
row(MacosTahoe, "window", NONE, &[1170, 1171], "pointer to §2.20 layout margins"),
row(MacosTahoe, "button", axes(4.0, 12.0), &[1184, 1185], "macOS 26: 12; 4 top / 4 bottom (API, the macOS 26.6.2 probe)"),
row(MacosTahoe, "input", all(4.0), &[1213, 1214], "macOS 26: 4; 4 top / 4 bottom (API)"),
row(MacosTahoe, "text_area", NONE, &[1714, 1715], "(none): textContainerInset's default is not stated"),
row(MacosTahoe, "checkbox", NONE, &[1237, 1238], "(none)"),
row(MacosTahoe, "menu", NONE, &[1261, 1262], "macOS 26 ❓")
.with(&[("row_height", None, 1260)]),
row(MacosTahoe, "tooltip", NONE, &[1283, 1284], "macOS 26 ❓"),
row(MacosTahoe, "progress_bar", all(0.0), &[1341, 1342], "macOS 26: 0; 0 (measured, macOS 26.6.2)"),
row(MacosTahoe, "tab", NONE, &[1352, 1353], "macOS 26 ❓")
.with(&[("item_gap", None, 1360)]),
row(MacosTahoe, "sidebar", NONE, &[1380, 1381], "macOS 26 ❓"),
Row {
preset: MacosTahoe,
widget: "toolbar",
padding: NONE,
lines: &[1392, 1393],
note: "macOS 26 ❓",
extra: &[("bar_height", None, 1390), ("item_gap", None, 1391)],
},
row(MacosTahoe, "status_bar", NONE, &[1414, 1415], "(none): no window status bar"),
row(MacosTahoe, "list", NONE, &[1433, 1434], "macOS 26 ❓")
.with(&[("row_height", Some(24.0), 1431), ("header_height", Some(28.0), 1432)]),
row(MacosTahoe, "popover", NONE, &[1461, 1462], "(none)"),
row(MacosTahoe, "dialog", NONE, &[1556, 1557], "macOS 26 ❓"),
row(MacosTahoe, "combo_box", [Some(4.0), None, Some(4.0), Some(12.0)], &[1618, 1623], "macOS 26: 12 left, the arrow on the right; 4 top / 4 bottom (API)")
.with(&[("arrow_area_width", None, 1620)]),
row(MacosTahoe, "segmented_control", NONE, &[1640, 1645], "macOS 26 ❓"),
row(MacosTahoe, "card", all(5.0), &[1662, 1663], "macOS 26: the same 5, inferred from the unversioned page"),
row(MacosTahoe, "expander", NONE, &[1677, 1678], "(none), app-defined")
.with(&[("arrow_gap", None, 1684), ("content_indent", None, 1685)]),
row(Windows11, "window", NONE, &[1170, 1171], "pointer to §2.20 layout margins"),
row(Windows11, "button", trbl(5.0, 11.0, 6.0, 11.0), &[1184, 1185], "11; 5 top / 6 bottom"),
row(Windows11, "input", trbl(5.0, 6.0, 6.0, 10.0), &[1213, 1214], "10 left / 6 right; 5 top / 6 bottom"),
row(Windows11, "text_area", trbl(5.0, 6.0, 6.0, 10.0), &[1714, 1715], "the same TextBox: TextControlThemePadding=10,5,6,6"),
row(Windows11, "checkbox", NONE, &[1237, 1238], "(none)"),
row(Windows11, "menu", trbl(4.0, 11.0, 5.0, 11.0), &[1261, 1262], "11; mouse context 4 top / 5 bottom")
.with(&[("row_height", None, 1260)]),
row(Windows11, "tooltip", trbl(6.0, 9.0, 8.0, 9.0), &[1283, 1284], "ToolTipBorderPadding=9,6,9,8"),
row(Windows11, "progress_bar", all(0.0), &[1341, 1342], "Control.Padding's default 0; 0"),
row(Windows11, "tab", axes(3.0, 8.0), &[1352, 1353], "without-close-button context: TabViewItemHeaderPaddingWithoutCloseButton=8,3,8,3")
.with(&[("item_gap", Some(0.0), 1360)]),
row(Windows11, "sidebar", [None, Some(3.0), None, Some(3.0)], &[1380, 1381], "(derived) 3 across, -1 + 4; the vertical ❓, no one value"),
Row {
preset: Windows11,
widget: "toolbar",
padding: trbl(0.0, 0.0, 0.0, 4.0),
lines: &[1392, 1393],
note: "Padding=\"4,0,0,0\"",
extra: &[("bar_height", Some(48.0), 1390), ("item_gap", Some(0.0), 1391)],
},
row(Windows11, "status_bar", NONE, &[1414, 1415], "(none): not specified"),
row(Windows11, "list", trbl(0.0, 12.0, 0.0, 16.0), &[1433, 1434], "Padding=16,0,12,0")
.with(&[("row_height", Some(40.0), 1431), ("header_height", None, 1432)]),
row(Windows11, "popover", trbl(15.0, 16.0, 17.0, 16.0), &[1461, 1462], "FlyoutContentPadding=16,15,16,17"),
row(Windows11, "dialog", all(24.0), &[1556, 1557], "ContentDialogPadding=24"),
row(Windows11, "combo_box", trbl(5.0, 0.0, 7.0, 12.0), &[1618, 1623], "ComboBoxPadding=12,5,0,7: 0 right, to the 38px arrow column")
.with(&[("arrow_area_width", Some(38.0), 1620)]),
row(Windows11, "segmented_control", [Some(5.0), Some(11.0), Some(6.0), Some(11.0)], &[1640, 1645], "Toolkit Segmented: the item content's 11,0,11,0 margin, the label's 0,5,0,6"),
row(Windows11, "card", all(12.0), &[1662, 1663], "12 (convention)"),
row(Windows11, "expander", trbl(0.0, 0.0, 0.0, 16.0), &[1677, 1678], "header context: ExpanderHeaderPadding=16,0,0,0")
.with(&[("arrow_gap", Some(30.0), 1684), ("content_indent", Some(16.0), 1685)]),
row(Material3, "window", NONE, &[1170, 1171], "(none): M3 has no window chrome"),
row(Material3, "button", axes(10.0, 24.0), &[1184, 1185], "material-web leading/trailing-space 24; its padding-block (40 − 20) / 2 = 10"),
row(Material3, "input", all(15.0), &[1213, 1214], "outlined field 16 from the edge, less the 1 outline; the same down"),
row(Material3, "text_area", all(15.0), &[1714, 1715], "the outlined field's 16 less the 1 outline; the same down"),
row(Material3, "checkbox", NONE, &[1237, 1238], "(none): the mark fills the box"),
row(Material3, "menu", [None, Some(16.0), None, Some(16.0)], &[1261, 1262], "16 (md-menu-item); vertical left to the row height: the cell's 14 is (derived), material-web's item 12, latest 8")
.with(&[("row_height", Some(48.0), 1260)]),
row(Material3, "tooltip", axes(4.0, 8.0), &[1283, 1284], "Compose PlainTooltipHorizontalPadding 8; PlainTooltipVerticalPadding 4"),
row(Material3, "progress_bar", NONE, &[1329], "§2.10's padding rows give 0, which the preset does not state yet (docs/todo.md, *Presets: values the M8 T15 platform-facts rows disagree with*)"),
row(Material3, "tab", [None, Some(16.0), None, Some(16.0)], &[1352, 1353], "material-web padding: 0 16px; vertical (derived) from the fixed height, not stated")
.with(&[("item_gap", None, 1360)]),
row(Material3, "sidebar", [None, Some(12.0), None, Some(12.0)], &[1380, 1381], "Compose NavigationDrawerItemDefaults.ItemPadding horizontal 12; the vertical app-defined, not stated"),
Row {
preset: Material3,
widget: "toolbar",
padding: NONE,
lines: &[1392, 1393],
note: "v0_192 has no app-bar space token (latest's 4 not taken); none",
extra: &[("bar_height", Some(64.0), 1390), ("item_gap", None, 1391)],
},
row(Material3, "status_bar", NONE, &[1414, 1415], "(none): M3 has no status bar"),
row(Material3, "list", [None, Some(16.0), None, Some(16.0)], &[1433, 1434], "list-item-leading/trailing-space 16; vertical 12 (list item) or 10 (latest), a list or a data table: not stated")
.with(&[("row_height", None, 1431), ("header_height", Some(56.0), 1432)]),
row(Material3, "popover", axes(8.0, 0.0), &[1461, 1462], "the menu container pads 8 top and bottom, 0 at the sides"),
row(Material3, "dialog", all(24.0), &[1556, 1557], "material-web dialog 24; 24 top / 24 bottom"),
row(Material3, "combo_box", trbl(15.0, 11.0, 15.0, 15.0), &[1618, 1623], "outlined field 16 leading / 12 trailing (the arrow's trailing space), 16 down, each less the 1 outline")
.with(&[("arrow_area_width", None, 1620)]),
row(Material3, "segmented_control", [None, Some(12.0), None, Some(12.0)], &[1640, 1645], "material-web labs spacing-leading/trailing 12; vertical (derived), not stated"),
row(Material3, "card", NONE, &[1662, 1663], "(app-defined)"),
row(Material3, "expander", NONE, &[1677, 1678], "(none): M3 has no expander")
.with(&[("arrow_gap", None, 1684), ("content_indent", None, 1685)]),
];
const WIDGETS: [&str; 19] = [
"window",
"button",
"input",
"text_area",
"checkbox",
"menu",
"tooltip",
"progress_bar",
"tab",
"sidebar",
"toolbar",
"status_bar",
"list",
"popover",
"dialog",
"combo_box",
"segmented_control",
"card",
"expander",
];
fn section(widget: &str) -> &'static str {
match widget {
"window" => "2.2",
"button" => "2.3",
"input" => "2.4",
"text_area" => "2.29",
"checkbox" => "2.5",
"menu" => "2.6",
"tooltip" => "2.7",
"progress_bar" => "2.10",
"tab" => "2.11",
"sidebar" => "2.12",
"toolbar" => "2.13",
"status_bar" => "2.14",
"list" => "2.15",
"popover" => "2.16",
"dialog" => "2.22",
"combo_box" => "2.24",
"segmented_control" => "2.25",
"card" => "2.26",
"expander" => "2.27",
_ => "",
}
}
fn padding(theme: &ResolvedTheme, widget: &str) -> Option<ResolvedPadding> {
let border = match widget {
"window" => &theme.window.border,
"button" => &theme.button.border,
"input" => &theme.input.border,
"text_area" => &theme.text_area.border,
"checkbox" => &theme.checkbox.border,
"menu" => &theme.menu.border,
"tooltip" => &theme.tooltip.border,
"progress_bar" => &theme.progress_bar.border,
"tab" => &theme.tab.border,
"sidebar" => &theme.sidebar.border,
"toolbar" => &theme.toolbar.border,
"status_bar" => &theme.status_bar.border,
"list" => &theme.list.border,
"popover" => &theme.popover.border,
"dialog" => &theme.dialog.border,
"combo_box" => &theme.combo_box.border,
"segmented_control" => &theme.segmented_control.border,
"card" => &theme.card.border,
"expander" => &theme.expander.border,
_ => return None,
};
Some(border.padding)
}
fn stated_sizes(v: &ThemeMode, widget: &str) -> Option<Vec<(&'static str, Option<f32>)>> {
let border = match widget {
"window" => &v.window.border,
"button" => &v.button.border,
"input" => &v.input.border,
"text_area" => &v.text_area.border,
"checkbox" => &v.checkbox.border,
"menu" => &v.menu.border,
"tooltip" => &v.tooltip.border,
"progress_bar" => &v.progress_bar.border,
"tab" => &v.tab.border,
"sidebar" => &v.sidebar.border,
"toolbar" => &v.toolbar.border,
"status_bar" => &v.status_bar.border,
"list" => &v.list.border,
"popover" => &v.popover.border,
"dialog" => &v.dialog.border,
"combo_box" => &v.combo_box.border,
"segmented_control" => &v.segmented_control.border,
"card" => &v.card.border,
"expander" => &v.expander.border,
_ => return None,
};
let side =
|f: fn(&crate::model::border::WidgetBorderSpec) -> Option<f32>| border.as_ref().and_then(f);
let mut sizes = vec![
("border.padding_top", side(|b| b.padding_top)),
("border.padding_right", side(|b| b.padding_right)),
("border.padding_bottom", side(|b| b.padding_bottom)),
("border.padding_left", side(|b| b.padding_left)),
];
match widget {
"toolbar" => {
sizes.push(("bar_height", v.toolbar.bar_height));
sizes.push(("item_gap", v.toolbar.item_gap));
}
"tab" => sizes.push(("item_gap", v.tab.item_gap)),
"expander" => {
sizes.push(("arrow_gap", v.expander.arrow_gap));
sizes.push(("content_indent", v.expander.content_indent));
}
"menu" => sizes.push(("row_height", v.menu.row_height)),
"list" => {
sizes.push(("row_height", v.list.row_height));
sizes.push(("header_height", v.list.header_height));
}
"combo_box" => sizes.push(("arrow_area_width", v.combo_box.arrow_area_width)),
_ => {}
}
Some(sizes)
}
fn extra_field(theme: &ResolvedTheme, widget: &str, field: &str) -> Option<Option<f32>> {
match (widget, field) {
("toolbar", "bar_height") => Some(theme.toolbar.bar_height),
("toolbar", "item_gap") => Some(theme.toolbar.item_gap),
("tab", "item_gap") => Some(theme.tab.item_gap),
("expander", "arrow_gap") => Some(theme.expander.arrow_gap),
("expander", "content_indent") => Some(theme.expander.content_indent),
("menu", "row_height") => Some(theme.menu.row_height),
("list", "row_height") => Some(theme.list.row_height),
("list", "header_height") => Some(theme.list.header_height),
("combo_box", "arrow_area_width") => Some(theme.combo_box.arrow_area_width),
_ => None,
}
}
fn resolve(variant: ThemeMode) -> Result<ResolvedTheme, String> {
variant
.into_resolved(&ResolutionContext::for_tests())
.map_err(|e| format!("resolution failed: {e}"))
}
fn static_variant(preset: NativePreset, mode: ColorMode) -> Result<ThemeMode, String> {
let full = Theme::preset(preset.key()).map_err(|e| e.to_string())?;
full.into_variant(mode).map_err(|e| e.to_string())
}
fn live_variant(preset: NativePreset, mode: ColorMode) -> Result<ThemeMode, String> {
let mut variant = static_variant(preset, mode)?;
if let Some(reader) = preset.reader_constants() {
variant.merge(&reader);
}
Ok(variant)
}
fn gate_variant(preset: NativePreset, mode: ColorMode, live: bool) -> Result<ThemeMode, String> {
if live {
live_variant(preset, mode)
} else {
static_variant(preset, mode)
}
}
fn source(preset: NativePreset, live: bool) -> String {
if live {
format!("{} + reader constants", preset.key())
} else {
preset.key().to_string()
}
}
fn facts_row(section: &str, key: &str) -> Option<&'static str> {
let title = format!("### {section} ");
let cell = format!("| `{key}`");
PLATFORM_FACTS
.lines()
.skip_while(|l| !l.starts_with(&title))
.skip(1)
.take_while(|l| !l.starts_with("### "))
.find(|l| l.starts_with(&cell))
}
fn cite(lines: &[usize]) -> String {
let lines: Vec<String> = lines.iter().map(|l| format!(":{l}")).collect();
format!("docs/platform-facts.md{}", lines.join(", "))
}
#[test]
fn native_themes_state_documented_sizes() {
let mut failures = Vec::new();
for preset in NativePreset::ALL {
for (mode, variant) in [(ColorMode::Light, "light"), (ColorMode::Dark, "dark")] {
for &live in preset.resolutions() {
let theme = match gate_variant(preset, mode, live).and_then(resolve) {
Ok(t) => t,
Err(e) => {
failures.push(format!("{} {variant}: {e}", source(preset, live)));
continue;
}
};
for row in ROWS.iter().filter(|r| r.preset == preset) {
let Some(got) = padding(&theme, row.widget) else {
failures.push(format!("unknown widget `{}`", row.widget));
continue;
};
let got = [got.top, got.right, got.bottom, got.left];
for (i, side) in ["top", "right", "bottom", "left"].iter().enumerate() {
if got[i] != row.padding[i] {
failures.push(format!(
"{}.border.padding_{side}: {} {variant}: resolved {:?}, \
platform-facts gives {:?} ({}; {})",
row.widget,
source(preset, live),
got[i],
row.padding[i],
cite(row.lines),
row.note,
));
}
}
for &(field, expected, line) in row.extra {
let got = extra_field(&theme, row.widget, field);
if got != Some(expected) {
failures.push(format!(
"{}.{field}: {} {variant}: resolved {:?}, \
platform-facts gives {expected:?} ({})",
row.widget,
source(preset, live),
got.flatten(),
cite(&[line]),
));
}
}
}
}
}
}
assert!(
failures.is_empty(),
"{} documented-size mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn every_preset_and_widget_has_one_row() {
for preset in NativePreset::ALL {
for widget in WIDGETS {
let n = ROWS
.iter()
.filter(|r| r.preset == preset && r.widget == widget)
.count();
assert_eq!(n, 1, "{preset:?} {widget}: {n} rows");
}
}
assert_eq!(ROWS.len(), NativePreset::ALL.len() * WIDGETS.len());
}
#[test]
fn every_row_names_rows_of_its_section() {
for row in ROWS {
let section = section(row.widget);
let fields = row.extra.iter().map(|&(field, _, _)| field);
for key in ["border.padding_horizontal", "border.padding_vertical"]
.into_iter()
.chain(fields)
{
assert!(
facts_row(section, key).is_some(),
"{:?} {}: §{section} has no `{key}` row",
row.preset,
row.widget,
);
}
}
}
const UNSOURCED_PRESETS: [&str; 11] = [
"catppuccin-latte",
"catppuccin-frappe",
"catppuccin-macchiato",
"catppuccin-mocha",
"dracula",
"gruvbox",
"nord",
"one-dark",
"solarized",
"tokyo-night",
"ios-13",
];
#[test]
fn every_bundled_preset_is_native_or_unsourced() {
let mut gated: Vec<&str> = NativePreset::ALL
.iter()
.map(|p| p.key())
.chain(UNSOURCED_PRESETS)
.collect();
gated.sort_unstable();
let mut bundled: Vec<&str> = crate::presets::list_presets()
.iter()
.map(|info| info.key)
.collect();
bundled.sort_unstable();
assert_eq!(gated, bundled);
}
const OPTIONAL_FIELDS: [&str; 6] = [
"checkbox.radio_dot_diameter",
"checkbox.check_mark_stroke_width",
"checkbox.radio_indicator_width",
"switch.unchecked_thumb_diameter",
"tab.active_indicator_width",
"tab.active_outward_corner_radius",
];
#[test]
fn unsourced_presets_state_no_gated_size() {
let mut failures = Vec::new();
for name in UNSOURCED_PRESETS {
for mode in [ColorMode::Light, ColorMode::Dark] {
let variant = match Theme::preset(name).and_then(|t| t.into_variant(mode)) {
Ok(v) => v,
Err(e) => {
failures.push(format!("{name} {mode:?}: {e}"));
continue;
}
};
for widget in WIDGETS {
let Some(sizes) = stated_sizes(&variant, widget) else {
failures.push(format!("unknown widget `{widget}`"));
continue;
};
for (key, value) in sizes {
if let Some(value) = value {
failures.push(format!(
"{name} {mode:?}: states {widget}.{key} = {value}, with no source"
));
}
}
}
for field in OPTIONAL_FIELDS {
match states_field(&variant, field) {
Some(false) => {}
Some(true) => {
failures.push(format!("{name} {mode:?}: states {field}, with no source"));
}
None => failures.push(format!("unknown field `{field}`")),
}
}
}
}
assert!(
failures.is_empty(),
"{} unsourced sizes:\n{}",
failures.len(),
failures.join("\n")
);
}
#[derive(Clone, Copy, Debug)]
enum Want {
Pt(f32),
Px(f32),
BodyTimes(f32),
Weight(u16),
Unstated,
}
struct FieldRow {
preset: NativePreset,
field: &'static str,
want: Want,
line: usize,
note: &'static str,
}
const fn field(
preset: NativePreset,
field: &'static str,
want: Want,
line: usize,
note: &'static str,
) -> FieldRow {
FieldRow {
preset,
field,
want,
line,
note,
}
}
use Want::{BodyTimes, Pt, Px, Unstated, Weight};
#[rustfmt::skip]
const FIELD_ROWS: &[FieldRow] = &[
field(KdeBreeze, "text_scale.caption.size", Pt(8.0), 1485, "smallestReadableFont; its default is 8pt (:573)"),
field(KdeBreeze, "text_scale.caption.weight", Weight(400), 1485, "smallestReadableFont; its default is 400 (:573)"),
field(KdeBreeze, "text_scale.section_heading.size", BodyTimes(1.20), 1486, "Kirigami Heading level 2: body × 1.20"),
field(KdeBreeze, "text_scale.section_heading.weight", Weight(400), 1486, "Font.Normal unless type: Primary"),
field(KdeBreeze, "text_scale.dialog_title.size", BodyTimes(1.35), 1487, "Kirigami Heading level 1: body × 1.35"),
field(KdeBreeze, "text_scale.dialog_title.weight", Weight(400), 1487, "Font.Normal unless type: Primary"),
field(KdeBreeze, "text_scale.display.size", Unstated, 1488, "(none): resolves from the body font"),
field(KdeBreeze, "text_scale.display.weight", Unstated, 1488, "(none): resolves from the body font"),
field(KdeBreeze, "dialog.title_font.size", Unstated, 1561, "← defaults.font"),
field(KdeBreeze, "dialog.title_font.weight", Unstated, 1562, "← defaults.font"),
field(KdeBreeze, "slider.track_height", Px(6.0), 1320, "Slider_GrooveThickness = 6"),
field(KdeBreeze, "slider.thumb_diameter", Px(20.0), 1321, "Slider_ControlThickness = 20"),
field(KdeBreeze, "progress_bar.track_height", Px(6.0), 1335, "ProgressBar_Thickness = 6"),
field(KdeBreeze, "checkbox.radio_dot_diameter", Px(6.0), 1241, "renderRadioButton: radius (18 - 12) / 2 = 3"),
field(KdeBreeze, "checkbox.check_mark_stroke_width", Px(2.0), 1242, "the check pen: PenWidth::Frame * 2 = 2.002"),
field(KdeBreeze, "checkbox.radio_indicator_width", Unstated, 1243, "← indicator_width: CheckBox_Size for both"),
field(KdeBreeze, "switch.unchecked_thumb_diameter", Unstated, 1525, "← thumb_diameter: one handle in both states"),
field(KdeBreeze, "tab.active_indicator_width", Px(3.0), 1362, "TabBar_ActiveEffectSize = 3"),
field(KdeBreeze, "tab.active_outward_corner_radius", Unstated, 1366, "(none): CornersTop"),
field(Adwaita, "text_scale.caption.size", Pt(9.0), 1485, ".caption: ≈9pt"),
field(Adwaita, "text_scale.caption.weight", Weight(400), 1485, ".caption: 400"),
field(Adwaita, "text_scale.section_heading.size", Pt(11.0), 1486, ".heading: 11pt"),
field(Adwaita, "text_scale.section_heading.weight", Weight(700), 1486, ".heading: 700"),
field(Adwaita, "text_scale.dialog_title.size", Pt(15.0), 1487, ".title-2: ≈15pt"),
field(Adwaita, "text_scale.dialog_title.weight", Weight(800), 1487, ".title-2: 800"),
field(Adwaita, "text_scale.display.size", Pt(20.0), 1488, ".title-1: ≈20pt"),
field(Adwaita, "text_scale.display.weight", Weight(800), 1488, ".title-1: 800"),
field(Adwaita, "dialog.title_font.size", Pt(15.0), 1561, "136% of base ≈15pt (.title-2)"),
field(Adwaita, "dialog.title_font.weight", Weight(800), 1562, "800 (.title-2)"),
field(Adwaita, "slider.track_height", Px(10.0), 1320, "libadwaita .scale: 10"),
field(Adwaita, "slider.thumb_diameter", Px(20.0), 1321, "libadwaita: 20"),
field(Adwaita, "progress_bar.track_height", Px(8.0), 1335, "libadwaita .progressbar: 8"),
field(Adwaita, "checkbox.radio_dot_diameter", Px(8.0), 1241, "bullet.svg: a circle of radius 4"),
field(Adwaita, "checkbox.check_mark_stroke_width", Px(2.0), 1242, "check.svg (1.10.0): a stroke 2 wide"),
field(Adwaita, "checkbox.radio_indicator_width", Unstated, 1243, "← indicator_width: one check, radio rule"),
field(Adwaita, "switch.unchecked_thumb_diameter", Unstated, 1525, "← thumb_diameter: one slider rule"),
field(Adwaita, "tab.active_indicator_width", Px(4.0), 1362, "_notebook.scss: inset 0 -4px"),
field(Adwaita, "tab.active_outward_corner_radius", Unstated, 1366, "(none): a square notebook tab"),
field(MacosMonterey, "text_scale.caption.size", Pt(10.0), 1485, ".caption1: 10pt"),
field(MacosMonterey, "text_scale.caption.weight", Weight(400), 1485, ".caption1: 400"),
field(MacosMonterey, "text_scale.section_heading.size", Pt(13.0), 1486, ".headline: 13pt"),
field(MacosMonterey, "text_scale.section_heading.weight", Weight(700), 1486, ".headline: 700"),
field(MacosMonterey, "text_scale.dialog_title.size", Pt(22.0), 1487, ".title1: 22pt"),
field(MacosMonterey, "text_scale.dialog_title.weight", Weight(400), 1487, ".title1: 400"),
field(MacosMonterey, "text_scale.display.size", Pt(26.0), 1488, ".largeTitle: 26pt"),
field(MacosMonterey, "text_scale.display.weight", Weight(400), 1488, ".largeTitle: 400"),
field(MacosMonterey, "dialog.title_font.size", Pt(13.0), 1561, "emphasized system font: systemFontSize, 13pt"),
field(MacosMonterey, "dialog.title_font.weight", Weight(700), 1562, "emphasized system font: Bold (700)"),
field(MacosMonterey, "slider.track_height", Px(4.0), 1320, "macOS 14-15: NSSlider 4 (API)"),
field(MacosMonterey, "slider.thumb_diameter", Px(20.0), 1321, "NSSlider knob: 20 (measured, the macOS 14.8.9 probe)"),
field(MacosMonterey, "progress_bar.track_height", Px(6.0), 1335, "NSProgressIndicator: 6"),
field(MacosMonterey, "checkbox.radio_dot_diameter", Px(6.0), 1241, "macOS 14-15: 6 (measured, the probe's radio render)"),
field(MacosMonterey, "checkbox.check_mark_stroke_width", Unstated, 1242, "(none): not published"),
field(MacosMonterey, "checkbox.radio_indicator_width", Unstated, 1243, "← indicator_width: not published"),
field(MacosMonterey, "switch.unchecked_thumb_diameter", Unstated, 1525, "← thumb_diameter: not published"),
field(MacosMonterey, "tab.active_indicator_width", Unstated, 1362, "(none): no line"),
field(MacosMonterey, "tab.active_outward_corner_radius", Unstated, 1366, "❓: the bezel's shape is unpublished"),
field(MacosTahoe, "text_scale.caption.size", Pt(10.0), 1485, ".caption1: 10pt, the same on 26"),
field(MacosTahoe, "text_scale.caption.weight", Weight(400), 1485, ".caption1: 400"),
field(MacosTahoe, "text_scale.section_heading.size", Pt(13.0), 1486, ".headline: 13pt"),
field(MacosTahoe, "text_scale.section_heading.weight", Weight(700), 1486, ".headline: 700"),
field(MacosTahoe, "text_scale.dialog_title.size", Pt(22.0), 1487, ".title1: 22pt"),
field(MacosTahoe, "text_scale.dialog_title.weight", Weight(400), 1487, ".title1: 400"),
field(MacosTahoe, "text_scale.display.size", Pt(26.0), 1488, ".largeTitle: 26pt"),
field(MacosTahoe, "text_scale.display.weight", Weight(400), 1488, ".largeTitle: 400"),
field(MacosTahoe, "dialog.title_font.size", Pt(13.0), 1561, "macOS 26 ❓: macos-monterey's 13pt, carried"),
field(MacosTahoe, "dialog.title_font.weight", Weight(700), 1562, "macOS 26 ❓: macos-monterey's Bold (700), carried"),
field(MacosTahoe, "slider.track_height", Px(6.0), 1320, "macOS 26: 6 (API)"),
field(MacosTahoe, "slider.thumb_diameter", Px(16.0), 1321, "macOS 26: the 20 x 16 capsule, 16 across the track"),
field(MacosTahoe, "progress_bar.track_height", Px(8.0), 1335, "macOS 26: 8 (measured)"),
field(MacosTahoe, "checkbox.radio_dot_diameter", Px(5.0), 1241, "macOS 26: 5 (measured, the probe's radio render)"),
field(MacosTahoe, "checkbox.check_mark_stroke_width", Unstated, 1242, "(none): not published"),
field(MacosTahoe, "checkbox.radio_indicator_width", Unstated, 1243, "← indicator_width: not published"),
field(MacosTahoe, "switch.unchecked_thumb_diameter", Unstated, 1525, "← thumb_diameter: not published"),
field(MacosTahoe, "tab.active_indicator_width", Unstated, 1362, "(none): no line"),
field(MacosTahoe, "tab.active_outward_corner_radius", Unstated, 1366, "❓: the bezel's shape is unpublished"),
field(Windows11, "text_scale.caption.size", Px(12.0), 1485, "Caption: 12epx"),
field(Windows11, "text_scale.caption.weight", Weight(400), 1485, "Caption: 400"),
field(Windows11, "text_scale.section_heading.size", Px(20.0), 1486, "Subtitle: 20epx"),
field(Windows11, "text_scale.section_heading.weight", Weight(600), 1486, "Subtitle: 600"),
field(Windows11, "text_scale.dialog_title.size", Px(28.0), 1487, "Title: 28epx"),
field(Windows11, "text_scale.dialog_title.weight", Weight(600), 1487, "Title: 600"),
field(Windows11, "text_scale.display.size", Px(68.0), 1488, "Display: 68epx"),
field(Windows11, "text_scale.display.weight", Weight(600), 1488, "Display: 600"),
field(Windows11, "dialog.title_font.size", Px(20.0), 1561, "20px (ContentDialog template)"),
field(Windows11, "dialog.title_font.weight", Weight(600), 1562, "SemiBold (600)"),
field(Windows11, "slider.track_height", Px(4.0), 1320, "WinUI3: 4"),
field(Windows11, "slider.thumb_diameter", Px(18.0), 1321, "WinUI3: 18"),
field(Windows11, "progress_bar.track_height", Px(1.0), 1335, "the groove, ProgressBarTrackHeight: 1"),
field(Windows11, "checkbox.radio_dot_diameter", Px(12.0), 1241, "RadioButtonCheckGlyphSize: 12"),
field(Windows11, "checkbox.check_mark_stroke_width", Unstated, 1242, "(none): a font glyph"),
field(Windows11, "checkbox.radio_indicator_width", Unstated, 1243, "← indicator_width: OuterEllipse 20 as CheckBoxSize 20"),
field(Windows11, "switch.unchecked_thumb_diameter", Unstated, 1525, "← thumb_diameter: SwitchKnobOff 12 as SwitchKnobOn 12"),
field(Windows11, "tab.active_indicator_width", Unstated, 1362, "(none): no line"),
field(Windows11, "tab.active_outward_corner_radius", Px(4.0), 1366, "TabViewItem.cpp:106: 4px curving-out corners"),
field(Material3, "text_scale.caption.size", Px(12.0), 1485, "body-small: 12"),
field(Material3, "text_scale.caption.weight", Weight(400), 1485, "body-small: 400"),
field(Material3, "text_scale.section_heading.size", Px(16.0), 1486, "title-medium: 16"),
field(Material3, "text_scale.section_heading.weight", Weight(500), 1486, "title-medium: 500"),
field(Material3, "text_scale.dialog_title.size", Px(24.0), 1487, "headline-small: 24"),
field(Material3, "text_scale.dialog_title.weight", Weight(400), 1487, "headline-small: 400"),
field(Material3, "text_scale.display.size", Px(36.0), 1488, "display-small: 36"),
field(Material3, "text_scale.display.weight", Weight(400), 1488, "display-small: 400"),
field(Material3, "dialog.title_font.size", Px(24.0), 1561, "comp.dialog headline-size: 24"),
field(Material3, "dialog.title_font.weight", Weight(400), 1562, "headline-small-weight: 400"),
field(Material3, "slider.track_height", Px(4.0), 1320, "active/inactive-track-height: 4"),
field(Material3, "slider.thumb_diameter", Px(20.0), 1321, "handle-width/height: 20"),
field(Material3, "progress_bar.track_height", Px(4.0), 1335, "linear track-height: 4"),
field(Material3, "checkbox.radio_dot_diameter", Px(10.0), 1241, "material-web radio.ts: inner circle r = 5"),
field(Material3, "checkbox.check_mark_stroke_width", Px(2.0), 1242, "material-web $_mark-stroke: 2px"),
field(Material3, "checkbox.radio_indicator_width", Px(20.0), 1243, "comp.radio-button icon-size: 20"),
field(Material3, "switch.unchecked_thumb_diameter", Px(16.0), 1525, "unselected-handle-width/height: 16"),
field(Material3, "tab.active_indicator_width", Px(3.0), 1362, "active-indicator-height: 3"),
field(Material3, "tab.active_outward_corner_radius", Unstated, 1366, "(none): corner-none"),
];
const FIELDS: [&str; 19] = [
"text_scale.caption.size",
"text_scale.caption.weight",
"text_scale.section_heading.size",
"text_scale.section_heading.weight",
"text_scale.dialog_title.size",
"text_scale.dialog_title.weight",
"text_scale.display.size",
"text_scale.display.weight",
"dialog.title_font.size",
"dialog.title_font.weight",
"slider.track_height",
"slider.thumb_diameter",
"progress_bar.track_height",
"checkbox.radio_dot_diameter",
"checkbox.check_mark_stroke_width",
"checkbox.radio_indicator_width",
"switch.unchecked_thumb_diameter",
"tab.active_indicator_width",
"tab.active_outward_corner_radius",
];
fn field_cell(field: &str) -> Option<(&'static str, &'static str)> {
Some(match field {
"text_scale.caption.size" | "text_scale.caption.weight" => ("2.19", "caption"),
"text_scale.section_heading.size" | "text_scale.section_heading.weight" => {
("2.19", "section_heading")
}
"text_scale.dialog_title.size" | "text_scale.dialog_title.weight" => {
("2.19", "dialog_title")
}
"text_scale.display.size" | "text_scale.display.weight" => ("2.19", "display"),
"dialog.title_font.size" => ("2.22", "title_font.size"),
"dialog.title_font.weight" => ("2.22", "title_font.weight"),
"slider.track_height" => ("2.9", "track_height"),
"slider.thumb_diameter" => ("2.9", "thumb_diameter"),
"progress_bar.track_height" => ("2.10", "track_height"),
"checkbox.radio_dot_diameter" => ("2.5", "radio_dot_diameter"),
"checkbox.check_mark_stroke_width" => ("2.5", "check_mark_stroke_width"),
"checkbox.radio_indicator_width" => ("2.5", "radio_indicator_width"),
"switch.unchecked_thumb_diameter" => ("2.21", "unchecked_thumb_diameter"),
"tab.active_indicator_width" => ("2.11", "active_indicator_width"),
"tab.active_outward_corner_radius" => ("2.11", "active_outward_corner_radius"),
_ => return None,
})
}
fn stated_entry<'a>(v: &'a ThemeMode, role: &str) -> Option<&'a crate::TextScaleEntry> {
match role {
"caption" => v.text_scale.caption.as_ref(),
"section_heading" => v.text_scale.section_heading.as_ref(),
"dialog_title" => v.text_scale.dialog_title.as_ref(),
"display" => v.text_scale.display.as_ref(),
_ => None,
}
}
fn states_field(v: &ThemeMode, field: &str) -> Option<bool> {
let title = v.dialog.title_font.as_ref();
Some(match field {
"dialog.title_font.size" => title.and_then(|f| f.size).is_some(),
"dialog.title_font.weight" => title.and_then(|f| f.weight).is_some(),
"slider.track_height" => v.slider.track_height.is_some(),
"slider.thumb_diameter" => v.slider.thumb_diameter.is_some(),
"progress_bar.track_height" => v.progress_bar.track_height.is_some(),
"checkbox.radio_dot_diameter" => v.checkbox.radio_dot_diameter.is_some(),
"checkbox.check_mark_stroke_width" => v.checkbox.check_mark_stroke_width.is_some(),
"checkbox.radio_indicator_width" => v.checkbox.radio_indicator_width.is_some(),
"switch.unchecked_thumb_diameter" => v.switch.unchecked_thumb_diameter.is_some(),
"tab.active_indicator_width" => v.tab.active_indicator_width.is_some(),
"tab.active_outward_corner_radius" => v.tab.active_outward_corner_radius.is_some(),
_ => {
let (role, sub) = field.strip_prefix("text_scale.")?.split_once('.')?;
let entry = stated_entry(v, role);
match sub {
"size" => entry.and_then(|e| e.size).is_some(),
"weight" => entry.and_then(|e| e.weight).is_some(),
_ => return None,
}
}
})
}
fn resolved_field(theme: &ResolvedTheme, field: &str) -> Option<f32> {
let ts = &theme.text_scale;
Some(match field {
"text_scale.caption.size" => ts.caption.size,
"text_scale.caption.weight" => f32::from(ts.caption.weight),
"text_scale.section_heading.size" => ts.section_heading.size,
"text_scale.section_heading.weight" => f32::from(ts.section_heading.weight),
"text_scale.dialog_title.size" => ts.dialog_title.size,
"text_scale.dialog_title.weight" => f32::from(ts.dialog_title.weight),
"text_scale.display.size" => ts.display.size,
"text_scale.display.weight" => f32::from(ts.display.weight),
"dialog.title_font.size" => theme.dialog.title_font.size,
"dialog.title_font.weight" => f32::from(theme.dialog.title_font.weight),
"slider.track_height" => theme.slider.track_height,
"slider.thumb_diameter" => theme.slider.thumb_diameter,
"progress_bar.track_height" => theme.progress_bar.track_height,
"checkbox.radio_dot_diameter" => theme.checkbox.radio_dot_diameter?,
"checkbox.check_mark_stroke_width" => theme.checkbox.check_mark_stroke_width?,
"checkbox.radio_indicator_width" => theme.checkbox.radio_indicator_width?,
"switch.unchecked_thumb_diameter" => theme.switch.unchecked_thumb_diameter?,
"tab.active_indicator_width" => theme.tab.active_indicator_width?,
"tab.active_outward_corner_radius" => theme.tab.active_outward_corner_radius?,
_ => return None,
})
}
fn gate_variants(preset: NativePreset) -> Vec<Result<(String, &'static str, ThemeMode), String>> {
let mut out = Vec::new();
for (mode, name) in [(ColorMode::Light, "light"), (ColorMode::Dark, "dark")] {
for &live in preset.resolutions() {
out.push(
gate_variant(preset, mode, live)
.map(|v| (source(preset, live), name, v))
.map_err(|e| format!("{} {name}: {e}", source(preset, live))),
);
}
}
out
}
#[test]
fn native_themes_state_documented_fields() {
let mut failures = Vec::new();
for preset in NativePreset::ALL {
for variant in gate_variants(preset) {
let (source, name, variant) = match variant {
Ok(v) => v,
Err(e) => {
failures.push(e);
continue;
}
};
let theme = match resolve(variant.clone()) {
Ok(t) => t,
Err(e) => {
failures.push(format!("{source} {name}: {e}"));
continue;
}
};
let body = theme.defaults.font.size;
for row in FIELD_ROWS.iter().filter(|r| r.preset == preset) {
let at = format!(
"{}: {source} {name} ({}; {})",
row.field,
cite(&[row.line]),
row.note
);
if let Want::Unstated = row.want {
match states_field(&variant, row.field) {
Some(false) => {}
Some(true) => failures.push(format!(
"{at}: stated, but platform-facts leaves it to inheritance"
)),
None => failures.push(format!("{at}: unknown field")),
}
continue;
}
let expected = match row.want {
Want::Pt(v) => v * ResolutionContext::for_tests().font_dpi / 72.0,
Want::Px(v) => v,
Want::BodyTimes(f) => body * f,
Want::Weight(w) => f32::from(w),
Want::Unstated => continue,
};
match resolved_field(&theme, row.field) {
Some(got) if (got - expected).abs() < 1e-3 => {}
Some(got) => failures.push(format!(
"{at}: resolved {got}, platform-facts gives {:?} = {expected}",
row.want
)),
None => failures.push(format!("{at}: unknown field")),
}
}
}
}
assert!(
failures.is_empty(),
"{} documented-field mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn windows_reader_font_dpi_is_the_gates() {
assert_eq!(
crate::windows::LOGICAL_DPI as f32,
ResolutionContext::for_tests().font_dpi
);
}
#[test]
fn every_preset_and_field_has_one_row() {
for preset in NativePreset::ALL {
for f in FIELDS {
let n = FIELD_ROWS
.iter()
.filter(|r| r.preset == preset && r.field == f)
.count();
assert_eq!(n, 1, "{preset:?} {f}: {n} rows");
}
}
for f in FIELDS {
assert!(field_cell(f).is_some(), "{f}: no platform-facts cell");
}
assert_eq!(FIELD_ROWS.len(), NativePreset::ALL.len() * FIELDS.len());
}
#[test]
fn every_field_row_names_a_row_of_its_section() {
for row in FIELD_ROWS {
let (section, key) = field_cell(row.field).unwrap_or(("?", "?"));
assert!(
facts_row(section, key).is_some(),
"{:?} {}: §{section} has no `{key}` row",
row.preset,
row.field,
);
}
}
#[test]
fn native_presets_state_no_text_scale_line_height() {
let mut failures = Vec::new();
for preset in NativePreset::ALL {
if preset == Material3 || preset == NativePreset::Windows11 {
continue;
}
let name = preset.key();
for mode in [ColorMode::Light, ColorMode::Dark] {
let variant = match Theme::preset(name).and_then(|t| t.into_variant(mode)) {
Ok(v) => v,
Err(e) => {
failures.push(format!("{name} {mode:?}: {e}"));
continue;
}
};
for role in ["caption", "section_heading", "dialog_title", "display"] {
if let Some(lh) = stated_entry(&variant, role).and_then(|e| e.line_height) {
failures.push(format!(
"{name} {mode:?}: states text_scale.{role}.line_height = {lh:?}, \
with no source"
));
}
}
}
}
assert!(
failures.is_empty(),
"{} unsourced line heights:\n{}",
failures.len(),
failures.join("\n")
);
}
#[rustfmt::skip]
const EXPANDER_STRUCTURE: [(NativePreset, Option<crate::model::ArrowSide>, Option<bool>); 6] = [
(KdeBreeze, Some(crate::model::ArrowSide::Leading), Some(false)),
(Adwaita, Some(crate::model::ArrowSide::Trailing), Some(true)),
(MacosMonterey, Some(crate::model::ArrowSide::Leading), None),
(MacosTahoe, Some(crate::model::ArrowSide::Leading), None),
(Windows11, Some(crate::model::ArrowSide::Trailing), Some(true)),
(Material3, None, None),
];
#[test]
fn native_themes_state_the_expanders_structure() {
assert!(facts_row("2.27", "arrow_side").is_some());
assert!(facts_row("2.27", "frame_enabled").is_some());
let mut failures = Vec::new();
for (preset, side, frame) in EXPANDER_STRUCTURE {
for variant in gate_variants(preset) {
match variant.and_then(|(source, name, v)| resolve(v).map(|t| (source, name, t))) {
Ok((source, name, t)) => {
let got = (t.expander.arrow_side, t.expander.frame_enabled);
if got != (side, frame) {
failures.push(format!(
"{source} {name}: expander side and frame {got:?}, \
platform-facts gives {:?} (docs/platform-facts.md:1683, :1686)",
(side, frame)
));
}
}
Err(e) => failures.push(e),
}
}
}
for name in UNSOURCED_PRESETS {
for mode in [ColorMode::Light, ColorMode::Dark] {
match Theme::preset(name).and_then(|t| t.into_variant(mode)) {
Ok(v) if v.expander.arrow_side.is_some() || v.expander.frame_enabled.is_some() => {
failures.push(format!(
"{name} {mode:?}: states the expander's side or frame, with no source"
));
}
Ok(_) => {}
Err(e) => failures.push(format!("{name} {mode:?}: {e}")),
}
}
}
assert!(failures.is_empty(), "{}", failures.join("\n"));
}
fn soft_field_failures<T: Copy + PartialEq + std::fmt::Debug>(
field: &str,
row: &str,
want: &[(NativePreset, Option<T>)],
resolved: fn(&ResolvedTheme) -> Option<T>,
stated: fn(&ThemeMode) -> Option<T>,
) -> Vec<String> {
let mut failures = Vec::new();
for preset in NativePreset::ALL {
let n = want.iter().filter(|(p, _)| *p == preset).count();
if n != 1 {
failures.push(format!("{preset:?}: {n} rows for {field}"));
}
}
for &(preset, expected) in want {
for variant in gate_variants(preset) {
match variant.and_then(|(source, name, v)| resolve(v).map(|t| (source, name, t))) {
Ok((source, name, t)) => {
let got = resolved(&t);
if got != expected {
failures.push(format!(
"{source} {name}: {field} {got:?}, platform-facts gives \
{expected:?} (docs/platform-facts.md {row})"
));
}
}
Err(e) => failures.push(e),
}
}
}
for name in UNSOURCED_PRESETS {
for mode in [ColorMode::Light, ColorMode::Dark] {
match Theme::preset(name).and_then(|t| t.into_variant(mode)) {
Ok(v) => {
if let Some(got) = stated(&v) {
failures.push(format!(
"{name} {mode:?}: states {field} = {got:?}, with no source"
));
}
}
Err(e) => failures.push(format!("{name} {mode:?}: {e}")),
}
}
}
failures
}
#[rustfmt::skip]
const TOOLTIP_SHOW_DELAY: [(NativePreset, Option<u16>); 6] = [
(KdeBreeze, Some(700)),
(Adwaita, Some(500)),
(MacosMonterey, None),
(MacosTahoe, None),
(Windows11, Some(800)),
(Material3, Some(0)),
];
#[test]
fn native_themes_state_the_tooltip_show_delay() {
assert!(facts_row("2.7", "show_delay").is_some());
let failures = soft_field_failures(
"tooltip.show_delay_ms",
"§2.7 show_delay",
&TOOLTIP_SHOW_DELAY,
|t| t.tooltip.show_delay,
|v| v.tooltip.show_delay,
);
assert!(failures.is_empty(), "{}", failures.join("\n"));
}
#[rustfmt::skip]
const DIALOG_ENTRANCE: [(NativePreset, Option<crate::model::DialogEntranceStyle>); 6] = [
(KdeBreeze, Some(crate::model::DialogEntranceStyle::FadeRise)),
(Adwaita, Some(crate::model::DialogEntranceStyle::ScaleFade)),
(MacosMonterey, None),
(MacosTahoe, None),
(Windows11, Some(crate::model::DialogEntranceStyle::ScaleFade)),
(Material3, Some(crate::model::DialogEntranceStyle::ScaleFade)),
];
#[test]
fn native_themes_state_the_dialog_entrance() {
assert!(facts_row("2.22", "entrance").is_some());
let failures = soft_field_failures(
"dialog.entrance",
"§2.22 entrance",
&DIALOG_ENTRANCE,
|t| t.dialog.entrance,
|v| v.dialog.entrance,
);
assert!(failures.is_empty(), "{}", failures.join("\n"));
}