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 pub icon_size: Option<f32>,
121 pub icon_color: Option<ColorRef>,
122 pub icon_gap: Option<f32>,
123 pub width: Option<f32>,
125 pub thumb: Option<ColorRef>,
128 pub scroll_bar: Option<Box<Style>>,
130}
131
132#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
134pub enum Material {
135 #[default]
136 None,
137 Glass,
139 Frosted,
141}
142
143#[derive(Clone, Copy, Debug, Default, PartialEq)]
146pub struct Reveal {
147 pub border: bool,
149 pub background: bool,
151 pub color: Color,
153 pub radius: f32,
155}
156
157impl Reveal {
158 pub fn on(&self) -> bool {
160 (self.border || self.background) && self.radius > 0.0 && self.color.a > 0
161 }
162}
163
164impl Material {
165 pub fn parse(name: &str) -> Material {
166 match name {
167 "glass" => Material::Glass,
168 "frosted" | "acrylic" => Material::Frosted,
169 _ => Material::None,
170 }
171 }
172
173 pub fn blur(self) -> f32 {
175 match self {
176 Material::None => 0.0,
177 Material::Glass => 8.0,
178 Material::Frosted => 22.0,
179 }
180 }
181}
182
183impl StyleProps {
184 pub fn new() -> Self {
185 Self::default()
186 }
187
188 pub fn fill(mut self, c: impl Into<ColorRef>) -> Self {
189 self.fill = Some(c.into());
190 self
191 }
192
193 pub fn text(mut self, c: impl Into<ColorRef>) -> Self {
194 self.text = Some(c.into());
195 self
196 }
197
198 pub fn border(mut self, c: impl Into<ColorRef>, width: f32) -> Self {
199 self.border_color = Some(c.into());
200 self.border_width = Some(width);
201 self
202 }
203
204 pub fn radius(mut self, m: impl Into<Metric>) -> Self {
205 self.radius = Some(m.into());
206 self
207 }
208
209 pub fn padding(mut self, b: impl Into<Bounds>) -> Self {
210 self.padding = Some(b.into());
211 self
212 }
213
214 pub fn font(mut self, name: &str) -> Self {
215 self.font = Some(name.to_string());
216 self
217 }
218
219 pub fn opacity(mut self, v: f32) -> Self {
220 self.opacity = Some(v);
221 self
222 }
223
224 pub fn transition(mut self, m: impl Into<Metric>) -> Self {
225 self.transition = Some(m.into());
226 self
227 }
228
229 pub fn shadow(mut self, token: &str) -> Self {
230 self.shadow = Some(token.to_string());
231 self
232 }
233
234 pub fn material(mut self, name: &str) -> Self {
236 self.material = Some(name.to_string());
237 self
238 }
239
240 pub fn reveal(mut self, mode: &str) -> Self {
242 self.reveal = Some(mode.to_string());
243 self
244 }
245
246 pub fn merge(&mut self, other: &StyleProps) {
248 if other.fill.is_some() {
249 self.fill.clone_from(&other.fill);
250 }
251 if other.text.is_some() {
252 self.text.clone_from(&other.text);
253 }
254 if other.border_color.is_some() {
255 self.border_color.clone_from(&other.border_color);
256 }
257 if other.border_width.is_some() {
258 self.border_width = other.border_width;
259 }
260 if other.radius.is_some() {
261 self.radius.clone_from(&other.radius);
262 }
263 if other.padding.is_some() {
264 self.padding = other.padding;
265 }
266 if other.font.is_some() {
267 self.font.clone_from(&other.font);
268 }
269 if other.opacity.is_some() {
270 self.opacity = other.opacity;
271 }
272 if other.transition.is_some() {
273 self.transition.clone_from(&other.transition);
274 }
275 if other.shadow.is_some() {
276 self.shadow.clone_from(&other.shadow);
277 }
278 if other.material.is_some() {
279 self.material.clone_from(&other.material);
280 }
281 if other.reveal.is_some() {
282 self.reveal.clone_from(&other.reveal);
283 }
284 if other.reveal_color.is_some() {
285 self.reveal_color.clone_from(&other.reveal_color);
286 }
287 if other.reveal_radius.is_some() {
288 self.reveal_radius = other.reveal_radius;
289 }
290 if other.text_case.is_some() {
291 self.text_case.clone_from(&other.text_case);
292 }
293 if other.letter_spacing.is_some() {
294 self.letter_spacing = other.letter_spacing;
295 }
296 if other.gap.is_some() {
297 self.gap = other.gap;
298 }
299 if other.border_sides.is_some() {
300 self.border_sides = other.border_sides;
301 }
302 if other.border_style.is_some() {
303 self.border_style.clone_from(&other.border_style);
304 }
305 if other.line_height.is_some() {
306 self.line_height = other.line_height;
307 }
308 if other.font_weight.is_some() {
309 self.font_weight = other.font_weight;
310 }
311 if other.font_size.is_some() {
312 self.font_size = other.font_size;
313 }
314 if other.italic.is_some() {
315 self.italic = other.italic;
316 }
317 if other.icon_size.is_some() {
318 self.icon_size = other.icon_size;
319 }
320 if other.icon_color.is_some() {
321 self.icon_color.clone_from(&other.icon_color);
322 }
323 if other.icon_gap.is_some() {
324 self.icon_gap = other.icon_gap;
325 }
326 if other.width.is_some() {
327 self.width = other.width;
328 }
329 if other.thumb.is_some() {
330 self.thumb.clone_from(&other.thumb);
331 }
332 if let Some(bar) = &other.scroll_bar {
333 match &mut self.scroll_bar {
334 Some(mine) => mine.merge(bar),
335 None => self.scroll_bar = Some(bar.clone()),
336 }
337 }
338 }
339}
340
341#[derive(Clone, Debug, Default, PartialEq)]
343pub struct Style {
344 pub base: StyleProps,
345 pub hover: StyleProps,
346 pub pressed: StyleProps,
347 pub focused: StyleProps,
348 pub checked: StyleProps,
349 pub disabled: StyleProps,
350}
351
352impl Style {
353 pub fn new(base: StyleProps) -> Self {
354 Self { base, ..Default::default() }
355 }
356
357 pub fn hover(mut self, p: StyleProps) -> Self {
358 self.hover = p;
359 self
360 }
361
362 pub fn pressed(mut self, p: StyleProps) -> Self {
363 self.pressed = p;
364 self
365 }
366
367 pub fn focused(mut self, p: StyleProps) -> Self {
368 self.focused = p;
369 self
370 }
371
372 pub fn checked(mut self, p: StyleProps) -> Self {
373 self.checked = p;
374 self
375 }
376
377 pub fn disabled(mut self, p: StyleProps) -> Self {
378 self.disabled = p;
379 self
380 }
381
382 pub fn merge(&mut self, other: &Style) {
384 self.base.merge(&other.base);
385 self.hover.merge(&other.hover);
386 self.pressed.merge(&other.pressed);
387 self.focused.merge(&other.focused);
388 self.checked.merge(&other.checked);
389 self.disabled.merge(&other.disabled);
390 }
391
392 fn state(&self, index: usize) -> &StyleProps {
394 match index {
395 0 => &self.hover,
396 1 => &self.pressed,
397 2 => &self.focused,
398 3 => &self.checked,
399 _ => &self.disabled,
400 }
401 }
402}
403
404#[derive(Clone, Debug, PartialEq)]
406pub struct ResolvedStyle {
407 pub fill: Color,
408 pub text: Color,
409 pub border_color: Color,
410 pub border_width: f32,
411 pub radius: f32,
412 pub padding: Bounds,
413 pub font: FontSpec,
414 pub opacity: f32,
415 pub transition_ms: f32,
416 pub shadow: Option<Shadow>,
418 pub material: Material,
420 pub reveal: Reveal,
422 pub text_case: crate::text::TextCase,
424 pub letter_spacing: f32,
426 pub border_sides: Option<Bounds>,
428 pub border_dash: Vec<f32>,
430 pub line_height: f32,
432 pub gap: Option<f32>,
434 pub icon_size: Option<f32>,
436 pub icon_color: Option<Color>,
437 pub icon_gap: Option<f32>,
438 pub scroll_bar: Option<Box<Style>>,
440}
441
442impl ResolvedStyle {
443 pub fn border_stroke(&self, color: Color, width: f32) -> crate::canvas::Stroke {
445 let stroke = crate::canvas::Stroke::new(color, width);
446 if self.border_dash.is_empty() { stroke } else { stroke.dashed(self.border_dash.clone()) }
447 }
448}
449
450impl Default for ResolvedStyle {
451 fn default() -> Self {
452 Self {
453 fill: Color::TRANSPARENT,
454 text: Color::BLACK,
455 border_color: Color::TRANSPARENT,
456 border_width: 0.0,
457 radius: 0.0,
458 padding: Bounds::ZERO,
459 font: FontSpec::default(),
460 opacity: 1.0,
461 transition_ms: 0.0,
462 shadow: None,
463 material: Material::None,
464 reveal: Reveal::default(),
465 text_case: crate::text::TextCase::None,
466 letter_spacing: 0.0,
467 border_sides: None,
468 border_dash: Vec::new(),
469 line_height: 0.0,
470 gap: None,
471 icon_size: None,
472 icon_color: None,
473 icon_gap: None,
474 scroll_bar: None,
475 }
476 }
477}
478
479impl ResolvedStyle {
480 pub fn lerp(&self, other: &ResolvedStyle, t: f32) -> ResolvedStyle {
483 let mix = |a: f32, b: f32| a + (b - a) * t;
484 ResolvedStyle {
485 fill: self.fill.lerp(other.fill, t),
486 text: self.text.lerp(other.text, t),
487 border_color: self.border_color.lerp(other.border_color, t),
488 border_width: mix(self.border_width, other.border_width),
489 radius: mix(self.radius, other.radius),
490 padding: other.padding,
491 font: other.font.clone(),
492 opacity: mix(self.opacity, other.opacity),
493 transition_ms: other.transition_ms,
494 shadow: match (self.shadow, other.shadow) {
495 (Some(a), Some(b)) => Some(Shadow {
496 color: a.color.lerp(b.color, t),
497 blur: mix(a.blur, b.blur),
498 offset: crate::PointF::new(mix(a.offset.x, b.offset.x), mix(a.offset.y, b.offset.y)),
499 spread: mix(a.spread, b.spread),
500 }),
501 _ => other.shadow,
502 },
503 material: other.material,
504 reveal: other.reveal,
505 text_case: other.text_case,
506 letter_spacing: mix(self.letter_spacing, other.letter_spacing),
507 border_sides: other.border_sides,
508 border_dash: other.border_dash.clone(),
509 line_height: mix(self.line_height, other.line_height),
510 gap: other.gap,
511 icon_size: other.icon_size,
512 icon_color: match (self.icon_color, other.icon_color) {
513 (Some(a), Some(b)) => Some(a.lerp(b, t)),
514 _ => other.icon_color,
515 },
516 icon_gap: other.icon_gap,
517 scroll_bar: other.scroll_bar.clone(),
518 }
519 }
520}
521
522#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
526pub enum Look {
527 #[default]
529 Aylis,
530 Material2,
532 Material3,
534 Ios7,
536 Ios13,
538 Ios26,
540 Win7,
542 Win10,
544 Win11,
546 WinPhone,
548}
549
550impl crate::prop::PropEnum for Look {
551 const NAMES: &'static [&'static str] = &["aylis", "material2", "material3", "ios7", "ios13", "ios26", "win7", "win10", "win11", "winphone"];
552
553 fn from_index(index: usize) -> Option<Self> {
554 [
555 Look::Aylis,
556 Look::Material2,
557 Look::Material3,
558 Look::Ios7,
559 Look::Ios13,
560 Look::Ios26,
561 Look::Win7,
562 Look::Win10,
563 Look::Win11,
564 Look::WinPhone,
565 ]
566 .get(index)
567 .copied()
568 }
569
570 fn index(self) -> usize {
571 self as usize
572 }
573}
574
575impl Look {
576 pub fn is_material(self) -> bool {
577 matches!(self, Look::Material2 | Look::Material3)
578 }
579
580 pub fn is_ios(self) -> bool {
581 matches!(self, Look::Ios7 | Look::Ios13 | Look::Ios26)
582 }
583
584 pub fn is_windows(self) -> bool {
585 matches!(self, Look::Win7 | Look::Win10 | Look::Win11 | Look::WinPhone)
586 }
587}
588
589#[derive(Clone, Debug, Default, PartialEq)]
591pub struct Theme {
592 pub name: String,
593 pub family: String,
596 pub dark: bool,
598 pub look: Look,
600 pub colors: HashMap<String, Color>,
601 pub fonts: HashMap<String, FontSpec>,
602 pub radius: HashMap<String, f32>,
603 pub spacing: HashMap<String, f32>,
604 pub durations: HashMap<String, f32>,
606 pub shadows: HashMap<String, Shadow>,
607 pub styles: HashMap<String, Style>,
609 pub components: HashMap<String, String>,
611}
612
613impl Theme {
614 pub fn apply_font_family(&mut self, family: &str) {
617 if family.is_empty() {
618 return;
619 }
620 for font in self.fonts.values_mut() {
621 if font.family != "monospace" && !font.family.to_lowercase().contains("mono") {
622 font.family = family.to_string();
623 }
624 }
625 }
626
627 pub fn new(name: impl Into<String>) -> Self {
628 Self { name: name.into(), ..Default::default() }
629 }
630
631 pub fn light() -> Self {
633 let mut t = Theme::new("Aylis Light");
634 t.family = "Aylis".into();
635 t.set_colors(&[
636 ("background", "#F5F5F7"),
637 ("surface", "#FFFFFF"),
638 ("text", "#1F2328"),
639 ("text_muted", "#6B7280"),
640 ("border", "#D0D7DE"),
641 ("primary", "#2563EB"),
642 ("primary_hover", "#3B82F6"),
643 ("primary_pressed", "#1D4ED8"),
644 ("on_primary", "#FFFFFF"),
645 ("on_danger", "#FFFFFF"),
647 ("on_success", "#FFFFFF"),
648 ("danger", "#DC2626"),
649 ("danger_hover", "#EF4444"),
650 ("danger_pressed", "#B91C1C"),
651 ("success", "#16A34A"),
652 ("warning", "#D97706"),
653 ("focus_ring", "#3B82F6"),
654 ("accent", "#9333EA"),
655 ("hover_overlay", "#0000000F"),
656 ("glass_fill", "#FFFFFF73"),
657 ("primary_container", "#DBEAFE"),
658 ("on_primary_container", "#1E3A8A"),
659 ("selection", "#2563EB40"),
660 ("track", "#D0D7DE"),
661 ("surface_2", "#F0F1F4"),
662 ("info", "#0284C7"),
663 ("primary_soft", "#2563EB1A"),
664 ("success_soft", "#16A34A1A"),
665 ("warning_soft", "#D977061F"),
666 ("danger_soft", "#DC26261A"),
667 ("info_soft", "#0284C71A"),
668 ("meter_low", "#16A34A"),
669 ("meter_mid", "#EAB308"),
670 ("meter_high", "#DC2626"),
671 ("presence_online", "#16A34A"),
672 ("presence_away", "#EAB308"),
673 ("presence_busy", "#DC2626"),
674 ("presence_offline", "#9CA3AF"),
675 ]);
676 t.shadows.insert("sm".into(), Shadow::new(Color::rgba(15, 23, 42, 20), 4.0, 0.0, 1.0));
677 t.shadows.insert("md".into(), Shadow::new(Color::rgba(15, 23, 42, 28), 12.0, 0.0, 4.0));
678 t.shadows.insert("lg".into(), Shadow::new(Color::rgba(15, 23, 42, 36), 28.0, 0.0, 10.0));
679 t.shadows.insert("2xl".into(), Shadow::new(Color::rgba(15, 23, 42, 64), 50.0, 0.0, 25.0));
681 t.add_standard_tokens();
682 t.add_standard_styles();
683 t
684 }
685
686 pub fn dark() -> Self {
688 let mut t = Theme::new("Aylis Dark");
689 t.family = "Aylis".into();
690 t.dark = true;
691 t.set_colors(&[
692 ("background", "#1E1E1E"),
693 ("surface", "#252526"),
694 ("text", "#E6E6E6"),
695 ("text_muted", "#9A9A9A"),
696 ("border", "#3C3C3C"),
697 ("primary", "#3B82F6"),
698 ("primary_hover", "#60A5FA"),
699 ("primary_pressed", "#2563EB"),
700 ("on_primary", "#FFFFFF"),
701 ("on_danger", "#FFFFFF"),
703 ("on_success", "#FFFFFF"),
704 ("danger", "#EF4444"),
705 ("danger_hover", "#F87171"),
706 ("danger_pressed", "#DC2626"),
707 ("success", "#22C55E"),
708 ("warning", "#F59E0B"),
709 ("focus_ring", "#60A5FA"),
710 ("accent", "#A855F7"),
711 ("hover_overlay", "#FFFFFF14"),
712 ("glass_fill", "#1E1E2273"),
713 ("primary_container", "#1E3A8A"),
714 ("on_primary_container", "#DBEAFE"),
715 ("selection", "#3B82F660"),
716 ("track", "#4A4A4F"),
717 ("surface_2", "#2D2D30"),
718 ("info", "#38BDF8"),
719 ("primary_soft", "#3B82F62E"),
720 ("success_soft", "#22C55E29"),
721 ("warning_soft", "#F59E0B29"),
722 ("danger_soft", "#EF444429"),
723 ("info_soft", "#38BDF829"),
724 ("meter_low", "#22C55E"),
725 ("meter_mid", "#FACC15"),
726 ("meter_high", "#EF4444"),
727 ("presence_online", "#22C55E"),
728 ("presence_away", "#FACC15"),
729 ("presence_busy", "#EF4444"),
730 ("presence_offline", "#6B7280"),
731 ]);
732 t.shadows.insert("sm".into(), Shadow::new(Color::rgba(0, 0, 0, 70), 4.0, 0.0, 1.0));
733 t.shadows.insert("md".into(), Shadow::new(Color::rgba(0, 0, 0, 90), 14.0, 0.0, 4.0));
734 t.shadows.insert("lg".into(), Shadow::new(Color::rgba(0, 0, 0, 110), 30.0, 0.0, 10.0));
735 t.shadows.insert("2xl".into(), Shadow::new(Color::rgba(0, 0, 0, 150), 50.0, 0.0, 25.0));
736 t.add_standard_tokens();
737 t.add_standard_styles();
738 t
739 }
740
741 fn set_colors(&mut self, colors: &[(&str, &str)]) {
742 for (name, hex) in colors {
743 let color = Color::from_hex(hex).expect("cor inválida no tema padrão");
744 self.colors.insert(name.to_string(), color);
745 }
746 }
747
748 fn add_standard_tokens(&mut self) {
749 self.fonts.insert("default".into(), FontSpec::new("", 14.0));
750 self.fonts.insert("title".into(), FontSpec::new("", 20.0).weight(FontWeight::Bold));
751 self.fonts.insert("heading".into(), FontSpec::new("", 16.0).weight(FontWeight::SemiBold));
752 self.fonts.insert("small".into(), FontSpec::new("", 12.0));
753 self.fonts.insert("strong".into(), FontSpec::new("", 14.0).weight(FontWeight::SemiBold));
754 self.fonts.insert("mono".into(), FontSpec::new("monospace", 13.0));
755 for (k, v) in [("sm", 4.0), ("md", 6.0), ("lg", 10.0)] {
756 self.radius.insert(k.into(), v);
757 }
758 for (k, v) in [("xs", 4.0), ("sm", 8.0), ("md", 12.0), ("lg", 16.0)] {
759 self.spacing.insert(k.into(), v);
760 }
761 for (k, v) in [("fast", 120.0), ("normal", 200.0), ("slow", 350.0)] {
762 self.durations.insert(k.into(), v);
763 }
764 }
765
766 fn add_standard_styles(&mut self) {
767 self.set_style("Form", Style::new(StyleProps::new().fill("background").text("text")));
768 self.set_style(
769 "Panel",
770 Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("sm")),
771 );
772 self.set_style("Label", Style::new(StyleProps::new().text("text")));
773 self.set_style(
774 "Button",
775 Style::new(
776 StyleProps::new()
777 .fill("primary")
778 .text("on_primary")
779 .radius("md")
780 .padding((12.0, 6.0))
781 .transition("fast"),
782 )
783 .hover(StyleProps::new().fill("primary_hover"))
784 .pressed(StyleProps::new().fill("primary_pressed"))
785 .focused(StyleProps::new().border("focus_ring", 2.0))
786 .disabled(StyleProps::new().opacity(0.5)),
787 );
788 self.set_style(
789 "Button.danger",
790 Style::new(StyleProps::new().fill("danger").text("on_danger"))
791 .hover(StyleProps::new().fill("danger_hover"))
792 .pressed(StyleProps::new().fill("danger_pressed")),
793 );
794 self.set_style(
796 "Button.round",
797 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).border(Color::TRANSPARENT, 0.0))
798 .hover(StyleProps::new().fill("primary_hover"))
799 .pressed(StyleProps::new().fill("primary_pressed")),
800 );
801 self.set_style(
802 "Button.flat",
803 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary"))
804 .hover(StyleProps::new().fill("hover_overlay"))
805 .pressed(StyleProps::new().fill("hover_overlay")),
806 );
807 self.set_style(
808 "Edit",
809 Style::new(
810 StyleProps::new()
811 .fill("surface")
812 .text("text")
813 .border("border", 1.0)
814 .radius("sm")
815 .padding((8.0, 4.0)),
816 )
817 .hover(StyleProps::new().border("text_muted", 1.0))
818 .focused(StyleProps::new().border("focus_ring", 2.0))
819 .disabled(StyleProps::new().opacity(0.5)),
820 );
821 self.set_style(
822 "ComboBox",
823 Style::new(
824 StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((8.0, 4.0)),
825 )
826 .hover(StyleProps::new().border("text_muted", 1.0))
827 .focused(StyleProps::new().border("focus_ring", 2.0))
828 .disabled(StyleProps::new().opacity(0.5)),
829 );
830 for class in ["CheckBox", "RadioButton"] {
832 self.set_style(
833 class,
834 Style::new(
835 StyleProps::new().fill("surface").text("text").border("text_muted", 1.5).radius("sm").transition("fast"),
836 )
837 .hover(StyleProps::new().border("primary", 1.5))
838 .checked(StyleProps::new().fill("primary").border("primary", 1.5))
839 .focused(StyleProps::new().border("focus_ring", 2.0))
840 .disabled(StyleProps::new().opacity(0.5)),
841 );
842 }
843 self.set_style(
845 "Switch",
846 Style::new(StyleProps::new().fill("track").text("text").transition("normal"))
847 .checked(StyleProps::new().fill("primary"))
848 .focused(StyleProps::new().border("focus_ring", 2.0))
849 .disabled(StyleProps::new().opacity(0.5)),
850 );
851 self.set_style("ProgressBar", Style::new(StyleProps::new().fill("track").radius(4.0)));
853 for class in ["Memo", "ListBox", "ListView", "TreeView", "Calendar"] {
855 self.set_style(
856 class,
857 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((6.0, 4.0)))
858 .focused(StyleProps::new().border("focus_ring", 2.0))
859 .disabled(StyleProps::new().opacity(0.5)),
860 );
861 self.set_style(
864 &format!("{class}.flat"),
865 Style::new(StyleProps::new().border(Color::TRANSPARENT, 0.0))
866 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0)),
867 );
868 }
869 self.set_style(
872 "CodeEditor",
873 Style::new(StyleProps::new().fill("surface").text("text").border(Color::TRANSPARENT, 0.0).radius(0.0).padding((4.0, 2.0)))
874 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0))
875 .disabled(StyleProps::new().opacity(0.5)),
876 );
877 self.set_style("TabStrip", Style::new(StyleProps::new().fill("surface_2").text("text").border(Color::TRANSPARENT, 0.0)));
880 self.set_style(
882 "TrackBar",
883 Style::new(StyleProps::new().fill("track").text("text").radius(3.0))
884 .focused(StyleProps::new().border("focus_ring", 2.0))
885 .disabled(StyleProps::new().opacity(0.5)),
886 );
887 self.set_style("GroupBox", Style::new(StyleProps::new().text("text").border("border", 1.0).radius("md")));
888 self.set_style("Expander", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
889 self.set_style("TabControl", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
890 self.set_style("TabItem", Style::new(StyleProps::new().text("text")));
891 self.set_style("StatusBar", Style::new(StyleProps::new().fill("surface").text("text_muted").border("border", 1.0)));
892 self.set_style("ToolBar", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0)));
893 self.set_style("RadioGroup", Style::new(StyleProps::new().text("text").border("border", 1.0).radius("md")));
894 self.set_style("Chart", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md")));
895 self.add_variant_styles();
896 }
897
898 fn add_variant_styles(&mut self) {
900 self.set_style(
902 "Button.secondary",
903 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0))
904 .hover(StyleProps::new().fill("surface_2").border("text_muted", 1.0))
905 .pressed(StyleProps::new().fill("track")),
906 );
907 self.set_style(
908 "Button.outline",
909 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").border("primary", 1.0))
910 .hover(StyleProps::new().fill("primary_soft"))
911 .pressed(StyleProps::new().fill("primary_soft")),
912 );
913 self.set_style(
914 "Button.success",
915 Style::new(StyleProps::new().fill("success").text("on_success"))
916 .hover(StyleProps::new().opacity(0.9))
917 .pressed(StyleProps::new().opacity(0.8)),
918 );
919 self.set_style(
921 "Button.positive",
922 Style::new(StyleProps::new().fill("success").text("on_success")).hover(StyleProps::new().opacity(0.9)).pressed(StyleProps::new().opacity(0.8)),
923 );
924 self.set_style(
925 "Button.negative",
926 Style::new(StyleProps::new().fill("danger").text("on_danger")).hover(StyleProps::new().fill("danger_hover")).pressed(StyleProps::new().fill("danger_pressed")),
927 );
928 self.set_style(
929 "Button.accent",
930 Style::new(StyleProps::new().fill("accent").text("on_primary")).hover(StyleProps::new().opacity(0.9)).pressed(StyleProps::new().opacity(0.8)),
931 );
932 self.set_style("Button.rounded", Style::new(StyleProps::new().radius(28.0)));
933 self.set_style("Button.unelevated", Style::new(StyleProps::new().shadow("none")));
934 self.set_style(
935 "Button.push",
936 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)),
937 );
938 self.set_style("Button.glossy", Style::new(StyleProps::new().fill("primary_hover").shadow("md")).hover(StyleProps::new().fill("primary")));
939 self.set_style(
940 "Button.icon",
941 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").padding(6.0).radius("md"))
942 .hover(StyleProps::new().fill("hover_overlay"))
943 .pressed(StyleProps::new().fill("track"))
944 .checked(StyleProps::new().fill("primary_soft").text("primary")),
945 );
946 self.set_style(
947 "Button.fab",
948 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).shadow("md").padding(10.0))
949 .hover(StyleProps::new().fill("primary_hover").shadow("lg"))
950 .pressed(StyleProps::new().fill("primary_pressed").shadow("sm")),
951 );
952 self.set_style(
953 "Button.link",
954 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").padding((2.0, 2.0)))
955 .hover(StyleProps::new().fill(Color::TRANSPARENT).text("primary_hover"))
956 .pressed(StyleProps::new().fill(Color::TRANSPARENT).text("primary_pressed")),
957 );
958 self.set_style(
959 "Button.pill",
960 Style::new(StyleProps::new().fill("surface_2").text("text").radius(999.0).padding((14.0, 6.0)))
961 .hover(StyleProps::new().fill("track"))
962 .checked(StyleProps::new().fill("primary").text("on_primary")),
963 );
964 self.set_style(
967 "Button.elevated",
968 Style::new(StyleProps::new().fill("surface").text("primary").radius(999.0).shadow("sm"))
969 .hover(StyleProps::new().shadow("md"))
970 .pressed(StyleProps::new().fill("surface_2")),
971 );
972 self.set_style(
973 "Button.tonal",
974 Style::new(StyleProps::new().fill("primary_soft").text("primary").border(Color::TRANSPARENT, 0.0).radius(999.0))
975 .hover(StyleProps::new().shadow("sm")),
976 );
977 self.set_style(
978 "Button.text",
979 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("primary").border(Color::TRANSPARENT, 0.0).shadow("none"))
980 .hover(StyleProps::new().fill("primary_soft")),
981 );
982 self.set_style(
983 "Button.fab_extended",
984 Style::new(StyleProps::new().fill("primary_container").text("on_primary_container").radius(16.0).shadow("md").padding((16.0, 0.0)))
985 .hover(StyleProps::new().shadow("lg")),
986 );
987 self.set_style(
988 "Button.icon_filled",
989 Style::new(StyleProps::new().fill("primary").text("on_primary").radius(999.0).padding(6.0)).hover(StyleProps::new().fill("primary_hover")),
990 );
991 self.set_style(
992 "Button.icon_tonal",
993 Style::new(StyleProps::new().fill("primary_soft").text("primary").radius(999.0).padding(6.0)).hover(StyleProps::new().shadow("sm")),
994 );
995 self.set_style(
996 "Button.icon_outlined",
997 Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").border("border", 1.0).radius(999.0).padding(6.0))
998 .hover(StyleProps::new().fill("hover_overlay")),
999 );
1000 self.set_style(
1001 "Button.glass",
1002 Style::new(StyleProps::new().fill("glass_fill").text("text").border(Color::TRANSPARENT, 0.0).radius(999.0).shadow("sm").material("glass"))
1003 .pressed(StyleProps::new().opacity(0.8)),
1004 );
1005 self.set_style(
1006 "Button.glass_prominent",
1007 Style::new(StyleProps::new().fill("primary").text("on_primary").border(Color::TRANSPARENT, 0.0).radius(999.0).shadow("sm").material("glass"))
1008 .pressed(StyleProps::new().opacity(0.85)),
1009 );
1010 self.set_style(
1012 "Edit.filled",
1013 Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 0.0).radius(4.0).padding((14.0, 4.0)))
1014 .hover(StyleProps::new().fill("track")),
1015 );
1016 self.set_style(
1017 "Edit.outlined",
1018 Style::new(StyleProps::new().fill(Color::TRANSPARENT).border("border", 1.0).radius(4.0).padding((14.0, 4.0)))
1019 .focused(StyleProps::new().border("primary", 2.0)),
1020 );
1021 let outlined = self.styles["Edit.outlined"].clone();
1023 self.set_style("ComboBox.outlined", outlined);
1024 self.set_style(
1025 "Edit.search",
1026 Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 1.0).radius(999.0).padding((12.0, 4.0)))
1027 .hover(StyleProps::new().border("border", 1.0))
1028 .focused(StyleProps::new().fill("surface").border("focus_ring", 2.0)),
1029 );
1030 self.set_style(
1031 "Edit.flat",
1032 Style::new(StyleProps::new().fill(Color::TRANSPARENT).border(Color::TRANSPARENT, 0.0).radius(0.0))
1033 .hover(StyleProps::new().fill("hover_overlay").border(Color::TRANSPARENT, 0.0))
1034 .focused(StyleProps::new().border(Color::TRANSPARENT, 0.0)),
1035 );
1036 self.set_style("Panel.card", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("sm")));
1038 self.set_style("Card", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("lg").shadow("sm")));
1040 self.set_style("Popover", Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("md").shadow("lg").padding(12.0)));
1041 self.set_style("Card.flat", Style::new(StyleProps::new().shadow("none")));
1042 self.set_style("Card.bordered", Style::new(StyleProps::new().border("border", 1.0).shadow("none")));
1043 self.set_style("Panel.flat", Style::new(StyleProps::new().fill("surface").border(Color::TRANSPARENT, 0.0).radius(0.0)));
1044 self.set_style("Panel.sidebar", Style::new(StyleProps::new().fill("surface_2").border("border", 1.0).radius(0.0)));
1045 self.set_style("Panel.overlay", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("lg")));
1046 self.set_style("Panel.filled", Style::new(StyleProps::new().fill("surface_2").border(Color::TRANSPARENT, 0.0).radius("lg").shadow("none")));
1047 self.set_style("Panel.outlined", Style::new(StyleProps::new().fill("surface").border("border", 1.0).radius("lg").shadow("none")));
1048 self.set_style("Panel.glass", Style::new(StyleProps::new().fill("glass_fill").border(Color::TRANSPARENT, 0.0).radius("lg").shadow("md").material("glass")));
1049 self.set_style("Button.reveal", Style::new(StyleProps::new().reveal("both")));
1051 self.set_style("Panel.reveal", Style::new(StyleProps::new().border("border", 1.0).reveal("border")));
1052 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)));
1053 self.set_style("Label.title", Style::new(StyleProps::new().font("title")));
1055 self.set_style("Label.heading", Style::new(StyleProps::new().font("heading")));
1056 self.set_style("Label.muted", Style::new(StyleProps::new().text("text_muted")));
1057 self.set_style("Label.caption", Style::new(StyleProps::new().text("text_muted").font("small")));
1058 self.set_style("Label.mono", Style::new(StyleProps::new().font("mono")));
1059 self.set_style(
1061 "Label.kbd",
1062 Style::new(StyleProps::new().font("mono").fill("surface_2").border("border", 1.0).radius(4.0).padding((6.0, 1.0))),
1063 );
1064 self.set_style("Label.link", Style::new(StyleProps::new().text("primary")).hover(StyleProps::new().text("primary_hover")));
1065 self.set_style("Label.error", Style::new(StyleProps::new().text("danger").font("small")));
1066 self.set_style("ProgressBar.thin", Style::new(StyleProps::new().radius(2.0)));
1068 self.set_style("ProgressBar.success", Style::new(StyleProps::new().text("success")));
1069 self.set_style("ProgressBar.danger", Style::new(StyleProps::new().text("danger")));
1070 self.set_style("Switch.success", Style::new(StyleProps::new()).checked(StyleProps::new().fill("success")));
1072 self.set_style("CheckBox.round", Style::new(StyleProps::new().radius(999.0)));
1073 self.set_style("Icon", Style::new(StyleProps::new().text("text")));
1075 let hint = if self.dark {
1079 StyleProps::new().fill("surface_2").text("text").border("border", 1.0)
1080 } else {
1081 StyleProps::new().fill("text").text("surface")
1082 };
1083 self.set_style("Hint", Style::new(hint.radius("sm").padding((8.0, 5.0)).font("small").shadow("md")));
1084 self.set_style("Icon.muted", Style::new(StyleProps::new().text("text_muted")));
1085 self.set_style("Badge", Style::new(StyleProps::new().fill("surface_2").text("text_muted").radius(999.0).padding((8.0, 2.0)).font("small")));
1086 for tone in ["primary", "success", "warning", "danger", "info"] {
1087 self.set_style(&format!("Badge.{tone}"), Style::new(StyleProps::new().fill(format!("{tone}_soft").as_str()).text(tone)));
1088 }
1089 self.set_style("Badge.live", Style::new(StyleProps::new().fill("danger").text("on_primary").font("strong")));
1090 self.set_style("Badge.outline", Style::new(StyleProps::new().fill(Color::TRANSPARENT).text("text").border("border", 1.0)));
1091 self.set_style("Banner", Style::new(StyleProps::new().text("text").radius("md").padding((12.0, 10.0))));
1092 self.set_style(
1093 "SegmentedControl",
1094 Style::new(StyleProps::new().fill("surface_2").text("text").radius("md").transition("fast")).disabled(StyleProps::new().opacity(0.5)),
1095 );
1096 self.set_style("LevelMeter", Style::new(StyleProps::new().fill("track").radius(2.0)));
1097 self.set_style(
1098 "HotkeyEdit",
1099 Style::new(StyleProps::new().fill("surface").text("text").border("border", 1.0).radius("sm").padding((8.0, 4.0)))
1100 .hover(StyleProps::new().border("text_muted", 1.0))
1101 .focused(StyleProps::new().border("focus_ring", 2.0))
1102 .disabled(StyleProps::new().opacity(0.5)),
1103 );
1104 self.set_style("SeekBar", Style::new(StyleProps::new().text("text")).disabled(StyleProps::new().opacity(0.5)));
1105 self.set_style("NodeGraph", Style::new(StyleProps::new().fill("background").text("text").border("border", 1.0).radius("lg")));
1106 self.set_style("ChatView", Style::new(StyleProps::new().fill("background").text("text")));
1107 self.set_style("DirectionPad", Style::new(StyleProps::new().text("text")).disabled(StyleProps::new().opacity(0.5)));
1108 }
1109
1110 pub fn set_style(&mut self, key: &str, style: Style) {
1111 self.styles.insert(key.to_string(), style);
1112 }
1113
1114 pub fn family_name(&self) -> &str {
1116 if self.family.is_empty() { &self.name } else { &self.family }
1117 }
1118
1119 pub fn mix(&mut self, class: &str, theme: &str) {
1122 if theme.is_empty() {
1123 self.components.remove(class);
1124 } else {
1125 self.components.insert(class.to_string(), theme.to_string());
1126 }
1127 }
1128
1129 pub fn mixed_for(&self, classes: &[&str]) -> Option<&str> {
1132 if self.components.is_empty() {
1133 return None;
1134 }
1135 classes.iter().find_map(|c| self.components.get(*c).map(String::as_str))
1136 }
1137
1138 pub fn variants_of(&self, class: &str) -> Vec<String> {
1140 let prefix = format!("{class}.");
1141 let mut v: Vec<String> = self.styles.keys().filter_map(|k| k.strip_prefix(&prefix).map(str::to_string)).collect();
1142 v.sort();
1143 v
1144 }
1145
1146 pub fn style(&self, key: &str) -> Option<&Style> {
1147 self.styles.get(key)
1148 }
1149
1150 pub fn color(&self, token: &str) -> Color {
1153 self.colors.get(token).copied().or_else(|| self.resolve_color(token)).unwrap_or(Color::TRANSPARENT)
1154 }
1155
1156 pub fn color_of(&self, c: &ColorRef) -> Color {
1157 match c {
1158 ColorRef::Value(v) => *v,
1159 ColorRef::Token(t) => self.color(t),
1160 }
1161 }
1162
1163 fn metric(map: &HashMap<String, f32>, m: &Metric) -> f32 {
1164 match m {
1165 Metric::Value(v) => *v,
1166 Metric::Token(t) => map.get(t).copied().unwrap_or(0.0),
1167 }
1168 }
1169
1170 pub fn reach(&self, classes: &[&str], variant: &str) -> f32 {
1177 let mut reach: f32 = 0.0;
1178 let mut add = |s: &Style| {
1179 for props in [&s.base, &s.hover, &s.pressed, &s.focused, &s.checked, &s.disabled] {
1180 if let Some(shadow) = props.shadow.as_deref().and_then(|t| self.shadows.get(t)) {
1181 reach = reach.max(shadow.reach());
1182 }
1183 }
1184 };
1185 for class in classes {
1186 if let Some(s) = self.styles.get(*class) {
1187 add(s);
1188 }
1189 if !variant.is_empty() {
1190 if let Some(s) = self.styles.get(&format!("{class}.{variant}")) {
1191 add(s);
1192 }
1193 }
1194 }
1195 reach
1196 }
1197
1198 pub fn resolve(&self, classes: &[&str], variant: &str, state: ControlState) -> ResolvedStyle {
1199 let mut layers: Vec<&Style> = Vec::new();
1200 for class in classes.iter().rev() {
1201 if let Some(s) = self.styles.get(*class) {
1202 layers.push(s);
1203 }
1204 if !variant.is_empty() {
1205 if let Some(s) = self.styles.get(&format!("{class}.{variant}")) {
1206 layers.push(s);
1207 }
1208 }
1209 }
1210
1211 let mut acc = StyleProps::default();
1212 for s in &layers {
1213 acc.merge(&s.base);
1214 }
1215 let active = [state.hover, state.pressed, state.focused, state.checked, state.disabled];
1216 let checked_as_pressed = state.checked && layers.iter().all(|s| s.checked == StyleProps::default());
1219 for (i, on) in active.into_iter().enumerate() {
1220 if on {
1221 for s in &layers {
1222 acc.merge(s.state(if i == 3 && checked_as_pressed { 1 } else { i }));
1223 }
1224 }
1225 }
1226
1227 let defaults = ResolvedStyle::default();
1228 ResolvedStyle {
1229 fill: acc.fill.as_ref().map_or(defaults.fill, |c| self.color_of(c)),
1230 text: acc
1231 .text
1232 .as_ref()
1233 .map(|c| self.color_of(c))
1234 .or_else(|| self.colors.get("text").copied())
1235 .unwrap_or(defaults.text),
1236 border_color: acc.border_color.as_ref().map_or(defaults.border_color, |c| self.color_of(c)),
1237 border_width: acc.border_width.unwrap_or(defaults.border_width),
1238 radius: acc.radius.as_ref().map_or(0.0, |m| Self::metric(&self.radius, m)),
1239 padding: acc.padding.unwrap_or(defaults.padding),
1240 font: {
1241 let mut font = self.fonts.get(acc.font.as_deref().unwrap_or("default")).cloned().unwrap_or(defaults.font);
1242 if let Some(w) = acc.font_weight {
1243 font.weight = w;
1244 }
1245 if let Some(size) = acc.font_size {
1246 font.size = size;
1247 }
1248 if let Some(italic) = acc.italic {
1249 font.italic = italic;
1250 }
1251 font
1252 },
1253 line_height: acc.line_height.unwrap_or(0.0),
1254 opacity: acc.opacity.unwrap_or(1.0),
1255 transition_ms: acc.transition.as_ref().map_or(0.0, |m| Self::metric(&self.durations, m)),
1256 shadow: acc.shadow.as_deref().and_then(|t| self.shadows.get(t)).copied(),
1257 material: acc.material.as_deref().map_or(Material::None, Material::parse),
1258 reveal: {
1259 let (border, background) = match acc.reveal.as_deref() {
1260 Some("border") => (true, false),
1261 Some("background") => (false, true),
1262 Some("both") => (true, true),
1263 _ => (false, false),
1264 };
1265 let color = acc
1266 .reveal_color
1267 .as_ref()
1268 .map(|c| self.color_of(c))
1269 .or_else(|| self.colors.get("reveal").copied())
1270 .unwrap_or(if self.dark { Color::rgba(255, 255, 255, 90) } else { Color::rgba(0, 0, 0, 56) });
1271 Reveal { border, background, color, radius: acc.reveal_radius.unwrap_or(96.0).max(0.0) }
1272 },
1273 text_case: match acc.text_case.as_deref() {
1274 Some("upper") => crate::text::TextCase::Upper,
1275 Some("lower") => crate::text::TextCase::Lower,
1276 _ => crate::text::TextCase::None,
1277 },
1278 letter_spacing: acc.letter_spacing.unwrap_or(0.0),
1279 gap: acc.gap,
1280 border_sides: acc.border_sides,
1281 border_dash: match acc.border_style.as_deref() {
1282 Some("dashed") => vec![6.0, 4.0],
1283 Some("dotted") => vec![1.5, 3.0],
1284 _ => Vec::new(),
1285 },
1286 icon_size: acc.icon_size,
1287 icon_color: acc.icon_color.as_ref().map(|c| self.color_of(c)),
1288 icon_gap: acc.icon_gap,
1289 scroll_bar: acc.scroll_bar,
1290 }
1291 }
1292
1293 pub fn scroll_bar(&self, own: Option<&Style>, hover: bool) -> ScrollBarLook {
1298 let mut acc = StyleProps::default();
1299 let layers = [self.styles.get("ScrollBar"), own];
1300 for s in layers.iter().flatten() {
1301 acc.merge(&s.base);
1302 }
1303 if hover {
1304 for s in layers.iter().flatten() {
1305 acc.merge(&s.hover);
1306 }
1307 }
1308 ScrollBarLook {
1309 width: acc.width.map(|w| w.max(0.0)),
1310 track: acc.fill.as_ref().map(|c| self.color_of(c)),
1311 thumb: acc.thumb.as_ref().map(|c| self.color_of(c)),
1312 radius: acc.radius.as_ref().map(|m| Self::metric(&self.radius, m)),
1313 }
1314 }
1315}
1316
1317#[derive(Clone, Copy, Debug, Default, PartialEq)]
1319pub struct ScrollBarLook {
1320 pub width: Option<f32>,
1322 pub track: Option<Color>,
1324 pub thumb: Option<Color>,
1326 pub radius: Option<f32>,
1328}