1use std::collections::HashMap;
20
21use crate::canvas::{FontSpec, FontWeight, Shadow};
22use crate::color::Color;
23use crate::component::ControlState;
24use crate::geometry::Bounds;
25
26#[derive(Clone, Debug, PartialEq)]
28pub enum ColorRef {
29 Token(String),
30 Value(Color),
31}
32
33impl From<Color> for ColorRef {
34 fn from(c: Color) -> Self {
35 ColorRef::Value(c)
36 }
37}
38
39impl From<&str> for ColorRef {
41 fn from(s: &str) -> Self {
42 match Color::from_hex(s) {
43 Some(c) if s.trim_start().starts_with('#') => ColorRef::Value(c),
44 _ => ColorRef::Token(s.to_string()),
45 }
46 }
47}
48
49#[derive(Clone, Debug, PartialEq)]
51pub enum Metric {
52 Token(String),
53 Value(f32),
54}
55
56impl From<f32> for Metric {
57 fn from(v: f32) -> Self {
58 Metric::Value(v)
59 }
60}
61
62impl From<&str> for Metric {
63 fn from(s: &str) -> Self {
64 Metric::Token(s.to_string())
65 }
66}
67
68#[derive(Clone, Debug, Default, PartialEq)]
70pub struct StyleProps {
71 pub fill: Option<ColorRef>,
72 pub text: Option<ColorRef>,
73 pub border_color: Option<ColorRef>,
74 pub border_width: Option<f32>,
75 pub radius: Option<Metric>,
77 pub padding: Option<Bounds>,
78 pub font: Option<String>,
80 pub opacity: Option<f32>,
81 pub transition: Option<Metric>,
83 pub shadow: Option<String>,
85 pub material: Option<String>,
88 pub reveal: Option<String>,
92 pub reveal_color: Option<ColorRef>,
95 pub reveal_radius: Option<f32>,
97 pub text_case: Option<String>,
99 pub letter_spacing: Option<f32>,
101 pub border_sides: Option<Bounds>,
104 pub border_style: Option<String>,
106 pub line_height: Option<f32>,
109 pub font_weight: Option<crate::canvas::FontWeight>,
112 pub font_size: Option<f32>,
113 pub italic: Option<bool>,
114 pub gap: Option<f32>,
118}
119
120#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
122pub enum Material {
123 #[default]
124 None,
125 Glass,
127 Frosted,
129}
130
131#[derive(Clone, Copy, Debug, Default, PartialEq)]
134pub struct Reveal {
135 pub border: bool,
137 pub background: bool,
139 pub color: Color,
141 pub radius: f32,
143}
144
145impl Reveal {
146 pub fn on(&self) -> bool {
148 (self.border || self.background) && self.radius > 0.0 && self.color.a > 0
149 }
150}
151
152impl Material {
153 pub fn parse(name: &str) -> Material {
154 match name {
155 "glass" => Material::Glass,
156 "frosted" | "acrylic" => Material::Frosted,
157 _ => Material::None,
158 }
159 }
160
161 pub fn blur(self) -> f32 {
163 match self {
164 Material::None => 0.0,
165 Material::Glass => 8.0,
166 Material::Frosted => 22.0,
167 }
168 }
169}
170
171impl StyleProps {
172 pub fn new() -> Self {
173 Self::default()
174 }
175
176 pub fn fill(mut self, c: impl Into<ColorRef>) -> Self {
177 self.fill = Some(c.into());
178 self
179 }
180
181 pub fn text(mut self, c: impl Into<ColorRef>) -> Self {
182 self.text = Some(c.into());
183 self
184 }
185
186 pub fn border(mut self, c: impl Into<ColorRef>, width: f32) -> Self {
187 self.border_color = Some(c.into());
188 self.border_width = Some(width);
189 self
190 }
191
192 pub fn radius(mut self, m: impl Into<Metric>) -> Self {
193 self.radius = Some(m.into());
194 self
195 }
196
197 pub fn padding(mut self, b: impl Into<Bounds>) -> Self {
198 self.padding = Some(b.into());
199 self
200 }
201
202 pub fn font(mut self, name: &str) -> Self {
203 self.font = Some(name.to_string());
204 self
205 }
206
207 pub fn opacity(mut self, v: f32) -> Self {
208 self.opacity = Some(v);
209 self
210 }
211
212 pub fn transition(mut self, m: impl Into<Metric>) -> Self {
213 self.transition = Some(m.into());
214 self
215 }
216
217 pub fn shadow(mut self, token: &str) -> Self {
218 self.shadow = Some(token.to_string());
219 self
220 }
221
222 pub fn material(mut self, name: &str) -> Self {
224 self.material = Some(name.to_string());
225 self
226 }
227
228 pub fn reveal(mut self, mode: &str) -> Self {
230 self.reveal = Some(mode.to_string());
231 self
232 }
233
234 pub fn merge(&mut self, other: &StyleProps) {
236 if other.fill.is_some() {
237 self.fill.clone_from(&other.fill);
238 }
239 if other.text.is_some() {
240 self.text.clone_from(&other.text);
241 }
242 if other.border_color.is_some() {
243 self.border_color.clone_from(&other.border_color);
244 }
245 if other.border_width.is_some() {
246 self.border_width = other.border_width;
247 }
248 if other.radius.is_some() {
249 self.radius.clone_from(&other.radius);
250 }
251 if other.padding.is_some() {
252 self.padding = other.padding;
253 }
254 if other.font.is_some() {
255 self.font.clone_from(&other.font);
256 }
257 if other.opacity.is_some() {
258 self.opacity = other.opacity;
259 }
260 if other.transition.is_some() {
261 self.transition.clone_from(&other.transition);
262 }
263 if other.shadow.is_some() {
264 self.shadow.clone_from(&other.shadow);
265 }
266 if other.material.is_some() {
267 self.material.clone_from(&other.material);
268 }
269 if other.reveal.is_some() {
270 self.reveal.clone_from(&other.reveal);
271 }
272 if other.reveal_color.is_some() {
273 self.reveal_color.clone_from(&other.reveal_color);
274 }
275 if other.reveal_radius.is_some() {
276 self.reveal_radius = other.reveal_radius;
277 }
278 if other.text_case.is_some() {
279 self.text_case.clone_from(&other.text_case);
280 }
281 if other.letter_spacing.is_some() {
282 self.letter_spacing = other.letter_spacing;
283 }
284 if other.gap.is_some() {
285 self.gap = other.gap;
286 }
287 if other.border_sides.is_some() {
288 self.border_sides = other.border_sides;
289 }
290 if other.border_style.is_some() {
291 self.border_style.clone_from(&other.border_style);
292 }
293 if other.line_height.is_some() {
294 self.line_height = other.line_height;
295 }
296 if other.font_weight.is_some() {
297 self.font_weight = other.font_weight;
298 }
299 if other.font_size.is_some() {
300 self.font_size = other.font_size;
301 }
302 if other.italic.is_some() {
303 self.italic = other.italic;
304 }
305 }
306}
307
308#[derive(Clone, Debug, Default, PartialEq)]
310pub struct Style {
311 pub base: StyleProps,
312 pub hover: StyleProps,
313 pub pressed: StyleProps,
314 pub focused: StyleProps,
315 pub checked: StyleProps,
316 pub disabled: StyleProps,
317}
318
319impl Style {
320 pub fn new(base: StyleProps) -> Self {
321 Self { base, ..Default::default() }
322 }
323
324 pub fn hover(mut self, p: StyleProps) -> Self {
325 self.hover = p;
326 self
327 }
328
329 pub fn pressed(mut self, p: StyleProps) -> Self {
330 self.pressed = p;
331 self
332 }
333
334 pub fn focused(mut self, p: StyleProps) -> Self {
335 self.focused = p;
336 self
337 }
338
339 pub fn checked(mut self, p: StyleProps) -> Self {
340 self.checked = p;
341 self
342 }
343
344 pub fn disabled(mut self, p: StyleProps) -> Self {
345 self.disabled = p;
346 self
347 }
348
349 fn state(&self, index: usize) -> &StyleProps {
351 match index {
352 0 => &self.hover,
353 1 => &self.pressed,
354 2 => &self.focused,
355 3 => &self.checked,
356 _ => &self.disabled,
357 }
358 }
359}
360
361#[derive(Clone, Debug, PartialEq)]
363pub struct ResolvedStyle {
364 pub fill: Color,
365 pub text: Color,
366 pub border_color: Color,
367 pub border_width: f32,
368 pub radius: f32,
369 pub padding: Bounds,
370 pub font: FontSpec,
371 pub opacity: f32,
372 pub transition_ms: f32,
373 pub shadow: Option<Shadow>,
375 pub material: Material,
377 pub reveal: Reveal,
379 pub text_case: crate::text::TextCase,
381 pub letter_spacing: f32,
383 pub border_sides: Option<Bounds>,
385 pub border_dash: Vec<f32>,
387 pub line_height: f32,
389 pub gap: Option<f32>,
391}
392
393impl ResolvedStyle {
394 pub fn border_stroke(&self, color: Color, width: f32) -> crate::canvas::Stroke {
396 let stroke = crate::canvas::Stroke::new(color, width);
397 if self.border_dash.is_empty() { stroke } else { stroke.dashed(self.border_dash.clone()) }
398 }
399}
400
401impl Default for ResolvedStyle {
402 fn default() -> Self {
403 Self {
404 fill: Color::TRANSPARENT,
405 text: Color::BLACK,
406 border_color: Color::TRANSPARENT,
407 border_width: 0.0,
408 radius: 0.0,
409 padding: Bounds::ZERO,
410 font: FontSpec::default(),
411 opacity: 1.0,
412 transition_ms: 0.0,
413 shadow: None,
414 material: Material::None,
415 reveal: Reveal::default(),
416 text_case: crate::text::TextCase::None,
417 letter_spacing: 0.0,
418 border_sides: None,
419 border_dash: Vec::new(),
420 line_height: 0.0,
421 gap: None,
422 }
423 }
424}
425
426impl ResolvedStyle {
427 pub fn lerp(&self, other: &ResolvedStyle, t: f32) -> ResolvedStyle {
430 let mix = |a: f32, b: f32| a + (b - a) * t;
431 ResolvedStyle {
432 fill: self.fill.lerp(other.fill, t),
433 text: self.text.lerp(other.text, t),
434 border_color: self.border_color.lerp(other.border_color, t),
435 border_width: mix(self.border_width, other.border_width),
436 radius: mix(self.radius, other.radius),
437 padding: other.padding,
438 font: other.font.clone(),
439 opacity: mix(self.opacity, other.opacity),
440 transition_ms: other.transition_ms,
441 shadow: match (self.shadow, other.shadow) {
442 (Some(a), Some(b)) => Some(Shadow {
443 color: a.color.lerp(b.color, t),
444 blur: mix(a.blur, b.blur),
445 offset: crate::PointF::new(mix(a.offset.x, b.offset.x), mix(a.offset.y, b.offset.y)),
446 spread: mix(a.spread, b.spread),
447 }),
448 _ => other.shadow,
449 },
450 material: other.material,
451 reveal: other.reveal,
452 text_case: other.text_case,
453 letter_spacing: mix(self.letter_spacing, other.letter_spacing),
454 border_sides: other.border_sides,
455 border_dash: other.border_dash.clone(),
456 line_height: mix(self.line_height, other.line_height),
457 gap: other.gap,
458 }
459 }
460}
461
462#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
466pub enum Look {
467 #[default]
469 Aylis,
470 Material2,
472 Material3,
474 Ios7,
476 Ios13,
478 Ios26,
480 Win7,
482 Win10,
484 Win11,
486 WinPhone,
488}
489
490impl crate::prop::PropEnum for Look {
491 const NAMES: &'static [&'static str] = &["aylis", "material2", "material3", "ios7", "ios13", "ios26", "win7", "win10", "win11", "winphone"];
492
493 fn from_index(index: usize) -> Option<Self> {
494 [
495 Look::Aylis,
496 Look::Material2,
497 Look::Material3,
498 Look::Ios7,
499 Look::Ios13,
500 Look::Ios26,
501 Look::Win7,
502 Look::Win10,
503 Look::Win11,
504 Look::WinPhone,
505 ]
506 .get(index)
507 .copied()
508 }
509
510 fn index(self) -> usize {
511 self as usize
512 }
513}
514
515impl Look {
516 pub fn is_material(self) -> bool {
517 matches!(self, Look::Material2 | Look::Material3)
518 }
519
520 pub fn is_ios(self) -> bool {
521 matches!(self, Look::Ios7 | Look::Ios13 | Look::Ios26)
522 }
523
524 pub fn is_windows(self) -> bool {
525 matches!(self, Look::Win7 | Look::Win10 | Look::Win11 | Look::WinPhone)
526 }
527}
528
529#[derive(Clone, Debug, Default, PartialEq)]
531pub struct Theme {
532 pub name: String,
533 pub family: String,
536 pub dark: bool,
538 pub look: Look,
540 pub colors: HashMap<String, Color>,
541 pub fonts: HashMap<String, FontSpec>,
542 pub radius: HashMap<String, f32>,
543 pub spacing: HashMap<String, f32>,
544 pub durations: HashMap<String, f32>,
546 pub shadows: HashMap<String, Shadow>,
547 pub styles: HashMap<String, Style>,
549 pub components: HashMap<String, String>,
551}
552
553impl Theme {
554 pub fn new(name: impl Into<String>) -> Self {
555 Self { name: name.into(), ..Default::default() }
556 }
557
558 pub fn light() -> Self {
560 let mut t = Theme::new("Aylis Light");
561 t.family = "Aylis".into();
562 t.set_colors(&[
563 ("background", "#F5F5F7"),
564 ("surface", "#FFFFFF"),
565 ("text", "#1F2328"),
566 ("text_muted", "#6B7280"),
567 ("border", "#D0D7DE"),
568 ("primary", "#2563EB"),
569 ("primary_hover", "#3B82F6"),
570 ("primary_pressed", "#1D4ED8"),
571 ("on_primary", "#FFFFFF"),
572 ("on_danger", "#FFFFFF"),
574 ("on_success", "#FFFFFF"),
575 ("danger", "#DC2626"),
576 ("danger_hover", "#EF4444"),
577 ("danger_pressed", "#B91C1C"),
578 ("success", "#16A34A"),
579 ("warning", "#D97706"),
580 ("focus_ring", "#3B82F6"),
581 ("accent", "#9333EA"),
582 ("hover_overlay", "#0000000F"),
583 ("glass_fill", "#FFFFFF73"),
584 ("primary_container", "#DBEAFE"),
585 ("on_primary_container", "#1E3A8A"),
586 ("selection", "#2563EB40"),
587 ("track", "#D0D7DE"),
588 ("surface_2", "#F0F1F4"),
589 ("info", "#0284C7"),
590 ("primary_soft", "#2563EB1A"),
591 ("success_soft", "#16A34A1A"),
592 ("warning_soft", "#D977061F"),
593 ("danger_soft", "#DC26261A"),
594 ("info_soft", "#0284C71A"),
595 ("meter_low", "#16A34A"),
596 ("meter_mid", "#EAB308"),
597 ("meter_high", "#DC2626"),
598 ("presence_online", "#16A34A"),
599 ("presence_away", "#EAB308"),
600 ("presence_busy", "#DC2626"),
601 ("presence_offline", "#9CA3AF"),
602 ]);
603 t.shadows.insert("sm".into(), Shadow::new(Color::rgba(15, 23, 42, 20), 4.0, 0.0, 1.0));
604 t.shadows.insert("md".into(), Shadow::new(Color::rgba(15, 23, 42, 28), 12.0, 0.0, 4.0));
605 t.shadows.insert("lg".into(), Shadow::new(Color::rgba(15, 23, 42, 36), 28.0, 0.0, 10.0));
606 t.shadows.insert("2xl".into(), Shadow::new(Color::rgba(15, 23, 42, 64), 50.0, 0.0, 25.0));
608 t.add_standard_tokens();
609 t.add_standard_styles();
610 t
611 }
612
613 pub fn dark() -> Self {
615 let mut t = Theme::new("Aylis Dark");
616 t.family = "Aylis".into();
617 t.dark = true;
618 t.set_colors(&[
619 ("background", "#1E1E1E"),
620 ("surface", "#252526"),
621 ("text", "#E6E6E6"),
622 ("text_muted", "#9A9A9A"),
623 ("border", "#3C3C3C"),
624 ("primary", "#3B82F6"),
625 ("primary_hover", "#60A5FA"),
626 ("primary_pressed", "#2563EB"),
627 ("on_primary", "#FFFFFF"),
628 ("on_danger", "#FFFFFF"),
630 ("on_success", "#FFFFFF"),
631 ("danger", "#EF4444"),
632 ("danger_hover", "#F87171"),
633 ("danger_pressed", "#DC2626"),
634 ("success", "#22C55E"),
635 ("warning", "#F59E0B"),
636 ("focus_ring", "#60A5FA"),
637 ("accent", "#A855F7"),
638 ("hover_overlay", "#FFFFFF14"),
639 ("glass_fill", "#1E1E2273"),
640 ("primary_container", "#1E3A8A"),
641 ("on_primary_container", "#DBEAFE"),
642 ("selection", "#3B82F660"),
643 ("track", "#4A4A4F"),
644 ("surface_2", "#2D2D30"),
645 ("info", "#38BDF8"),
646 ("primary_soft", "#3B82F62E"),
647 ("success_soft", "#22C55E29"),
648 ("warning_soft", "#F59E0B29"),
649 ("danger_soft", "#EF444429"),
650 ("info_soft", "#38BDF829"),
651 ("meter_low", "#22C55E"),
652 ("meter_mid", "#FACC15"),
653 ("meter_high", "#EF4444"),
654 ("presence_online", "#22C55E"),
655 ("presence_away", "#FACC15"),
656 ("presence_busy", "#EF4444"),
657 ("presence_offline", "#6B7280"),
658 ]);
659 t.shadows.insert("sm".into(), Shadow::new(Color::rgba(0, 0, 0, 70), 4.0, 0.0, 1.0));
660 t.shadows.insert("md".into(), Shadow::new(Color::rgba(0, 0, 0, 90), 14.0, 0.0, 4.0));
661 t.shadows.insert("lg".into(), Shadow::new(Color::rgba(0, 0, 0, 110), 30.0, 0.0, 10.0));
662 t.shadows.insert("2xl".into(), Shadow::new(Color::rgba(0, 0, 0, 150), 50.0, 0.0, 25.0));
663 t.add_standard_tokens();
664 t.add_standard_styles();
665 t
666 }
667
668 fn set_colors(&mut self, colors: &[(&str, &str)]) {
669 for (name, hex) in colors {
670 let color = Color::from_hex(hex).expect("cor inválida no tema padrão");
671 self.colors.insert(name.to_string(), color);
672 }
673 }
674
675 fn add_standard_tokens(&mut self) {
676 self.fonts.insert("default".into(), FontSpec::new("", 14.0));
677 self.fonts.insert("title".into(), FontSpec::new("", 20.0).weight(FontWeight::Bold));
678 self.fonts.insert("heading".into(), FontSpec::new("", 16.0).weight(FontWeight::SemiBold));
679 self.fonts.insert("small".into(), FontSpec::new("", 12.0));
680 self.fonts.insert("strong".into(), FontSpec::new("", 14.0).weight(FontWeight::SemiBold));
681 self.fonts.insert("mono".into(), FontSpec::new("monospace", 13.0));
682 for (k, v) in [("sm", 4.0), ("md", 6.0), ("lg", 10.0)] {
683 self.radius.insert(k.into(), v);
684 }
685 for (k, v) in [("xs", 4.0), ("sm", 8.0), ("md", 12.0), ("lg", 16.0)] {
686 self.spacing.insert(k.into(), v);
687 }
688 for (k, v) in [("fast", 120.0), ("normal", 200.0), ("slow", 350.0)] {
689 self.durations.insert(k.into(), v);
690 }
691 }
692
693 fn add_standard_styles(&mut self) {
694 self.set_style("Form", Style::new(StyleProps::new().fill("background").text("text")));
695 self.set_style(
696 "Panel",
697 Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("sm")),
698 );
699 self.set_style("Label", Style::new(StyleProps::new().text("text")));
700 self.set_style(
701 "Button",
702 Style::new(
703 StyleProps::new()
704 .fill("primary")
705 .text("on_primary")
706 .radius("md")
707 .padding((12.0, 6.0))
708 .transition("fast"),
709 )
710 .hover(StyleProps::new().fill("primary_hover"))
711 .pressed(StyleProps::new().fill("primary_pressed"))
712 .focused(StyleProps::new().border("focus_ring", 2.0))
713 .disabled(StyleProps::new().opacity(0.5)),
714 );
715 self.set_style(
716 "Button.danger",
717 Style::new(StyleProps::new().fill("danger").text("on_danger"))
718 .hover(StyleProps::new().fill("danger_hover"))
719 .pressed(StyleProps::new().fill("danger_pressed")),
720 );
721 self.set_style(
723 "Button.round",
724 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).border(Color::TRANSPARENT, 0.0))
725 .hover(StyleProps::new().fill("primary_hover"))
726 .pressed(StyleProps::new().fill("primary_pressed")),
727 );
728 self.set_style(
729 "Button.flat",
730 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary"))
731 .hover(StyleProps::new().fill("hover_overlay"))
732 .pressed(StyleProps::new().fill("hover_overlay")),
733 );
734 self.set_style(
735 "Edit",
736 Style::new(
737 StyleProps::new()
738 .fill("surface")
739 .text("text")
740 .border("border", 1.0)
741 .radius("sm")
742 .padding((8.0, 4.0)),
743 )
744 .hover(StyleProps::new().border("text_muted", 1.0))
745 .focused(StyleProps::new().border("focus_ring", 2.0))
746 .disabled(StyleProps::new().opacity(0.5)),
747 );
748 self.set_style(
749 "ComboBox",
750 Style::new(
751 StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((8.0, 4.0)),
752 )
753 .hover(StyleProps::new().border("text_muted", 1.0))
754 .focused(StyleProps::new().border("focus_ring", 2.0))
755 .disabled(StyleProps::new().opacity(0.5)),
756 );
757 for class in ["CheckBox", "RadioButton"] {
759 self.set_style(
760 class,
761 Style::new(
762 StyleProps::new().fill("surface").text("text").border("text_muted", 1.5).radius("sm").transition("fast"),
763 )
764 .hover(StyleProps::new().border("primary", 1.5))
765 .checked(StyleProps::new().fill("primary").border("primary", 1.5))
766 .focused(StyleProps::new().border("focus_ring", 2.0))
767 .disabled(StyleProps::new().opacity(0.5)),
768 );
769 }
770 self.set_style(
772 "Switch",
773 Style::new(StyleProps::new().fill("track").text("text").transition("normal"))
774 .checked(StyleProps::new().fill("primary"))
775 .focused(StyleProps::new().border("focus_ring", 2.0))
776 .disabled(StyleProps::new().opacity(0.5)),
777 );
778 self.set_style("ProgressBar", Style::new(StyleProps::new().fill("track").radius(4.0)));
780 for class in ["Memo", "ListBox", "ListView", "TreeView", "Calendar"] {
782 self.set_style(
783 class,
784 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((6.0, 4.0)))
785 .focused(StyleProps::new().border("focus_ring", 2.0))
786 .disabled(StyleProps::new().opacity(0.5)),
787 );
788 self.set_style(
791 &format!("{class}.flat"),
792 Style::new(StyleProps::new().border(Color::TRANSPARENT, 0.0))
793 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0)),
794 );
795 }
796 self.set_style(
799 "CodeEditor",
800 Style::new(StyleProps::new().fill("surface").text("text").border(Color::TRANSPARENT, 0.0).radius(0.0).padding((4.0, 2.0)))
801 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0))
802 .disabled(StyleProps::new().opacity(0.5)),
803 );
804 self.set_style("TabStrip", Style::new(StyleProps::new().fill("surface_2").text("text").border(Color::TRANSPARENT, 0.0)));
807 self.set_style(
809 "TrackBar",
810 Style::new(StyleProps::new().fill("track").text("text").radius(3.0))
811 .focused(StyleProps::new().border("focus_ring", 2.0))
812 .disabled(StyleProps::new().opacity(0.5)),
813 );
814 self.set_style("GroupBox", Style::new(StyleProps::new().text("text").border("border", 1.0).radius("md")));
815 self.set_style("Expander", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
816 self.set_style("TabControl", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
817 self.set_style("TabItem", Style::new(StyleProps::new().text("text")));
818 self.set_style("StatusBar", Style::new(StyleProps::new().fill("surface").text("text_muted").border("border", 1.0)));
819 self.set_style("ToolBar", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0)));
820 self.set_style("RadioGroup", Style::new(StyleProps::new().text("text").border("border", 1.0).radius("md")));
821 self.set_style("Chart", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
822 self.add_variant_styles();
823 }
824
825 fn add_variant_styles(&mut self) {
827 self.set_style(
829 "Button.secondary",
830 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0))
831 .hover(StyleProps::new().fill("surface_2").border("text_muted", 1.0))
832 .pressed(StyleProps::new().fill("track")),
833 );
834 self.set_style(
835 "Button.outline",
836 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").border("primary", 1.0))
837 .hover(StyleProps::new().fill("primary_soft"))
838 .pressed(StyleProps::new().fill("primary_soft")),
839 );
840 self.set_style(
841 "Button.success",
842 Style::new(StyleProps::new().fill("success").text("on_success"))
843 .hover(StyleProps::new().opacity(0.9))
844 .pressed(StyleProps::new().opacity(0.8)),
845 );
846 self.set_style(
848 "Button.positive",
849 Style::new(StyleProps::new().fill("success").text("on_success")).hover(StyleProps::new().opacity(0.9)).pressed(StyleProps::new().opacity(0.8)),
850 );
851 self.set_style(
852 "Button.negative",
853 Style::new(StyleProps::new().fill("danger").text("on_danger")).hover(StyleProps::new().fill("danger_hover")).pressed(StyleProps::new().fill("danger_pressed")),
854 );
855 self.set_style(
856 "Button.accent",
857 Style::new(StyleProps::new().fill("accent").text("on_primary")).hover(StyleProps::new().opacity(0.9)).pressed(StyleProps::new().opacity(0.8)),
858 );
859 self.set_style("Button.rounded", Style::new(StyleProps::new().radius(28.0)));
860 self.set_style("Button.unelevated", Style::new(StyleProps::new().shadow("none")));
861 self.set_style(
862 "Button.push",
863 Style::new(StyleProps::new().border(Color::rgba(0, 0, 0, 0x40), 3.0)).pressed(StyleProps::new().fill("primary_pressed").border(Color::rgba(0, 0, 0, 0x40), 1.0)),
864 );
865 self.set_style("Button.glossy", Style::new(StyleProps::new().fill("primary_hover").shadow("md")).hover(StyleProps::new().fill("primary")));
866 self.set_style(
867 "Button.icon",
868 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").padding(6.0).radius("md"))
869 .hover(StyleProps::new().fill("hover_overlay"))
870 .pressed(StyleProps::new().fill("track"))
871 .checked(StyleProps::new().fill("primary_soft").text("primary")),
872 );
873 self.set_style(
874 "Button.fab",
875 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).shadow("md").padding(10.0))
876 .hover(StyleProps::new().fill("primary_hover").shadow("lg"))
877 .pressed(StyleProps::new().fill("primary_pressed").shadow("sm")),
878 );
879 self.set_style(
880 "Button.link",
881 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").padding((2.0, 2.0)))
882 .hover(StyleProps::new().fill(Color::TRANSPARENT).text("primary_hover"))
883 .pressed(StyleProps::new().fill(Color::TRANSPARENT).text("primary_pressed")),
884 );
885 self.set_style(
886 "Button.pill",
887 Style::new(StyleProps::new().fill("surface_2").text("text").radius(999.0).padding((14.0, 6.0)))
888 .hover(StyleProps::new().fill("track"))
889 .checked(StyleProps::new().fill("primary").text("on_primary")),
890 );
891 self.set_style(
894 "Button.elevated",
895 Style::new(StyleProps::new().fill("surface").text("primary").radius(999.0).shadow("sm"))
896 .hover(StyleProps::new().shadow("md"))
897 .pressed(StyleProps::new().fill("surface_2")),
898 );
899 self.set_style(
900 "Button.tonal",
901 Style::new(StyleProps::new().fill("primary_soft").text("primary").border(Color::TRANSPARENT, 0.0).radius(999.0))
902 .hover(StyleProps::new().shadow("sm")),
903 );
904 self.set_style(
905 "Button.text",
906 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").border(Color::TRANSPARENT, 0.0).shadow("none"))
907 .hover(StyleProps::new().fill("primary_soft")),
908 );
909 self.set_style(
910 "Button.fab_extended",
911 Style::new(StyleProps::new().fill("primary_container").text("on_primary_container").radius(16.0).shadow("md").padding((16.0, 0.0)))
912 .hover(StyleProps::new().shadow("lg")),
913 );
914 self.set_style(
915 "Button.icon_filled",
916 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).padding(6.0)).hover(StyleProps::new().fill("primary_hover")),
917 );
918 self.set_style(
919 "Button.icon_tonal",
920 Style::new(StyleProps::new().fill("primary_soft").text("primary").radius(999.0).padding(6.0)).hover(StyleProps::new().shadow("sm")),
921 );
922 self.set_style(
923 "Button.icon_outlined",
924 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").border("border", 1.0).radius(999.0).padding(6.0))
925 .hover(StyleProps::new().fill("hover_overlay")),
926 );
927 self.set_style(
928 "Button.glass",
929 Style::new(StyleProps::new().fill("glass_fill").text("text").border(Color::TRANSPARENT, 0.0).radius(999.0).shadow("sm").material("glass"))
930 .pressed(StyleProps::new().opacity(0.8)),
931 );
932 self.set_style(
933 "Button.glass_prominent",
934 Style::new(StyleProps::new().fill("primary").text("on_primary").border(Color::TRANSPARENT, 0.0).radius(999.0).shadow("sm").material("glass"))
935 .pressed(StyleProps::new().opacity(0.85)),
936 );
937 self.set_style(
939 "Edit.filled",
940 Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 0.0).radius(4.0).padding((14.0, 4.0)))
941 .hover(StyleProps::new().fill("track")),
942 );
943 self.set_style(
944 "Edit.outlined",
945 Style::new(StyleProps::new().fill(Color::TRANSPARENT).border("border", 1.0).radius(4.0).padding((14.0, 4.0)))
946 .focused(StyleProps::new().border("primary", 2.0)),
947 );
948 self.set_style(
949 "Edit.search",
950 Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 1.0).radius(999.0).padding((12.0, 4.0)))
951 .hover(StyleProps::new().border("border", 1.0))
952 .focused(StyleProps::new().fill("surface").border("focus_ring", 2.0)),
953 );
954 self.set_style(
955 "Edit.flat",
956 Style::new(StyleProps::new().fill(Color::TRANSPARENT).border(Color::TRANSPARENT, 0.0).radius(0.0))
957 .hover(StyleProps::new().fill("hover_overlay").border(Color::TRANSPARENT, 0.0))
958 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0)),
959 );
960 self.set_style("Panel.card", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("sm")));
962 self.set_style("Card", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("lg").shadow("sm")));
964 self.set_style("Popover", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md").shadow("lg").padding(12.0)));
965 self.set_style("Card.flat", Style::new(StyleProps::new().shadow("none")));
966 self.set_style("Card.bordered", Style::new(StyleProps::new().border("border", 1.0).shadow("none")));
967 self.set_style("Panel.flat", Style::new(StyleProps::new().fill("surface").border(Color::TRANSPARENT, 0.0).radius(0.0)));
968 self.set_style("Panel.sidebar", Style::new(StyleProps::new().fill("surface_2").border("border", 1.0).radius(0.0)));
969 self.set_style("Panel.overlay", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("lg")));
970 self.set_style("Panel.filled", Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 0.0).radius("lg").shadow("none")));
971 self.set_style("Panel.outlined", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("none")));
972 self.set_style("Panel.glass", Style::new(StyleProps::new().fill("glass_fill").border(Color::TRANSPARENT, 0.0).radius("lg").shadow("md").material("glass")));
973 self.set_style("Button.reveal", Style::new(StyleProps::new().reveal("both")));
975 self.set_style("Panel.reveal", Style::new(StyleProps::new().border("border", 1.0).reveal("border")));
976 self.set_style("ToolBar.floating", Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 0.0).radius(999.0).shadow("md").padding(6.0)));
977 self.set_style("Label.title", Style::new(StyleProps::new().font("title")));
979 self.set_style("Label.heading", Style::new(StyleProps::new().font("heading")));
980 self.set_style("Label.muted", Style::new(StyleProps::new().text("text_muted")));
981 self.set_style("Label.caption", Style::new(StyleProps::new().text("text_muted").font("small")));
982 self.set_style("Label.mono", Style::new(StyleProps::new().font("mono")));
983 self.set_style(
985 "Label.kbd",
986 Style::new(StyleProps::new().font("mono").fill("surface_2").border("border", 1.0).radius(4.0).padding((6.0, 1.0))),
987 );
988 self.set_style("Label.link", Style::new(StyleProps::new().text("primary")).hover(StyleProps::new().text("primary_hover")));
989 self.set_style("Label.error", Style::new(StyleProps::new().text("danger").font("small")));
990 self.set_style("ProgressBar.thin", Style::new(StyleProps::new().radius(2.0)));
992 self.set_style("ProgressBar.success", Style::new(StyleProps::new().text("success")));
993 self.set_style("ProgressBar.danger", Style::new(StyleProps::new().text("danger")));
994 self.set_style("Switch.success", Style::new(StyleProps::new()).checked(StyleProps::new().fill("success")));
996 self.set_style("CheckBox.round", Style::new(StyleProps::new().radius(999.0)));
997 self.set_style("Icon", Style::new(StyleProps::new().text("text")));
999 let hint = if self.dark {
1003 StyleProps::new().fill("surface_2").text("text").border("border", 1.0)
1004 } else {
1005 StyleProps::new().fill("text").text("surface")
1006 };
1007 self.set_style("Hint", Style::new(hint.radius("sm").padding((8.0, 5.0)).font("small").shadow("md")));
1008 self.set_style("Icon.muted", Style::new(StyleProps::new().text("text_muted")));
1009 self.set_style("Badge", Style::new(StyleProps::new().fill("surface_2").text("text_muted").radius(999.0).padding((8.0, 2.0)).font("small")));
1010 for tone in ["primary", "success", "warning", "danger", "info"] {
1011 self.set_style(&format!("Badge.{tone}"), Style::new(StyleProps::new().fill(format!("{tone}_soft").as_str()).text(tone)));
1012 }
1013 self.set_style("Badge.live", Style::new(StyleProps::new().fill("danger").text("on_primary").font("strong")));
1014 self.set_style("Badge.outline", Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").border("border", 1.0)));
1015 self.set_style("Banner", Style::new(StyleProps::new().text("text").radius("md").padding((12.0, 10.0))));
1016 self.set_style(
1017 "SegmentedControl",
1018 Style::new(StyleProps::new().fill("surface_2").text("text").radius("md").transition("fast")).disabled(StyleProps::new().opacity(0.5)),
1019 );
1020 self.set_style("LevelMeter", Style::new(StyleProps::new().fill("track").radius(2.0)));
1021 self.set_style(
1022 "HotkeyEdit",
1023 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((8.0, 4.0)))
1024 .hover(StyleProps::new().border("text_muted", 1.0))
1025 .focused(StyleProps::new().border("focus_ring", 2.0))
1026 .disabled(StyleProps::new().opacity(0.5)),
1027 );
1028 self.set_style("SeekBar", Style::new(StyleProps::new().text("text")).disabled(StyleProps::new().opacity(0.5)));
1029 self.set_style("NodeGraph", Style::new(StyleProps::new().fill("background").text("text").border("border", 1.0).radius("lg")));
1030 self.set_style("ChatView", Style::new(StyleProps::new().fill("background").text("text")));
1031 self.set_style("DirectionPad", Style::new(StyleProps::new().text("text")).disabled(StyleProps::new().opacity(0.5)));
1032 }
1033
1034 pub fn set_style(&mut self, key: &str, style: Style) {
1035 self.styles.insert(key.to_string(), style);
1036 }
1037
1038 pub fn family_name(&self) -> &str {
1040 if self.family.is_empty() { &self.name } else { &self.family }
1041 }
1042
1043 pub fn mix(&mut self, class: &str, theme: &str) {
1046 if theme.is_empty() {
1047 self.components.remove(class);
1048 } else {
1049 self.components.insert(class.to_string(), theme.to_string());
1050 }
1051 }
1052
1053 pub fn mixed_for(&self, classes: &[&str]) -> Option<&str> {
1056 if self.components.is_empty() {
1057 return None;
1058 }
1059 classes.iter().find_map(|c| self.components.get(*c).map(String::as_str))
1060 }
1061
1062 pub fn variants_of(&self, class: &str) -> Vec<String> {
1064 let prefix = format!("{class}.");
1065 let mut v: Vec<String> = self.styles.keys().filter_map(|k| k.strip_prefix(&prefix).map(str::to_string)).collect();
1066 v.sort();
1067 v
1068 }
1069
1070 pub fn style(&self, key: &str) -> Option<&Style> {
1071 self.styles.get(key)
1072 }
1073
1074 pub fn color(&self, token: &str) -> Color {
1077 self.colors.get(token).copied().or_else(|| self.resolve_color(token)).unwrap_or(Color::TRANSPARENT)
1078 }
1079
1080 pub fn color_of(&self, c: &ColorRef) -> Color {
1081 match c {
1082 ColorRef::Value(v) => *v,
1083 ColorRef::Token(t) => self.color(t),
1084 }
1085 }
1086
1087 fn metric(map: &HashMap<String, f32>, m: &Metric) -> f32 {
1088 match m {
1089 Metric::Value(v) => *v,
1090 Metric::Token(t) => map.get(t).copied().unwrap_or(0.0),
1091 }
1092 }
1093
1094 pub fn reach(&self, classes: &[&str], variant: &str) -> f32 {
1101 let mut reach: f32 = 0.0;
1102 let mut add = |s: &Style| {
1103 for props in [&s.base, &s.hover, &s.pressed, &s.focused, &s.checked, &s.disabled] {
1104 if let Some(shadow) = props.shadow.as_deref().and_then(|t| self.shadows.get(t)) {
1105 reach = reach.max(shadow.reach());
1106 }
1107 }
1108 };
1109 for class in classes {
1110 if let Some(s) = self.styles.get(*class) {
1111 add(s);
1112 }
1113 if !variant.is_empty() {
1114 if let Some(s) = self.styles.get(&format!("{class}.{variant}")) {
1115 add(s);
1116 }
1117 }
1118 }
1119 reach
1120 }
1121
1122 pub fn resolve(&self, classes: &[&str], variant: &str, state: ControlState) -> ResolvedStyle {
1123 let mut layers: Vec<&Style> = Vec::new();
1124 for class in classes.iter().rev() {
1125 if let Some(s) = self.styles.get(*class) {
1126 layers.push(s);
1127 }
1128 if !variant.is_empty() {
1129 if let Some(s) = self.styles.get(&format!("{class}.{variant}")) {
1130 layers.push(s);
1131 }
1132 }
1133 }
1134
1135 let mut acc = StyleProps::default();
1136 for s in &layers {
1137 acc.merge(&s.base);
1138 }
1139 let active = [state.hover, state.pressed, state.focused, state.checked, state.disabled];
1140 let checked_as_pressed = state.checked && layers.iter().all(|s| s.checked == StyleProps::default());
1143 for (i, on) in active.into_iter().enumerate() {
1144 if on {
1145 for s in &layers {
1146 acc.merge(s.state(if i == 3 && checked_as_pressed { 1 } else { i }));
1147 }
1148 }
1149 }
1150
1151 let defaults = ResolvedStyle::default();
1152 ResolvedStyle {
1153 fill: acc.fill.as_ref().map_or(defaults.fill, |c| self.color_of(c)),
1154 text: acc
1155 .text
1156 .as_ref()
1157 .map(|c| self.color_of(c))
1158 .or_else(|| self.colors.get("text").copied())
1159 .unwrap_or(defaults.text),
1160 border_color: acc.border_color.as_ref().map_or(defaults.border_color, |c| self.color_of(c)),
1161 border_width: acc.border_width.unwrap_or(defaults.border_width),
1162 radius: acc.radius.as_ref().map_or(0.0, |m| Self::metric(&self.radius, m)),
1163 padding: acc.padding.unwrap_or(defaults.padding),
1164 font: {
1165 let mut font = self.fonts.get(acc.font.as_deref().unwrap_or("default")).cloned().unwrap_or(defaults.font);
1166 if let Some(w) = acc.font_weight {
1167 font.weight = w;
1168 }
1169 if let Some(size) = acc.font_size {
1170 font.size = size;
1171 }
1172 if let Some(italic) = acc.italic {
1173 font.italic = italic;
1174 }
1175 font
1176 },
1177 line_height: acc.line_height.unwrap_or(0.0),
1178 opacity: acc.opacity.unwrap_or(1.0),
1179 transition_ms: acc.transition.as_ref().map_or(0.0, |m| Self::metric(&self.durations, m)),
1180 shadow: acc.shadow.as_deref().and_then(|t| self.shadows.get(t)).copied(),
1181 material: acc.material.as_deref().map_or(Material::None, Material::parse),
1182 reveal: {
1183 let (border, background) = match acc.reveal.as_deref() {
1184 Some("border") => (true, false),
1185 Some("background") => (false, true),
1186 Some("both") => (true, true),
1187 _ => (false, false),
1188 };
1189 let color = acc
1190 .reveal_color
1191 .as_ref()
1192 .map(|c| self.color_of(c))
1193 .or_else(|| self.colors.get("reveal").copied())
1194 .unwrap_or(if self.dark { Color::rgba(255, 255, 255, 90) } else { Color::rgba(0, 0, 0, 56) });
1195 Reveal { border, background, color, radius: acc.reveal_radius.unwrap_or(96.0).max(0.0) }
1196 },
1197 text_case: match acc.text_case.as_deref() {
1198 Some("upper") => crate::text::TextCase::Upper,
1199 Some("lower") => crate::text::TextCase::Lower,
1200 _ => crate::text::TextCase::None,
1201 },
1202 letter_spacing: acc.letter_spacing.unwrap_or(0.0),
1203 gap: acc.gap,
1204 border_sides: acc.border_sides,
1205 border_dash: match acc.border_style.as_deref() {
1206 Some("dashed") => vec![6.0, 4.0],
1207 Some("dotted") => vec![1.5, 3.0],
1208 _ => Vec::new(),
1209 },
1210 }
1211 }
1212}