1use std::sync::OnceLock;
2
3use sui_core::Color;
4use sui_layout::Padding as Insets;
5use sui_text::{FontFamilyStack, TextStyle};
6
7use crate::animation::{AnimationSpec, Easing, Stagger};
8use crate::hdr_theme::{
9 HdrThemeTokens, WidgetColorRole, WidgetEffectRole, WidgetLuminanceRole, WidgetMaterialRole,
10 resolve_widget_hdr_style,
11};
12
13#[derive(Debug, Clone, Copy, PartialEq)]
25pub struct ThemeMotion {
26 pub duration_instant: f32,
28 pub duration_fast: f32,
30 pub duration_normal: f32,
32 pub duration_slow: f32,
34 pub duration_slower: f32,
36 pub easing_standard: Easing,
39 pub easing_emphasized: Easing,
41 pub easing_decelerate: Easing,
44 pub easing_accelerate: Easing,
47}
48
49impl ThemeMotion {
50 pub const fn standard() -> Self {
52 Self {
53 duration_instant: 0.0,
54 duration_fast: 0.07,
55 duration_normal: 0.14,
56 duration_slow: 0.22,
57 duration_slower: 0.34,
58 easing_standard: Easing::CubicBezier {
59 x1: 0.2,
60 y1: 0.0,
61 x2: 0.0,
62 y2: 1.0,
63 },
64 easing_emphasized: Easing::CubicBezier {
65 x1: 0.45,
66 y1: 0.0,
67 x2: 0.15,
68 y2: 1.0,
69 },
70 easing_decelerate: Easing::CubicBezier {
71 x1: 0.2,
72 y1: 0.0,
73 x2: 0.0,
74 y2: 1.0,
75 },
76 easing_accelerate: Easing::CubicBezier {
77 x1: 0.45,
78 y1: 0.0,
79 x2: 1.0,
80 y2: 1.0,
81 },
82 }
83 }
84
85 pub fn hover_duration(&self) -> f64 {
86 f64::from(self.duration_fast)
87 }
88
89 pub fn press_duration(&self) -> f64 {
90 f64::from(self.duration_fast)
91 }
92
93 pub fn focus_duration(&self) -> f64 {
94 f64::from(self.duration_normal)
95 }
96
97 pub fn toggle_duration(&self) -> f64 {
98 f64::from(self.duration_normal)
99 }
100
101 pub fn entrance_duration(&self) -> f64 {
102 f64::from(self.duration_normal)
103 }
104
105 pub fn tab_switch_duration(&self) -> f64 {
106 f64::from(self.duration_fast)
107 }
108
109 pub const fn hover_easing(&self) -> Easing {
110 self.easing_standard
111 }
112
113 pub const fn press_easing(&self) -> Easing {
114 self.easing_standard
115 }
116
117 pub const fn focus_easing(&self) -> Easing {
118 self.easing_decelerate
119 }
120
121 pub const fn toggle_easing(&self) -> Easing {
122 self.easing_emphasized
123 }
124
125 pub const fn entrance_easing(&self) -> Easing {
126 self.easing_decelerate
127 }
128
129 pub const fn tab_switch_easing(&self) -> Easing {
130 self.easing_standard
131 }
132
133 pub fn hover_spec(&self) -> AnimationSpec {
136 AnimationSpec::tween(self.hover_duration(), self.hover_easing())
137 }
138
139 pub fn press_spec(&self) -> AnimationSpec {
140 AnimationSpec::tween(self.press_duration(), self.press_easing())
141 }
142
143 pub fn focus_spec(&self) -> AnimationSpec {
144 AnimationSpec::tween(self.focus_duration(), self.focus_easing())
145 }
146
147 pub fn toggle_spec(&self) -> AnimationSpec {
148 AnimationSpec::tween(self.toggle_duration(), self.toggle_easing())
149 }
150
151 pub fn entrance_spec(&self) -> AnimationSpec {
152 AnimationSpec::tween(self.entrance_duration(), self.entrance_easing())
153 }
154
155 pub fn tab_switch_spec(&self) -> AnimationSpec {
156 AnimationSpec::tween(self.tab_switch_duration(), self.tab_switch_easing())
157 }
158
159 pub fn exit_spec(&self) -> AnimationSpec {
161 AnimationSpec::tween(f64::from(self.duration_normal), self.easing_accelerate)
162 }
163
164 pub fn layout_spec(&self) -> AnimationSpec {
167 AnimationSpec::tween(f64::from(self.duration_slow), self.easing_standard)
168 }
169
170 pub fn stagger(&self) -> Stagger {
173 let interval = f64::from(self.duration_fast) * 0.5;
174 Stagger::new(interval).max_delay(f64::from(self.duration_slow))
175 }
176}
177
178impl Default for ThemeMotion {
179 fn default() -> Self {
180 Self::standard()
181 }
182}
183
184#[derive(Debug, Clone, Copy, PartialEq, Eq)]
185pub struct ThemeFontStack {
186 pub primary: &'static str,
187 pub fallbacks: &'static [&'static str],
188}
189
190impl From<ThemeFontStack> for FontFamilyStack {
191 fn from(value: ThemeFontStack) -> Self {
192 Self::new(value.primary, value.fallbacks)
193 }
194}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197pub struct ThemeFontFamilies {
198 pub sans: ThemeFontStack,
199 pub serif: ThemeFontStack,
200 pub mono: ThemeFontStack,
201}
202
203impl Default for ThemeFontFamilies {
204 fn default() -> Self {
205 Self {
206 sans: ThemeFontStack {
207 primary: "ui-sans-serif",
208 fallbacks: &[
209 "system-ui",
210 "sans-serif",
211 "Apple Color Emoji",
212 "Segoe UI Emoji",
213 "Segoe UI Symbol",
214 "Noto Color Emoji",
215 ],
216 },
217 serif: ThemeFontStack {
218 primary: "ui-serif",
219 fallbacks: &["Georgia", "Cambria", "Times New Roman", "Times", "serif"],
220 },
221 mono: ThemeFontStack {
222 primary: "ui-monospace",
223 fallbacks: &[
224 "SFMono-Regular",
225 "Menlo",
226 "Monaco",
227 "Consolas",
228 "Liberation Mono",
229 "Courier New",
230 "monospace",
231 ],
232 },
233 }
234 }
235}
236
237#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
238pub enum ThemeColorScheme {
239 #[default]
240 Light,
241 Dark,
242 HighContrast,
243}
244
245impl ThemeColorScheme {
246 pub const fn is_dark(self) -> bool {
248 matches!(self, Self::Dark | Self::HighContrast)
249 }
250}
251
252#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
253pub enum ThemeDensity {
254 Compact,
255 #[default]
256 Comfortable,
257 Touch,
258}
259
260impl ThemeDensity {
261 fn pick<T>(self, compact: T, comfortable: T, touch: T) -> T {
263 match self {
264 Self::Compact => compact,
265 Self::Comfortable => comfortable,
266 Self::Touch => touch,
267 }
268 }
269}
270
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
280pub enum ControlSize {
281 Small,
282 #[default]
283 Medium,
284 Large,
285}
286
287impl ControlSize {
288 const fn legacy_density(self) -> ThemeDensity {
289 match self {
290 Self::Small => ThemeDensity::Compact,
291 Self::Medium => ThemeDensity::Comfortable,
292 Self::Large => ThemeDensity::Touch,
293 }
294 }
295
296 const fn control_height(self) -> f32 {
297 match self {
298 Self::Small => 28.0,
299 Self::Medium => 32.0,
300 Self::Large => 40.0,
301 }
302 }
303
304 const fn row_height(self) -> f32 {
305 match self {
306 Self::Small => 24.0,
307 Self::Medium => 28.0,
308 Self::Large => 36.0,
309 }
310 }
311
312 const fn icon_size(self) -> f32 {
313 match self {
314 Self::Small => 13.0,
315 Self::Medium => 15.0,
316 Self::Large => 17.0,
317 }
318 }
319}
320
321#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
322pub enum SemanticTone {
323 #[default]
324 Neutral,
325 Accent,
326 Info,
327 Success,
328 Warning,
329 Danger,
330}
331
332#[derive(Debug, Clone, Copy, PartialEq)]
337pub struct NeutralRamp {
338 pub window: Color,
340 pub subtle: Color,
342 pub panel: Color,
344 pub overlay: Color,
347 pub control: Color,
349 pub control_hover: Color,
350 pub control_active: Color,
351 pub button: Color,
353 pub button_hover: Color,
354 pub button_active: Color,
355 pub field: Color,
357 pub border_subtle: Color,
359 pub border: Color,
361 pub border_strong: Color,
363 pub border_control: Color,
366 pub text: Color,
367 pub text_secondary: Color,
368 pub text_tertiary: Color,
370 pub text_disabled: Color,
371}
372
373impl NeutralRamp {
374 pub fn light() -> Self {
376 Self {
377 window: Color::WHITE,
378 subtle: rgb8(250, 250, 250),
379 panel: Color::WHITE,
380 overlay: Color::WHITE,
381 control: rgb8(245, 245, 245),
382 control_hover: rgb8(235, 235, 235),
383 control_active: rgb8(224, 224, 224),
384 button: Color::WHITE,
385 button_hover: rgb8(245, 245, 245),
386 button_active: rgb8(235, 235, 235),
387 field: Color::WHITE,
388 border_subtle: rgb8(240, 240, 240),
389 border: rgb8(229, 229, 229),
390 border_strong: rgb8(212, 212, 212),
391 border_control: rgb8(140, 140, 140),
392 text: rgb8(23, 23, 23),
393 text_secondary: rgb8(82, 82, 82),
394 text_tertiary: rgb8(115, 115, 115),
395 text_disabled: rgb8(163, 163, 163),
396 }
397 }
398
399 pub fn dark() -> Self {
402 Self {
403 window: rgb8(9, 13, 20),
404 subtle: rgb8(13, 18, 25),
405 panel: rgb8(18, 23, 30),
406 overlay: rgb8(25, 30, 37),
407 control: rgb8(28, 33, 40),
408 control_hover: rgb8(37, 42, 50),
409 control_active: rgb8(43, 49, 57),
410 button: rgb8(28, 33, 40),
411 button_hover: rgb8(37, 42, 50),
412 button_active: rgb8(43, 49, 57),
413 field: rgb8(13, 18, 25),
414 border_subtle: rgb8(31, 37, 45),
415 border: rgb8(43, 50, 60),
416 border_strong: rgb8(59, 66, 76),
417 border_control: rgb8(108, 116, 127),
418 text: rgb8(238, 240, 243),
419 text_secondary: rgb8(185, 190, 198),
420 text_tertiary: rgb8(142, 148, 158),
421 text_disabled: rgb8(87, 93, 101),
422 }
423 }
424
425 pub fn neutral_dark() -> Self {
427 Self {
428 window: rgb8(13, 13, 13),
429 subtle: rgb8(18, 18, 18),
430 panel: rgb8(23, 23, 23),
431 overlay: rgb8(30, 30, 30),
432 control: rgb8(33, 33, 33),
433 control_hover: rgb8(42, 42, 42),
434 control_active: rgb8(48, 48, 48),
435 button: rgb8(33, 33, 33),
436 button_hover: rgb8(42, 42, 42),
437 button_active: rgb8(48, 48, 48),
438 field: rgb8(18, 18, 18),
439 border_subtle: rgb8(36, 36, 36),
440 border: rgb8(49, 49, 49),
441 border_strong: rgb8(65, 65, 65),
442 border_control: rgb8(115, 115, 115),
443 text: rgb8(240, 240, 240),
444 text_secondary: rgb8(190, 190, 190),
445 text_tertiary: rgb8(148, 148, 148),
446 text_disabled: rgb8(92, 92, 92),
447 }
448 }
449
450 pub fn void() -> Self {
454 Self {
455 window: Color::BLACK,
456 subtle: Color::BLACK,
457 panel: Color::BLACK,
458 overlay: rgb8(12, 17, 24),
459 control: rgb8(15, 20, 27),
460 control_hover: rgb8(25, 30, 38),
461 control_active: rgb8(32, 38, 46),
462 button: rgb8(15, 20, 27),
463 button_hover: rgb8(25, 30, 38),
464 button_active: rgb8(32, 38, 46),
465 field: rgb8(7, 11, 18),
466 border_subtle: rgb8(21, 26, 33),
467 border: rgb8(36, 43, 52),
468 border_strong: rgb8(52, 59, 69),
469 border_control: rgb8(104, 111, 123),
470 text: rgb8(228, 230, 234),
471 text_secondary: rgb8(177, 182, 190),
472 text_tertiary: rgb8(137, 144, 153),
473 text_disabled: rgb8(82, 87, 95),
474 }
475 }
476}
477
478#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
482pub enum DecorativeHue {
483 Red,
484 Orange,
485 Amber,
486 Green,
487 Teal,
488 Cyan,
489 Blue,
490 Violet,
491 Magenta,
492}
493
494impl DecorativeHue {
495 pub const ALL: [Self; 9] = [
496 Self::Red,
497 Self::Orange,
498 Self::Amber,
499 Self::Green,
500 Self::Teal,
501 Self::Cyan,
502 Self::Blue,
503 Self::Violet,
504 Self::Magenta,
505 ];
506}
507
508#[derive(Debug, Clone, Copy, PartialEq)]
511pub struct DecorativeColors {
512 pub red: Color,
513 pub orange: Color,
514 pub amber: Color,
515 pub green: Color,
516 pub teal: Color,
517 pub cyan: Color,
518 pub blue: Color,
519 pub violet: Color,
520 pub magenta: Color,
521}
522
523impl DecorativeColors {
524 pub fn light() -> Self {
527 Self {
528 red: rgb8(234, 60, 63),
529 orange: rgb8(215, 105, 0),
530 amber: rgb8(223, 156, 0),
531 green: rgb8(0, 158, 72),
532 teal: rgb8(0, 155, 139),
533 cyan: rgb8(0, 150, 175),
534 blue: rgb8(22, 122, 250),
535 violet: rgb8(135, 93, 240),
536 magenta: rgb8(196, 70, 189),
537 }
538 }
539
540 pub fn dark() -> Self {
543 Self {
544 red: rgb8(255, 113, 107),
545 orange: rgb8(255, 138, 55),
546 amber: rgb8(252, 177, 0),
547 green: rgb8(55, 209, 108),
548 teal: rgb8(0, 208, 187),
549 cyan: rgb8(0, 201, 234),
550 blue: rgb8(104, 165, 255),
551 violet: rgb8(168, 143, 255),
552 magenta: rgb8(228, 114, 220),
553 }
554 }
555
556 pub fn get(&self, hue: DecorativeHue) -> Color {
557 match hue {
558 DecorativeHue::Red => self.red,
559 DecorativeHue::Orange => self.orange,
560 DecorativeHue::Amber => self.amber,
561 DecorativeHue::Green => self.green,
562 DecorativeHue::Teal => self.teal,
563 DecorativeHue::Cyan => self.cyan,
564 DecorativeHue::Blue => self.blue,
565 DecorativeHue::Violet => self.violet,
566 DecorativeHue::Magenta => self.magenta,
567 }
568 }
569
570 pub fn set(&mut self, hue: DecorativeHue, color: Color) {
571 match hue {
572 DecorativeHue::Red => self.red = color,
573 DecorativeHue::Orange => self.orange = color,
574 DecorativeHue::Amber => self.amber = color,
575 DecorativeHue::Green => self.green = color,
576 DecorativeHue::Teal => self.teal = color,
577 DecorativeHue::Cyan => self.cyan = color,
578 DecorativeHue::Blue => self.blue = color,
579 DecorativeHue::Violet => self.violet = color,
580 DecorativeHue::Magenta => self.magenta = color,
581 }
582 }
583}
584
585const SUI_AZURE: Color = Color::rgba(23.0 / 255.0, 98.0 / 255.0, 244.0 / 255.0, 1.0);
587const SUI_VIOLET: Color = Color::rgba(125.0 / 255.0, 77.0 / 255.0, 231.0 / 255.0, 1.0);
589
590#[derive(Debug, Clone, Copy, PartialEq)]
595pub struct ThemeColors {
596 pub name: &'static str,
597 pub scheme: ThemeColorScheme,
598 pub neutrals: NeutralRamp,
599 pub primary: Color,
602 pub on_primary: Color,
603 pub secondary: Color,
605 pub on_secondary: Color,
606 pub info: Color,
607 pub on_info: Color,
608 pub success: Color,
609 pub on_success: Color,
610 pub warning: Color,
611 pub on_warning: Color,
612 pub danger: Color,
613 pub on_danger: Color,
614 pub decorative: DecorativeColors,
615}
616
617impl ThemeColors {
618 pub fn sui() -> Self {
622 Self::light()
623 }
624
625 pub fn light() -> Self {
627 Self {
628 name: "light",
629 scheme: ThemeColorScheme::Light,
630 neutrals: NeutralRamp::light(),
631 ..Self::branded_sources()
632 }
633 }
634
635 pub fn dark() -> Self {
638 Self {
639 name: "dark",
640 scheme: ThemeColorScheme::Dark,
641 neutrals: NeutralRamp::dark(),
642 decorative: DecorativeColors::dark(),
643 ..Self::branded_sources()
644 }
645 }
646
647 pub fn high_contrast() -> Self {
649 Self {
650 name: "void",
651 scheme: ThemeColorScheme::HighContrast,
652 neutrals: NeutralRamp::void(),
653 decorative: DecorativeColors::dark(),
654 ..Self::branded_sources()
655 }
656 }
657
658 pub fn neutral() -> Self {
662 Self {
663 name: "neutral",
664 scheme: ThemeColorScheme::Light,
665 neutrals: NeutralRamp::light(),
666 primary: rgb8(23, 23, 23),
667 on_primary: Color::WHITE,
668 secondary: rgb8(82, 82, 82),
669 on_secondary: Color::WHITE,
670 ..Self::branded_sources()
671 }
672 }
673
674 pub fn neutral_dark() -> Self {
676 Self {
677 name: "neutral-dark",
678 scheme: ThemeColorScheme::Dark,
679 neutrals: NeutralRamp::neutral_dark(),
680 primary: rgb8(240, 240, 240),
681 on_primary: rgb8(23, 23, 23),
682 secondary: rgb8(190, 190, 190),
683 on_secondary: rgb8(23, 23, 23),
684 decorative: DecorativeColors::dark(),
685 ..Self::branded_sources()
686 }
687 }
688
689 pub fn with_scheme(scheme: ThemeColorScheme) -> Self {
690 match scheme {
691 ThemeColorScheme::Light => Self::light(),
692 ThemeColorScheme::Dark => Self::dark(),
693 ThemeColorScheme::HighContrast => Self::high_contrast(),
694 }
695 }
696
697 fn branded_sources() -> Self {
701 Self {
702 name: "light",
703 scheme: ThemeColorScheme::Light,
704 neutrals: NeutralRamp::light(),
705 primary: SUI_AZURE,
706 on_primary: Color::WHITE,
707 secondary: SUI_VIOLET,
708 on_secondary: Color::WHITE,
709 info: rgb8(0, 166, 222),
710 on_info: rgb8(2, 42, 61),
711 success: rgb8(6, 168, 78),
712 on_success: rgb8(12, 46, 22),
713 warning: rgb8(244, 165, 0),
714 on_warning: rgb8(63, 37, 0),
715 danger: rgb8(220, 38, 39),
716 on_danger: Color::WHITE,
717 decorative: DecorativeColors::light(),
718 }
719 }
720}
721
722impl Default for ThemeColors {
723 fn default() -> Self {
724 Self::light()
725 }
726}
727
728#[derive(Debug, Clone, Copy, PartialEq)]
729pub struct ThemeBreakpoints {
730 pub sm: f32,
731 pub md: f32,
732 pub lg: f32,
733 pub xl: f32,
734 pub _2xl: f32,
735}
736
737impl Default for ThemeBreakpoints {
738 fn default() -> Self {
739 Self {
740 sm: 640.0,
741 md: 768.0,
742 lg: 1024.0,
743 xl: 1280.0,
744 _2xl: 1536.0,
745 }
746 }
747}
748
749#[derive(Debug, Clone, Copy, PartialEq)]
750pub struct ThemeContainers {
751 pub _3xs: f32,
752 pub _2xs: f32,
753 pub xs: f32,
754 pub sm: f32,
755 pub md: f32,
756 pub lg: f32,
757 pub xl: f32,
758 pub _2xl: f32,
759 pub _3xl: f32,
760 pub _4xl: f32,
761 pub _5xl: f32,
762 pub _6xl: f32,
763 pub _7xl: f32,
764}
765
766impl Default for ThemeContainers {
767 fn default() -> Self {
768 Self {
769 _3xs: 256.0,
770 _2xs: 288.0,
771 xs: 320.0,
772 sm: 384.0,
773 md: 448.0,
774 lg: 512.0,
775 xl: 576.0,
776 _2xl: 672.0,
777 _3xl: 768.0,
778 _4xl: 896.0,
779 _5xl: 1024.0,
780 _6xl: 1152.0,
781 _7xl: 1280.0,
782 }
783 }
784}
785
786#[derive(Debug, Clone, Copy, PartialEq)]
787pub struct ThemeTextToken {
788 pub size: f32,
789 pub line_height: f32,
790}
791
792#[derive(Debug, Clone, Copy, PartialEq)]
793pub struct ThemeTextScale {
794 pub xs: ThemeTextToken,
795 pub sm: ThemeTextToken,
796 pub base: ThemeTextToken,
797 pub lg: ThemeTextToken,
798 pub xl: ThemeTextToken,
799 pub _2xl: ThemeTextToken,
800 pub _3xl: ThemeTextToken,
801 pub _4xl: ThemeTextToken,
802 pub _5xl: ThemeTextToken,
803 pub _6xl: ThemeTextToken,
804 pub _7xl: ThemeTextToken,
805 pub _8xl: ThemeTextToken,
806 pub _9xl: ThemeTextToken,
807}
808
809impl Default for ThemeTextScale {
810 fn default() -> Self {
811 Self {
812 xs: ThemeTextToken {
813 size: 12.0,
814 line_height: 17.0,
815 },
816 sm: ThemeTextToken {
817 size: 13.0,
818 line_height: 18.0,
819 },
820 base: ThemeTextToken {
821 size: 15.0,
822 line_height: 22.0,
823 },
824 lg: ThemeTextToken {
825 size: 17.0,
826 line_height: 25.0,
827 },
828 xl: ThemeTextToken {
829 size: 19.0,
830 line_height: 27.0,
831 },
832 _2xl: ThemeTextToken {
833 size: 21.0,
834 line_height: 29.0,
835 },
836 _3xl: ThemeTextToken {
837 size: 25.0,
838 line_height: 33.0,
839 },
840 _4xl: ThemeTextToken {
841 size: 31.0,
842 line_height: 39.0,
843 },
844 _5xl: ThemeTextToken {
845 size: 37.0,
846 line_height: 45.0,
847 },
848 _6xl: ThemeTextToken {
849 size: 49.0,
850 line_height: 55.0,
851 },
852 _7xl: ThemeTextToken {
853 size: 61.0,
854 line_height: 67.0,
855 },
856 _8xl: ThemeTextToken {
857 size: 73.0,
858 line_height: 79.0,
859 },
860 _9xl: ThemeTextToken {
861 size: 97.0,
862 line_height: 103.0,
863 },
864 }
865 }
866}
867
868#[derive(Debug, Clone, Copy, PartialEq, Eq)]
869pub struct ThemeFontWeights {
870 pub thin: u16,
871 pub extralight: u16,
872 pub light: u16,
873 pub normal: u16,
874 pub medium: u16,
875 pub semibold: u16,
876 pub bold: u16,
877 pub extrabold: u16,
878 pub black: u16,
879}
880
881impl Default for ThemeFontWeights {
882 fn default() -> Self {
883 Self {
884 thin: 100,
885 extralight: 200,
886 light: 300,
887 normal: 400,
888 medium: 500,
889 semibold: 600,
890 bold: 700,
891 extrabold: 800,
892 black: 900,
893 }
894 }
895}
896
897#[derive(Debug, Clone, Copy, PartialEq)]
898pub struct ThemeTracking {
899 pub tighter: f32,
900 pub tight: f32,
901 pub normal: f32,
902 pub wide: f32,
903 pub wider: f32,
904 pub widest: f32,
905}
906
907impl Default for ThemeTracking {
908 fn default() -> Self {
909 Self {
910 tighter: 0.0,
911 tight: 0.0,
912 normal: 0.0,
913 wide: 0.02,
914 wider: 0.04,
915 widest: 0.06,
916 }
917 }
918}
919
920#[derive(Debug, Clone, Copy, PartialEq)]
921pub struct ThemeLeading {
922 pub tight: f32,
923 pub snug: f32,
924 pub normal: f32,
925 pub relaxed: f32,
926 pub loose: f32,
927}
928
929impl Default for ThemeLeading {
930 fn default() -> Self {
931 Self {
932 tight: 1.3,
933 snug: 1.4,
934 normal: 1.5,
935 relaxed: 1.6,
936 loose: 1.65,
937 }
938 }
939}
940
941#[derive(Debug, Clone, Copy, PartialEq)]
942pub struct ThemeRadii {
943 pub xs: f32,
944 pub sm: f32,
945 pub md: f32,
946 pub lg: f32,
947 pub xl: f32,
948 pub _2xl: f32,
949 pub _3xl: f32,
950 pub _4xl: f32,
951}
952
953impl Default for ThemeRadii {
954 fn default() -> Self {
955 Self {
956 xs: 2.0,
957 sm: 4.0,
958 md: 6.0,
959 lg: 8.0,
960 xl: 10.0,
961 _2xl: 14.0,
962 _3xl: 18.0,
963 _4xl: 999.0,
964 }
965 }
966}
967
968#[derive(Debug, Clone, Copy, PartialEq)]
969pub struct ThemeShadowLayer {
970 pub offset_x: f32,
971 pub offset_y: f32,
972 pub blur: f32,
973 pub spread: f32,
974 pub color: Color,
975 pub inset: bool,
976}
977
978#[derive(Debug, Clone, Copy, PartialEq)]
979pub struct ThemeShadow {
980 pub first: Option<ThemeShadowLayer>,
981 pub second: Option<ThemeShadowLayer>,
982}
983
984impl ThemeShadow {
985 pub const fn empty() -> Self {
986 Self {
987 first: None,
988 second: None,
989 }
990 }
991
992 pub const fn single(layer: ThemeShadowLayer) -> Self {
993 Self {
994 first: Some(layer),
995 second: None,
996 }
997 }
998
999 pub const fn double(first: ThemeShadowLayer, second: ThemeShadowLayer) -> Self {
1000 Self {
1001 first: Some(first),
1002 second: Some(second),
1003 }
1004 }
1005}
1006
1007impl ThemeShadowLayer {
1008 pub fn to_shadow_params(&self) -> sui_scene::ShadowParams {
1011 let shadow = sui_scene::ShadowParams::new(
1012 self.offset_x,
1013 self.offset_y,
1014 self.blur,
1015 self.spread,
1016 self.color,
1017 );
1018 if self.inset {
1019 shadow.with_placement(sui_scene::ShadowPlacement::Inside)
1020 } else {
1021 shadow
1022 }
1023 }
1024
1025 pub const fn is_outer(&self) -> bool {
1028 !self.inset
1029 }
1030}
1031
1032impl ThemeShadow {
1033 fn painting_order(&self) -> impl Iterator<Item = ThemeShadowLayer> {
1036 self.second.into_iter().chain(self.first)
1037 }
1038}
1039
1040pub fn paint_theme_shadow(
1051 paint: &mut sui_runtime::PaintCtx,
1052 rect: sui_core::Rect,
1053 radii: [f32; 4],
1054 shadow: &ThemeShadow,
1055) {
1056 for layer in shadow.painting_order().filter(ThemeShadowLayer::is_outer) {
1057 paint.draw_shadow(rect, radii, layer.to_shadow_params());
1058 }
1059}
1060
1061pub fn paint_theme_inset_shadow(
1067 paint: &mut sui_runtime::PaintCtx,
1068 rect: sui_core::Rect,
1069 radii: [f32; 4],
1070 shadow: &ThemeShadow,
1071) {
1072 for layer in shadow.painting_order().filter(|layer| !layer.is_outer()) {
1073 paint.draw_shadow(rect, radii, layer.to_shadow_params());
1074 }
1075}
1076
1077pub fn paint_theme_glow(
1083 paint: &mut sui_runtime::PaintCtx,
1084 rect: sui_core::Rect,
1085 radii: [f32; 4],
1086 glow: &ThemeShadow,
1087) {
1088 for layer in glow.painting_order() {
1089 paint.draw_shadow(
1090 rect,
1091 radii,
1092 layer
1093 .to_shadow_params()
1094 .with_placement(sui_scene::ShadowPlacement::Outside),
1095 );
1096 }
1097}
1098
1099#[derive(Debug, Clone, Copy, PartialEq)]
1100pub struct ThemeBoxShadowScale {
1101 pub _2xs: ThemeShadow,
1102 pub xs: ThemeShadow,
1103 pub sm: ThemeShadow,
1104 pub md: ThemeShadow,
1105 pub lg: ThemeShadow,
1106 pub xl: ThemeShadow,
1107 pub _2xl: ThemeShadow,
1108}
1109
1110#[derive(Debug, Clone, Copy, PartialEq)]
1111pub struct ThemeInsetShadowScale {
1112 pub _2xs: ThemeShadow,
1113 pub xs: ThemeShadow,
1114 pub sm: ThemeShadow,
1115}
1116
1117#[derive(Debug, Clone, Copy, PartialEq)]
1118pub struct ThemeDropShadowScale {
1119 pub xs: ThemeShadow,
1120 pub sm: ThemeShadow,
1121 pub md: ThemeShadow,
1122 pub lg: ThemeShadow,
1123 pub xl: ThemeShadow,
1124 pub _2xl: ThemeShadow,
1125}
1126
1127#[derive(Debug, Clone, Copy, PartialEq)]
1128pub struct ThemeTextShadowScale {
1129 pub _2xs: ThemeShadow,
1130 pub xs: ThemeShadow,
1131 pub sm: ThemeShadow,
1132 pub md: ThemeShadow,
1133 pub lg: ThemeShadow,
1134}
1135
1136#[derive(Debug, Clone, Copy, PartialEq)]
1137pub struct ThemeShadows {
1138 pub box_shadow: ThemeBoxShadowScale,
1139 pub inset: ThemeInsetShadowScale,
1140 pub drop: ThemeDropShadowScale,
1141 pub text: ThemeTextShadowScale,
1142}
1143
1144impl Default for ThemeShadows {
1145 fn default() -> Self {
1146 let black_005 = Color::BLACK.with_alpha(0.05);
1147 let black_01 = Color::BLACK.with_alpha(0.1);
1148 let black_012 = Color::BLACK.with_alpha(0.12);
1149 let black_015 = Color::BLACK.with_alpha(0.15);
1150 let black_02 = Color::BLACK.with_alpha(0.2);
1151 let black_025 = Color::BLACK.with_alpha(0.25);
1152 let black_075 = Color::BLACK.with_alpha(0.075);
1153
1154 Self {
1155 box_shadow: ThemeBoxShadowScale {
1156 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, black_005, false)),
1157 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, black_005, false)),
1158 sm: ThemeShadow::double(
1159 shadow_layer(0.0, 1.0, 3.0, 0.0, black_01, false),
1160 shadow_layer(0.0, 1.0, 2.0, -1.0, black_01, false),
1161 ),
1162 md: ThemeShadow::double(
1163 shadow_layer(0.0, 4.0, 6.0, -1.0, black_01, false),
1164 shadow_layer(0.0, 2.0, 4.0, -2.0, black_01, false),
1165 ),
1166 lg: ThemeShadow::double(
1167 shadow_layer(0.0, 10.0, 15.0, -3.0, black_01, false),
1168 shadow_layer(0.0, 4.0, 6.0, -4.0, black_01, false),
1169 ),
1170 xl: ThemeShadow::double(
1171 shadow_layer(0.0, 20.0, 25.0, -5.0, black_01, false),
1172 shadow_layer(0.0, 8.0, 10.0, -6.0, black_01, false),
1173 ),
1174 _2xl: ThemeShadow::single(shadow_layer(0.0, 25.0, 50.0, -12.0, black_025, false)),
1175 },
1176 inset: ThemeInsetShadowScale {
1177 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, black_005, true)),
1178 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black_005, true)),
1179 sm: ThemeShadow::single(shadow_layer(0.0, 2.0, 4.0, 0.0, black_005, true)),
1180 },
1181 drop: ThemeDropShadowScale {
1182 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black_005, false)),
1183 sm: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, black_015, false)),
1184 md: ThemeShadow::single(shadow_layer(0.0, 3.0, 3.0, 0.0, black_012, false)),
1185 lg: ThemeShadow::single(shadow_layer(0.0, 4.0, 4.0, 0.0, black_015, false)),
1186 xl: ThemeShadow::single(shadow_layer(0.0, 9.0, 7.0, 0.0, black_01, false)),
1187 _2xl: ThemeShadow::single(shadow_layer(0.0, 25.0, 25.0, 0.0, black_015, false)),
1188 },
1189 text: ThemeTextShadowScale {
1190 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, black_015, false)),
1191 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black_02, false)),
1192 sm: ThemeShadow::double(
1193 shadow_layer(0.0, 1.0, 0.0, 0.0, black_075, false),
1194 shadow_layer(0.0, 1.0, 1.0, 0.0, black_075, false),
1195 ),
1196 md: ThemeShadow::double(
1197 shadow_layer(0.0, 1.0, 1.0, 0.0, black_01, false),
1198 shadow_layer(0.0, 2.0, 4.0, 0.0, black_01, false),
1199 ),
1200 lg: ThemeShadow::double(
1201 shadow_layer(0.0, 1.0, 2.0, 0.0, black_01, false),
1202 shadow_layer(0.0, 4.0, 8.0, 0.0, black_01, false),
1203 ),
1204 },
1205 }
1206 }
1207}
1208
1209impl ThemeShadows {
1210 pub fn for_colors(colors: &ThemeColors) -> Self {
1214 match colors.scheme {
1215 ThemeColorScheme::Light => Self::light_with_ink(colors.neutrals.text),
1216 ThemeColorScheme::Dark => Self::dark(),
1217 ThemeColorScheme::HighContrast => Self::none(),
1218 }
1219 }
1220
1221 pub fn light() -> Self {
1224 Self::light_with_ink(rgb8(23, 23, 23))
1225 }
1226
1227 pub fn light_with_ink(shadow_ink: Color) -> Self {
1229 let ink = |alpha: f32| shadow_ink.with_alpha(alpha);
1230
1231 Self {
1232 box_shadow: ThemeBoxShadowScale {
1233 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, ink(0.04), false)),
1234 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, ink(0.06), false)),
1235 sm: ThemeShadow::double(
1236 shadow_layer(0.0, 1.0, 3.0, 0.0, ink(0.07), false),
1237 shadow_layer(0.0, 1.0, 2.0, -1.0, ink(0.06), false),
1238 ),
1239 md: ThemeShadow::single(shadow_layer(0.0, 2.0, 10.0, 0.0, ink(0.08), false)),
1240 lg: ThemeShadow::single(shadow_layer(0.0, 8.0, 24.0, -2.0, ink(0.12), false)),
1241 xl: ThemeShadow::single(shadow_layer(0.0, 16.0, 40.0, -4.0, ink(0.16), false)),
1242 _2xl: ThemeShadow::single(shadow_layer(0.0, 24.0, 56.0, -8.0, ink(0.20), false)),
1243 },
1244 inset: ThemeInsetShadowScale {
1245 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, ink(0.05), true)),
1246 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, ink(0.05), true)),
1247 sm: ThemeShadow::single(shadow_layer(0.0, 2.0, 4.0, 0.0, ink(0.05), true)),
1248 },
1249 drop: ThemeDropShadowScale {
1250 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, ink(0.05), false)),
1251 sm: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, ink(0.12), false)),
1252 md: ThemeShadow::single(shadow_layer(0.0, 3.0, 3.0, 0.0, ink(0.10), false)),
1253 lg: ThemeShadow::single(shadow_layer(0.0, 4.0, 4.0, 0.0, ink(0.12), false)),
1254 xl: ThemeShadow::single(shadow_layer(0.0, 9.0, 7.0, 0.0, ink(0.09), false)),
1255 _2xl: ThemeShadow::single(shadow_layer(0.0, 25.0, 25.0, 0.0, ink(0.12), false)),
1256 },
1257 text: ThemeTextShadowScale {
1258 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, ink(0.12), false)),
1259 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, ink(0.16), false)),
1260 sm: ThemeShadow::double(
1261 shadow_layer(0.0, 1.0, 0.0, 0.0, ink(0.07), false),
1262 shadow_layer(0.0, 1.0, 1.0, 0.0, ink(0.07), false),
1263 ),
1264 md: ThemeShadow::double(
1265 shadow_layer(0.0, 1.0, 1.0, 0.0, ink(0.09), false),
1266 shadow_layer(0.0, 2.0, 4.0, 0.0, ink(0.09), false),
1267 ),
1268 lg: ThemeShadow::double(
1269 shadow_layer(0.0, 1.0, 2.0, 0.0, ink(0.09), false),
1270 shadow_layer(0.0, 4.0, 8.0, 0.0, ink(0.09), false),
1271 ),
1272 },
1273 }
1274 }
1275
1276 pub fn dark() -> Self {
1279 let black = |alpha: f32| Color::BLACK.with_alpha(alpha);
1280
1281 Self {
1282 box_shadow: ThemeBoxShadowScale {
1283 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, black(0.24), false)),
1284 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, black(0.30), false)),
1285 sm: ThemeShadow::double(
1286 shadow_layer(0.0, 2.0, 6.0, 0.0, black(0.32), false),
1287 shadow_layer(0.0, 1.0, 2.0, -1.0, black(0.28), false),
1288 ),
1289 md: ThemeShadow::single(shadow_layer(0.0, 4.0, 16.0, 0.0, black(0.40), false)),
1290 lg: ThemeShadow::single(shadow_layer(0.0, 10.0, 28.0, -2.0, black(0.46), false)),
1291 xl: ThemeShadow::single(shadow_layer(0.0, 20.0, 48.0, -4.0, black(0.55), false)),
1292 _2xl: ThemeShadow::single(shadow_layer(0.0, 28.0, 64.0, -8.0, black(0.60), false)),
1293 },
1294 inset: ThemeInsetShadowScale {
1295 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, black(0.24), true)),
1296 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black(0.24), true)),
1297 sm: ThemeShadow::single(shadow_layer(0.0, 2.0, 4.0, 0.0, black(0.24), true)),
1298 },
1299 drop: ThemeDropShadowScale {
1300 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black(0.24), false)),
1301 sm: ThemeShadow::single(shadow_layer(0.0, 1.0, 2.0, 0.0, black(0.34), false)),
1302 md: ThemeShadow::single(shadow_layer(0.0, 3.0, 3.0, 0.0, black(0.32), false)),
1303 lg: ThemeShadow::single(shadow_layer(0.0, 4.0, 4.0, 0.0, black(0.34), false)),
1304 xl: ThemeShadow::single(shadow_layer(0.0, 9.0, 7.0, 0.0, black(0.30), false)),
1305 _2xl: ThemeShadow::single(shadow_layer(0.0, 25.0, 25.0, 0.0, black(0.34), false)),
1306 },
1307 text: ThemeTextShadowScale {
1308 _2xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 0.0, 0.0, black(0.30), false)),
1309 xs: ThemeShadow::single(shadow_layer(0.0, 1.0, 1.0, 0.0, black(0.36), false)),
1310 sm: ThemeShadow::double(
1311 shadow_layer(0.0, 1.0, 0.0, 0.0, black(0.20), false),
1312 shadow_layer(0.0, 1.0, 1.0, 0.0, black(0.20), false),
1313 ),
1314 md: ThemeShadow::double(
1315 shadow_layer(0.0, 1.0, 1.0, 0.0, black(0.24), false),
1316 shadow_layer(0.0, 2.0, 4.0, 0.0, black(0.24), false),
1317 ),
1318 lg: ThemeShadow::double(
1319 shadow_layer(0.0, 1.0, 2.0, 0.0, black(0.24), false),
1320 shadow_layer(0.0, 4.0, 8.0, 0.0, black(0.24), false),
1321 ),
1322 },
1323 }
1324 }
1325
1326 pub fn none() -> Self {
1329 let empty_box = ThemeBoxShadowScale {
1330 _2xs: ThemeShadow::empty(),
1331 xs: ThemeShadow::empty(),
1332 sm: ThemeShadow::empty(),
1333 md: ThemeShadow::empty(),
1334 lg: ThemeShadow::empty(),
1335 xl: ThemeShadow::empty(),
1336 _2xl: ThemeShadow::empty(),
1337 };
1338
1339 Self {
1340 box_shadow: empty_box,
1341 inset: ThemeInsetShadowScale {
1342 _2xs: ThemeShadow::empty(),
1343 xs: ThemeShadow::empty(),
1344 sm: ThemeShadow::empty(),
1345 },
1346 drop: ThemeDropShadowScale {
1347 xs: ThemeShadow::empty(),
1348 sm: ThemeShadow::empty(),
1349 md: ThemeShadow::empty(),
1350 lg: ThemeShadow::empty(),
1351 xl: ThemeShadow::empty(),
1352 _2xl: ThemeShadow::empty(),
1353 },
1354 text: ThemeTextShadowScale {
1355 _2xs: ThemeShadow::empty(),
1356 xs: ThemeShadow::empty(),
1357 sm: ThemeShadow::empty(),
1358 md: ThemeShadow::empty(),
1359 lg: ThemeShadow::empty(),
1360 },
1361 }
1362 }
1363}
1364
1365#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1367pub enum GlowTone {
1368 #[default]
1370 Accent,
1371 Secondary,
1373}
1374
1375#[derive(Debug, Clone, Copy, PartialEq)]
1381pub struct ThemeGlows {
1382 pub accent: ThemeShadow,
1384 pub secondary: ThemeShadow,
1386}
1387
1388impl ThemeGlows {
1389 pub fn for_colors(colors: &ThemeColors) -> Self {
1393 let (blur, alpha) = match colors.scheme {
1394 ThemeColorScheme::Light => return Self::none(),
1395 ThemeColorScheme::Dark => (16.0, 0.24),
1396 ThemeColorScheme::HighContrast => (10.0, 0.14),
1397 };
1398 let halo = |color: Color, blur: f32| {
1399 let strength = if color.to_oklch().chroma < 0.04 {
1400 0.5
1401 } else {
1402 1.0
1403 };
1404 ThemeShadow::single(shadow_layer(
1405 0.0,
1406 0.0,
1407 blur,
1408 0.0,
1409 color.with_alpha(alpha * strength),
1410 false,
1411 ))
1412 };
1413 Self {
1414 accent: halo(colors.primary, blur),
1415 secondary: halo(colors.secondary, blur + 2.0),
1416 }
1417 }
1418
1419 pub fn none() -> Self {
1420 Self {
1421 accent: ThemeShadow::empty(),
1422 secondary: ThemeShadow::empty(),
1423 }
1424 }
1425
1426 pub fn get(&self, tone: GlowTone) -> ThemeShadow {
1427 match tone {
1428 GlowTone::Accent => self.accent,
1429 GlowTone::Secondary => self.secondary,
1430 }
1431 }
1432}
1433
1434impl Default for ThemeGlows {
1435 fn default() -> Self {
1436 Self::for_colors(&ThemeColors::default())
1437 }
1438}
1439
1440#[derive(Debug, Clone, Copy, PartialEq)]
1441pub struct ThemeBlurScale {
1442 pub xs: f32,
1443 pub sm: f32,
1444 pub md: f32,
1445 pub lg: f32,
1446 pub xl: f32,
1447 pub _2xl: f32,
1448 pub _3xl: f32,
1449}
1450
1451impl Default for ThemeBlurScale {
1452 fn default() -> Self {
1453 Self {
1454 xs: 4.0,
1455 sm: 8.0,
1456 md: 12.0,
1457 lg: 16.0,
1458 xl: 24.0,
1459 _2xl: 40.0,
1460 _3xl: 64.0,
1461 }
1462 }
1463}
1464
1465#[derive(Debug, Clone, Copy, PartialEq)]
1466pub struct ThemePerspective {
1467 pub dramatic: f32,
1468 pub near: f32,
1469 pub normal: f32,
1470 pub midrange: f32,
1471 pub distant: f32,
1472}
1473
1474impl Default for ThemePerspective {
1475 fn default() -> Self {
1476 Self {
1477 dramatic: 100.0,
1478 near: 300.0,
1479 normal: 500.0,
1480 midrange: 800.0,
1481 distant: 1200.0,
1482 }
1483 }
1484}
1485
1486#[derive(Debug, Clone, Copy, PartialEq)]
1487pub struct ThemeAspectRatios {
1488 pub video: f32,
1489}
1490
1491impl Default for ThemeAspectRatios {
1492 fn default() -> Self {
1493 Self { video: 16.0 / 9.0 }
1494 }
1495}
1496
1497#[derive(Debug, Clone, Copy, PartialEq)]
1502pub struct ToneRoles {
1503 pub solid: Color,
1504 pub on_solid: Color,
1505 pub hover: Color,
1506 pub pressed: Color,
1507 pub soft: Color,
1508 pub text: Color,
1509 pub border: Color,
1510}
1511
1512impl ToneRoles {
1513 pub fn derive(solid: Color, on_solid: Color, scheme: ThemeColorScheme, panel: Color) -> Self {
1517 let dark = scheme.is_dark();
1518 let lightness = solid.to_oklch().lightness;
1519 let (hover, pressed) = if dark {
1520 (shift_lightness(solid, 0.03), shift_lightness(solid, -0.035))
1521 } else if lightness < 0.32 {
1522 (shift_lightness(solid, 0.07), shift_lightness(solid, 0.12))
1523 } else {
1524 (
1525 shift_lightness(solid, -0.045),
1526 shift_lightness(solid, -0.09),
1527 )
1528 };
1529 let soft = solid.with_alpha(if dark { 0.16 } else { 0.10 }).over(panel);
1533 let border = solid.with_alpha(if dark { 0.45 } else { 0.35 }).over(panel);
1534 let text = with_min_contrast(solid, soft, if dark { 6.0 } else { 4.5 });
1535
1536 Self {
1537 solid,
1538 on_solid,
1539 hover,
1540 pressed,
1541 soft,
1542 text,
1543 border,
1544 }
1545 }
1546}
1547
1548#[derive(Debug, Clone, Copy, PartialEq)]
1550pub struct DecorativePalette {
1551 pub red: ToneRoles,
1552 pub orange: ToneRoles,
1553 pub amber: ToneRoles,
1554 pub green: ToneRoles,
1555 pub teal: ToneRoles,
1556 pub cyan: ToneRoles,
1557 pub blue: ToneRoles,
1558 pub violet: ToneRoles,
1559 pub magenta: ToneRoles,
1560}
1561
1562impl DecorativePalette {
1563 pub fn from_colors(colors: &ThemeColors) -> Self {
1564 let roles = |hue: DecorativeHue| {
1565 let solid = colors.decorative.get(hue);
1566 ToneRoles::derive(
1567 solid,
1568 readable_ink(solid),
1569 colors.scheme,
1570 colors.neutrals.panel,
1571 )
1572 };
1573 Self {
1574 red: roles(DecorativeHue::Red),
1575 orange: roles(DecorativeHue::Orange),
1576 amber: roles(DecorativeHue::Amber),
1577 green: roles(DecorativeHue::Green),
1578 teal: roles(DecorativeHue::Teal),
1579 cyan: roles(DecorativeHue::Cyan),
1580 blue: roles(DecorativeHue::Blue),
1581 violet: roles(DecorativeHue::Violet),
1582 magenta: roles(DecorativeHue::Magenta),
1583 }
1584 }
1585
1586 pub fn get(&self, hue: DecorativeHue) -> ToneRoles {
1587 match hue {
1588 DecorativeHue::Red => self.red,
1589 DecorativeHue::Orange => self.orange,
1590 DecorativeHue::Amber => self.amber,
1591 DecorativeHue::Green => self.green,
1592 DecorativeHue::Teal => self.teal,
1593 DecorativeHue::Cyan => self.cyan,
1594 DecorativeHue::Blue => self.blue,
1595 DecorativeHue::Violet => self.violet,
1596 DecorativeHue::Magenta => self.magenta,
1597 }
1598 }
1599
1600 pub fn categorical(&self, index: usize) -> ToneRoles {
1603 self.get(DecorativeHue::ALL[index % DecorativeHue::ALL.len()])
1604 }
1605}
1606
1607impl Default for DecorativePalette {
1608 fn default() -> Self {
1609 DefaultTheme::default().decorative
1610 }
1611}
1612
1613#[derive(Debug, Clone, Copy, PartialEq)]
1614pub struct ControlPalette {
1615 pub text: Color,
1616 pub text_muted: Color,
1617 pub placeholder: Color,
1618 pub text_disabled: Color,
1619 pub surface: Color,
1620 pub surface_raised: Color,
1621 pub control: Color,
1623 pub control_hover: Color,
1624 pub control_active: Color,
1625 pub button: Color,
1627 pub button_hover: Color,
1628 pub button_pressed: Color,
1629 pub button_border: Color,
1631 pub field: Color,
1633 pub surface_hover: Color,
1634 pub surface_pressed: Color,
1635 pub surface_focus: Color,
1636 pub border: Color,
1637 pub border_strong: Color,
1638 pub border_hover: Color,
1639 pub border_focus: Color,
1641 pub border_control: Color,
1643 pub focus: Color,
1646 pub focus_ring: Color,
1647 pub caret: Color,
1648 pub selection: Color,
1650 pub selection_border: Color,
1654 pub accent: Color,
1655 pub accent_hover: Color,
1656 pub accent_pressed: Color,
1657 pub accent_border: Color,
1658 pub accent_border_hover: Color,
1659 pub accent_border_focus: Color,
1660 pub accent_text: Color,
1662 pub accent_soft: Color,
1664 pub accent_soft_text: Color,
1667 pub info: Color,
1668 pub info_text: Color,
1669 pub info_hover: Color,
1670 pub info_pressed: Color,
1671 pub info_border: Color,
1672 pub info_soft: Color,
1673 pub info_soft_text: Color,
1674 pub success: Color,
1675 pub success_text: Color,
1676 pub success_hover: Color,
1677 pub success_pressed: Color,
1678 pub success_border: Color,
1679 pub success_soft: Color,
1680 pub success_soft_text: Color,
1681 pub warning: Color,
1682 pub warning_text: Color,
1683 pub warning_hover: Color,
1684 pub warning_pressed: Color,
1685 pub warning_border: Color,
1686 pub warning_soft: Color,
1687 pub warning_soft_text: Color,
1688 pub danger: Color,
1689 pub danger_text: Color,
1690 pub danger_hover: Color,
1691 pub danger_pressed: Color,
1692 pub danger_border: Color,
1693 pub danger_soft: Color,
1694 pub danger_soft_text: Color,
1695}
1696
1697impl ControlPalette {
1698 pub fn from_colors(colors: &ThemeColors) -> Self {
1699 let dark = colors.scheme.is_dark();
1700 let neutrals = colors.neutrals;
1701 let panel = neutrals.panel;
1702 let tone = |solid: Color, on_solid: Color| {
1703 ToneRoles::derive(solid, on_solid, colors.scheme, panel)
1704 };
1705 let accent = tone(colors.primary, colors.on_primary);
1706 let info = tone(colors.info, colors.on_info);
1707 let success = tone(colors.success, colors.on_success);
1708 let warning = tone(colors.warning, colors.on_warning);
1709 let danger = tone(colors.danger, colors.on_danger);
1710 let focus = with_min_contrast(colors.primary, panel, if dark { 4.5 } else { 3.0 });
1711
1712 Self {
1713 text: neutrals.text,
1714 text_muted: neutrals.text_secondary,
1715 placeholder: neutrals.text_tertiary,
1716 text_disabled: neutrals.text_disabled,
1717 surface: neutrals.window,
1718 surface_raised: panel,
1719 control: neutrals.control,
1720 control_hover: neutrals.control_hover,
1721 control_active: neutrals.control_active,
1722 button: neutrals.button,
1723 button_hover: neutrals.button_hover,
1724 button_pressed: neutrals.button_active,
1725 button_border: neutrals.border_strong,
1726 field: neutrals.field,
1727 surface_hover: neutrals.control_hover,
1728 surface_pressed: neutrals.control_active,
1729 surface_focus: neutrals.field,
1731 border: neutrals.border,
1732 border_strong: neutrals.border_strong,
1733 border_hover: mix(neutrals.border_strong, neutrals.border_control, 0.45),
1734 border_focus: mix(neutrals.border_strong, neutrals.border_control, 0.65),
1735 border_control: neutrals.border_control,
1736 focus,
1737 focus_ring: focus,
1738 caret: neutrals.text,
1739 selection: neutrals
1740 .text
1741 .with_alpha(if dark { 0.10 } else { 0.08 })
1742 .over(panel),
1743 selection_border: accent.border,
1744 accent: accent.solid,
1745 accent_hover: accent.hover,
1746 accent_pressed: accent.pressed,
1747 accent_border: accent.border,
1748 accent_border_hover: mix(accent.border, accent.solid, 0.35),
1749 accent_border_focus: accent.solid,
1750 accent_text: accent.on_solid,
1751 accent_soft: accent.soft,
1752 accent_soft_text: accent.text,
1753 info: info.solid,
1754 info_text: info.on_solid,
1755 info_hover: info.hover,
1756 info_pressed: info.pressed,
1757 info_border: info.border,
1758 info_soft: info.soft,
1759 info_soft_text: info.text,
1760 success: success.solid,
1761 success_text: success.on_solid,
1762 success_hover: success.hover,
1763 success_pressed: success.pressed,
1764 success_border: success.border,
1765 success_soft: success.soft,
1766 success_soft_text: success.text,
1767 warning: warning.solid,
1768 warning_text: warning.on_solid,
1769 warning_hover: warning.hover,
1770 warning_pressed: warning.pressed,
1771 warning_border: warning.border,
1772 warning_soft: warning.soft,
1773 warning_soft_text: warning.text,
1774 danger: danger.solid,
1775 danger_text: danger.on_solid,
1776 danger_hover: danger.hover,
1777 danger_pressed: danger.pressed,
1778 danger_border: danger.border,
1779 danger_soft: danger.soft,
1780 danger_soft_text: danger.text,
1781 }
1782 }
1783}
1784
1785impl Default for ControlPalette {
1786 fn default() -> Self {
1787 DefaultTheme::default().palette
1788 }
1789}
1790
1791fn shift_lightness(color: Color, delta: f32) -> Color {
1793 let oklch = color.to_oklch();
1794 oklch
1795 .with_lightness((oklch.lightness + delta).clamp(0.0, 1.0))
1796 .to_srgb()
1797 .with_alpha(color.alpha)
1798}
1799
1800fn with_min_contrast(color: Color, background: Color, target: f32) -> Color {
1804 if color.contrast_ratio(background) >= target {
1805 return color;
1806 }
1807 let oklch = color.to_oklch();
1808 let step = if background.relative_luminance() < 0.18 {
1809 0.01
1810 } else {
1811 -0.01
1812 };
1813 let mut lightness = oklch.lightness;
1814 let mut candidate = color;
1815 while (0.0..=1.0).contains(&(lightness + step)) {
1816 lightness += step;
1817 candidate = oklch.with_lightness(lightness).to_srgb();
1818 if candidate.contrast_ratio(background) >= target {
1819 break;
1820 }
1821 }
1822 candidate.with_alpha(color.alpha)
1823}
1824
1825fn readable_ink(solid: Color) -> Color {
1827 let hue = solid.to_oklch().hue;
1828 let deep = Color::oklch(0.26, 0.06, hue);
1829 if Color::WHITE.contrast_ratio(solid) >= deep.contrast_ratio(solid) {
1830 Color::WHITE
1831 } else {
1832 deep
1833 }
1834}
1835
1836#[derive(Debug, Clone, Copy, PartialEq)]
1837pub struct SurfacePalette {
1838 pub dark: bool,
1839 pub window: Color,
1840 pub window_subtle: Color,
1843 pub sidebar: Color,
1844 pub panel: Color,
1845 pub surface_2: Color,
1847 pub surface_3: Color,
1850 pub overlay: Color,
1852 pub titlebar: Color,
1853 pub field: Color,
1854 pub border: Color,
1855 pub border_strong: Color,
1856 pub border_subtle: Color,
1858 pub text: Color,
1859 pub text_muted: Color,
1860 pub text_faint: Color,
1861 pub text_disabled: Color,
1863 pub text_invert: Color,
1865 pub accent: Color,
1866 pub accent_hover: Color,
1867 pub on_accent: Color,
1868 pub accent_text: Color,
1871 pub accent_soft: Color,
1874 pub accent_border: Color,
1876 pub focus: Color,
1878 pub hover: Color,
1879 pub selected: Color,
1880 pub selected_border: Color,
1882 pub overlay_scrim: Color,
1883 pub tooltip: Color,
1884 pub tooltip_border: Color,
1885 pub tooltip_text: Color,
1886 pub(crate) canvas: Color,
1889 pub(crate) canvas_grid: Color,
1890 pub(crate) canvas_axis_x: Color,
1891 pub(crate) canvas_axis_y: Color,
1892 pub(crate) pixel_canvas_paper: Color,
1893 pub(crate) pixel_canvas_document_edge: Color,
1894 pub(crate) pixel_canvas_shadow_near: Color,
1895 pub(crate) pixel_canvas_shadow_far: Color,
1896 pub(crate) pixel_canvas_grid: Color,
1897 pub(crate) canvas_ruler: Color,
1898 pub(crate) canvas_ruler_border: Color,
1899 pub(crate) canvas_ruler_tick: Color,
1900 pub(crate) canvas_ruler_text: Color,
1901 pub(crate) checkerboard_light: Color,
1902 pub(crate) checkerboard_dark: Color,
1903 pub(crate) color_picker_chrome_border: Color,
1904 pub(crate) color_picker_plane_border: Color,
1905 pub(crate) color_picker_bar_border: Color,
1906 pub(crate) color_picker_marker_outer: Color,
1907 pub(crate) color_picker_marker_dark: Color,
1908 pub(crate) color_picker_marker_light: Color,
1909 pub(crate) color_picker_sdr_marker: Color,
1910 pub(crate) color_picker_hdr_divider: Color,
1911 pub good: Color,
1912 pub good_text: Color,
1914 pub good_soft: Color,
1916 pub warn: Color,
1917 pub warn_text: Color,
1919 pub warn_soft: Color,
1921 pub bad: Color,
1922 pub bad_text: Color,
1924 pub bad_soft: Color,
1926 pub info: Color,
1928 pub info_text: Color,
1930 pub info_soft: Color,
1932}
1933
1934impl SurfacePalette {
1935 pub fn from_theme_parts(colors: &ThemeColors, controls: &ControlPalette) -> Self {
1936 let dark = colors.scheme.is_dark();
1937 let neutrals = colors.neutrals;
1938 let text_muted = controls.text_muted;
1939 let text_faint = controls.placeholder;
1940 let window_subtle = neutrals.subtle;
1941 let overlay = neutrals.overlay;
1942 let shadow_ink = if dark { Color::BLACK } else { neutrals.text };
1943
1944 Self {
1945 dark,
1946 window: controls.surface,
1947 window_subtle,
1948 sidebar: window_subtle,
1951 panel: controls.surface_raised,
1952 surface_2: controls.control,
1953 surface_3: controls.control_hover,
1954 overlay,
1955 titlebar: window_subtle,
1956 field: controls.field,
1957 border: controls.border,
1958 border_strong: controls.border_strong,
1959 border_subtle: neutrals.border_subtle,
1960 text: controls.text,
1961 text_muted,
1962 text_faint,
1963 text_disabled: neutrals.text_disabled,
1964 text_invert: if dark { neutrals.window } else { Color::WHITE },
1965 accent: controls.accent,
1966 accent_hover: controls.accent_hover,
1967 on_accent: controls.accent_text,
1968 accent_text: controls.accent_soft_text,
1969 accent_soft: controls.accent_soft,
1970 accent_border: controls.accent_border,
1971 focus: controls.focus,
1972 hover: controls.text.with_alpha(if dark { 0.06 } else { 0.045 }),
1973 selected: controls.selection,
1974 selected_border: controls.selection_border,
1975 overlay_scrim: match colors.scheme {
1976 ThemeColorScheme::Light => neutrals.text.with_alpha(0.40),
1977 ThemeColorScheme::Dark => mix(neutrals.window, Color::BLACK, 0.5).with_alpha(0.64),
1978 ThemeColorScheme::HighContrast => Color::BLACK.with_alpha(0.72),
1979 },
1980 tooltip: overlay,
1983 tooltip_border: controls.border_strong,
1984 tooltip_text: text_muted,
1985 canvas: controls.surface,
1986 canvas_grid: controls.border.with_alpha(if dark { 0.30 } else { 0.18 }),
1987 canvas_axis_x: colors.danger.with_alpha(if dark { 0.72 } else { 0.55 }),
1988 canvas_axis_y: colors.success.with_alpha(if dark { 0.72 } else { 0.55 }),
1989 pixel_canvas_paper: if dark {
1990 mix(controls.surface_raised, controls.text, 0.10)
1991 } else {
1992 window_subtle
1993 },
1994 pixel_canvas_document_edge: controls.text.with_alpha(if dark { 0.82 } else { 0.72 }),
1995 pixel_canvas_shadow_near: shadow_ink.with_alpha(if dark { 0.30 } else { 0.16 }),
1996 pixel_canvas_shadow_far: shadow_ink.with_alpha(if dark { 0.18 } else { 0.08 }),
1997 pixel_canvas_grid: controls.text.with_alpha(if dark { 0.32 } else { 0.28 }),
1998 canvas_ruler: controls.surface_raised,
1999 canvas_ruler_border: controls.border.with_alpha(0.78),
2000 canvas_ruler_tick: controls.text_muted.with_alpha(0.72),
2001 canvas_ruler_text: controls.text.with_alpha(0.76),
2002 checkerboard_light: if dark {
2003 mix(controls.surface_raised, controls.text, 0.18)
2004 } else {
2005 window_subtle
2006 },
2007 checkerboard_dark: if dark {
2008 mix(controls.surface_raised, controls.text, 0.10)
2009 } else {
2010 neutrals.border
2011 },
2012 color_picker_chrome_border: controls.text.with_alpha(if dark { 0.24 } else { 0.18 }),
2013 color_picker_plane_border: controls.text.with_alpha(if dark { 0.22 } else { 0.16 }),
2014 color_picker_bar_border: controls.text.with_alpha(if dark { 0.20 } else { 0.14 }),
2015 color_picker_marker_outer: controls.surface_raised.with_alpha(0.92),
2016 color_picker_marker_dark: Color::BLACK.with_alpha(0.84),
2017 color_picker_marker_light: Color::WHITE.with_alpha(0.95),
2018 color_picker_sdr_marker: controls.surface_raised.with_alpha(0.30),
2019 color_picker_hdr_divider: controls.surface_raised.with_alpha(0.28),
2020 good: colors.success,
2021 good_text: controls.success_soft_text,
2022 good_soft: controls.success_soft,
2023 warn: colors.warning,
2024 warn_text: controls.warning_soft_text,
2025 warn_soft: controls.warning_soft,
2026 bad: colors.danger,
2027 bad_text: controls.danger_soft_text,
2028 bad_soft: controls.danger_soft,
2029 info: colors.info,
2030 info_text: controls.info_soft_text,
2031 info_soft: controls.info_soft,
2032 }
2033 }
2034}
2035
2036impl Default for SurfacePalette {
2037 fn default() -> Self {
2038 DefaultTheme::default().surfaces
2039 }
2040}
2041
2042#[derive(Debug, Clone, Copy, PartialEq)]
2043pub struct ControlTypography {
2044 pub body_font_size: f32,
2045 pub body_line_height: f32,
2046}
2047
2048impl ControlTypography {
2049 pub fn from_text_scale(text: &ThemeTextScale) -> Self {
2050 Self::for_density(text, ThemeDensity::default())
2051 }
2052
2053 pub fn for_density(text: &ThemeTextScale, density: ThemeDensity) -> Self {
2057 let token = match density {
2058 ThemeDensity::Compact => text.sm,
2059 ThemeDensity::Comfortable => text.base,
2060 ThemeDensity::Touch => text.lg,
2061 };
2062 Self {
2063 body_font_size: token.size,
2064 body_line_height: token.line_height,
2065 }
2066 }
2067
2068 pub fn for_size(text: &ThemeTextScale, size: ControlSize) -> Self {
2072 let token = match size {
2073 ControlSize::Small => text.sm,
2074 ControlSize::Medium => text.base,
2075 ControlSize::Large => text.lg,
2076 };
2077 Self {
2078 body_font_size: token.size,
2079 body_line_height: token.line_height,
2080 }
2081 }
2082}
2083
2084impl Default for ControlTypography {
2085 fn default() -> Self {
2086 Self::from_text_scale(&ThemeTextScale::default())
2087 }
2088}
2089
2090#[derive(Debug, Clone, Copy, PartialEq)]
2091pub struct ControlStateMetrics {
2092 pub hover_blend: f32,
2093 pub pressed_blend: f32,
2094 pub selected_blend: f32,
2095 pub tab_selected_blend: f32,
2096 pub disabled_opacity: f32,
2097 pub disabled_content_opacity: f32,
2098 pub pressed_offset: f32,
2099 pub active_indicator_thickness: f32,
2100}
2101
2102impl ControlStateMetrics {
2103 pub fn for_density(density: ThemeDensity) -> Self {
2104 match density {
2105 ThemeDensity::Compact => Self {
2106 hover_blend: 0.78,
2107 pressed_blend: 0.88,
2108 selected_blend: 0.20,
2109 tab_selected_blend: 0.07,
2110 disabled_opacity: 0.70,
2111 disabled_content_opacity: 0.46,
2112 pressed_offset: 0.0,
2113 active_indicator_thickness: 2.0,
2114 },
2115 ThemeDensity::Comfortable => Self {
2116 hover_blend: 0.86,
2117 pressed_blend: 1.0,
2118 selected_blend: 0.22,
2119 tab_selected_blend: 0.08,
2120 disabled_opacity: 0.74,
2121 disabled_content_opacity: 0.50,
2122 pressed_offset: 0.0,
2123 active_indicator_thickness: 3.0,
2124 },
2125 ThemeDensity::Touch => Self {
2126 hover_blend: 0.94,
2127 pressed_blend: 1.0,
2128 selected_blend: 0.24,
2129 tab_selected_blend: 0.09,
2130 disabled_opacity: 0.78,
2131 disabled_content_opacity: 0.54,
2132 pressed_offset: 0.0,
2133 active_indicator_thickness: 4.0,
2134 },
2135 }
2136 }
2137}
2138
2139impl Default for ControlStateMetrics {
2140 fn default() -> Self {
2141 Self::for_density(ThemeDensity::default())
2142 }
2143}
2144
2145#[derive(Debug, Clone, Copy, PartialEq)]
2146pub struct ControlMetrics {
2147 pub min_height: f32,
2148 pub touch_target_size: f32,
2149 pub button_min_width: f32,
2150 pub button_padding: Insets,
2151 pub checkbox_padding: Insets,
2152 pub checkbox_indicator_size: f32,
2153 pub checkbox_gap: f32,
2154 pub icon_label_gap: f32,
2155 pub separator_thickness: f32,
2156 pub icon_size: f32,
2157 pub icon_button_size: f32,
2158 pub switch_track_width: f32,
2159 pub switch_track_height: f32,
2160 pub switch_thumb_inset: f32,
2161 pub slider_min_width: f32,
2162 pub slider_padding: Insets,
2163 pub slider_track_height: f32,
2164 pub slider_thumb_size: f32,
2165 pub number_input_stepper_width: f32,
2166 pub text_input_min_width: f32,
2167 pub text_input_padding: Insets,
2168 pub text_area_min_height: f32,
2169 pub select_menu_max_height: f32,
2170 pub select_menu_gap: f32,
2171 pub select_menu_edge_padding: f32,
2172 pub tab_height: f32,
2173 pub tab_min_width: f32,
2174 pub tab_gap: f32,
2175 pub tab_padding: Insets,
2176 pub tab_panel_padding: Insets,
2177 pub tab_panel_gap: f32,
2178 pub menu_row_height: f32,
2179 pub menu_padding: Insets,
2180 pub menu_item_padding: Insets,
2181 pub menu_shortcut_width: f32,
2182 pub menu_min_width: f32,
2184 pub popover_padding: Insets,
2185 pub popover_gap: f32,
2186 pub popover_reveal_offset: f32,
2187 pub tooltip_padding: Insets,
2188 pub tooltip_min_width: f32,
2189 pub tooltip_gap: f32,
2190 pub tooltip_reveal_offset: f32,
2191 pub dialog_min_width: f32,
2192 pub dialog_max_width: f32,
2193 pub dialog_outer_margin: f32,
2194 pub dialog_padding: Insets,
2195 pub dialog_title_font_size: f32,
2196 pub dialog_title_line_height: f32,
2197 pub dialog_description_gap: f32,
2198 pub dialog_body_gap: f32,
2199 pub dialog_footer_gap: f32,
2200 pub dialog_action_gap: f32,
2201 pub dialog_action_min_width: f32,
2202 pub toolbar_extent: f32,
2203 pub toolbar_padding: Insets,
2204 pub toolbar_spacing: f32,
2205 pub command_group_padding: Insets,
2206 pub command_group_spacing: f32,
2207 pub command_group_radius: f32,
2208 pub tool_palette_item_size: f32,
2209 pub tool_palette_icon_size: f32,
2210 pub preset_strip_item_height: f32,
2211 pub preset_strip_item_min_width: f32,
2212 pub preset_strip_item_padding: Insets,
2213 pub preset_strip_gap: f32,
2214 pub preset_strip_label_padding: Insets,
2215 pub action_card_min_width: f32,
2216 pub action_card_min_height: f32,
2217 pub action_card_padding: Insets,
2218 pub action_card_icon_box_size: f32,
2219 pub action_card_icon_size: f32,
2220 pub action_card_icon_gap: f32,
2221 pub action_card_text_gap: f32,
2222 pub action_card_trailing_gap: f32,
2223 pub action_card_chevron_size: f32,
2224 pub action_card_accent_width: f32,
2225 pub action_card_accent_inset: f32,
2226 pub status_bar_height: f32,
2227 pub status_bar_segment_padding: f32,
2228 pub status_bar_segment_min_width: f32,
2229 pub status_bar_separator_inset: f32,
2230 pub progress_bar_min_width: f32,
2231 pub progress_bar_height: f32,
2232 pub progress_bar_value_height: f32,
2233 pub progress_bar_label_padding: Insets,
2234 pub property_row_label_width: f32,
2235 pub property_row_inline_gap: f32,
2236 pub property_row_stacked_gap: f32,
2237 pub form_row_label_width: f32,
2238 pub form_row_control_width: f32,
2239 pub form_row_gap: f32,
2240 pub field_group_spacing: f32,
2241 pub form_section_padding: Insets,
2242 pub form_section_body_gap: f32,
2243 pub form_section_header_gap: f32,
2244 pub form_section_description_gap: f32,
2245 pub form_section_max_width: f32,
2246 pub form_section_radius: f32,
2247 pub panel_section_gap: f32,
2248 pub panel_section_action_gap: f32,
2249 pub panel_section_disclosure_size: f32,
2250 pub dock_panel_header_height: f32,
2251 pub dock_panel_padding: Insets,
2252 pub data_viewport_padding: Insets,
2253 pub data_row_padding: Insets,
2254 pub data_row_icon_size: f32,
2255 pub data_row_icon_gap: f32,
2256 pub data_row_trailing_gap: f32,
2257 pub data_scroll_thumb_width: f32,
2258 pub data_scroll_thumb_inset: f32,
2259 pub data_scroll_thumb_radius: f32,
2260 pub data_scroll_thumb_min_length: f32,
2261 pub data_scroll_thumb_opacity: f32,
2262 pub list_row_height: f32,
2263 pub layer_row_height: f32,
2264 pub layer_action_size: f32,
2265 pub layer_action_icon_inset: f32,
2266 pub layer_lock_icon_inset: f32,
2267 pub layer_visibility_stroke_width: f32,
2268 pub layer_visibility_slash_stroke_width: f32,
2269 pub layer_thumbnail_size: f32,
2270 pub layer_thumbnail_inset: f32,
2271 pub layer_thumbnail_radius: f32,
2272 pub layer_thumbnail_disabled_opacity: f32,
2273 pub layer_thumbnail_disabled_border_opacity: f32,
2274 pub tree_row_height: f32,
2275 pub tree_indent: f32,
2276 pub tree_disclosure_size: f32,
2277 pub tree_disclosure_gap: f32,
2278 pub table_row_height: f32,
2279 pub table_header_height: f32,
2280 pub table_cell_padding: f32,
2281 pub table_header_separator_inset: f32,
2282 pub table_separator_width: f32,
2283 pub table_row_border_opacity: f32,
2284 pub breadcrumb_height: f32,
2285 pub breadcrumb_item_padding: Insets,
2286 pub breadcrumb_gap: f32,
2287 pub breadcrumb_separator_size: f32,
2288 pub image_corner_radius: f32,
2289 pub color_swatch_width: f32,
2290 pub color_swatch_height: f32,
2291 pub color_swatch_inner_inset: f32,
2292 pub color_swatch_checker_size: f32,
2293 pub color_palette_swatch_size: f32,
2294 pub color_palette_gap: f32,
2295 pub color_palette_swatch_inset: f32,
2296 pub color_palette_selected_swatch_inset: f32,
2297 pub color_palette_checker_size: f32,
2298 pub brush_preview_min_width: f32,
2299 pub brush_preview_min_height: f32,
2300 pub brush_preview_padding: Insets,
2301 pub brush_preview_swatch_width: f32,
2302 pub brush_preview_swatch_gap: f32,
2303 pub brush_preview_checker_size: f32,
2304 pub brush_preview_text_height: f32,
2305 pub brush_preview_text_font_size: f32,
2306 pub brush_preview_text_line_height: f32,
2307 pub color_picker_content_inset: f32,
2308 pub color_picker_panel_gap: f32,
2309 pub color_picker_top_bar_height: f32,
2310 pub color_picker_swatch_width: f32,
2311 pub color_picker_swatch_gap: f32,
2312 pub color_picker_section_gap: f32,
2313 pub color_picker_wheel_size: f32,
2314 pub color_picker_map_size: f32,
2315 pub color_picker_row_height: f32,
2316 pub color_picker_row_gap: f32,
2317 pub color_picker_right_panel_width: f32,
2318 pub color_picker_field_height: f32,
2319 pub color_picker_field_gap: f32,
2320 pub color_picker_dropdown_gap: f32,
2321 pub color_picker_encoding_menu_row_height: f32,
2322 pub scroll_bar_thickness: f32,
2323 pub scroll_bar_min_thumb_length: f32,
2324 pub split_view_divider_thickness: f32,
2325 pub split_view_drag_target_thickness: f32,
2326 pub floating_workspace_margin: f32,
2327 pub floating_view_title_bar_height: f32,
2328 pub floating_view_title_padding: Insets,
2329 pub floating_view_resize_handle_size: f32,
2330 pub canvas_ruler_extent: f32,
2331 pub canvas_ruler_major_tick: f32,
2332 pub canvas_ruler_minor_tick: f32,
2333 pub canvas_ruler_target_major_spacing: f32,
2334 pub canvas_ruler_label_padding: Insets,
2335 pub canvas_ruler_label_max_width: f32,
2336 pub canvas_grid_step: f32,
2337 pub canvas_axis_overscan: f32,
2338 pub pixel_canvas_fit_padding: f32,
2339 pub pixel_canvas_grid_zoom: f32,
2340 pub pixel_canvas_nearest_sampling_zoom: f32,
2341 pub pixel_canvas_zoom_step: f32,
2342 pub corner_radius: f32,
2343 pub indicator_corner_radius: f32,
2344 pub border_width: f32,
2345 pub focus_ring_width: f32,
2346 pub focus_ring_outset: f32,
2347 pub caret_width: f32,
2348}
2349
2350const fn symmetric_insets(horizontal: f32, vertical: f32) -> Insets {
2353 Insets {
2354 left: horizontal,
2355 top: vertical,
2356 right: horizontal,
2357 bottom: vertical,
2358 }
2359}
2360
2361impl ControlMetrics {
2362 pub fn from_tokens(spacing: f32, radius: ThemeRadii, density: ThemeDensity) -> Self {
2363 let unit = spacing.max(1.0);
2364 Self {
2367 min_height: density.pick(28.0, 32.0, 36.0),
2368 touch_target_size: density.pick(28.0, 36.0, 44.0),
2369 button_min_width: 64.0,
2370 button_padding: density.pick(
2371 symmetric_insets(unit * 1.5, unit * 0.75),
2372 symmetric_insets(unit * 2.0, unit * 1.25),
2373 symmetric_insets(unit * 2.5, unit * 1.5),
2374 ),
2375 checkbox_padding: density.pick(
2376 symmetric_insets(unit, unit * 0.5),
2377 symmetric_insets(unit * 1.5, unit),
2378 symmetric_insets(unit * 2.0, unit * 1.5),
2379 ),
2380 checkbox_indicator_size: 15.0,
2381 checkbox_gap: 6.0,
2382 icon_label_gap: 6.0,
2383 separator_thickness: 1.0,
2384 icon_size: density.pick(14.0, 16.0, 18.0),
2385 icon_button_size: density.pick(28.0, 32.0, 36.0),
2386 switch_track_width: 32.0,
2387 switch_track_height: 19.0,
2388 switch_thumb_inset: 3.0,
2389 slider_min_width: density.pick(120.0, 140.0, 160.0),
2390 slider_padding: density.pick(
2391 symmetric_insets(unit * 1.5, unit * 0.5),
2392 symmetric_insets(unit * 2.0, unit),
2393 symmetric_insets(unit * 2.5, unit * 1.5),
2394 ),
2395 slider_track_height: 4.0,
2396 slider_thumb_size: 14.0,
2397 number_input_stepper_width: density.pick(22.0, 24.0, 30.0),
2398 text_input_min_width: density.pick(150.0, 180.0, 200.0),
2399 text_input_padding: density.pick(
2400 symmetric_insets(unit * 2.0, unit * 0.75),
2401 symmetric_insets(unit * 2.0, unit * 1.25),
2402 symmetric_insets(unit * 2.0, unit * 1.5),
2403 ),
2404 text_area_min_height: density.pick(56.0, 64.0, 72.0),
2405 select_menu_max_height: density.pick(176.0, 200.0, 230.0),
2406 select_menu_gap: 6.0,
2407 select_menu_edge_padding: 8.0,
2408 tab_height: density.pick(32.0, 36.0, 40.0),
2409 tab_min_width: density.pick(84.0, 96.0, 104.0),
2410 tab_gap: 6.0,
2411 tab_padding: density.pick(
2412 symmetric_insets(unit * 2.5, unit * 0.75),
2413 symmetric_insets(unit * 2.5, unit),
2414 symmetric_insets(unit * 2.5, unit * 1.5),
2415 ),
2416 tab_panel_padding: density.pick(
2417 Insets::all(unit * 3.0),
2418 Insets::all(unit * 4.0),
2419 Insets::all(unit * 4.5),
2420 ),
2421 tab_panel_gap: density.pick(unit * 2.0, unit * 3.0, unit * 3.5),
2422 menu_row_height: density.pick(24.0, 28.0, 36.0),
2423 menu_padding: density.pick(
2424 Insets::all(unit * 0.75),
2425 Insets::all(unit),
2426 Insets::all(unit * 1.5),
2427 ),
2428 menu_item_padding: density.pick(
2429 symmetric_insets(unit * 2.0, unit * 0.5),
2430 symmetric_insets(unit * 2.5, unit),
2431 symmetric_insets(unit * 3.0, unit * 1.5),
2432 ),
2433 menu_shortcut_width: density.pick(84.0, 96.0, 112.0),
2434 menu_min_width: density.pick(144.0, 160.0, 200.0),
2435 popover_padding: density.pick(
2436 Insets::all(unit * 2.5),
2437 Insets::all(unit * 3.5),
2438 Insets::all(unit * 4.0),
2439 ),
2440 popover_gap: unit * 2.0,
2441 popover_reveal_offset: density.pick(8.0, 10.0, 12.0),
2442 tooltip_padding: density.pick(
2443 symmetric_insets(unit * 2.0, unit * 1.5),
2444 Insets::all(unit * 2.25),
2445 symmetric_insets(unit * 3.0, unit * 2.5),
2446 ),
2447 tooltip_min_width: density.pick(80.0, 96.0, 112.0),
2448 tooltip_gap: density.pick(unit * 2.0, unit * 2.5, unit * 3.0),
2449 tooltip_reveal_offset: density.pick(6.0, 8.0, 10.0),
2450 dialog_min_width: density.pick(240.0, 280.0, 320.0),
2451 dialog_max_width: density.pick(440.0, 520.0, 600.0),
2452 dialog_outer_margin: density.pick(unit * 4.0, unit * 6.0, unit * 8.0),
2453 dialog_padding: density.pick(
2454 Insets::all(unit * 3.5),
2455 Insets::all(18.0),
2456 Insets::all(unit * 6.0),
2457 ),
2458 dialog_title_font_size: density.pick(16.0, 16.0, 18.0),
2459 dialog_title_line_height: density.pick(24.0, 24.0, 26.0),
2460 dialog_description_gap: density.pick(unit * 1.5, unit * 2.0, unit * 2.5),
2461 dialog_body_gap: density.pick(unit * 3.0, 14.0, unit * 4.5),
2462 dialog_footer_gap: density.pick(unit * 3.5, 18.0, unit * 5.0),
2463 dialog_action_gap: density.pick(unit * 2.0, unit * 2.5, unit * 3.0),
2464 dialog_action_min_width: density.pick(92.0, 110.0, 128.0),
2465 toolbar_extent: density.pick(40.0, 52.0, 56.0),
2466 toolbar_padding: density.pick(
2467 Insets::all(unit * 1.5),
2468 Insets::all(unit * 2.0),
2469 Insets::all(unit * 2.25),
2470 ),
2471 toolbar_spacing: density.pick(unit * 1.5, unit * 2.0, unit * 2.25),
2472 command_group_padding: density.pick(
2473 Insets::all(unit * 0.25),
2474 Insets::all(unit * 0.5),
2475 Insets::all(unit * 0.75),
2476 ),
2477 command_group_spacing: density.pick(unit * 0.5, unit * 0.75, unit * 0.875),
2478 command_group_radius: density.pick(radius.md, radius.lg, radius.xl),
2479 tool_palette_item_size: density.pick(30.0, 40.0, 44.0),
2480 tool_palette_icon_size: density.pick(16.0, 20.0, 22.0),
2481 preset_strip_item_height: density.pick(24.0, 28.0, 36.0),
2482 preset_strip_item_min_width: density.pick(36.0, 44.0, 50.0),
2483 preset_strip_item_padding: density.pick(
2484 symmetric_insets(unit * 2.0, unit),
2485 symmetric_insets(unit * 3.0, unit),
2486 symmetric_insets(unit * 3.5, unit * 1.5),
2487 ),
2488 preset_strip_gap: density.pick(unit, unit * 1.5, unit * 1.75),
2489 preset_strip_label_padding: density.pick(
2490 Insets::all(unit * 0.75),
2491 Insets::all(unit),
2492 Insets::all(unit * 1.5),
2493 ),
2494 action_card_min_width: density.pick(252.0, 280.0, 300.0),
2495 action_card_min_height: density.pick(84.0, 104.0, 112.0),
2496 action_card_padding: density.pick(
2497 Insets {
2498 left: unit * 3.0,
2499 top: unit * 2.5,
2500 right: unit * 2.5,
2501 bottom: unit * 2.5,
2502 },
2503 Insets {
2504 left: unit * 4.0,
2505 top: unit * 3.5,
2506 right: unit * 3.5,
2507 bottom: unit * 3.5,
2508 },
2509 Insets {
2510 left: unit * 4.5,
2511 top: unit * 4.0,
2512 right: unit * 4.0,
2513 bottom: unit * 4.0,
2514 },
2515 ),
2516 action_card_icon_box_size: density.pick(32.0, 38.0, 42.0),
2517 action_card_icon_size: density.pick(16.0, 20.0, 22.0),
2518 action_card_icon_gap: density.pick(unit * 2.5, unit * 3.0, unit * 3.25),
2519 action_card_text_gap: density.pick(unit, unit * 1.25, unit * 1.5),
2520 action_card_trailing_gap: density.pick(18.0, 22.0, 26.0),
2521 action_card_chevron_size: density.pick(14.0, 16.0, 18.0),
2522 action_card_accent_width: density.pick(2.0, 3.0, 3.5),
2523 action_card_accent_inset: density.pick(unit * 2.0, unit * 2.5, unit * 2.75),
2524 status_bar_height: density.pick(34.0, 40.0, 46.0),
2525 status_bar_segment_padding: density.pick(unit * 2.0, unit * 2.5, unit * 2.75),
2526 status_bar_segment_min_width: density.pick(72.0, 86.0, 96.0),
2527 status_bar_separator_inset: density.pick(unit * 1.25, unit * 1.5, unit * 1.75),
2528 progress_bar_min_width: density.pick(180.0, 240.0, 260.0),
2529 progress_bar_height: density.pick(14.0, 18.0, 22.0),
2530 progress_bar_value_height: density.pick(22.0, 18.0, 22.0),
2531 progress_bar_label_padding: density.pick(
2532 Insets::all(unit * 0.5),
2533 Insets::all(unit * 0.5),
2534 Insets::all(unit * 0.75),
2535 ),
2536 property_row_label_width: density.pick(96.0, 112.0, 136.0),
2537 property_row_inline_gap: density.pick(unit * 1.5, unit * 2.0, unit * 3.0),
2538 property_row_stacked_gap: density.pick(unit, unit * 1.5, unit * 2.0),
2539 form_row_label_width: density.pick(112.0, 128.0, 144.0),
2540 form_row_control_width: density.pick(300.0, 340.0, 380.0),
2541 form_row_gap: density.pick(unit * 2.0, unit * 3.0, unit * 4.0),
2542 field_group_spacing: density.pick(unit * 1.5, unit * 2.0, unit * 3.0),
2543 form_section_padding: density.pick(
2544 Insets {
2545 left: unit * 2.5,
2546 top: unit * 2.0,
2547 right: unit * 2.5,
2548 bottom: unit * 2.5,
2549 },
2550 Insets {
2551 left: 14.0,
2552 top: unit * 3.0,
2553 right: 14.0,
2554 bottom: 14.0,
2555 },
2556 Insets {
2557 left: unit * 4.5,
2558 top: unit * 4.0,
2559 right: unit * 4.5,
2560 bottom: unit * 4.5,
2561 },
2562 ),
2563 form_section_body_gap: density.pick(unit * 2.0, unit * 3.0, unit * 4.0),
2564 form_section_header_gap: density.pick(unit * 2.0, unit * 2.5, unit * 3.0),
2565 form_section_description_gap: density.pick(unit * 0.5, unit * 0.75, unit),
2566 form_section_max_width: density.pick(600.0, 640.0, 720.0),
2567 form_section_radius: density.pick(radius.md, radius.lg, radius.xl),
2568 panel_section_gap: density.pick(unit * 1.5, unit * 2.0, unit * 3.0),
2569 panel_section_action_gap: density.pick(unit, unit * 1.5, unit * 2.0),
2570 panel_section_disclosure_size: density.pick(14.0, 16.0, 20.0),
2571 dock_panel_header_height: density.pick(28.0, 34.0, 44.0),
2572 dock_panel_padding: density.pick(
2573 symmetric_insets(unit * 2.0, unit * 1.5),
2574 symmetric_insets(unit * 2.5, unit * 2.0),
2575 symmetric_insets(unit * 3.5, unit * 3.0),
2576 ),
2577 data_viewport_padding: density.pick(
2578 Insets::all(unit * 0.75),
2579 Insets::all(unit),
2580 Insets::all(unit * 1.5),
2581 ),
2582 data_row_padding: density.pick(
2583 Insets {
2584 left: unit * 2.0,
2585 top: unit * 0.5,
2586 right: unit * 1.5,
2587 bottom: unit * 0.5,
2588 },
2589 Insets {
2590 left: unit * 2.5,
2591 top: unit * 0.75,
2592 right: unit * 2.0,
2593 bottom: unit * 0.75,
2594 },
2595 Insets {
2596 left: unit * 3.0,
2597 top: unit * 1.25,
2598 right: unit * 2.5,
2599 bottom: unit * 1.25,
2600 },
2601 ),
2602 data_row_icon_size: density.pick(12.0, 14.0, 16.0),
2603 data_row_icon_gap: density.pick(unit * 1.5, unit * 2.0, unit * 2.25),
2604 data_row_trailing_gap: density.pick(unit * 2.0, unit * 3.0, unit * 3.5),
2605 data_scroll_thumb_width: density.pick(3.0, 4.0, 6.0),
2606 data_scroll_thumb_inset: density.pick(5.0, 6.0, 8.0),
2607 data_scroll_thumb_radius: density.pick(radius.sm, radius.sm, radius.md),
2608 data_scroll_thumb_min_length: density.pick(24.0, 28.0, 44.0),
2609 data_scroll_thumb_opacity: density.pick(0.68, 0.75, 0.78),
2610 list_row_height: density.pick(24.0, 28.0, 36.0),
2611 layer_row_height: density.pick(28.0, 32.0, 40.0),
2612 layer_action_size: density.pick(20.0, 24.0, 28.0),
2613 layer_action_icon_inset: density.pick(4.5, 5.0, 7.0),
2614 layer_lock_icon_inset: density.pick(3.5, 4.0, 6.0),
2615 layer_visibility_stroke_width: density.pick(1.25, 1.4, 1.8),
2616 layer_visibility_slash_stroke_width: density.pick(1.45, 1.6, 2.0),
2617 layer_thumbnail_size: density.pick(22.0, 28.0, 32.0),
2618 layer_thumbnail_inset: density.pick(1.5, 2.0, 3.0),
2619 layer_thumbnail_radius: density.pick(radius.md, radius.md, radius.lg),
2620 layer_thumbnail_disabled_opacity: density.pick(0.34, 0.36, 0.40),
2621 layer_thumbnail_disabled_border_opacity: density.pick(0.52, 0.55, 0.60),
2622 tree_row_height: density.pick(24.0, 28.0, 36.0),
2623 tree_indent: density.pick(unit * 3.5, 16.0, 20.0),
2624 tree_disclosure_size: density.pick(10.0, 12.0, 14.0),
2625 tree_disclosure_gap: density.pick(unit, 6.0, unit * 1.5),
2626 table_row_height: density.pick(24.0, 28.0, 36.0),
2627 table_header_height: density.pick(24.0, 28.0, 36.0),
2628 table_cell_padding: unit * 2.5,
2629 table_header_separator_inset: density.pick(3.0, 4.0, 8.0),
2630 table_separator_width: density.pick(1.0, 1.0, 1.5),
2631 table_row_border_opacity: density.pick(0.50, 0.55, 0.60),
2632 breadcrumb_height: density.pick(28.0, 36.0, 40.0),
2633 breadcrumb_item_padding: density.pick(
2634 symmetric_insets(unit * 2.0, unit * 0.75),
2635 symmetric_insets(unit * 2.0, unit),
2636 symmetric_insets(unit * 2.5, unit * 1.5),
2637 ),
2638 breadcrumb_gap: density.pick(unit * 4.0, unit * 5.0, unit * 5.5),
2639 breadcrumb_separator_size: density.pick(9.0, 10.0, 11.0),
2640 image_corner_radius: density.pick(radius.md, radius.lg, radius.xl),
2641 color_swatch_width: density.pick(48.0, 56.0, 72.0),
2642 color_swatch_height: density.pick(28.0, 32.0, 44.0),
2643 color_swatch_inner_inset: density.pick(1.0, 1.0, 1.5),
2644 color_swatch_checker_size: density.pick(5.0, 6.0, 8.0),
2645 color_palette_swatch_size: density.pick(24.0, 28.0, 40.0),
2646 color_palette_gap: density.pick(unit * 1.25, unit * 1.5, unit * 2.0),
2647 color_palette_swatch_inset: density.pick(2.0, 2.0, 3.0),
2648 color_palette_selected_swatch_inset: density.pick(3.0, 3.0, 4.0),
2649 color_palette_checker_size: density.pick(5.0, 5.0, 7.0),
2650 brush_preview_min_width: density.pick(220.0, 260.0, 320.0),
2651 brush_preview_min_height: density.pick(58.0, 70.0, 88.0),
2652 brush_preview_padding: density.pick(
2653 Insets::all(unit * 1.5),
2654 Insets::all(unit * 2.0),
2655 Insets::all(unit * 3.0),
2656 ),
2657 brush_preview_swatch_width: density.pick(46.0, 54.0, 72.0),
2658 brush_preview_swatch_gap: density.pick(unit * 2.0, unit * 2.5, unit * 3.5),
2659 brush_preview_checker_size: density.pick(5.0, 6.0, 8.0),
2660 brush_preview_text_height: density.pick(15.0, 16.0, 18.0),
2661 brush_preview_text_font_size: density.pick(10.0, 11.0, 12.0),
2662 brush_preview_text_line_height: density.pick(13.0, 14.0, 16.0),
2663 color_picker_content_inset: density.pick(unit * 3.0, 14.0, unit * 4.5),
2664 color_picker_panel_gap: density.pick(unit * 2.5, 14.0, unit * 4.5),
2665 color_picker_top_bar_height: density.pick(40.0, 52.0, 64.0),
2666 color_picker_swatch_width: density.pick(64.0, 96.0, 112.0),
2667 color_picker_swatch_gap: density.pick(unit * 2.0, unit * 2.5, unit * 3.0),
2668 color_picker_section_gap: density.pick(14.0, 14.0, 18.0),
2669 color_picker_wheel_size: density.pick(128.0, 166.0, 210.0),
2670 color_picker_map_size: density.pick(132.0, 210.0, 240.0),
2671 color_picker_row_height: density.pick(24.0, 24.0, 44.0),
2672 color_picker_row_gap: density.pick(unit * 2.0, unit * 2.0, unit * 3.0),
2673 color_picker_right_panel_width: density.pick(150.0, 226.0, 280.0),
2674 color_picker_field_height: density.pick(28.0, 30.0, 44.0),
2675 color_picker_field_gap: density.pick(12.0, 12.0, 16.0),
2676 color_picker_dropdown_gap: density.pick(unit, unit, unit * 2.0),
2677 color_picker_encoding_menu_row_height: density.pick(28.0, 28.0, 44.0),
2678 scroll_bar_thickness: density.pick(10.0, 12.0, 18.0),
2679 scroll_bar_min_thumb_length: density.pick(24.0, 28.0, 44.0),
2680 split_view_divider_thickness: density.pick(1.0, 1.0, 2.0),
2681 split_view_drag_target_thickness: density.pick(10.0, 12.0, 44.0),
2682 floating_workspace_margin: density.pick(unit * 2.0, unit * 3.0, unit * 4.5),
2683 floating_view_title_bar_height: density.pick(30.0, 32.0, 52.0),
2684 floating_view_title_padding: density.pick(
2685 symmetric_insets(unit * 2.5, unit * 1.5),
2686 symmetric_insets(14.0, unit * 2.0),
2687 symmetric_insets(unit * 4.5, unit * 3.5),
2688 ),
2689 floating_view_resize_handle_size: density.pick(16.0, 18.0, 28.0),
2690 canvas_ruler_extent: density.pick(20.0, 22.0, 32.0),
2691 canvas_ruler_major_tick: density.pick(9.0, 10.0, 16.0),
2692 canvas_ruler_minor_tick: density.pick(4.0, 5.0, 8.0),
2693 canvas_ruler_target_major_spacing: density.pick(84.0, 96.0, 120.0),
2694 canvas_ruler_label_padding: density.pick(
2695 Insets::all(unit * 0.5),
2696 symmetric_insets(unit * 0.75, unit * 0.5),
2697 Insets::all(unit),
2698 ),
2699 canvas_ruler_label_max_width: density.pick(48.0, 54.0, 72.0),
2700 canvas_grid_step: density.pick(32.0, 40.0, 48.0),
2701 canvas_axis_overscan: density.pick(72.0, 80.0, 96.0),
2702 pixel_canvas_fit_padding: density.pick(20.0, 24.0, 32.0),
2703 pixel_canvas_grid_zoom: 6.0,
2704 pixel_canvas_nearest_sampling_zoom: 1.0,
2705 pixel_canvas_zoom_step: 1.1,
2706 corner_radius: density.pick(radius.md, radius.md, radius.lg),
2707 indicator_corner_radius: radius.sm + 1.0,
2708 border_width: 1.0,
2709 focus_ring_width: 2.0,
2710 focus_ring_outset: 2.0,
2712 caret_width: 2.0,
2713 }
2714 }
2715}
2716
2717impl Default for ControlMetrics {
2718 fn default() -> Self {
2719 Self::from_tokens(4.0, ThemeRadii::default(), ThemeDensity::default())
2720 }
2721}
2722
2723#[derive(Debug, Clone, Copy, PartialEq)]
2724pub struct DefaultTheme {
2725 pub fonts: ThemeFontFamilies,
2726 pub colors: ThemeColors,
2727 pub density: ThemeDensity,
2728 pub control_size: Option<ControlSize>,
2730 pub spacing: f32,
2731 pub breakpoints: ThemeBreakpoints,
2732 pub containers: ThemeContainers,
2733 pub text: ThemeTextScale,
2734 pub font_weights: ThemeFontWeights,
2735 pub tracking: ThemeTracking,
2736 pub leading: ThemeLeading,
2737 pub radius: ThemeRadii,
2738 pub shadows: ThemeShadows,
2739 pub glows: ThemeGlows,
2742 pub blur: ThemeBlurScale,
2743 pub perspective: ThemePerspective,
2744 pub aspect: ThemeAspectRatios,
2745 pub motion: ThemeMotion,
2746 pub hdr: HdrThemeTokens,
2747 pub palette: ControlPalette,
2748 pub surfaces: SurfacePalette,
2749 pub decorative: DecorativePalette,
2751 pub typography: ControlTypography,
2752 pub interaction: ControlStateMetrics,
2753 pub metrics: ControlMetrics,
2754}
2755
2756impl DefaultTheme {
2757 pub fn new() -> Self {
2758 Self::default()
2759 }
2760
2761 pub fn sui() -> Self {
2763 Self::light()
2764 }
2765
2766 pub fn light() -> Self {
2768 static THEME: OnceLock<DefaultTheme> = OnceLock::new();
2769 *THEME.get_or_init(|| Self::from_colors(ThemeColors::light()))
2770 }
2771
2772 pub fn neutral() -> Self {
2775 static THEME: OnceLock<DefaultTheme> = OnceLock::new();
2776 *THEME.get_or_init(|| Self::from_colors(ThemeColors::neutral()))
2777 }
2778
2779 pub fn neutral_dark() -> Self {
2781 static THEME: OnceLock<DefaultTheme> = OnceLock::new();
2782 *THEME.get_or_init(|| Self::from_colors(ThemeColors::neutral_dark()))
2783 }
2784
2785 pub fn dark() -> Self {
2787 static THEME: OnceLock<DefaultTheme> = OnceLock::new();
2788 *THEME.get_or_init(|| Self::from_colors(ThemeColors::dark()))
2789 }
2790
2791 pub fn high_contrast() -> Self {
2792 static THEME: OnceLock<DefaultTheme> = OnceLock::new();
2793 *THEME.get_or_init(|| Self::from_colors(ThemeColors::high_contrast()))
2794 }
2795
2796 pub fn void() -> Self {
2799 Self::high_contrast()
2800 }
2801
2802 pub fn compact() -> Self {
2803 Self::default().with_size(ControlSize::Small)
2804 }
2805
2806 pub fn comfortable() -> Self {
2807 Self::default().with_size(ControlSize::Medium)
2808 }
2809
2810 pub fn touch() -> Self {
2811 Self::default().with_size(ControlSize::Large)
2812 }
2813
2814 pub fn from_colors(colors: ThemeColors) -> Self {
2817 let text = ThemeTextScale::default();
2818 let radius = ThemeRadii::default();
2819 let spacing = 4.0;
2820 let density = ThemeDensity::default();
2821 let hdr = HdrThemeTokens::from_colors(colors);
2822 let palette = ControlPalette::from_colors(&colors);
2823 let surfaces = SurfacePalette::from_theme_parts(&colors, &palette);
2824 let decorative = DecorativePalette::from_colors(&colors);
2825
2826 let mut theme = Self {
2827 fonts: ThemeFontFamilies::default(),
2828 colors,
2829 density,
2830 control_size: Some(ControlSize::Medium),
2831 spacing,
2832 breakpoints: ThemeBreakpoints::default(),
2833 containers: ThemeContainers::default(),
2834 text,
2835 font_weights: ThemeFontWeights::default(),
2836 tracking: ThemeTracking::default(),
2837 leading: ThemeLeading::default(),
2838 radius,
2839 shadows: ThemeShadows::for_colors(&colors),
2840 glows: ThemeGlows::for_colors(&colors),
2841 blur: ThemeBlurScale::default(),
2842 perspective: ThemePerspective::default(),
2843 aspect: ThemeAspectRatios::default(),
2844 motion: ThemeMotion::default(),
2845 hdr,
2846 palette,
2847 surfaces,
2848 decorative,
2849 typography: ControlTypography::for_density(&text, density),
2850 interaction: ControlStateMetrics::for_density(density),
2851 metrics: ControlMetrics::from_tokens(spacing, radius, density),
2852 };
2853 theme.sync_size_fields(ControlSize::Medium);
2854 theme
2855 }
2856
2857 pub fn with_density(mut self, density: ThemeDensity) -> Self {
2858 self.density = density;
2859 self.control_size = None;
2860 self.sync_density_fields();
2861 self
2862 }
2863
2864 pub fn with_size(mut self, size: ControlSize) -> Self {
2871 self.density = size.legacy_density();
2872 self.control_size = Some(size);
2873 self.sync_size_fields(size);
2874 self
2875 }
2876
2877 fn sync_size_fields(&mut self, size: ControlSize) {
2878 self.interaction = ControlStateMetrics::for_density(size.legacy_density());
2879 self.metrics =
2880 ControlMetrics::from_tokens(self.spacing, self.radius, size.legacy_density());
2881 self.metrics.min_height = size.control_height();
2882 self.metrics.touch_target_size = 44.0;
2883 self.metrics.icon_size = size.icon_size();
2884 self.metrics.icon_button_size = size.control_height();
2885 self.metrics.list_row_height = size.row_height();
2886 self.metrics.tree_row_height = size.row_height();
2887 self.metrics.table_row_height = size.row_height();
2888 self.metrics.menu_row_height = size.row_height();
2889 self.typography = ControlTypography::for_size(&self.text, size);
2890 }
2891
2892 fn sync_density_fields(&mut self) {
2893 if let Some(size) = self
2894 .control_size
2895 .filter(|size| size.legacy_density() == self.density)
2896 {
2897 self.sync_size_fields(size);
2898 return;
2899 }
2900 self.control_size = None;
2901 self.typography = ControlTypography::for_density(&self.text, self.density);
2902 self.interaction = ControlStateMetrics::for_density(self.density);
2903 self.metrics = ControlMetrics::from_tokens(self.spacing, self.radius, self.density);
2904 }
2905
2906 pub fn sync_derived_fields(&mut self) {
2907 self.hdr.sync_semantic_defaults(self.colors);
2908 self.palette = ControlPalette::from_colors(&self.colors);
2909 self.surfaces = SurfacePalette::from_theme_parts(&self.colors, &self.palette);
2910 self.decorative = DecorativePalette::from_colors(&self.colors);
2911 self.shadows = ThemeShadows::for_colors(&self.colors);
2912 self.glows = ThemeGlows::for_colors(&self.colors);
2913 self.sync_density_fields();
2914 }
2915
2916 pub fn text_style(&self, color: Color) -> TextStyle {
2917 self.text_style_with_font_stack(color, self.fonts.sans)
2918 }
2919
2920 pub fn serif_text_style(&self, color: Color) -> TextStyle {
2922 self.text_style_with_font_stack(color, self.fonts.serif)
2923 }
2924
2925 pub fn mono_text_style(&self, color: Color) -> TextStyle {
2927 self.text_style_with_font_stack(color, self.fonts.mono)
2928 }
2929
2930 fn text_style_with_font_stack(&self, color: Color, fonts: ThemeFontStack) -> TextStyle {
2931 TextStyle {
2932 font_families: Some(fonts.into()),
2933 font_size: self.typography.body_font_size.max(1.0),
2934 line_height: self.typography.body_line_height.max(1.0),
2935 color,
2936 ..TextStyle::default()
2937 }
2938 }
2939
2940 pub fn semantic_tone_colors(&self, tone: SemanticTone) -> (Color, Color) {
2941 match tone {
2942 SemanticTone::Neutral => (self.palette.button, self.palette.text),
2943 SemanticTone::Accent => (self.palette.accent, self.palette.accent_text),
2944 SemanticTone::Info => (self.palette.info, self.palette.info_text),
2945 SemanticTone::Success => (self.palette.success, self.palette.success_text),
2946 SemanticTone::Warning => (self.palette.warning, self.palette.warning_text),
2947 SemanticTone::Danger => (self.palette.danger, self.palette.danger_text),
2948 }
2949 }
2950
2951 pub fn glow_for_output(
2959 &self,
2960 tone: GlowTone,
2961 output: Option<sui_runtime::OutputColorRange>,
2962 ) -> ThemeShadow {
2963 let glow = self.glows.get(tone);
2964 let role = match tone {
2965 GlowTone::Accent => WidgetColorRole::Accent,
2966 GlowTone::Secondary => WidgetColorRole::Secondary,
2967 };
2968 let style = resolve_widget_hdr_style(
2969 &self.hdr.limited_to(output),
2970 role,
2971 WidgetLuminanceRole::EmissiveIndicator,
2972 WidgetMaterialRole::Flat,
2973 Some(WidgetEffectRole::Glow),
2974 );
2975 if style.effect.is_none() {
2976 return glow;
2977 }
2978 let color = crate::controls::apply_hdr_policy_cap(style.color, style.peak_lift);
2979 let lit = |layer: Option<ThemeShadowLayer>| {
2980 layer.map(|layer| ThemeShadowLayer {
2981 color: color.with_alpha(layer.color.alpha),
2982 ..layer
2983 })
2984 };
2985 ThemeShadow {
2986 first: lit(glow.first),
2987 second: lit(glow.second),
2988 }
2989 }
2990
2991 pub fn tone_roles(&self, tone: SemanticTone) -> ToneRoles {
2995 let palette = &self.palette;
2996 match tone {
2997 SemanticTone::Neutral => ToneRoles {
2998 solid: palette.button,
2999 on_solid: palette.text,
3000 hover: palette.button_hover,
3001 pressed: palette.button_pressed,
3002 soft: palette.control,
3003 text: palette.text,
3004 border: palette.button_border,
3005 },
3006 SemanticTone::Accent => ToneRoles {
3007 solid: palette.accent,
3008 on_solid: palette.accent_text,
3009 hover: palette.accent_hover,
3010 pressed: palette.accent_pressed,
3011 soft: palette.accent_soft,
3012 text: palette.accent_soft_text,
3013 border: palette.accent_border,
3014 },
3015 SemanticTone::Info => ToneRoles {
3016 solid: palette.info,
3017 on_solid: palette.info_text,
3018 hover: palette.info_hover,
3019 pressed: palette.info_pressed,
3020 soft: palette.info_soft,
3021 text: palette.info_soft_text,
3022 border: palette.info_border,
3023 },
3024 SemanticTone::Success => ToneRoles {
3025 solid: palette.success,
3026 on_solid: palette.success_text,
3027 hover: palette.success_hover,
3028 pressed: palette.success_pressed,
3029 soft: palette.success_soft,
3030 text: palette.success_soft_text,
3031 border: palette.success_border,
3032 },
3033 SemanticTone::Warning => ToneRoles {
3034 solid: palette.warning,
3035 on_solid: palette.warning_text,
3036 hover: palette.warning_hover,
3037 pressed: palette.warning_pressed,
3038 soft: palette.warning_soft,
3039 text: palette.warning_soft_text,
3040 border: palette.warning_border,
3041 },
3042 SemanticTone::Danger => ToneRoles {
3043 solid: palette.danger,
3044 on_solid: palette.danger_text,
3045 hover: palette.danger_hover,
3046 pressed: palette.danger_pressed,
3047 soft: palette.danger_soft,
3048 text: palette.danger_soft_text,
3049 border: palette.danger_border,
3050 },
3051 }
3052 }
3053
3054 pub fn semantic_tone_color(&self, tone: SemanticTone) -> Color {
3055 self.semantic_tone_colors(tone).0
3056 }
3057
3058 pub fn semantic_tone_text_color(&self, tone: SemanticTone) -> Color {
3059 self.semantic_tone_colors(tone).1
3060 }
3061
3062 pub fn semantic_tone_soft_colors(&self, tone: SemanticTone) -> (Color, Color) {
3067 match tone {
3068 SemanticTone::Neutral => (self.palette.control, self.palette.text_muted),
3069 SemanticTone::Accent => (self.palette.accent_soft, self.palette.accent_soft_text),
3070 SemanticTone::Info => (self.palette.info_soft, self.palette.info_soft_text),
3071 SemanticTone::Success => (self.palette.success_soft, self.palette.success_soft_text),
3072 SemanticTone::Warning => (self.palette.warning_soft, self.palette.warning_soft_text),
3073 SemanticTone::Danger => (self.palette.danger_soft, self.palette.danger_soft_text),
3074 }
3075 }
3076
3077 pub fn for_disabled_control(mut self) -> Self {
3082 let interaction = self.interaction;
3083 let p = &mut self.palette;
3084 let control = p.control;
3085 let fade = |color: Color| {
3086 color
3087 .mix_oklab(control, 0.72)
3088 .with_alpha(color.alpha * interaction.disabled_opacity)
3089 };
3090 let soften =
3091 |color: Color| color.with_alpha(color.alpha * interaction.disabled_content_opacity);
3092 let ink = p.text_disabled;
3093 p.text = ink;
3094 p.text_muted = ink;
3095 p.placeholder = ink;
3096 p.caret = ink;
3097 p.field = fade(p.field);
3098 p.button = fade(p.button);
3099 p.button_hover = p.button;
3100 p.button_pressed = p.button;
3101 p.button_border = soften(p.button_border);
3102 p.border = soften(p.border);
3103 p.border_strong = soften(p.border_strong);
3104 p.border_control = soften(p.border_control);
3105 p.border_hover = p.border_control;
3106 p.border_focus = p.border_control;
3107 p.accent = fade(p.accent);
3108 p.accent_hover = p.accent;
3109 p.accent_pressed = p.accent;
3110 p.accent_border = soften(p.accent_border);
3111 p.accent_border_hover = p.accent_border;
3112 p.accent_border_focus = p.accent_border;
3113 p.accent_text = soften(p.accent_text);
3114 p.accent_soft = fade(p.accent_soft);
3115 p.accent_soft_text = ink;
3116 p.selection = fade(p.selection);
3117 p.focus_ring = Color::TRANSPARENT;
3118 self
3119 }
3120
3121 pub fn body_text_style(&self) -> TextStyle {
3122 self.text_style(self.palette.text)
3123 }
3124
3125 pub fn placeholder_text_style(&self) -> TextStyle {
3126 self.text_style(self.palette.placeholder)
3127 }
3128
3129 pub fn button_text_style(&self) -> TextStyle {
3130 self.text_style(self.palette.accent_text)
3131 }
3132}
3133
3134impl Default for DefaultTheme {
3135 fn default() -> Self {
3136 Self::sui()
3137 }
3138}
3139
3140fn mix(from: Color, to: Color, amount: f32) -> Color {
3141 let amount = amount.clamp(0.0, 1.0);
3142
3143 Color::new(
3144 from.space,
3145 from.red + (to.red - from.red) * amount,
3146 from.green + (to.green - from.green) * amount,
3147 from.blue + (to.blue - from.blue) * amount,
3148 from.alpha + (to.alpha - from.alpha) * amount,
3149 )
3150 .clamped()
3151}
3152
3153fn rgb8(red: u8, green: u8, blue: u8) -> Color {
3154 Color::rgba(
3155 f32::from(red) / 255.0,
3156 f32::from(green) / 255.0,
3157 f32::from(blue) / 255.0,
3158 1.0,
3159 )
3160}
3161
3162fn shadow_layer(
3163 offset_x: f32,
3164 offset_y: f32,
3165 blur: f32,
3166 spread: f32,
3167 color: Color,
3168 inset: bool,
3169) -> ThemeShadowLayer {
3170 ThemeShadowLayer {
3171 offset_x,
3172 offset_y,
3173 blur,
3174 spread,
3175 color,
3176 inset,
3177 }
3178}
3179
3180#[cfg(test)]
3181mod tests {
3182 use super::{
3183 Color, ControlSize, ControlTypography, DecorativeHue, DefaultTheme, GlowTone, SemanticTone,
3184 ThemeColorScheme, ThemeColors, ThemeDensity, ThemeShadow, ThemeTextScale, rgb8,
3185 };
3186 use crate::hdr_theme::HdrThemeMode;
3187 use sui_core::ColorSpace;
3188 use sui_runtime::OutputColorRange;
3189
3190 #[test]
3191 fn glows_brighten_to_the_accents_hdr_color_where_the_output_and_theme_allow() {
3192 assert_eq!(
3193 DefaultTheme::light().glow_for_output(GlowTone::Accent, None),
3194 ThemeShadow::empty(),
3195 "light themes have no glow"
3196 );
3197 let dark = DefaultTheme::dark();
3198 assert_eq!(
3199 dark.glow_for_output(GlowTone::Accent, Some(OutputColorRange::HighDynamicRange)),
3200 dark.glows.accent,
3201 "the theme's token while its HDR mode is off"
3202 );
3203
3204 let mut hdr = dark;
3205 hdr.hdr.mode = HdrThemeMode::FullHdr;
3206 let token = dark.glows.accent.first.unwrap();
3207 let bright = hdr
3208 .glow_for_output(GlowTone::Accent, Some(OutputColorRange::HighDynamicRange))
3209 .first
3210 .unwrap();
3211 assert_eq!(bright.color.space, ColorSpace::LinearDisplayP3);
3212 assert_eq!(bright.color.alpha, token.color.alpha);
3213 assert_eq!(bright.blur, token.blur);
3214 let wide = hdr
3215 .glow_for_output(GlowTone::Accent, Some(OutputColorRange::WideGamut))
3216 .first
3217 .unwrap();
3218 assert_eq!(wide.color.space, ColorSpace::DisplayP3);
3219 assert_eq!(
3220 hdr.glow_for_output(GlowTone::Accent, Some(OutputColorRange::Standard)),
3221 dark.glows.accent,
3222 "a standard output gets the token"
3223 );
3224 assert_ne!(
3225 hdr.glow_for_output(
3226 GlowTone::Secondary,
3227 Some(OutputColorRange::HighDynamicRange)
3228 )
3229 .first
3230 .unwrap()
3231 .color,
3232 bright.color,
3233 "the secondary glow takes the secondary color"
3234 );
3235 }
3236
3237 #[test]
3238 fn default_theme_uses_body_text_scale_for_typography() {
3239 let theme = DefaultTheme::default();
3240
3241 assert_eq!(theme.typography.body_font_size, theme.text.base.size);
3242 assert_eq!(
3243 theme.typography.body_line_height,
3244 theme.text.base.line_height
3245 );
3246 assert_eq!(theme.typography.body_font_size, 15.0);
3247 assert_eq!(theme.typography.body_line_height, 22.0);
3248 assert_eq!(theme.control_size, Some(ControlSize::Medium));
3249 assert_eq!(theme.density, ThemeDensity::Comfortable);
3250 assert_eq!(theme.metrics.min_height, 32.0);
3251 assert_eq!(theme.metrics.touch_target_size, 44.0);
3252 assert_eq!(theme.metrics.icon_button_size, 32.0);
3253 assert_eq!(
3254 theme.body_text_style().font_families,
3255 Some(theme.fonts.sans.into())
3256 );
3257 assert_eq!(
3258 theme.serif_text_style(theme.palette.text).font_families,
3259 Some(theme.fonts.serif.into())
3260 );
3261 assert_eq!(
3262 theme.mono_text_style(theme.palette.text).font_families,
3263 Some(theme.fonts.mono.into())
3264 );
3265 }
3266
3267 #[test]
3268 fn density_presets_update_control_metrics_and_interactions() {
3269 let compact = DefaultTheme::compact();
3270 let comfortable = DefaultTheme::comfortable();
3271 let touch = DefaultTheme::touch();
3272
3273 assert_eq!(compact.density, ThemeDensity::Compact);
3274 assert_eq!(comfortable.density, ThemeDensity::Comfortable);
3275 assert_eq!(touch.density, ThemeDensity::Touch);
3276 assert!(compact.metrics.min_height < comfortable.metrics.min_height);
3277 assert!(comfortable.metrics.min_height < touch.metrics.min_height);
3278 assert!(compact.metrics.menu_row_height < comfortable.metrics.menu_row_height);
3279 assert!(comfortable.metrics.menu_row_height < touch.metrics.menu_row_height);
3280 assert!(compact.metrics.list_row_height < comfortable.metrics.list_row_height);
3281 assert!(comfortable.metrics.list_row_height < touch.metrics.list_row_height);
3282 assert!(compact.metrics.layer_row_height < comfortable.metrics.layer_row_height);
3283 assert!(comfortable.metrics.layer_row_height < touch.metrics.layer_row_height);
3284 assert!(
3285 compact.metrics.layer_action_icon_inset < comfortable.metrics.layer_action_icon_inset
3286 );
3287 assert!(
3288 comfortable.metrics.layer_action_icon_inset < touch.metrics.layer_action_icon_inset
3289 );
3290 assert!(
3291 compact.metrics.layer_visibility_stroke_width
3292 < comfortable.metrics.layer_visibility_stroke_width
3293 );
3294 assert!(
3295 comfortable.metrics.layer_visibility_stroke_width
3296 < touch.metrics.layer_visibility_stroke_width
3297 );
3298 assert!(compact.metrics.layer_thumbnail_inset < comfortable.metrics.layer_thumbnail_inset);
3299 assert!(comfortable.metrics.layer_thumbnail_inset < touch.metrics.layer_thumbnail_inset);
3300 assert!(
3301 compact.metrics.layer_thumbnail_radius <= comfortable.metrics.layer_thumbnail_radius
3302 );
3303 assert!(comfortable.metrics.layer_thumbnail_radius < touch.metrics.layer_thumbnail_radius);
3304 assert!(compact.metrics.table_row_height < comfortable.metrics.table_row_height);
3305 assert!(comfortable.metrics.table_row_height < touch.metrics.table_row_height);
3306 assert!(
3307 compact.metrics.data_scroll_thumb_width < comfortable.metrics.data_scroll_thumb_width
3308 );
3309 assert!(
3310 comfortable.metrics.data_scroll_thumb_width < touch.metrics.data_scroll_thumb_width
3311 );
3312 assert!(
3313 compact.metrics.data_scroll_thumb_min_length
3314 < comfortable.metrics.data_scroll_thumb_min_length
3315 );
3316 assert!(
3317 comfortable.metrics.data_scroll_thumb_min_length
3318 < touch.metrics.data_scroll_thumb_min_length
3319 );
3320 assert!(
3321 compact.metrics.table_header_separator_inset
3322 < comfortable.metrics.table_header_separator_inset
3323 );
3324 assert!(
3325 comfortable.metrics.table_header_separator_inset
3326 < touch.metrics.table_header_separator_inset
3327 );
3328 assert!(compact.metrics.breadcrumb_height < comfortable.metrics.breadcrumb_height);
3329 assert!(comfortable.metrics.breadcrumb_height < touch.metrics.breadcrumb_height);
3330 assert!(
3331 compact.metrics.action_card_min_height < comfortable.metrics.action_card_min_height
3332 );
3333 assert!(comfortable.metrics.action_card_min_height < touch.metrics.action_card_min_height);
3334 assert!(compact.metrics.status_bar_height < comfortable.metrics.status_bar_height);
3335 assert!(comfortable.metrics.status_bar_height < touch.metrics.status_bar_height);
3336 assert!(compact.metrics.progress_bar_height < comfortable.metrics.progress_bar_height);
3337 assert!(comfortable.metrics.progress_bar_height < touch.metrics.progress_bar_height);
3338 assert!(compact.metrics.tooltip_gap < comfortable.metrics.tooltip_gap);
3339 assert!(comfortable.metrics.tooltip_gap < touch.metrics.tooltip_gap);
3340 assert!(compact.metrics.tooltip_min_width < comfortable.metrics.tooltip_min_width);
3341 assert!(comfortable.metrics.tooltip_min_width < touch.metrics.tooltip_min_width);
3342 assert!(compact.metrics.popover_reveal_offset < comfortable.metrics.popover_reveal_offset);
3343 assert!(comfortable.metrics.popover_reveal_offset < touch.metrics.popover_reveal_offset);
3344 assert!(compact.metrics.dialog_max_width < comfortable.metrics.dialog_max_width);
3345 assert!(comfortable.metrics.dialog_max_width < touch.metrics.dialog_max_width);
3346 assert!(
3347 compact.metrics.dialog_action_min_width < comfortable.metrics.dialog_action_min_width
3348 );
3349 assert!(
3350 comfortable.metrics.dialog_action_min_width < touch.metrics.dialog_action_min_width
3351 );
3352 assert!(compact.metrics.toolbar_extent < comfortable.metrics.toolbar_extent);
3353 assert!(comfortable.metrics.toolbar_extent < touch.metrics.toolbar_extent);
3354 assert!(
3355 compact.metrics.tool_palette_item_size < comfortable.metrics.tool_palette_item_size
3356 );
3357 assert!(comfortable.metrics.tool_palette_item_size < touch.metrics.tool_palette_item_size);
3358 assert!(
3359 compact.metrics.preset_strip_item_height < comfortable.metrics.preset_strip_item_height
3360 );
3361 assert!(
3362 comfortable.metrics.preset_strip_item_height < touch.metrics.preset_strip_item_height
3363 );
3364 assert!(
3365 compact.metrics.property_row_label_width < comfortable.metrics.property_row_label_width
3366 );
3367 assert!(
3368 comfortable.metrics.property_row_label_width < touch.metrics.property_row_label_width
3369 );
3370 assert!(compact.metrics.form_row_gap < comfortable.metrics.form_row_gap);
3371 assert!(comfortable.metrics.form_row_gap < touch.metrics.form_row_gap);
3372 assert!(compact.metrics.field_group_spacing < comfortable.metrics.field_group_spacing);
3373 assert!(comfortable.metrics.field_group_spacing < touch.metrics.field_group_spacing);
3374 assert!(
3375 compact.metrics.form_section_max_width < comfortable.metrics.form_section_max_width
3376 );
3377 assert!(comfortable.metrics.form_section_max_width < touch.metrics.form_section_max_width);
3378 assert!(compact.metrics.panel_section_gap < comfortable.metrics.panel_section_gap);
3379 assert!(comfortable.metrics.panel_section_gap < touch.metrics.panel_section_gap);
3380 assert!(
3381 compact.metrics.dock_panel_header_height < comfortable.metrics.dock_panel_header_height
3382 );
3383 assert!(
3384 comfortable.metrics.dock_panel_header_height < touch.metrics.dock_panel_header_height
3385 );
3386 assert!(compact.metrics.tab_height < comfortable.metrics.tab_height);
3387 assert!(comfortable.metrics.tab_height < touch.metrics.tab_height);
3388 assert!(compact.metrics.scroll_bar_thickness < comfortable.metrics.scroll_bar_thickness);
3389 assert!(comfortable.metrics.scroll_bar_thickness < touch.metrics.scroll_bar_thickness);
3390 assert!(
3391 compact.metrics.scroll_bar_min_thumb_length
3392 < comfortable.metrics.scroll_bar_min_thumb_length
3393 );
3394 assert!(
3395 comfortable.metrics.scroll_bar_min_thumb_length
3396 < touch.metrics.scroll_bar_min_thumb_length
3397 );
3398 assert!(
3399 compact.metrics.split_view_drag_target_thickness
3400 < comfortable.metrics.split_view_drag_target_thickness
3401 );
3402 assert!(
3403 comfortable.metrics.split_view_drag_target_thickness
3404 < touch.metrics.split_view_drag_target_thickness
3405 );
3406 assert!(
3407 compact.metrics.floating_workspace_margin
3408 < comfortable.metrics.floating_workspace_margin
3409 );
3410 assert!(
3411 comfortable.metrics.floating_workspace_margin < touch.metrics.floating_workspace_margin
3412 );
3413 assert!(
3414 compact.metrics.floating_view_title_bar_height
3415 < comfortable.metrics.floating_view_title_bar_height
3416 );
3417 assert!(
3418 comfortable.metrics.floating_view_title_bar_height
3419 < touch.metrics.floating_view_title_bar_height
3420 );
3421 assert!(
3422 compact.metrics.floating_view_resize_handle_size
3423 < comfortable.metrics.floating_view_resize_handle_size
3424 );
3425 assert!(
3426 comfortable.metrics.floating_view_resize_handle_size
3427 < touch.metrics.floating_view_resize_handle_size
3428 );
3429 assert!(compact.metrics.canvas_ruler_extent < comfortable.metrics.canvas_ruler_extent);
3430 assert!(comfortable.metrics.canvas_ruler_extent < touch.metrics.canvas_ruler_extent);
3431 assert!(
3432 compact.metrics.canvas_ruler_label_max_width
3433 < comfortable.metrics.canvas_ruler_label_max_width
3434 );
3435 assert!(
3436 comfortable.metrics.canvas_ruler_label_max_width
3437 < touch.metrics.canvas_ruler_label_max_width
3438 );
3439 assert!(
3440 compact.metrics.canvas_ruler_target_major_spacing
3441 < comfortable.metrics.canvas_ruler_target_major_spacing
3442 );
3443 assert!(
3444 comfortable.metrics.canvas_ruler_target_major_spacing
3445 < touch.metrics.canvas_ruler_target_major_spacing
3446 );
3447 assert!(compact.metrics.canvas_grid_step < comfortable.metrics.canvas_grid_step);
3448 assert!(comfortable.metrics.canvas_grid_step < touch.metrics.canvas_grid_step);
3449 assert!(
3450 compact.metrics.pixel_canvas_fit_padding < comfortable.metrics.pixel_canvas_fit_padding
3451 );
3452 assert!(
3453 comfortable.metrics.pixel_canvas_fit_padding < touch.metrics.pixel_canvas_fit_padding
3454 );
3455 assert!(compact.metrics.icon_size < comfortable.metrics.icon_size);
3456 assert!(comfortable.metrics.icon_size < touch.metrics.icon_size);
3457 assert!(
3458 compact.interaction.tab_selected_blend < comfortable.interaction.tab_selected_blend
3459 );
3460 assert!(comfortable.interaction.tab_selected_blend < touch.interaction.tab_selected_blend);
3461 assert_eq!(compact.interaction.pressed_offset, 0.0);
3462 assert_eq!(comfortable.interaction.pressed_offset, 0.0);
3463 assert_eq!(touch.interaction.pressed_offset, 0.0);
3464 }
3465
3466 #[test]
3467 fn contextual_control_sizes_match_authored_geometry_and_typography() {
3468 assert_eq!(ControlSize::default(), ControlSize::Medium);
3469
3470 let base = DefaultTheme::default();
3471 let small = base.with_size(ControlSize::Small);
3472 let medium = base.with_size(ControlSize::Medium);
3473 let large = base.with_size(ControlSize::Large);
3474
3475 assert_eq!(small.density, ThemeDensity::Compact);
3476 assert_eq!(medium.density, ThemeDensity::Comfortable);
3477 assert_eq!(large.density, ThemeDensity::Touch);
3478 assert_eq!(small.metrics.min_height, 28.0);
3479 assert_eq!(medium.metrics.min_height, 32.0);
3480 assert_eq!(large.metrics.min_height, 40.0);
3481 assert_eq!(small.metrics.list_row_height, 24.0);
3482 assert_eq!(medium.metrics.list_row_height, 28.0);
3483 assert_eq!(large.metrics.list_row_height, 36.0);
3484 assert_eq!(small.metrics.touch_target_size, 44.0);
3485 assert_eq!(medium.metrics.touch_target_size, 44.0);
3486 assert_eq!(large.metrics.touch_target_size, 44.0);
3487 assert_eq!(small.typography.body_font_size, base.text.sm.size);
3488 assert_eq!(medium.typography.body_font_size, base.text.base.size);
3489 assert_eq!(large.typography.body_font_size, base.text.lg.size);
3490 assert_eq!(large.text, base.text);
3491 assert_eq!(small.metrics.menu_row_height, 24.0);
3492 assert_eq!(medium.metrics.menu_row_height, 28.0);
3493 assert_eq!(large.metrics.menu_row_height, 36.0);
3494 }
3495
3496 #[test]
3497 fn contextual_control_size_uses_the_callers_scaled_text_ramp() {
3498 let mut theme = DefaultTheme::default();
3499 theme.text.sm.size = 15.0;
3500 theme.text.base.size = 17.0;
3501 theme.text.lg.size = 19.0;
3502
3503 let small = theme.with_size(ControlSize::Small);
3504 let medium = theme.with_size(ControlSize::Medium);
3505 let large = theme.with_size(ControlSize::Large);
3506
3507 assert_eq!(small.typography.body_font_size, 15.0);
3508 assert_eq!(medium.typography.body_font_size, 17.0);
3509 assert_eq!(large.typography.body_font_size, 19.0);
3510 }
3511
3512 #[test]
3513 fn contextual_control_size_survives_derived_theme_refresh() {
3514 let mut theme = DefaultTheme::dark().with_size(ControlSize::Small);
3515 theme.text.sm.size = 14.5;
3516
3517 theme.sync_derived_fields();
3518
3519 assert_eq!(theme.control_size, Some(ControlSize::Small));
3520 assert_eq!(theme.metrics.min_height, 28.0);
3521 assert_eq!(theme.metrics.touch_target_size, 44.0);
3522 assert_eq!(theme.typography.body_font_size, 14.5);
3523 }
3524
3525 #[test]
3526 fn default_theme_initializes_hdr_tokens() {
3527 let theme = DefaultTheme::default();
3528
3529 assert_eq!(theme.hdr.mode, HdrThemeMode::Disabled);
3530 assert_eq!(
3531 theme.hdr.color_roles.surface.sdr,
3532 theme.colors.neutrals.window
3533 );
3534 assert_eq!(theme.hdr.color_roles.accent.sdr, theme.colors.primary);
3535 assert_eq!(
3536 theme.hdr.color_roles.accent_text.sdr,
3537 theme.colors.on_primary
3538 );
3539 }
3540
3541 fn built_in_presets() -> [DefaultTheme; 5] {
3542 [
3543 DefaultTheme::light(),
3544 DefaultTheme::dark(),
3545 DefaultTheme::void(),
3546 DefaultTheme::neutral(),
3547 DefaultTheme::neutral_dark(),
3548 ]
3549 }
3550
3551 fn assert_contrast(label: &str, foreground: Color, background: Color, minimum: f32) {
3552 let ratio = foreground.contrast_ratio(background);
3553 assert!(
3554 ratio >= minimum,
3555 "{label}: contrast {ratio:.2} is below {minimum} ({foreground:?} on {background:?})"
3556 );
3557 }
3558
3559 fn hue_distance(first: f32, second: f32) -> f32 {
3560 let distance = (first - second).rem_euclid(360.0);
3561 distance.min(360.0 - distance)
3562 }
3563
3564 #[test]
3565 fn sui_presets_share_the_azure_brand_across_schemes() {
3566 let light = ThemeColors::light();
3567 let dark = ThemeColors::dark();
3568 let void = ThemeColors::high_contrast();
3569
3570 assert_eq!(light.primary, rgb8(23, 98, 244));
3571 assert_eq!(light.on_primary, Color::WHITE);
3572 assert_eq!(light.secondary, rgb8(125, 77, 231));
3573 for colors in [dark, void] {
3574 assert_eq!(colors.primary, light.primary);
3575 assert_eq!(colors.secondary, light.secondary);
3576 assert_eq!(colors.danger, light.danger);
3577 assert_eq!(colors.success, light.success);
3578 assert_eq!(colors.warning, light.warning);
3579 assert_eq!(colors.info, light.info);
3580 }
3581 assert_eq!(void.neutrals.window, Color::BLACK);
3582 assert_eq!(void.neutrals.panel, Color::BLACK);
3583 }
3584
3585 #[test]
3586 fn light_surfaces_are_pure_and_dark_surfaces_carry_a_constant_faint_tint() {
3587 let light = ThemeColors::light().neutrals;
3588 for color in [
3589 light.window,
3590 light.subtle,
3591 light.panel,
3592 light.control,
3593 light.control_hover,
3594 light.border,
3595 light.border_strong,
3596 light.border_control,
3597 light.text,
3598 light.text_secondary,
3599 light.text_tertiary,
3600 ] {
3601 assert!(
3602 color.to_oklch().chroma < 1.0e-3,
3603 "light neutrals must be achromatic: {color:?}"
3604 );
3605 }
3606
3607 let dark = ThemeColors::dark().neutrals;
3608 for color in [
3609 dark.window,
3610 dark.subtle,
3611 dark.panel,
3612 dark.overlay,
3613 dark.control,
3614 dark.control_hover,
3615 dark.border,
3616 ] {
3617 let oklch = color.to_oklch();
3618 assert!(
3619 (0.012..=0.022).contains(&oklch.chroma),
3620 "dark surface tint drifted: {oklch:?}"
3621 );
3622 assert!(
3623 (245.0..=270.0).contains(&oklch.hue),
3624 "dark surfaces lean blue: {oklch:?}"
3625 );
3626 }
3627 assert!(dark.text.to_oklch().chroma < 0.01);
3629
3630 let neutral_dark = ThemeColors::neutral_dark().neutrals;
3631 assert!(neutral_dark.panel.to_oklch().chroma < 1.0e-3);
3632 assert!(
3633 (neutral_dark.panel.to_oklch().lightness - dark.panel.to_oklch().lightness).abs()
3634 < 0.01,
3635 "the neutral dark ramp is the untinted twin of the SUI dark ramp"
3636 );
3637 }
3638
3639 #[test]
3640 fn built_in_presets_meet_contrast_floors() {
3641 for theme in built_in_presets() {
3642 let name = theme.colors.name;
3643 let palette = theme.palette;
3644 let panel = palette.surface_raised;
3645
3646 assert_contrast(name, palette.text, panel, 12.0);
3647 assert_contrast(name, palette.text_muted, panel, 7.0);
3648 assert_contrast(name, palette.placeholder, panel, 4.5);
3649 assert_contrast(name, palette.placeholder, palette.surface, 4.5);
3650 assert_contrast(name, palette.border_control, panel, 3.0);
3651 assert_contrast(name, palette.focus_ring, panel, 3.0);
3652 assert_contrast(name, palette.accent_text, palette.accent, 4.5);
3653 assert_contrast(name, palette.danger_text, palette.danger, 4.5);
3654 assert_contrast(name, palette.success_text, palette.success, 4.5);
3655 assert_contrast(name, palette.warning_text, palette.warning, 4.5);
3656 assert_contrast(name, palette.info_text, palette.info, 4.5);
3657 for (soft, text) in [
3658 (palette.accent_soft, palette.accent_soft_text),
3659 (palette.info_soft, palette.info_soft_text),
3660 (palette.success_soft, palette.success_soft_text),
3661 (palette.warning_soft, palette.warning_soft_text),
3662 (palette.danger_soft, palette.danger_soft_text),
3663 ] {
3664 assert_contrast(name, text, soft, 4.5);
3665 assert_contrast(name, text, panel, 4.5);
3666 }
3667 for hue in DecorativeHue::ALL {
3668 let roles = theme.decorative.get(hue);
3669 assert_contrast(name, roles.text, roles.soft, 4.5);
3670 assert_contrast(name, roles.on_solid, roles.solid, 3.0);
3673 }
3674 }
3675 }
3676
3677 #[test]
3678 fn interaction_roles_are_neutral_while_focus_follows_the_brand() {
3679 for theme in built_in_presets() {
3680 let palette = theme.palette;
3681 assert_eq!(palette.caret, palette.text);
3682 assert!(palette.selection.to_oklch().chroma < 0.02);
3683 assert_eq!(palette.selection_border, palette.accent_border);
3684 assert_eq!(palette.surface_focus, palette.field);
3685 assert_ne!(palette.border_focus, palette.border_strong);
3686 assert_eq!(palette.focus, palette.focus_ring);
3687 assert_eq!(theme.surfaces.selected, palette.selection);
3688 assert_eq!(theme.surfaces.selected_border, palette.selection_border);
3689 let focus_hue = palette.focus.to_oklch().hue;
3690 let primary = theme.colors.primary.to_oklch();
3691 if primary.chroma > 0.04 {
3692 assert!(hue_distance(focus_hue, primary.hue) < 2.0);
3693 }
3694 }
3695 }
3696
3697 #[test]
3698 fn changing_primary_rederives_every_accent_role() {
3699 let mut colors = ThemeColors::light();
3700 colors.primary = Color::rgba(0.86, 0.12, 0.47, 1.0);
3701 let theme = DefaultTheme::from_colors(colors);
3702 let hue = colors.primary.to_oklch().hue;
3703
3704 assert_eq!(theme.palette.accent, colors.primary);
3705 for (role, tolerance) in [
3708 (theme.palette.accent_hover, 3.0),
3709 (theme.palette.accent_pressed, 3.0),
3710 (theme.palette.accent_soft_text, 3.0),
3711 (theme.palette.focus_ring, 3.0),
3712 (theme.palette.accent_soft, 20.0),
3713 (theme.palette.accent_border, 20.0),
3714 (theme.palette.selection_border, 20.0),
3715 ] {
3716 let role_hue = role.to_oklch().hue;
3717 assert!(
3718 hue_distance(role_hue, hue) < tolerance,
3719 "role {role:?} must follow the edited primary hue {hue}, got {role_hue}"
3720 );
3721 }
3722 assert!(
3723 theme.palette.accent_hover.to_oklch().lightness < colors.primary.to_oklch().lightness,
3724 "light themes darken on hover"
3725 );
3726 let glow = DefaultTheme::from_colors(ThemeColors {
3727 primary: colors.primary,
3728 ..ThemeColors::dark()
3729 })
3730 .glows
3731 .accent
3732 .first
3733 .expect("dark themes glow");
3734 assert!(hue_distance(glow.color.to_oklch().hue, hue) < 3.0);
3735 }
3736
3737 #[test]
3738 fn neutral_presets_have_an_achromatic_brand_and_keep_semantic_colors() {
3739 let light = DefaultTheme::neutral();
3740 let dark = DefaultTheme::neutral_dark();
3741
3742 assert_eq!(light.colors.name, "neutral");
3743 assert_eq!(light.colors.neutrals, ThemeColors::light().neutrals);
3744 assert_eq!(light.colors.primary, rgb8(23, 23, 23));
3745 assert_eq!(dark.colors.name, "neutral-dark");
3746 assert_eq!(dark.colors.primary, rgb8(240, 240, 240));
3747 for theme in [light, dark] {
3748 for color in [
3749 theme.colors.primary,
3750 theme.colors.secondary,
3751 theme.palette.accent,
3752 theme.palette.accent_hover,
3753 theme.palette.focus,
3754 theme.palette.selection,
3755 ] {
3756 assert!(color.to_oklch().chroma < 1.0e-3, "{color:?}");
3757 }
3758 assert_eq!(theme.colors.danger, ThemeColors::light().danger);
3759 assert_eq!(theme.hdr.color_roles.accent.wide_gamut, None);
3760 assert_eq!(theme.hdr.color_roles.accent.hdr, None);
3761 assert!(theme.hdr.color_roles.danger.wide_gamut.is_some());
3762 }
3763 assert!(
3765 light.palette.accent_hover.to_oklch().lightness
3766 > light.palette.accent.to_oklch().lightness
3767 );
3768 let glow = dark.glows.accent.first.expect("neutral dark glows quietly");
3769 assert!(glow.color.alpha < DefaultTheme::dark().glows.accent.first.unwrap().color.alpha);
3770 }
3771
3772 #[test]
3773 fn neutral_buttons_use_the_raised_face_and_full_ink() {
3774 let light = DefaultTheme::light();
3775 assert_eq!(
3776 light.semantic_tone_colors(SemanticTone::Neutral),
3777 (light.palette.button, light.palette.text)
3778 );
3779 assert_eq!(light.palette.button, Color::WHITE);
3780 assert_eq!(light.palette.button_border, light.palette.border_strong);
3781 assert!(
3782 light.palette.button_hover.relative_luminance()
3783 < light.palette.button.relative_luminance()
3784 );
3785 assert!(
3786 light.palette.button_pressed.relative_luminance()
3787 < light.palette.button_hover.relative_luminance()
3788 );
3789
3790 let dark = DefaultTheme::dark();
3791 assert!(
3792 dark.palette.button_hover.relative_luminance()
3793 > dark.palette.button.relative_luminance()
3794 );
3795 }
3796
3797 #[test]
3798 fn decorative_palette_derives_roles_for_every_hue() {
3799 let theme = DefaultTheme::light();
3800 let mut hues = Vec::new();
3801 for hue in DecorativeHue::ALL {
3802 let roles = theme.decorative.get(hue);
3803 assert_eq!(roles.solid, theme.colors.decorative.get(hue));
3804 assert!(
3805 roles.solid.to_oklch().chroma > 0.10,
3806 "{hue:?} must be vibrant"
3807 );
3808 hues.push(roles.solid.to_oklch().hue);
3809 }
3810 for (index, first) in hues.iter().enumerate() {
3811 for second in &hues[index + 1..] {
3812 assert!(
3813 hue_distance(*first, *second) > 15.0,
3814 "decorative hues must stay distinct"
3815 );
3816 }
3817 }
3818 assert_eq!(theme.decorative.categorical(0), theme.decorative.red);
3819 assert_eq!(theme.decorative.categorical(10), theme.decorative.orange);
3820 }
3821
3822 #[test]
3823 fn elevation_follows_the_mesh_ladder_per_scheme() {
3824 let light = DefaultTheme::light();
3825 let dark = DefaultTheme::dark();
3826 let void = DefaultTheme::void();
3827
3828 let light_sm = light.shadows.box_shadow.xs.first.expect("light xs shadow");
3830 assert!(light_sm.color.alpha > 0.0 && light_sm.color.alpha < 0.1);
3831 let dark_sm = dark.shadows.box_shadow.xs.first.expect("dark xs shadow");
3833 assert!(dark_sm.color.alpha > light_sm.color.alpha);
3834 assert_eq!(void.shadows.box_shadow.xs, ThemeShadow::empty());
3836 assert_eq!(void.shadows.box_shadow._2xl, ThemeShadow::empty());
3837 assert_eq!(void.shadows.drop.sm, ThemeShadow::empty());
3838
3839 assert_eq!(light.glows.accent, ThemeShadow::empty());
3841 let dark_glow = dark.glows.accent.first.expect("dark accent glow");
3842 let void_glow = void.glows.accent.first.expect("void accent glow");
3843 assert!(dark_glow.blur > void_glow.blur);
3844 assert!(dark_glow.color.alpha > void_glow.color.alpha);
3845 assert_eq!(dark_glow.offset_x, 0.0);
3846 assert_eq!(dark_glow.offset_y, 0.0);
3847 }
3848
3849 #[test]
3850 fn density_tiers_match_the_mesh_contract() {
3851 let text = ThemeTextScale::default();
3852 assert_eq!(
3853 ControlTypography::for_density(&text, ThemeDensity::Compact),
3854 ControlTypography::for_size(&text, ControlSize::Small)
3855 );
3856 assert_eq!(
3857 ControlTypography::for_density(&text, ThemeDensity::Comfortable),
3858 ControlTypography::for_size(&text, ControlSize::Medium)
3859 );
3860 assert_eq!(
3861 ControlTypography::for_density(&text, ThemeDensity::Touch),
3862 ControlTypography::for_size(&text, ControlSize::Large)
3863 );
3864 let legacy_compact = DefaultTheme::default().with_density(ThemeDensity::Compact);
3865 let legacy_comfortable = DefaultTheme::default().with_density(ThemeDensity::Comfortable);
3866 let legacy_touch = DefaultTheme::default().with_density(ThemeDensity::Touch);
3867 assert_eq!(legacy_compact.typography.body_font_size, text.sm.size);
3868 assert_eq!(legacy_comfortable.typography.body_font_size, text.base.size);
3869 assert_eq!(legacy_touch.typography.body_font_size, text.lg.size);
3870
3871 let compact = DefaultTheme::compact();
3872 let comfortable = DefaultTheme::comfortable();
3873 let touch = DefaultTheme::touch();
3874
3875 assert_eq!(compact.metrics.min_height, 28.0);
3877 assert_eq!(comfortable.metrics.min_height, 32.0);
3878 assert_eq!(touch.metrics.min_height, 40.0);
3879 assert_eq!(compact.metrics.touch_target_size, 44.0);
3880 assert_eq!(comfortable.metrics.touch_target_size, 44.0);
3881 assert_eq!(touch.metrics.touch_target_size, 44.0);
3882
3883 assert_eq!(compact.metrics.list_row_height, 24.0);
3885 assert_eq!(comfortable.metrics.list_row_height, 28.0);
3886 assert_eq!(touch.metrics.list_row_height, 36.0);
3887 for theme in [&compact, &comfortable, &touch] {
3888 assert_eq!(theme.metrics.tree_row_height, theme.metrics.list_row_height);
3889 assert_eq!(
3890 theme.metrics.table_row_height,
3891 theme.metrics.list_row_height
3892 );
3893 assert_eq!(theme.metrics.menu_row_height, theme.metrics.list_row_height);
3894 }
3895
3896 assert_eq!(compact.typography.body_font_size, 13.0);
3898 assert_eq!(comfortable.typography.body_font_size, 15.0);
3899 assert_eq!(touch.typography.body_font_size, 17.0);
3900
3901 assert_eq!(compact.metrics.corner_radius, 6.0);
3903 assert_eq!(comfortable.metrics.corner_radius, 6.0);
3904 assert_eq!(touch.metrics.corner_radius, 8.0);
3905
3906 for theme in [&compact, &comfortable, &touch] {
3908 assert_eq!(theme.metrics.switch_track_width, 32.0);
3909 assert_eq!(theme.metrics.switch_track_height, 19.0);
3910 assert_eq!(theme.metrics.checkbox_indicator_size, 15.0);
3911 }
3912 }
3913
3914 #[test]
3915 fn mesh_motion_ladder_is_70_140_220_340() {
3916 let motion = super::ThemeMotion::standard();
3917 assert_eq!(motion.duration_fast, 0.07);
3918 assert_eq!(motion.duration_normal, 0.14);
3919 assert_eq!(motion.duration_slow, 0.22);
3920 assert_eq!(motion.duration_slower, 0.34);
3921 }
3922
3923 #[test]
3924 fn light_and_dark_themes_derive_hdr_role_colors_from_semantics() {
3925 let light = DefaultTheme::light();
3926 let dark = DefaultTheme::dark();
3927
3928 assert_eq!(
3929 light.hdr.color_roles.surface.sdr,
3930 light.colors.neutrals.window
3931 );
3932 assert_eq!(light.hdr.color_roles.text.sdr, light.colors.neutrals.text);
3933 assert_eq!(
3934 dark.hdr.color_roles.surface.sdr,
3935 dark.colors.neutrals.window
3936 );
3937 assert_eq!(dark.hdr.color_roles.accent.sdr, dark.colors.primary);
3938 }
3939
3940 #[test]
3941 fn sync_derived_fields_updates_semantic_palette_and_typography() {
3942 let mut theme = DefaultTheme::default();
3943 theme.colors.primary = Color::rgba(0.2, 0.3, 0.4, 1.0);
3944 theme.text.base.size = 11.0;
3945 theme.text.base.line_height = 15.0;
3946 theme.text.lg.size = 15.0;
3947 theme.text.lg.line_height = 22.0;
3948 theme.density = ThemeDensity::Touch;
3949 theme.sync_derived_fields();
3950
3951 assert_eq!(theme.palette.accent, Color::rgba(0.2, 0.3, 0.4, 1.0));
3952 assert_eq!(theme.palette.caret, theme.colors.neutrals.text);
3953 assert_eq!(theme.surfaces.accent, theme.palette.accent);
3954 assert_eq!(theme.surfaces.window, theme.palette.surface);
3955 assert_eq!(theme.surfaces.tooltip, theme.surfaces.overlay);
3957 assert_eq!(theme.surfaces.tooltip_text, theme.palette.text_muted);
3958 assert!(theme.surfaces.overlay_scrim.alpha > 0.0);
3959 assert_eq!(theme.typography.body_font_size, 15.0);
3961 assert_eq!(theme.typography.body_line_height, 22.0);
3962 assert_eq!(
3963 theme.metrics.min_height,
3964 DefaultTheme::default()
3965 .with_density(ThemeDensity::Touch)
3966 .metrics
3967 .min_height
3968 );
3969 assert_eq!(
3970 theme.interaction.pressed_offset,
3971 DefaultTheme::default()
3972 .with_density(ThemeDensity::Touch)
3973 .interaction
3974 .pressed_offset
3975 );
3976 }
3977
3978 #[test]
3979 fn control_palette_exposes_semantic_status_colors() {
3980 let theme = DefaultTheme::default();
3981
3982 assert_eq!(theme.palette.info, theme.colors.info);
3983 assert_eq!(theme.palette.info_text, theme.colors.on_info);
3984 assert_eq!(theme.palette.success, theme.colors.success);
3985 assert_eq!(theme.palette.success_text, theme.colors.on_success);
3986 assert_eq!(theme.palette.warning, theme.colors.warning);
3987 assert_eq!(theme.palette.warning_text, theme.colors.on_warning);
3988 assert_eq!(theme.palette.danger, theme.colors.danger);
3989 assert_eq!(theme.palette.danger_text, theme.colors.on_danger);
3990 assert_eq!(
3991 theme.semantic_tone_colors(SemanticTone::Warning),
3992 (theme.palette.warning, theme.palette.warning_text)
3993 );
3994 assert_eq!(
3995 theme.semantic_tone_color(SemanticTone::Danger),
3996 theme.palette.danger
3997 );
3998 assert_eq!(
3999 theme.semantic_tone_text_color(SemanticTone::Success),
4000 theme.palette.success_text
4001 );
4002 }
4003
4004 #[test]
4005 fn sync_derived_fields_updates_hdr_semantic_fallbacks() {
4006 let mut theme = DefaultTheme::default();
4007 let stale_wide_gamut = Color::display_p3(0.9, 0.4, 0.2, 1.0);
4008 let stale_hdr = Color::linear_display_p3(1.6, 0.5, 0.3, 1.0);
4009
4010 theme.hdr.color_roles.accent.wide_gamut = Some(stale_wide_gamut);
4011 theme.hdr.color_roles.accent.hdr = Some(stale_hdr);
4012 theme.colors = ThemeColors::dark();
4013 theme.colors.neutrals.window = Color::rgba(0.96, 0.97, 0.98, 1.0);
4014 theme.sync_derived_fields();
4015
4016 let accent = theme.hdr.color_roles.accent;
4017 assert_eq!(
4018 theme.hdr.color_roles.surface.sdr,
4019 theme.colors.neutrals.window
4020 );
4021 assert_eq!(accent.sdr, theme.colors.primary);
4022 let wide = accent
4023 .wide_gamut
4024 .expect("chromatic accent has a P3 variant");
4025 assert_ne!(wide, stale_wide_gamut);
4026 assert!(wide.to_oklch().chroma > theme.colors.primary.to_oklch().chroma);
4027 assert!(hue_distance(wide.to_oklch().hue, theme.colors.primary.to_oklch().hue) < 2.0);
4028 let hdr = accent.hdr.expect("the accent is a live-signal role");
4029 assert!(hdr.to_linear_srgb().blue > wide.to_linear_srgb().blue);
4030 }
4031
4032 #[test]
4033 fn dark_theme_uses_professional_dark_tokens() {
4034 let theme = DefaultTheme::dark();
4035
4036 assert_eq!(theme.colors.scheme, ThemeColorScheme::Dark);
4037 assert_eq!(theme.colors.name, "dark");
4038 assert_ne!(theme.colors.neutrals.window, Color::BLACK);
4039 assert_eq!(theme.palette.surface, theme.colors.neutrals.window);
4040 assert_ne!(theme.palette.surface_raised, Color::BLACK);
4041 assert_eq!(theme.palette.text, theme.colors.neutrals.text);
4042 assert_ne!(theme.palette.text, Color::WHITE);
4043 assert_eq!(theme.palette.caret, theme.colors.neutrals.text);
4044 assert_eq!(theme.palette.accent, theme.colors.primary);
4045 assert_eq!(theme.palette.accent_text, theme.colors.on_primary);
4046 assert_eq!(theme.surfaces.window, theme.palette.surface);
4047 assert_eq!(theme.surfaces.panel, theme.palette.surface_raised);
4048 assert_eq!(theme.surfaces.border, theme.palette.border);
4049 assert_ne!(theme.surfaces.border, Color::WHITE);
4050 assert_ne!(theme.surfaces.text_faint, theme.palette.surface);
4051 }
4052
4053 #[test]
4054 fn high_contrast_scheme_uses_true_black_oled_palette() {
4055 let theme = DefaultTheme::high_contrast();
4056
4057 assert_eq!(theme.colors.scheme, ThemeColorScheme::HighContrast);
4058 assert_eq!(theme.colors.name, "void");
4059 assert_eq!(theme.palette.surface, theme.colors.neutrals.window);
4060 assert_eq!(theme.palette.surface, Color::BLACK);
4061 assert_eq!(theme.surfaces.window, Color::BLACK);
4062 assert_eq!(theme.palette.surface_raised, Color::BLACK);
4065 assert!(theme.palette.border.alpha > 0.0);
4066 assert_ne!(theme.palette.control, Color::BLACK);
4067 assert_ne!(theme.palette.field, Color::BLACK);
4068 assert_ne!(theme.palette.text, Color::WHITE);
4069 assert_ne!(theme.palette.control_hover, Color::BLACK);
4070 assert_ne!(theme.palette.control_active, Color::BLACK);
4071 assert_ne!(theme.palette.surface_focus, Color::BLACK);
4072 assert_eq!(theme.palette.text, theme.colors.neutrals.text);
4073 assert_eq!(
4074 theme.metrics.border_width,
4075 DefaultTheme::default().metrics.border_width
4076 );
4077 assert_eq!(
4078 theme.metrics.focus_ring_width,
4079 DefaultTheme::default().metrics.focus_ring_width
4080 );
4081
4082 let touch = DefaultTheme::high_contrast().with_density(ThemeDensity::Touch);
4083 assert_eq!(touch.density, ThemeDensity::Touch);
4084 assert!(touch.metrics.min_height > theme.metrics.min_height);
4085 assert_eq!(
4086 touch.metrics.border_width,
4087 DefaultTheme::touch().metrics.border_width
4088 );
4089 assert_eq!(
4090 touch.metrics.focus_ring_width,
4091 DefaultTheme::touch().metrics.focus_ring_width
4092 );
4093 }
4094}