mod color;
mod palette;
mod shape;
mod styles;
mod typography;
pub use palette::Material3Palette;
use std::rc::Rc;
use teksilo_core::presets::intui;
use teksilo_core::styles::{Theme, ThemeAppearance};
use teksilo_tokens::{InputTokens, TargetDensity};
pub fn input_tokens(density: TargetDensity) -> InputTokens {
let mut tokens = InputTokens::for_density(density);
if density == TargetDensity::Touch {
tokens.target_size = 48.0;
}
tokens
}
pub fn light() -> Theme {
let mut theme = intui::light().with_id("material3.light");
apply_material3_overrides(&mut theme, ThemeAppearance::Light);
theme
}
fn reproject(base: &Theme, density: TargetDensity) -> Theme {
let mut theme = base.clone();
theme.input = input_tokens(density);
install_style_slots(&mut theme);
theme
}
pub fn dark() -> Theme {
let mut theme = intui::dark().with_id("material3.dark");
apply_material3_overrides(&mut theme, ThemeAppearance::Dark);
theme
}
fn apply_material3_overrides(theme: &mut Theme, appearance: ThemeAppearance) {
let light = appearance == ThemeAppearance::Light;
theme.colors = if light {
color::m3_light_colors()
} else {
color::m3_dark_colors()
};
theme.shape = if light {
shape::m3_light_shape()
} else {
shape::m3_dark_shape()
};
theme.typography = typography::m3_typography();
theme.input = input_tokens(theme.input.density);
theme.extensions.insert(if light {
Material3Palette::light()
} else {
Material3Palette::dark()
});
install_style_slots(theme);
theme
.extensions
.insert(teksilo_core::styles::DensityProjection(reproject));
}
fn install_style_slots(theme: &mut Theme) {
theme.style_slots.button = Some(Rc::new(styles::button::m3_button_style(&theme.input)));
theme.style_slots.toggle = Some(Rc::new(styles::toggle::M3ToggleStyle));
theme.style_slots.card = Some(Rc::new(styles::card::M3CardStyle));
}
#[cfg(feature = "bundled-fonts")]
mod bundled_fonts {
use teksilo_text::{FontFaceId, FontRegistrar, TextFontService};
const ROBOTO: &[u8] = include_bytes!("../fonts/Roboto.ttf");
pub struct RobotoRegistrar;
impl FontRegistrar for RobotoRegistrar {
fn register_on_service(&self, service: &mut TextFontService) -> Option<FontFaceId> {
let regular = service.register_font_as(ROBOTO, "Roboto", 400, false);
service.register_font_as(ROBOTO, "Roboto", 500, false);
service.set_default_font(regular, 14.0);
Some(regular)
}
}
}
#[cfg(feature = "bundled-fonts")]
pub fn font_registrar() -> impl teksilo_text::FontRegistrar {
bundled_fonts::RobotoRegistrar
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
use teksilo_core::styles::ComponentStyleSlots;
use teksilo_tokens::Color;
#[test]
fn light_reports_light_appearance() {
assert_eq!(light().appearance, ThemeAppearance::Light);
assert!(!light().is_dark());
}
#[test]
fn dark_reports_dark_appearance() {
assert_eq!(dark().appearance, ThemeAppearance::Dark);
assert!(dark().is_dark());
}
#[test]
fn ids_are_namespaced() {
assert_eq!(light().id.as_str(), "material3.light");
assert_eq!(dark().id.as_str(), "material3.dark");
}
#[test]
fn light_and_dark_have_distinct_surfaces() {
assert_ne!(light().colors.surface_main, dark().colors.surface_main);
}
#[test]
fn on_error_and_containers_use_m3_values() {
let l = light().colors;
assert_eq!(l.text_on_error, Color::from_hex("#FFFFFF"));
assert_eq!(l.surface_error_container, Color::from_hex("#F9DEDC"));
assert_eq!(l.surface_container, Color::from_hex("#F3EDF7"));
let d = dark().colors;
assert_eq!(d.text_on_error, Color::from_hex("#601410"));
}
#[test]
fn destructive_button_label_is_on_error() {
use teksilo_core::styles::ButtonVariant;
use teksilo_tokens::TextRole;
let style = styles::button::m3_button_style(&InputTokens::default());
assert_eq!(
style.label_roles.get(&ButtonVariant::Destructive),
Some(&TextRole::OnError),
);
}
fn filled_fill(
state: teksilo_core::styles::WidgetState,
colors: &teksilo_tokens::ColorTokens,
) -> Color {
use teksilo_core::styles::ButtonVariant;
let style = styles::button::m3_button_style(&InputTokens::default());
style.recipes[&ButtonVariant::Filled]
.fill
.resolve(state)
.resolve_flat(colors)
.expect("a flat fill")
}
#[test]
fn filled_button_hover_and_pressed_are_the_m3_state_layers() {
use teksilo_core::styles::WidgetState;
for theme in [light(), dark()] {
let c = &theme.colors;
for (state, alpha) in [
(WidgetState::Idle, 0.0),
(WidgetState::Hovered, 0.08),
(WidgetState::Pressed, 0.12),
] {
let want = c.accent.mix(c.text_on_accent, alpha);
let got = filled_fill(state, c);
for (g, w) in got.to_array().iter().zip(want.to_array()) {
assert!((g - w).abs() < 1e-6, "{state:?}: {got:?} != {want:?}");
}
}
}
}
#[test]
fn filled_button_label_reads_7_to_1_in_every_state_under_high_contrast() {
use teksilo_core::styles::{PaintState, WidgetState};
use teksilo_tokens::contrast::composite_contrast;
for (name, theme) in [("light", light()), ("dark", dark())] {
for window_active in [true, false] {
let c = theme.paint_colors(PaintState {
window_active,
high_contrast: true,
});
for state in [
WidgetState::Idle,
WidgetState::Hovered,
WidgetState::Pressed,
WidgetState::Focused,
] {
let r = composite_contrast(c.text_on_accent, filled_fill(state, &c));
assert!(
r >= 7.0,
"{name}, window active {window_active}, {state:?}: {r:.2}"
);
}
}
}
}
#[test]
fn accent_is_m3_primary() {
assert_eq!(light().colors.accent, Color::from_hex("#6750A4"));
assert_eq!(dark().colors.accent, Color::from_hex("#D0BCFF"));
}
#[test]
fn body_is_m3_body_medium_size() {
assert!((light().typography.body.size - 14.0).abs() < 0.01);
}
#[test]
fn typography_tracks_letters() {
assert!(light().typography.body.letter_spacing > 0.0);
}
#[test]
fn palette_extension_present() {
assert!(light().extension::<Material3Palette>().is_some());
assert!(dark().extension::<Material3Palette>().is_some());
let p = dark().extension::<Material3Palette>().copied().unwrap();
assert_eq!(p.tertiary, Color::from_hex("#EFB8C8"));
}
const NOT_TRANSCRIBED: &str =
"the M3 component is not transcribed; the recipe paints with the M3-mapped colour roles";
const NO_M3_COMPONENT: &str =
"M3 specifies no such component; the recipe paints with the M3-mapped colour roles";
const MATERIAL3_UNFILLED: &[(&str, &str)] = &[
("split_button", NOT_TRANSCRIBED),
("splitter", NO_M3_COMPONENT),
("icon_button", NOT_TRANSCRIBED),
("checkbox", NOT_TRANSCRIBED),
("radio", NOT_TRANSCRIBED),
("radio_tile", NO_M3_COMPONENT),
("slider", NOT_TRANSCRIBED),
("text_input", NOT_TRANSCRIBED),
("combo_box", NOT_TRANSCRIBED),
("menu_item", NOT_TRANSCRIBED),
("panel", NO_M3_COMPONENT),
("chart", NO_M3_COMPONENT),
("popover", NOT_TRANSCRIBED),
("tooltip", NOT_TRANSCRIBED),
("scroll_bar", NO_M3_COMPONENT),
("standard_item", NOT_TRANSCRIBED),
("tab", NOT_TRANSCRIBED),
("dialog", NOT_TRANSCRIBED),
("snackbar", NOT_TRANSCRIBED),
("toast", NO_M3_COMPONENT),
("banner", NO_M3_COMPONENT),
("badge", NOT_TRANSCRIBED),
("progress_bar", NOT_TRANSCRIBED),
("link", NO_M3_COMPONENT),
("segmented_control", NOT_TRANSCRIBED),
("avatar", NO_M3_COMPONENT),
("calendar", NOT_TRANSCRIBED),
("color_picker", NO_M3_COMPONENT),
("spin_box", NO_M3_COMPONENT),
(
"date_edit",
"the recipe is a passthrough around the TextInput, so it follows whatever the text_input slot paints",
),
("search_field", NOT_TRANSCRIBED),
("rich_text_editor", NO_M3_COMPONENT),
("table", NO_M3_COMPONENT),
(
"list_container",
"the recipe is only the drag-insertion line, drawn in the M3-mapped accent role",
),
("drop_zone", NO_M3_COMPONENT),
("drop_target", NO_M3_COMPONENT),
("grid_view", NO_M3_COMPONENT),
(
"web_view",
"the page paints itself; the recipe only tints the overlay shown over it, from theme roles",
),
(
"text_selection",
"its geometry is teksilo_core::text_touch's, shared with hit testing, and its colours follow the M3-mapped accent role",
),
];
#[test]
fn installed_and_unfilled_slots_partition_the_roster() {
let unfilled: BTreeSet<&str> = MATERIAL3_UNFILLED.iter().map(|(name, _)| *name).collect();
assert_eq!(
unfilled.len(),
MATERIAL3_UNFILLED.len(),
"a slot is listed twice"
);
for (name, reason) in MATERIAL3_UNFILLED {
assert!(
!reason.trim().is_empty(),
"{name} is listed without a reason"
);
}
let names: BTreeSet<&str> = ComponentStyleSlots::NAMES.iter().copied().collect();
for t in [light(), dark(), light().with_density(TargetDensity::Touch)] {
let installed: BTreeSet<&str> = t.style_slots.installed().into_iter().collect();
let both: Vec<_> = installed.intersection(&unfilled).collect();
assert!(
both.is_empty(),
"installed yet listed as unfilled: {both:?}"
);
let covered: BTreeSet<&str> = installed.union(&unfilled).copied().collect();
let missing: Vec<_> = names.difference(&covered).collect();
let unknown: Vec<_> = covered.difference(&names).collect();
assert!(
missing.is_empty() && unknown.is_empty(),
"neither installed nor listed: {missing:?}; not a slot: {unknown:?}"
);
}
}
#[test]
fn dark_elevation_is_stronger_than_light() {
assert!(dark().shape.shadow_lg.color.a() > light().shape.shadow_lg.color.a());
}
#[test]
fn motion_inherits_desktop_tooltip_delays() {
use std::time::Duration;
for t in [light(), dark()] {
assert_eq!(t.motion.tooltip_delay, Duration::from_millis(500));
assert_eq!(t.motion.tooltip_delay_heavy, Duration::from_millis(700));
assert_eq!(t.motion.tooltip_reshow_delay, Duration::from_millis(100));
}
}
#[test]
fn a_density_switch_keeps_material3s_own_48_dp_target() {
let base = light();
let before = base
.style_slots
.button
.clone()
.expect("button slot installed");
let touch = base.with_density(TargetDensity::Touch);
let after = touch
.style_slots
.button
.clone()
.expect("button slot survives");
assert_eq!(touch.input.density, TargetDensity::Touch);
assert_eq!(
touch.input.target_size, 48.0,
"the projection must use Material 3's ladder, not `InputTokens::for_density`"
);
assert!(
!Rc::ptr_eq(&before, &after),
"the projection must rebuild the preset's own slots, not carry them across"
);
let compact =
styles::button::m3_button_style(&InputTokens::for_density(TargetDensity::Compact));
let touched = styles::button::m3_button_style(&input_tokens(TargetDensity::Touch));
let variant = teksilo_core::styles::ButtonVariant::Filled;
assert_eq!(
compact.recipes[&variant].min_size.height, 40.0,
"M3's own 40 dp button at Compact"
);
assert_eq!(
touched.recipes[&variant].min_size.height, 48.0,
"M3's 48 dp touch target, not the generic 44"
);
assert_eq!(touch.id, base.id);
assert!(touch.extension::<Material3Palette>().is_some());
}
}