egui_pop_core/kit/tone.rs
1//! Tones: what a part says (accent, info, success, warning, danger,
2//! muted, record), a second axis beside the interaction state. It is
3//! egui-pop's one tone: progress, status bar items, library badges and
4//! micro-indicators all take this enum.
5//!
6//! A tone is not a state: a danger badge is danger whether or not it is
7//! under the pointer. It is compiled ahead like the states: a component's
8//! tokens give, per tone, only what differs from its own box, and
9//! [`install`](super::install) merges every tone with every combination
10//! of states into a table read by index at draw.
11//!
12//! Tokens: `component.tone.<tone>.<term>` (what the tone changes in every
13//! state) and `component.tone.<tone>.<state>.<term>` (what it changes in
14//! one state). A box of a tone and states is merged in this order: `rest`,
15//! the tone, then for each state in [`State::ORDER`] the state's patch
16//! followed by the tone's own patch for that state. The tone stands in
17//! for the resting look; a state still wins over it for what the state
18//! gives (a `disabled` badge looks disabled), unless the tone gives that
19//! state too.
20//!
21//! A component whose tokens give no tone has no table and pays nothing; a
22//! tone a component does not give draws its own box (`Neutral`).
23
24use serde::Serialize;
25
26use super::{BoxPatch, BoxStyle, State, Tokens};
27
28/// What a part says, the same names for every component. `Neutral` is the
29/// component's own box (no tone); the others are read from
30/// `component.tone.<name>`, their defaults aliasing the system's status
31/// tokens (`accent.default`, `info`, `success`, `warning`, `danger`,
32/// `text.muted`; `record` aliases `danger` too). Serialized by its token
33/// name (`"danger"`).
34#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize)]
35#[serde(rename_all = "snake_case")]
36pub enum Tone {
37 /// The component's own box.
38 #[default]
39 Neutral,
40 /// Running, active, chosen: the accent.
41 Accent,
42 Info,
43 Success,
44 Warning,
45 Danger,
46 /// Paused, skipped, secondary.
47 Muted,
48 /// Recording: not an error, but drawn in the danger colour unless a
49 /// kit tells them apart (`component.tone.record.*`).
50 Record,
51}
52
53impl Tone {
54 /// The tones a component's tokens can give, with their token names,
55 /// in their table's order.
56 pub const NAMED: [(Tone, &'static str); 7] = [
57 (Tone::Accent, "accent"),
58 (Tone::Info, "info"),
59 (Tone::Success, "success"),
60 (Tone::Warning, "warning"),
61 (Tone::Danger, "danger"),
62 (Tone::Muted, "muted"),
63 (Tone::Record, "record"),
64 ];
65
66 /// Its token name (`neutral` has none: it is the component's box).
67 pub fn name(self) -> &'static str {
68 match self {
69 Tone::Neutral => "neutral",
70 Tone::Accent => "accent",
71 Tone::Info => "info",
72 Tone::Success => "success",
73 Tone::Warning => "warning",
74 Tone::Danger => "danger",
75 Tone::Muted => "muted",
76 Tone::Record => "record",
77 }
78 }
79
80 /// The tone of a token name ([`Tone::name`]), `neutral` included.
81 pub fn from_name(name: &str) -> Option<Tone> {
82 if name == "neutral" {
83 return Some(Tone::Neutral);
84 }
85 Tone::NAMED
86 .iter()
87 .find(|(_, n)| *n == name)
88 .map(|(tone, _)| *tone)
89 }
90
91 fn from_bits(bits: u8) -> Tone {
92 match bits {
93 1 => Tone::Accent,
94 2 => Tone::Info,
95 3 => Tone::Success,
96 4 => Tone::Warning,
97 5 => Tone::Danger,
98 6 => Tone::Muted,
99 7 => Tone::Record,
100 _ => Tone::Neutral,
101 }
102 }
103}
104
105/// The state flags take the low five bits of a [`State`]; its tone the
106/// three above them, so a part's tone travels with its state (in
107/// [`Part::state`](super::Part)) and every handler's `states.get` honours
108/// it without a new argument.
109const TONE_SHIFT: u8 = 5;
110const FLAGS: u8 = (1 << TONE_SHIFT) - 1;
111
112impl State {
113 /// `self` in `tone` (replacing the tone it had).
114 #[must_use]
115 pub fn toned(self, tone: Tone) -> State {
116 State((self.0 & FLAGS) | ((tone as u8) << TONE_SHIFT))
117 }
118
119 /// Its tone (`Neutral` unless set with [`State::toned`]).
120 pub fn tone(self) -> Tone {
121 Tone::from_bits(self.0 >> TONE_SHIFT)
122 }
123
124 /// Its flags only, the index of its box among 32.
125 pub(super) fn flags(self) -> usize {
126 usize::from(self.0 & FLAGS)
127 }
128}
129
130/// A toned component's boxes: per tone of [`Tone::NAMED`], every
131/// combination of states.
132pub(super) type Tones = [[BoxStyle; 32]; 7];
133
134/// The tone table of a component, or none when its tokens give no tone.
135pub(super) fn compile(
136 t: &Tokens,
137 component: &str,
138 rest: &BoxStyle,
139 states: &[(State, BoxPatch)],
140) -> Option<Box<Tones>> {
141 let tones: Vec<(BoxPatch, Vec<BoxPatch>)> = Tone::NAMED
142 .iter()
143 .map(|(_, name)| {
144 let prefix = format!("{component}.tone.{name}");
145 let own = BoxPatch::read(t, &prefix);
146 let per_state = State::ORDER
147 .iter()
148 .map(|(_, s)| BoxPatch::read(t, &format!("{prefix}.{s}")))
149 .collect();
150 (own, per_state)
151 })
152 .collect();
153 let none = BoxPatch::default();
154 if tones
155 .iter()
156 .all(|(own, per)| *own == none && per.iter().all(|p| *p == none))
157 {
158 return None;
159 }
160 let table = std::array::from_fn(|k| {
161 let (own, per_state) = &tones[k];
162 std::array::from_fn(|bits| {
163 let state = State(bits as u8);
164 let mut b = *rest;
165 own.apply(&mut b);
166 for ((s, patch), toned) in states.iter().zip(per_state) {
167 if state.contains(*s) {
168 patch.apply(&mut b);
169 toned.apply(&mut b);
170 }
171 }
172 b
173 })
174 });
175 Some(Box::new(table))
176}
177
178/// The box of a state in its tone, from a component's boxes and its tone
179/// table.
180pub(super) fn get<'a>(
181 boxes: &'a [BoxStyle; 32],
182 tones: Option<&'a Tones>,
183 state: State,
184) -> &'a BoxStyle {
185 let i = state.flags();
186 match (tones, state.0 >> TONE_SHIFT) {
187 (Some(table), n @ 1..=7) => &table[usize::from(n - 1)][i],
188 _ => &boxes[i],
189 }
190}