返回 CodeWhale
menu_style.rs
根目录 / crates / tui-kit / src / menu_style.rs
1 //! Shared selection-row styles and non-color status marks for menus/pickers.
2 //!
3 //! Contract:
4 //! - The selection vocabulary is single-sourced here. Every menu, picker, and
5 //! option list renders its selected row with [`selected_row_style`] (or one
6 //! of the documented variants below) instead of hand-copying the
7 //! `SELECTION_TEXT`-on-`SELECTION_BG` + bold trio.
8 //! - This module owns *styling* only. Verbs, action-hint labels, keybindings,
9 //! and localized strings stay with the views and `ActionHint`; nothing here
10 //! changes what any surface says.
11 //! - Status tones are palette tokens (`palette::STATUS_*`), never per-view
12 //! colors, and every [`StatusKind`] pairs a charter glyph (`glyphs.rs`) with
13 //! an English word so state never depends on color alone. Surfaces that
14 //! already source their tone from the live `UiTheme` (the footer) keep that
15 //! tone and consume only the glyph/word half of the mark.
16
17 use ratatui::style::{Color, Modifier, Style};
18
19 use crate::glyphs;
20 use codewhale_palette::{self as palette, UiTheme};
21
22 /// Canonical selected-row treatment: selection ink on the selection
23 /// background, bolded so the active row reads even without color.
24 #[must_use]
25 pub fn selected_row_style() -> Style {
26 Style::default()
27 .fg(palette::SELECTION_TEXT)
28 .bg(palette::SELECTION_BG)
29 .add_modifier(Modifier::BOLD)
30 }
31
32 /// Hover is an elevated band with an underline, distinct from the filled,
33 /// bold keyboard selection even on terminals without color.
34 #[must_use]
35 pub fn hovered_row_style() -> Style {
36 Style::default()
37 .bg(palette::SURFACE_ELEVATED)
38 .add_modifier(Modifier::UNDERLINED)
39 }
40
41 /// Selected row with a caller-chosen foreground (the provider picker tints
42 /// per-field ink while keeping the shared selection background).
43 #[must_use]
44 pub fn selected_row_style_with_fg(fg: Color) -> Style {
45 Style::default()
46 .fg(fg)
47 .bg(palette::SELECTION_BG)
48 .add_modifier(Modifier::BOLD)
49 }
50
51 /// Selection background alone, for filler/spacer cells so the highlight band
52 /// runs the full width of a selected row.
53 #[must_use]
54 pub fn selected_row_bg_style() -> Style {
55 Style::default().bg(palette::SELECTION_BG)
56 }
57
58 /// Selected-but-disabled row (e.g. a locked model): the cursor position is
59 /// still visible, but muted ink on the elevated surface plus a dim modifier
60 /// says the row cannot be chosen.
61 #[must_use]
62 pub fn disabled_selected_row_style() -> Style {
63 Style::default()
64 .fg(palette::TEXT_MUTED)
65 .bg(palette::SURFACE_ELEVATED)
66 .add_modifier(Modifier::DIM)
67 }
68
69 /// Theme-preview variant: the theme picker shows each candidate theme's *own*
70 /// selection treatment, so ink and background come from the previewed theme
71 /// rather than the global tokens. `UiTheme` has no dedicated selection-ink
72 /// field, so the theme's body text reads on its selection background — the
73 /// exact pairing the picker has always rendered.
74 #[must_use]
75 pub fn theme_selected_row_style(theme: &UiTheme) -> Style {
76 Style::default()
77 .fg(theme.text_body)
78 .bg(theme.selection_bg)
79 .add_modifier(Modifier::BOLD)
80 }
81
82 /// Non-color status cue: a charter glyph, an English word, and a palette
83 /// status tone. Surfaces may render any subset, but glyph and word are always
84 /// both available so the state never depends on color alone.
85 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
86 pub struct StatusMark {
87 pub glyph: &'static str,
88 pub word: &'static str,
89 pub tone: Color,
90 }
91
92 /// The status states shared by the footer, work surface, and pickers.
93 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
94 pub enum StatusKind {
95 Ready,
96 Working,
97 Paused,
98 Done,
99 /// Chartered vocabulary entry. No surface renders a failure mark yet, so
100 /// nothing constructs it outside the exhaustiveness sweep below.
101 Failed,
102 Attention,
103 }
104
105 impl StatusKind {
106 /// Every kind, for exhaustive checks. Consumed by the non-color-meaning
107 /// test gate rather than by a runtime renderer.
108 #[cfg(test)]
109 pub const ALL: [StatusKind; 6] = [
110 StatusKind::Ready,
111 StatusKind::Working,
112 StatusKind::Paused,
113 StatusKind::Done,
114 StatusKind::Failed,
115 StatusKind::Attention,
116 ];
117 }
118
119 /// Single source pairing each status with its charter glyph, English word,
120 /// and palette tone. Words match the established footer vocabulary
121 /// (`working`, `paused`, `idle`); tones are `palette::STATUS_*` tokens, not
122 /// per-view colors. `Working` has no dedicated charter glyph yet, so it
123 /// borrows the charter's neutral dot until one is chartered.
124 #[must_use]
125 pub const fn status_mark(kind: StatusKind) -> StatusMark {
126 match kind {
127 StatusKind::Ready => StatusMark {
128 glyph: glyphs::READY,
129 word: "idle",
130 tone: palette::STATUS_SUCCESS,
131 },
132 StatusKind::Working => StatusMark {
133 glyph: glyphs::NEUTRAL,
134 word: "working",
135 tone: palette::WHALE_ACTION,
136 },
137 StatusKind::Paused => StatusMark {
138 glyph: glyphs::PAUSED,
139 word: "paused",
140 tone: palette::STATUS_WARNING,
141 },
142 StatusKind::Done => StatusMark {
143 glyph: glyphs::DONE,
144 word: "done",
145 tone: palette::STATUS_SUCCESS,
146 },
147 StatusKind::Failed => StatusMark {
148 glyph: glyphs::FAILED,
149 word: "failed",
150 tone: palette::STATUS_ERROR,
151 },
152 StatusKind::Attention => StatusMark {
153 glyph: glyphs::ATTENTION,
154 word: "attention",
155 tone: palette::STATUS_WARNING,
156 },
157 }
158 }
159
160 #[cfg(test)]
161 mod tests {
162 use super::*;
163
164 #[test]
165 fn selected_row_style_is_the_canonical_trio() {
166 assert_eq!(
167 selected_row_style(),
168 Style::default()
169 .fg(palette::SELECTION_TEXT)
170 .bg(palette::SELECTION_BG)
171 .add_modifier(Modifier::BOLD)
172 );
173 }
174
175 #[test]
176 fn selected_row_variants_keep_the_shared_background() {
177 assert_eq!(
178 selected_row_bg_style(),
179 Style::default().bg(palette::SELECTION_BG)
180 );
181 assert_eq!(
182 selected_row_style_with_fg(palette::WHALE_ACTION),
183 Style::default()
184 .fg(palette::WHALE_ACTION)
185 .bg(palette::SELECTION_BG)
186 .add_modifier(Modifier::BOLD)
187 );
188 }
189
190 #[test]
191 fn disabled_selected_row_is_muted_ink_on_elevated_surface() {
192 assert_eq!(
193 disabled_selected_row_style(),
194 Style::default()
195 .fg(palette::TEXT_MUTED)
196 .bg(palette::SURFACE_ELEVATED)
197 .add_modifier(Modifier::DIM)
198 );
199 }
200
201 #[test]
202 fn hovered_row_is_elevated_band_without_selection_ink() {
203 let hovered = hovered_row_style();
204 assert_eq!(
205 hovered,
206 Style::default()
207 .bg(palette::SURFACE_ELEVATED)
208 .add_modifier(Modifier::UNDERLINED)
209 );
210 assert_ne!(hovered, selected_row_style());
211 assert_ne!(hovered, selected_row_bg_style());
212 }
213
214 #[test]
215 fn theme_variant_uses_the_previewed_themes_own_tokens() {
216 let theme = palette::UI_THEME;
217 assert_eq!(
218 theme_selected_row_style(&theme),
219 Style::default()
220 .fg(theme.text_body)
221 .bg(theme.selection_bg)
222 .add_modifier(Modifier::BOLD)
223 );
224 }
225
226 #[test]
227 fn every_status_kind_pairs_a_glyph_with_a_word() {
228 // Non-color redundancy: neither cue may be empty, so a status never
229 // depends on color alone.
230 for kind in StatusKind::ALL {
231 let mark = status_mark(kind);
232 assert!(!mark.glyph.trim().is_empty(), "{kind:?} needs a glyph");
233 assert!(!mark.word.trim().is_empty(), "{kind:?} needs a word");
234 }
235 }
236
237 #[test]
238 fn status_tones_are_palette_status_tokens() {
239 let tokens = [
240 palette::STATUS_SUCCESS,
241 palette::STATUS_WARNING,
242 palette::STATUS_ERROR,
243 palette::WHALE_ACTION,
244 ];
245 for kind in StatusKind::ALL {
246 let tone = status_mark(kind).tone;
247 assert!(
248 tokens.contains(&tone),
249 "{kind:?} tone is not a STATUS_* token"
250 );
251 }
252 assert_eq!(status_mark(StatusKind::Ready).tone, palette::STATUS_SUCCESS);
253 assert_eq!(status_mark(StatusKind::Working).tone, palette::WHALE_ACTION);
254 assert_eq!(
255 status_mark(StatusKind::Paused).tone,
256 palette::STATUS_WARNING
257 );
258 assert_eq!(status_mark(StatusKind::Done).tone, palette::STATUS_SUCCESS);
259 assert_eq!(status_mark(StatusKind::Failed).tone, palette::STATUS_ERROR);
260 assert_eq!(
261 status_mark(StatusKind::Attention).tone,
262 palette::STATUS_WARNING
263 );
264 }
265 }
266
266 lines RUST