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};
pub fn light() -> Theme {
let mut theme = intui::light().with_id("material3.light");
apply_material3_overrides(&mut theme, ThemeAppearance::Light);
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.extensions.insert(if light {
Material3Palette::light()
} else {
Material3Palette::dark()
});
theme.style_slots.button = Some(Rc::new(styles::button::m3_button_style()));
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 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();
assert_eq!(
style.label_roles.get(&ButtonVariant::Destructive),
Some(&TextRole::OnError),
);
}
#[test]
fn filled_button_hover_is_a_state_layer() {
use teksilo_core::styles::{ButtonVariant, FillRecipe};
let style = styles::button::m3_button_style();
let filled = &style.recipes[&ButtonVariant::Filled];
assert!(matches!(
filled.fill.hover,
Some(FillRecipe::StateLayer { .. })
));
}
#[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"));
}
#[test]
fn widget_style_slots_installed() {
for t in [light(), dark()] {
assert!(t.style_slots.button.is_some(), "button slot");
assert!(t.style_slots.toggle.is_some(), "toggle slot");
assert!(t.style_slots.card.is_some(), "card slot");
}
}
#[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));
}
}
}