gpui_material_components/
tone.rs1use gpui_material_theme::ColorRole;
2
3#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
8pub enum SemanticTone {
9 #[default]
11 Default,
12 Danger,
14 Success,
16 Warning,
18 Info,
20}
21
22impl SemanticTone {
23 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 pub const fn role(self) -> Option<ColorRole> {
39 match self.roles() {
40 Some((role, _, _, _)) => Some(role),
41 None => None,
42 }
43 }
44
45 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 pub const fn container_role(self) -> Option<ColorRole> {
55 match self.roles() {
56 Some((_, _, container, _)) => Some(container),
57 None => None,
58 }
59 }
60
61 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}