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    /// Component default theme palette (no semantic override).
10    #[default]
11    Default,
12    /// Destructive or error semantic palette (`Error` / `ErrorContainer`).
13    Danger,
14    /// Positive or confirmation semantic palette (`Success` / `SuccessContainer`).
15    Success,
16    /// Cautionary semantic palette (`Warning` / `WarningContainer`).
17    Warning,
18    /// Informational semantic palette (`Info` / `InfoContainer`).
19    Info,
20}
21
22impl SemanticTone {
23    /// Resolves `(role, on_role, container, on_container)` from
24    /// [`gpui_material_theme::ColorRole`], returning `None` for
25    /// [`SemanticTone::Default`].
26    pub const fn roles(self) -> Option<(ColorRole, ColorRole, ColorRole, ColorRole)> {
27        use ColorRole::*;
28        match self {
29            Self::Default => None,
30            Self::Danger => Some((Error, OnError, ErrorContainer, OnErrorContainer)),
31            Self::Success => Some((Success, OnSuccess, SuccessContainer, OnSuccessContainer)),
32            Self::Warning => Some((Warning, OnWarning, WarningContainer, OnWarningContainer)),
33            Self::Info => Some((Info, OnInfo, InfoContainer, OnInfoContainer)),
34        }
35    }
36
37    /// Resolves the primary foreground/accent role for this tone.
38    pub const fn role(self) -> Option<ColorRole> {
39        match self.roles() {
40            Some((role, _, _, _)) => Some(role),
41            None => None,
42        }
43    }
44
45    /// Resolves the foreground role paired with [`Self::role`].
46    pub const fn on_role(self) -> Option<ColorRole> {
47        match self.roles() {
48            Some((_, on_role, _, _)) => Some(on_role),
49            None => None,
50        }
51    }
52
53    /// Resolves the tonal container role for this tone.
54    pub const fn container_role(self) -> Option<ColorRole> {
55        match self.roles() {
56            Some((_, _, container, _)) => Some(container),
57            None => None,
58        }
59    }
60
61    /// Resolves the foreground role paired with [`Self::container_role`].
62    pub const fn on_container_role(self) -> Option<ColorRole> {
63        match self.roles() {
64            Some((_, _, _, on_container)) => Some(on_container),
65            None => None,
66        }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn semantic_tone_resolves_expected_color_roles() {
76        use ColorRole::*;
77        assert_eq!(SemanticTone::Default.roles(), None);
78        assert_eq!(SemanticTone::Default.role(), None);
79        assert_eq!(SemanticTone::Default.on_role(), None);
80        assert_eq!(SemanticTone::Default.container_role(), None);
81        assert_eq!(SemanticTone::Default.on_container_role(), None);
82
83        let cases = [
84            (
85                SemanticTone::Danger,
86                Error,
87                OnError,
88                ErrorContainer,
89                OnErrorContainer,
90            ),
91            (
92                SemanticTone::Success,
93                Success,
94                OnSuccess,
95                SuccessContainer,
96                OnSuccessContainer,
97            ),
98            (
99                SemanticTone::Warning,
100                Warning,
101                OnWarning,
102                WarningContainer,
103                OnWarningContainer,
104            ),
105            (
106                SemanticTone::Info,
107                Info,
108                OnInfo,
109                InfoContainer,
110                OnInfoContainer,
111            ),
112        ];
113
114        for (tone, role, on_role, container, on_container) in cases {
115            assert_eq!(tone.roles(), Some((role, on_role, container, on_container)));
116            assert_eq!(tone.role(), Some(role));
117            assert_eq!(tone.on_role(), Some(on_role));
118            assert_eq!(tone.container_role(), Some(container));
119            assert_eq!(tone.on_container_role(), Some(on_container));
120        }
121    }
122}