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