Skip to main content

gpui_material_components/
tone.rs

1use gpui_material_theme::ColorRole;
2
3/// Semantic palette shortcut shared across Material components.
4///
5/// Explicit per-instance color or border overrides always take precedence over
6/// semantic tone defaults, and disabled states keep their disabled appearance.
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
8pub enum SemanticTone {
9    #[default]
10    Default,
11    Danger,
12    Success,
13    Warning,
14    Info,
15}
16
17impl SemanticTone {
18    /// Resolves `(role, on_role, container, on_container)` from
19    /// [`gpui_material_theme::ColorRole`], returning `None` for
20    /// [`SemanticTone::Default`].
21    pub const fn roles(self) -> Option<(ColorRole, ColorRole, ColorRole, ColorRole)> {
22        use ColorRole::*;
23        match self {
24            Self::Default => None,
25            Self::Danger => Some((Error, OnError, ErrorContainer, OnErrorContainer)),
26            Self::Success => Some((Success, OnSuccess, SuccessContainer, OnSuccessContainer)),
27            Self::Warning => Some((Warning, OnWarning, WarningContainer, OnWarningContainer)),
28            Self::Info => Some((Info, OnInfo, InfoContainer, OnInfoContainer)),
29        }
30    }
31
32    /// Resolves the primary foreground/accent role for this tone.
33    pub const fn role(self) -> Option<ColorRole> {
34        match self.roles() {
35            Some((role, _, _, _)) => Some(role),
36            None => None,
37        }
38    }
39
40    /// Resolves the foreground role paired with [`Self::role`].
41    pub const fn on_role(self) -> Option<ColorRole> {
42        match self.roles() {
43            Some((_, on_role, _, _)) => Some(on_role),
44            None => None,
45        }
46    }
47
48    /// Resolves the tonal container role for this tone.
49    pub const fn container_role(self) -> Option<ColorRole> {
50        match self.roles() {
51            Some((_, _, container, _)) => Some(container),
52            None => None,
53        }
54    }
55
56    /// Resolves the foreground role paired with [`Self::container_role`].
57    pub const fn on_container_role(self) -> Option<ColorRole> {
58        match self.roles() {
59            Some((_, _, _, on_container)) => Some(on_container),
60            None => None,
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn semantic_tone_resolves_expected_color_roles() {
71        use ColorRole::*;
72        assert_eq!(SemanticTone::Default.roles(), None);
73        assert_eq!(SemanticTone::Default.role(), None);
74        assert_eq!(SemanticTone::Default.on_role(), None);
75        assert_eq!(SemanticTone::Default.container_role(), None);
76        assert_eq!(SemanticTone::Default.on_container_role(), None);
77
78        let cases = [
79            (
80                SemanticTone::Danger,
81                Error,
82                OnError,
83                ErrorContainer,
84                OnErrorContainer,
85            ),
86            (
87                SemanticTone::Success,
88                Success,
89                OnSuccess,
90                SuccessContainer,
91                OnSuccessContainer,
92            ),
93            (
94                SemanticTone::Warning,
95                Warning,
96                OnWarning,
97                WarningContainer,
98                OnWarningContainer,
99            ),
100            (
101                SemanticTone::Info,
102                Info,
103                OnInfo,
104                InfoContainer,
105                OnInfoContainer,
106            ),
107        ];
108
109        for (tone, role, on_role, container, on_container) in cases {
110            assert_eq!(tone.roles(), Some((role, on_role, container, on_container)));
111            assert_eq!(tone.role(), Some(role));
112            assert_eq!(tone.on_role(), Some(on_role));
113            assert_eq!(tone.container_role(), Some(container));
114            assert_eq!(tone.on_container_role(), Some(on_container));
115        }
116    }
117}