1mod color;
105pub mod motion;
106pub mod palette;
107pub mod shape;
108pub mod styles;
109pub mod typography;
110
111pub use palette::{MacOsAccentRamp, MacOsBezel, MacOsPalette, SystemAccent};
112
113use std::rc::Rc;
114
115use teksilo_core::presets::intui;
116use teksilo_core::styles::{Theme, ThemeAppearance};
117use teksilo_tokens::{Color, InputTokens, TargetDensity};
118
119pub fn light() -> Theme {
121 build(ThemeAppearance::Light, MacOsPalette::light())
122}
123
124pub fn dark() -> Theme {
126 build(ThemeAppearance::Dark, MacOsPalette::dark())
127}
128
129pub fn light_with_accent(accent: Color) -> Theme {
137 build(
138 ThemeAppearance::Light,
139 MacOsPalette::light_with_accent(light_ramp_for(accent)),
140 )
141}
142
143pub fn dark_with_accent(accent: Color) -> Theme {
145 build(
146 ThemeAppearance::Dark,
147 MacOsPalette::dark_with_accent(dark_ramp_for(accent)),
148 )
149}
150
151fn light_ramp_for(accent: Color) -> MacOsAccentRamp {
155 let default = MacOsAccentRamp::system_blue_light();
156 if accent == default.base {
157 default
158 } else {
159 MacOsAccentRamp::light_from_base(accent)
160 }
161}
162
163fn dark_ramp_for(accent: Color) -> MacOsAccentRamp {
164 let default = MacOsAccentRamp::system_blue_dark();
165 if accent == default.base {
166 default
167 } else {
168 MacOsAccentRamp::dark_from_base(accent)
169 }
170}
171
172fn build(appearance: ThemeAppearance, palette: MacOsPalette) -> Theme {
173 let light = appearance == ThemeAppearance::Light;
174 let mut theme = if light {
178 intui::light().with_id("macos.light")
179 } else {
180 intui::dark().with_id("macos.dark")
181 };
182
183 theme.colors = if light {
184 color::macos_light_colors(&palette)
185 } else {
186 color::macos_dark_colors(&palette)
187 };
188 theme.shape = if light {
189 shape::macos_light_shape()
190 } else {
191 shape::macos_dark_shape()
192 };
193 theme.typography = typography::macos_typography();
194 theme.motion = motion::macos_motion();
195 theme.extensions.insert(palette);
196
197 install_styles(&mut theme);
198 theme
199 .extensions
200 .insert(teksilo_core::styles::DensityProjection(reproject));
201 theme
202}
203
204fn reproject(base: &Theme, density: TargetDensity) -> Theme {
217 let mut theme = base.clone();
218 theme.input = InputTokens::for_density(density);
219 install_styles(&mut theme);
220 theme
221}
222
223fn install_styles(theme: &mut Theme) {
227 let tokens = theme.input;
230 let slots = &mut theme.style_slots;
231
232 slots.button = Some(Rc::new(styles::button::MacOsButtonStyle));
234 slots.toggle = Some(Rc::new(styles::toggle::MacOsToggleStyle));
235 slots.checkbox = Some(Rc::new(styles::checkbox::MacOsCheckboxStyle));
236 slots.radio = Some(Rc::new(styles::radio::MacOsRadioStyle));
237 slots.text_input = Some(Rc::new(styles::text_input::MacOsTextInputStyle));
238 slots.slider = Some(Rc::new(styles::slider::MacOsSliderStyle));
239 slots.menu_item = Some(Rc::new(styles::menu_item::MacOsMenuItemStyle::for_tokens(
240 &tokens,
241 )));
242 slots.standard_item = Some(Rc::new(styles::standard_item::MacOsStandardItemStyle));
243
244 slots.card = Some(Rc::new(styles::metrics::MacOsCardStyle));
246 slots.panel = Some(Rc::new(styles::metrics::macos_panel_style_for(&tokens)));
247 slots.popover = Some(Rc::new(styles::metrics::macos_popover_style_for(&tokens)));
248 slots.tooltip = Some(Rc::new(styles::metrics::macos_tooltip_style_for(&tokens)));
249 slots.dialog = Some(Rc::new(styles::metrics::macos_dialog_style_for(&tokens)));
250 slots.snackbar = Some(Rc::new(styles::metrics::macos_snackbar_style_for(&tokens)));
251 slots.toast = Some(Rc::new(styles::metrics::macos_toast_style_for(&tokens)));
252 slots.banner = Some(Rc::new(styles::metrics::macos_banner_style_for(&tokens)));
253 slots.combo_box = Some(Rc::new(styles::metrics::macos_combo_box_style_for(&tokens)));
254 slots.icon_button = Some(Rc::new(styles::metrics::macos_icon_button_style_for(
255 &tokens,
256 )));
257 slots.link = Some(Rc::new(styles::metrics::macos_link_style_for(&tokens)));
258 slots.segmented_control = Some(Rc::new(styles::metrics::macos_segmented_control_style_for(
259 &tokens,
260 )));
261 slots.badge = Some(Rc::new(styles::metrics::macos_badge_style_for(&tokens)));
262 slots.progress_bar = Some(Rc::new(styles::metrics::macos_progress_bar_style_for(
263 &tokens,
264 )));
265 slots.scroll_bar = Some(Rc::new(styles::metrics::macos_scroll_bar_style_for(
266 &tokens,
267 )));
268 slots.tab = Some(Rc::new(styles::metrics::macos_tab_style_for(&tokens)));
269 slots.table = Some(Rc::new(styles::metrics::macos_table_style_for(&tokens)));
270 slots.calendar = Some(Rc::new(styles::metrics::macos_calendar_style_for(&tokens)));
271 slots.search_field = Some(Rc::new(styles::metrics::macos_search_field_style_for(
272 &tokens,
273 )));
274 slots.avatar = Some(Rc::new(styles::metrics::macos_avatar_style_for(&tokens)));
275}
276
277#[cfg(test)]
278mod tests {
279 use super::*;
280 use crate::palette::hex;
281 use std::collections::BTreeSet;
282 use teksilo_core::styles::ComponentStyleSlots;
283
284 #[test]
285 fn light_reports_light_appearance() {
286 assert_eq!(light().appearance, ThemeAppearance::Light);
287 assert!(!light().is_dark());
288 }
289
290 #[test]
291 fn dark_reports_dark_appearance() {
292 assert_eq!(dark().appearance, ThemeAppearance::Dark);
293 assert!(dark().is_dark());
294 }
295
296 #[test]
297 fn ids_are_namespaced() {
298 assert_eq!(light().id.as_str(), "macos.light");
299 assert_eq!(dark().id.as_str(), "macos.dark");
300 assert_eq!(light_with_accent(Color::WHITE).id.as_str(), "macos.light");
303 assert_eq!(dark_with_accent(Color::WHITE).id.as_str(), "macos.dark");
304 }
305
306 #[test]
307 fn surfaces_are_the_appkit_window_backgrounds() {
308 assert_eq!(light().colors.surface_main, hex("#ECECEC"));
309 assert_eq!(dark().colors.surface_main, hex("#323232"));
310 assert_ne!(light().colors.surface_main, dark().colors.surface_main);
311 }
312
313 #[test]
314 fn the_accent_is_the_selection_shade_not_the_raw_control_accent() {
315 assert_eq!(light().colors.accent, hex("#0063E1"));
317 assert_eq!(dark().colors.accent, hex("#0058D0"));
318 assert_eq!(light().colors.focus_ring, SystemAccent::Blue.light());
319 assert_eq!(dark().colors.focus_ring, SystemAccent::Blue.dark());
320 assert_eq!(light().colors.text_on_accent, Color::WHITE);
321 assert_eq!(dark().colors.text_on_accent, Color::WHITE);
322 }
323
324 #[test]
326 fn the_focus_indicator_is_the_accent_unlike_fluent() {
327 for t in [light(), dark()] {
328 assert_eq!(t.colors.focus_ring, t.colors.border_focused);
329 assert_eq!(
332 t.colors.focus_ring,
333 t.extension::<MacOsPalette>().unwrap().accent_ramp.base
334 );
335 }
336 }
337
338 #[test]
339 fn geometry_is_the_two_measured_radii() {
340 for t in [light(), dark()] {
341 assert_eq!(t.shape.radius_control, 6.0);
342 assert_eq!(t.shape.radius_popup, 10.0);
343 assert_eq!(t.shape.focus_ring_offset, 0.0);
344 }
345 }
346
347 #[test]
348 fn body_is_the_published_macos_ramp_with_signed_tracking() {
349 let t = light();
350 assert!((t.typography.body.size - 13.0).abs() < 0.01);
351 assert!(t.typography.body.letter_spacing < 0.0);
352 assert_eq!(t.typography.small.letter_spacing, 0.0);
353 assert!(t.typography.tiny.letter_spacing > 0.0);
354 }
355
356 #[test]
357 fn motion_uses_the_core_animation_default() {
358 use std::time::Duration;
359 for t in [light(), dark()] {
360 assert_eq!(t.motion.duration_normal, Duration::from_millis(250));
361 }
362 }
363
364 #[test]
365 fn palette_extension_present_and_appearance_matched() {
366 let l = light().extension::<MacOsPalette>().copied().unwrap();
367 let d = dark().extension::<MacOsPalette>().copied().unwrap();
368 assert_eq!(l.window_background, hex("#ECECEC"));
370 assert_eq!(d.window_background, hex("#323232"));
371 assert!(l.bezel.inner_light.a() == 0.0);
372 assert!(d.bezel.inner_light.a() > 0.0);
373 }
374
375 const MACOS_UNFILLED: &[(&str, &str)] = &[
378 (
379 "split_button",
380 "no AppKit split-button bezel is transcribed; the recipe frame paints from the theme surface and border roles",
381 ),
382 (
383 "splitter",
384 "the recipe's thin resting divider in the theme border role already reads as a split-view divider",
385 ),
386 (
387 "radio_tile",
388 "AppKit has no selectable card; the recipe surface reads the preset's surface and accent roles",
389 ),
390 (
391 "chart",
392 "AppKit has no chart chrome; the teksilo-charts default reads the series colours and the theme border role",
393 ),
394 (
395 "color_picker",
396 "NSColorPanel is a system panel with no in-window chrome to transcribe; the recipe reads the theme roles",
397 ),
398 (
399 "spin_box",
400 "no NSStepper chrome is transcribed; the recipe frames the widget's field and step buttons in theme roles",
401 ),
402 (
403 "date_edit",
404 "the recipe is a passthrough around the TextInput, which the text_input slot already styles",
405 ),
406 (
407 "rich_text_editor",
408 "the recipe frames the viewport in theme border and surface roles; the text is document content",
409 ),
410 (
411 "list_container",
412 "the recipe is only the drag-insertion line, drawn in the accent role this preset maps",
413 ),
414 (
415 "drop_zone",
416 "AppKit has no drop-zone control; the recipe tints its region from the theme roles",
417 ),
418 (
419 "drop_target",
420 "AppKit has no drop-target control; the recipe border reads the theme roles and its hints are cards, which the card slot styles",
421 ),
422 (
423 "grid_view",
424 "the recipe's focus ring, marquee and insertion bar use the focused and accent roles this preset maps",
425 ),
426 (
427 "web_view",
428 "the page paints itself; the recipe only tints the overlay shown over it, from theme roles",
429 ),
430 (
431 "text_selection",
432 "touch chrome on a platform without touch; its geometry is teksilo_core::text_touch's, shared with hit testing",
433 ),
434 ];
435
436 #[test]
440 fn installed_and_unfilled_slots_partition_the_roster() {
441 let unfilled: BTreeSet<&str> = MACOS_UNFILLED.iter().map(|(name, _)| *name).collect();
442 assert_eq!(
443 unfilled.len(),
444 MACOS_UNFILLED.len(),
445 "a slot is listed twice"
446 );
447 for (name, reason) in MACOS_UNFILLED {
448 assert!(
449 !reason.trim().is_empty(),
450 "{name} is listed without a reason"
451 );
452 }
453 let names: BTreeSet<&str> = ComponentStyleSlots::NAMES.iter().copied().collect();
454 for t in [light(), dark(), light().with_density(TargetDensity::Touch)] {
455 let installed: BTreeSet<&str> = t.style_slots.installed().into_iter().collect();
456 let both: Vec<_> = installed.intersection(&unfilled).collect();
457 assert!(
458 both.is_empty(),
459 "installed yet listed as unfilled: {both:?}"
460 );
461 let covered: BTreeSet<&str> = installed.union(&unfilled).copied().collect();
462 let missing: Vec<_> = names.difference(&covered).collect();
463 let unknown: Vec<_> = covered.difference(&names).collect();
464 assert!(
465 missing.is_empty() && unknown.is_empty(),
466 "neither installed nor listed: {missing:?}; not a slot: {unknown:?}"
467 );
468 }
469 }
470
471 #[test]
472 fn custom_accent_rebuilds_the_whole_accent_family() {
473 let seed = SystemAccent::Purple.light();
474 let t = light_with_accent(seed);
475 let base = light();
476 assert_ne!(t.colors.accent, base.colors.accent);
477 assert_ne!(t.colors.focus_ring, base.colors.focus_ring);
478 assert_ne!(
479 t.colors.selection_bg_active,
480 base.colors.selection_bg_active
481 );
482 assert_eq!(t.colors.surface_main, base.colors.surface_main);
484 assert_eq!(t.colors.text_primary, base.colors.text_primary);
485 assert_eq!(t.colors.text_link, base.colors.text_link);
487 let p = t.extension::<MacOsPalette>().copied().unwrap();
489 assert_eq!(p.accent_ramp.base, seed);
490 }
491
492 #[test]
493 fn passing_the_default_accent_reproduces_the_measured_ramp() {
494 assert_eq!(
495 light_with_accent(SystemAccent::Blue.light()).colors.accent,
496 light().colors.accent
497 );
498 assert_eq!(
499 dark_with_accent(SystemAccent::Blue.dark()).colors.accent,
500 dark().colors.accent
501 );
502 }
503
504 #[test]
505 fn every_system_accent_builds_a_usable_theme() {
506 for accent in SystemAccent::ALL {
507 for t in [
508 light_with_accent(accent.light()),
509 dark_with_accent(accent.dark()),
510 ] {
511 let c = &t.colors;
512 assert!(
513 crate::palette::over(c.text_on_accent, c.accent).contrast_ratio(c.accent)
514 >= 4.5,
515 "{accent:?}: an accent-filled label is unreadable"
516 );
517 assert!(t.style_slots.button.is_some());
519 }
520 }
521 }
522
523 #[test]
524 fn the_theme_survives_the_window_inactive_projection() {
525 for t in [light(), dark()] {
529 let inactive = t.for_inactive_window();
530 assert_ne!(inactive.colors.accent, t.colors.accent);
531 assert_ne!(inactive.colors.focus_ring, t.colors.focus_ring);
532 assert_eq!(inactive.colors.surface_main, t.colors.surface_main);
533 }
534 }
535 #[test]
542 fn a_density_switch_re_derives_the_installed_slots() {
543 let base = light();
544 let before = base
545 .style_slots
546 .standard_item
547 .clone()
548 .expect("slot installed");
549
550 let touch = base.with_density(TargetDensity::Touch);
551 let after = touch
552 .style_slots
553 .standard_item
554 .clone()
555 .expect("slot survives");
556
557 assert_eq!(touch.input.density, TargetDensity::Touch);
558 assert!(
559 !Rc::ptr_eq(&before, &after),
560 "the projection must rebuild the preset's own slots, not carry them across"
561 );
562
563 let compact = styles::standard_item::macos_standard_item_recipe_for(
564 &InputTokens::for_density(TargetDensity::Compact),
565 );
566 let touched = styles::standard_item::macos_standard_item_recipe_for(
567 &InputTokens::for_density(TargetDensity::Touch),
568 );
569 assert_eq!(
570 compact.min_height_single_line, 24.0,
571 "`NSTableView.rowHeight` on macOS 11+"
572 );
573 assert_eq!(touched.min_height_single_line, 44.0, "the Touch target");
574
575 assert_eq!(touch.id, base.id);
576 assert_eq!(touch.colors, base.colors);
577 assert!(touch.extension::<MacOsPalette>().is_some());
578 }
579
580 #[test]
584 fn a_density_switch_reprojects_the_menu_metrics() {
585 use teksilo_core::styles::MenuItemStyle;
586
587 let base = light();
588 let metrics = |t: &Theme| {
589 t.style_slots
590 .menu_item
591 .as_ref()
592 .expect("slot installed")
593 .metrics()
594 };
595 for density in [
596 TargetDensity::Compact,
597 TargetDensity::Comfortable,
598 TargetDensity::Touch,
599 ] {
600 let t = base.with_density(density);
601 assert_eq!(
602 metrics(&t),
603 styles::menu_item::MacOsMenuItemStyle::for_tokens(&InputTokens::for_density(
604 density
605 ))
606 .metrics(),
607 "{density:?}",
608 );
609 }
610 let touch = metrics(&base.with_density(TargetDensity::Touch));
611 let compact = metrics(&base);
612 assert!(touch.separator_height > compact.separator_height);
613 assert!(touch.trailing_column_width > compact.trailing_column_width);
614 assert_eq!(touch.item_height, compact.item_height);
616 let back = base
617 .with_density(TargetDensity::Touch)
618 .with_density(TargetDensity::Compact);
619 assert_eq!(metrics(&back), compact);
620 }
621}