photon-ui 0.4.3

Blazing fast minimal TUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Semantic color tokens for the Beam Design Language.
//!
//! Provides a [`Palette`] trait that maps semantic names (e.g. `bg.page`,
//! `text.primary`) to concrete [`Color`] values. The [`Theme`] enum
//! implements this trait for the built-in Light and Dark variants.
//!
//! A custom palette can be installed globally with [`Theme::set_palette`];
//! when present it takes precedence over the built-in Light/Dark colors.

use std::{
    cell::RefCell,
    sync::Arc,
};

use super::Color;

// ── Thread-local active theme ─────────────────────────────────────

thread_local! {
    static ACTIVE_THEME: RefCell<Theme> = const { RefCell::new(Theme::Dark) };
    static ACTIVE_PALETTE: RefCell<Option<PaletteHandle>> = const { RefCell::new(None) };
}

/// A handle to a [`Palette`], used to install custom themes.
pub type PaletteHandle = Arc<dyn Palette>;

/// The active theme variant.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
pub enum Theme {
    /// Light variant of the Beam Design Language.
    Light,
    /// Dark variant of the Beam Design Language (default).
    #[default]
    Dark,
}

impl Theme {
    /// Set the globally active built-in theme.
    pub fn set(theme: Theme) {
        ACTIVE_THEME.with(|t| *t.borrow_mut() = theme);
    }

    /// Get the globally active built-in theme.
    pub fn current() -> Self {
        ACTIVE_THEME.with(|t| *t.borrow())
    }

    /// Install a custom palette globally.
    ///
    /// The custom palette takes precedence over the built-in Light/Dark theme
    /// until [`Theme::clear_palette`] is called.
    pub fn set_palette(palette: PaletteHandle) {
        ACTIVE_PALETTE.with(|p| *p.borrow_mut() = Some(palette));
    }

    /// Remove the globally active custom palette.
    pub fn clear_palette() {
        ACTIVE_PALETTE.with(|p| *p.borrow_mut() = None);
    }

    /// Returns true when a custom palette is currently installed.
    pub fn has_palette() -> bool {
        ACTIVE_PALETTE.with(|p| p.borrow().is_some())
    }

    /// Get the active palette.
    ///
    /// Returns the custom palette if one is installed, otherwise returns a
    /// handle to the current built-in theme.
    pub fn palette() -> PaletteHandle {
        ACTIVE_PALETTE.with(|p| match p.borrow().as_ref() {
            | Some(handle) => Arc::clone(handle),
            | None => Arc::new(Self::current()),
        })
    }

    /// Run a closure with a specific built-in theme, then restore the
    /// previous theme and any previously installed custom palette.
    ///
    /// The closure runs with the requested built-in theme and no custom
    /// palette; the previous state is restored before returning.
    pub fn with<T>(theme: Theme, f: impl FnOnce() -> T) -> T {
        let previous_theme = Self::current();
        let previous_palette = Self::palette_option();
        Self::set(theme);
        Self::clear_palette();
        let result = f();
        Self::set(previous_theme);
        match previous_palette {
            | Some(palette) => Self::set_palette(palette),
            | None => Self::clear_palette(),
        }
        result
    }

    fn palette_option() -> Option<PaletteHandle> {
        ACTIVE_PALETTE.with(|p| p.borrow().as_ref().map(Arc::clone))
    }
}

// ── Palette trait ─────────────────────────────────────────────────

/// A semantic color palette for terminal user interfaces.
///
/// Implement this trait to define a custom theme. Each method maps a
/// well-known UI role to a concrete [`Color`]. When a custom palette is
/// installed with [`Theme::set_palette`], components resolve colors through
/// these roles instead of the built-in Light/Dark colors.
pub trait Palette {
    /// Page / canvas background.
    fn background(&self) -> Color;
    /// Elevated surface background (cards, panels, navigation).
    fn surface(&self) -> Color;
    /// Input field background.
    fn field(&self) -> Color;

    /// Primary text color.
    fn text(&self) -> Color;
    /// Muted / secondary text color.
    fn text_muted(&self) -> Color;
    /// Text rendered on top of the accent color.
    fn text_on_accent(&self) -> Color;

    /// Accent / brand color.
    fn accent(&self) -> Color;
    /// Accent color when hovered.
    fn accent_hover(&self) -> Color;

    /// Default border color.
    fn border(&self) -> Color;
    /// Subtle border / divider color.
    fn border_muted(&self) -> Color;
    /// Focus ring / focused border color.
    fn focus(&self) -> Color;

    /// Success / positive state color.
    fn success(&self) -> Color;
    /// Warning / caution state color.
    fn warning(&self) -> Color;
    /// Error / negative state color.
    fn error(&self) -> Color;
    /// Informational state color.
    fn info(&self) -> Color;
}

impl Palette for Theme {
    fn background(&self) -> Color {
        match self {
            | Theme::Light => Color::WARM_IVORY,
            | Theme::Dark => Color::SUNBEAM_BLACK,
        }
    }

    fn surface(&self) -> Color {
        match self {
            | Theme::Light => Color::CREAM,
            | Theme::Dark => Color::CARD_DARK,
        }
    }

    fn field(&self) -> Color {
        match self {
            | Theme::Light => Color::WHITE,
            | Theme::Dark => Color::CARD_DARK,
        }
    }

    fn text(&self) -> Color {
        match self {
            | Theme::Light => Color::SUNBEAM_BLACK,
            | Theme::Dark => Color::WHITE,
        }
    }

    fn text_muted(&self) -> Color {
        match self {
            | Theme::Light => Color(0x66, 0x66, 0x66),
            | Theme::Dark => Color(0xbb, 0xbb, 0xbb),
        }
    }

    fn text_on_accent(&self) -> Color {
        Color::WHITE
    }

    fn accent(&self) -> Color {
        Color::SUNBEAM_ORANGE
    }

    fn accent_hover(&self) -> Color {
        Color::SUNBEAM_FLAME
    }

    fn border(&self) -> Color {
        match self {
            | Theme::Light => Color(0x7f, 0x63, 0x15),
            | Theme::Dark => Color(0x55, 0x55, 0x55),
        }
    }

    fn border_muted(&self) -> Color {
        match self {
            | Theme::Light => Color(0xdd, 0xcc, 0xaa),
            | Theme::Dark => Color(0x44, 0x44, 0x44),
        }
    }

    fn focus(&self) -> Color {
        Color::BEAM_ORANGE
    }

    fn success(&self) -> Color {
        Color(0x22, 0x99, 0x55)
    }

    fn warning(&self) -> Color {
        Color::SUNSHINE_900
    }

    fn error(&self) -> Color {
        Color(0xdd, 0x33, 0x33)
    }

    fn info(&self) -> Color {
        Color(0x33, 0x77, 0xcc)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug)]
    struct MagentaPalette;

    impl Palette for MagentaPalette {
        fn background(&self) -> Color {
            Color::from_hex("#111111").unwrap()
        }

        fn surface(&self) -> Color {
            Color::from_hex("#222222").unwrap()
        }

        fn field(&self) -> Color {
            Color::from_hex("#333333").unwrap()
        }

        fn text(&self) -> Color {
            Color::from_hex("#444444").unwrap()
        }

        fn text_muted(&self) -> Color {
            Color::from_hex("#555555").unwrap()
        }

        fn text_on_accent(&self) -> Color {
            Color::from_hex("#666666").unwrap()
        }

        fn accent(&self) -> Color {
            Color::from_hex("#ff00ff").unwrap()
        }

        fn accent_hover(&self) -> Color {
            Color::from_hex("#ff11ff").unwrap()
        }

        fn border(&self) -> Color {
            Color::from_hex("#777777").unwrap()
        }

        fn border_muted(&self) -> Color {
            Color::from_hex("#888888").unwrap()
        }

        fn focus(&self) -> Color {
            Color::from_hex("#999999").unwrap()
        }

        fn success(&self) -> Color {
            Color::from_hex("#00ff00").unwrap()
        }

        fn warning(&self) -> Color {
            Color::from_hex("#ffff00").unwrap()
        }

        fn error(&self) -> Color {
            Color::from_hex("#ff0000").unwrap()
        }

        fn info(&self) -> Color {
            Color::from_hex("#0000ff").unwrap()
        }
    }

    #[test]
    fn light_palette_values() {
        let t = Theme::Light;
        assert_eq!(t.background(), Color::WARM_IVORY);
        assert_eq!(t.text(), Color::SUNBEAM_BLACK);
        assert_eq!(t.accent(), Color::SUNBEAM_ORANGE);
    }

    #[test]
    fn dark_palette_values() {
        let t = Theme::Dark;
        assert_eq!(t.background(), Color::SUNBEAM_BLACK);
        assert_eq!(t.text(), Color::WHITE);
        assert_eq!(t.accent(), Color::SUNBEAM_ORANGE);
    }

    #[test]
    fn theme_switching() {
        Theme::set(Theme::Light);
        assert_eq!(Theme::current(), Theme::Light);

        Theme::set(Theme::Dark);
        assert_eq!(Theme::current(), Theme::Dark);

        Theme::set(Theme::Light); // restore
    }

    #[test]
    fn theme_with_restores_previous() {
        Theme::set(Theme::Light);
        let result = Theme::with(Theme::Dark, || {
            assert_eq!(Theme::current(), Theme::Dark);
            42
        });
        assert_eq!(result, 42);
        assert_eq!(Theme::current(), Theme::Light);
    }

    #[test]
    fn custom_palette_overrides_built_in() {
        Theme::set(Theme::Light);
        Theme::set_palette(Arc::new(MagentaPalette));
        let palette = Theme::palette();
        assert_eq!(palette.accent(), Color::from_hex("#ff00ff").unwrap());
        assert_eq!(palette.background(), Color::from_hex("#111111").unwrap());
        Theme::clear_palette();
    }

    #[test]
    fn clear_palette_reverts_to_theme() {
        Theme::set(Theme::Light);
        Theme::set_palette(Arc::new(MagentaPalette));
        Theme::clear_palette();
        let palette = Theme::palette();
        assert_eq!(palette.background(), Color::WARM_IVORY);
        assert_eq!(palette.accent(), Color::SUNBEAM_ORANGE);
    }

    #[test]
    fn with_clears_and_restores_custom_palette() {
        Theme::set(Theme::Light);
        Theme::set_palette(Arc::new(MagentaPalette));
        Theme::with(Theme::Dark, || {
            assert!(!Theme::has_palette());
            assert_eq!(Theme::palette().accent(), Color::SUNBEAM_ORANGE);
        });
        assert!(Theme::has_palette());
        assert_eq!(
            Theme::palette().accent(),
            Color::from_hex("#ff00ff").unwrap()
        );
        Theme::clear_palette();
    }

    #[test]
    fn light_palette_exposes_all_roles() {
        let t = Theme::Light;
        assert_eq!(t.background(), Color::WARM_IVORY);
        assert_eq!(t.surface(), Color::CREAM);
        assert_eq!(t.field(), Color::WHITE);
        assert_eq!(t.text(), Color::SUNBEAM_BLACK);
        assert_eq!(t.text_muted(), Color(0x66, 0x66, 0x66));
        assert_eq!(t.text_on_accent(), Color::WHITE);
        assert_eq!(t.accent(), Color::SUNBEAM_ORANGE);
        assert_eq!(t.accent_hover(), Color::SUNBEAM_FLAME);
        assert_eq!(t.border(), Color(0x7f, 0x63, 0x15));
        assert_eq!(t.border_muted(), Color(0xdd, 0xcc, 0xaa));
        assert_eq!(t.focus(), Color::BEAM_ORANGE);
        assert_eq!(t.success(), Color(0x22, 0x99, 0x55));
        assert_eq!(t.warning(), Color::SUNSHINE_900);
        assert_eq!(t.error(), Color(0xdd, 0x33, 0x33));
        assert_eq!(t.info(), Color(0x33, 0x77, 0xcc));
    }

    #[test]
    fn dark_palette_exposes_all_roles() {
        let t = Theme::Dark;
        assert_eq!(t.background(), Color::SUNBEAM_BLACK);
        assert_eq!(t.surface(), Color::CARD_DARK);
        assert_eq!(t.field(), Color::CARD_DARK);
        assert_eq!(t.text(), Color::WHITE);
        assert_eq!(t.text_muted(), Color(0xbb, 0xbb, 0xbb));
        assert_eq!(t.text_on_accent(), Color::WHITE);
        assert_eq!(t.accent(), Color::SUNBEAM_ORANGE);
        assert_eq!(t.accent_hover(), Color::SUNBEAM_FLAME);
        assert_eq!(t.border(), Color(0x55, 0x55, 0x55));
        assert_eq!(t.border_muted(), Color(0x44, 0x44, 0x44));
        assert_eq!(t.focus(), Color::BEAM_ORANGE);
        assert_eq!(t.success(), Color(0x22, 0x99, 0x55));
        assert_eq!(t.warning(), Color::SUNSHINE_900);
        assert_eq!(t.error(), Color(0xdd, 0x33, 0x33));
        assert_eq!(t.info(), Color(0x33, 0x77, 0xcc));
    }

    #[test]
    fn custom_palette_exposes_all_roles() {
        Theme::set(Theme::Light);
        Theme::set_palette(Arc::new(MagentaPalette));
        let p = Theme::palette();
        assert_eq!(p.background(), Color::from_hex("#111111").unwrap());
        assert_eq!(p.surface(), Color::from_hex("#222222").unwrap());
        assert_eq!(p.field(), Color::from_hex("#333333").unwrap());
        assert_eq!(p.text(), Color::from_hex("#444444").unwrap());
        assert_eq!(p.text_muted(), Color::from_hex("#555555").unwrap());
        assert_eq!(p.text_on_accent(), Color::from_hex("#666666").unwrap());
        assert_eq!(p.accent(), Color::from_hex("#ff00ff").unwrap());
        assert_eq!(p.accent_hover(), Color::from_hex("#ff11ff").unwrap());
        assert_eq!(p.border(), Color::from_hex("#777777").unwrap());
        assert_eq!(p.border_muted(), Color::from_hex("#888888").unwrap());
        assert_eq!(p.focus(), Color::from_hex("#999999").unwrap());
        assert_eq!(p.success(), Color::from_hex("#00ff00").unwrap());
        assert_eq!(p.warning(), Color::from_hex("#ffff00").unwrap());
        assert_eq!(p.error(), Color::from_hex("#ff0000").unwrap());
        assert_eq!(p.info(), Color::from_hex("#0000ff").unwrap());
        Theme::clear_palette();
    }
}