xengui 0.2.7

a retained-mode gui library in rust
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::{ Border, Outline, properties::StyleValue };

// SPDX-License-Identifier: Apache-2.0
use super::{ Background, Color, Edges, Length };
use std::cell::RefCell;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemeMode {
    Light,
    Dark,
    Auto,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
    name: String,
    mode: ThemeMode,

    pub primary: Color,
    pub accent: Color,
    pub background: Color,
    pub surface: Color,
    pub surface_hover: Color,
    pub foreground: Color,
    pub foreground_muted: Color,
    pub border: Color,
    pub border_hover: Color,

    pub hover: Color,
    pub pressed: Color,
    pub disabled: Color,

    pub selection: Color,
    pub selection_color: Color,
    pub selection_border_color: Color,
    pub selection_border_width: Length,
    pub selection_border_radius: Length,
    pub caret_color: Color,

    pub radius_xs: Length,
    pub radius_sm: Length,
    pub radius_md: Length,
    pub radius_lg: Length,
    pub radius_xl: Length,
    pub radius_2xl: Length,
    pub radius_3xl: Length,
    pub radius_4xl: Length,

    pub space_xs: Length,
    pub space_sm: Length,
    pub space_md: Length,
    pub space_lg: Length,
    pub space_xl: Length,
    pub space_2xl: Length,
    pub space_3xl: Length,
    pub space_4xl: Length,

    pub border_width: Length,
}

impl Theme {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            mode: ThemeMode::Light,

            // Colors
            primary: Color::BLUE_500,
            accent: Color::BLUE_500,

            background: Color::WHITE,
            surface: Color::NEUTRAL_50,
            surface_hover: Color::NEUTRAL_100,

            foreground: Color::NEUTRAL_900,
            foreground_muted: Color::NEUTRAL_500,

            border: Color::NEUTRAL_200,
            border_hover: Color::NEUTRAL_300,

            hover: Color::NEUTRAL_100,
            pressed: Color::NEUTRAL_200,
            disabled: Color::NEUTRAL_300.with_alpha(50),

            selection: Color::BLUE_500.with_alpha(80),
            selection_color: Color::BLUE_200,
            selection_border_color: Color::TRANSPARENT,
            selection_border_width: Length::px(0.0),
            selection_border_radius: Length::px(4.0),
            caret_color: Color::WHITE,

            // Border radius
            radius_xs: Length::px(2.0), // rounded-sm
            radius_sm: Length::px(4.0), // rounded
            radius_md: Length::px(6.0), // rounded-md
            radius_lg: Length::px(8.0), // rounded-lg
            radius_xl: Length::px(12.0), // rounded-xl
            radius_2xl: Length::px(16.0), // rounded-2xl
            radius_3xl: Length::px(24.0), // rounded-3xl
            radius_4xl: Length::px(9999.0), // rounded-full

            // Spacing
            space_xs: Length::px(2.0),
            space_sm: Length::px(4.0),
            space_md: Length::px(8.0),
            space_lg: Length::px(12.0),
            space_xl: Length::px(16.0),
            space_2xl: Length::px(24.0),
            space_3xl: Length::px(32.0),
            space_4xl: Length::px(48.0),

            border_width: Length::px(1.0),
        }
    }

    pub fn light() -> Self {
        Self::new("light")
            .mode(ThemeMode::Light)
            .primary(Color::BLUE_500)
            .accent(Color::BLUE_500)

            .background(Color::WHITE)
            .surface(Color::NEUTRAL_50)
            .surface_hover(Color::NEUTRAL_100)

            .foreground(Color::NEUTRAL_900)
            .foreground_muted(Color::NEUTRAL_500)

            .border(Color::NEUTRAL_200)
            .border_hover(Color::NEUTRAL_300)

            .hover(Color::NEUTRAL_100)
            .pressed(Color::NEUTRAL_200)
            .disabled(Color::NEUTRAL_300.with_alpha(50))

            .selection(Color::BLUE_500.with_alpha(80))
            .selection_color(Color::BLUE_200)
            .caret_color(Color::BLUE_500)
            .selection_border_color(Color::TRANSPARENT)
            .selection_border_width(Length::px(0.0))
            .selection_border_radius(Length::px(4.0))
    }

    pub fn dark() -> Self {
        Self::new("dark")
            .mode(ThemeMode::Dark)
            .primary(Color::BLUE_400)
            .accent(Color::BLUE_400)

            .background(Color::NEUTRAL_950)
            .surface(Color::NEUTRAL_900)
            .surface_hover(Color::NEUTRAL_800)

            .foreground(Color::NEUTRAL_50)
            .foreground_muted(Color::NEUTRAL_300)

            .border(Color::NEUTRAL_800)
            .border_hover(Color::NEUTRAL_700)

            .hover(Color::NEUTRAL_800)
            .pressed(Color::NEUTRAL_700)
            .disabled(Color::NEUTRAL_700.with_alpha(50))

            .selection(Color::BLUE_500.with_alpha(80))
            .selection_color(Color::BLUE_200)
            .caret_color(Color::BLUE_400)
            .selection_border_color(Color::TRANSPARENT)
            .selection_border_width(Length::px(0.0))
            .selection_border_radius(Length::px(4.0))
    }

    pub fn auto() -> Self {
        let mut theme = Self::light();
        theme.mode = ThemeMode::Auto;
        theme
    }

    pub fn mode(mut self, mode: ThemeMode) -> Self {
        self.mode = mode;
        self
    }

    pub fn primary(mut self, color: Color) -> Self {
        self.primary = color;
        self
    }

    pub fn accent(mut self, color: Color) -> Self {
        self.accent = color;
        self
    }

    pub fn background(mut self, color: Color) -> Self {
        self.background = color;
        self
    }

    pub fn surface(mut self, color: Color) -> Self {
        self.surface = color;
        self
    }

    pub fn surface_hover(mut self, color: Color) -> Self {
        self.surface_hover = color;
        self
    }

    pub fn foreground(mut self, color: Color) -> Self {
        self.foreground = color;
        self
    }

    pub fn foreground_muted(mut self, color: Color) -> Self {
        self.foreground_muted = color;
        self
    }

    pub fn border(mut self, color: Color) -> Self {
        self.border = color;
        self
    }

    pub fn border_hover(mut self, color: Color) -> Self {
        self.border_hover = color;
        self
    }

    pub fn hover(mut self, color: Color) -> Self {
        self.hover = color;
        self
    }

    pub fn pressed(mut self, color: Color) -> Self {
        self.pressed = color;
        self
    }

    pub fn disabled(mut self, color: Color) -> Self {
        self.disabled = color;
        self
    }

    pub fn selection(mut self, color: Color) -> Self {
        self.selection = color;
        self
    }

    pub fn selection_color(mut self, color: Color) -> Self {
        self.selection_color = color;
        self
    }

    pub fn caret_color(mut self, color: Color) -> Self {
        self.caret_color = color;
        self
    }

    pub fn selection_border_width(mut self, width: Length) -> Self {
        self.selection_border_width = width;
        self
    }

    pub fn selection_border_color(mut self, color: Color) -> Self {
        self.selection_border_color = color;
        self
    }

    pub fn selection_border_radius(mut self, radius: Length) -> Self {
        self.selection_border_radius = radius;
        self
    }

    /* Radius: start */
    pub fn radius_xs(mut self, radius: impl Into<Length>) -> Self {
        self.radius_xs = radius.into();
        self
    }

    pub fn radius_sm(mut self, radius: impl Into<Length>) -> Self {
        self.radius_sm = radius.into();
        self
    }

    pub fn radius_md(mut self, radius: impl Into<Length>) -> Self {
        self.radius_md = radius.into();
        self
    }

    pub fn radius_lg(mut self, radius: impl Into<Length>) -> Self {
        self.radius_lg = radius.into();
        self
    }

    pub fn radius_xl(mut self, radius: impl Into<Length>) -> Self {
        self.radius_xl = radius.into();
        self
    }

    pub fn radius_2xl(mut self, radius: impl Into<Length>) -> Self {
        self.radius_2xl = radius.into();
        self
    }

    pub fn radius_3xl(mut self, radius: impl Into<Length>) -> Self {
        self.radius_3xl = radius.into();
        self
    }

    pub fn radius_4xl(mut self, radius: impl Into<Length>) -> Self {
        self.radius_4xl = radius.into();
        self
    }
    /* Radius: end */

    /* Padding: start */
    pub fn space_xs(mut self, space: impl Into<Length>) -> Self {
        self.space_xs = space.into();
        self
    }

    pub fn space_sm(mut self, space: impl Into<Length>) -> Self {
        self.space_sm = space.into();
        self
    }

    pub fn space_md(mut self, space: impl Into<Length>) -> Self {
        self.space_md = space.into();
        self
    }

    pub fn space_lg(mut self, space: impl Into<Length>) -> Self {
        self.space_lg = space.into();
        self
    }

    pub fn space_xl(mut self, space: impl Into<Length>) -> Self {
        self.space_xl = space.into();
        self
    }

    pub fn space_2xl(mut self, space: impl Into<Length>) -> Self {
        self.space_2xl = space.into();
        self
    }

    pub fn space_3xl(mut self, space: impl Into<Length>) -> Self {
        self.space_3xl = space.into();
        self
    }

    pub fn space_4xl(mut self, space: impl Into<Length>) -> Self {
        self.space_4xl = space.into();
        self
    }
    /* Padding: end */

    pub fn border_width(mut self, width: impl Into<Length>) -> Self {
        self.border_width = width.into();
        self
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub const fn is_dark(&self) -> bool {
        matches!(self.mode, ThemeMode::Dark)
    }

    pub const fn is_auto(&self) -> bool {
        matches!(self.mode, ThemeMode::Auto)
    }

    // Only the color set flips with the system theme; spacing/radius
    // tokens the user configured on this theme are preserved as-is.
    pub fn resolved_for_system(&self, system_is_dark: bool) -> Self {
        if !self.is_auto() {
            return self.clone();
        }
        let palette = if system_is_dark { Self::dark() } else { Self::light() };
        Self {
            primary: palette.primary,
            accent: palette.accent,
            background: palette.background,
            surface: palette.surface,
            surface_hover: palette.surface_hover,
            foreground: palette.foreground,
            border: palette.border,
            ..self.clone()
        }
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::light()
    }
}

// Which theme should become active on the next render pass; requested via
// `set_active_theme`/`set_active_theme_by_name` from anywhere in user code.
pub enum ThemeSwitch {
    Index(usize),
    Name(String),
}

thread_local! {
    static CURRENT_THEME: RefCell<Theme> = RefCell::new(Theme::default());
    static THEME_SWITCH: RefCell<Option<ThemeSwitch>> = const { RefCell::new(None) };
}

pub fn set_current_theme(theme: Theme) {
    CURRENT_THEME.with(|cell| {
        *cell.borrow_mut() = theme;
    });
}

pub fn take_theme_switch() -> Option<ThemeSwitch> {
    THEME_SWITCH.with(|cell| cell.borrow_mut().take())
}

pub fn current_theme() -> Theme {
    CURRENT_THEME.with(|cell| cell.borrow().clone())
}

/// Switches the app's active theme by index into `AppConfig::themes`,
/// triggering a rebuild on the next frame.
pub fn set_active_theme(index: usize) {
    THEME_SWITCH.with(|cell| {
        *cell.borrow_mut() = Some(ThemeSwitch::Index(index));
    });
    crate::hooks::mark_dirty_and_redraw();
}

/// Switches the app's active theme by matching `Theme::name()` against
/// `AppConfig::themes`, triggering a rebuild on the next frame.
pub fn set_active_theme_by_name(name: impl Into<String>) {
    THEME_SWITCH.with(|cell| {
        *cell.borrow_mut() = Some(ThemeSwitch::Name(name.into()));
    });
    crate::hooks::mark_dirty_and_redraw();
}

pub struct ValueMarker;
pub struct FnMarker;

pub trait IntoThemed<T, Marker> {
    fn resolve_themed(self) -> T;
}

impl IntoThemed<Color, ValueMarker> for Color {
    fn resolve_themed(self) -> Color {
        self
    }
}

impl<F: FnOnce(&Theme) -> Color> IntoThemed<Color, FnMarker> for F {
    fn resolve_themed(self) -> Color {
        CURRENT_THEME.with(|cell| self(&cell.borrow()))
    }
}

impl IntoThemed<Background, ValueMarker> for Color {
    fn resolve_themed(self) -> Background {
        Background::Color(self)
    }
}

impl IntoThemed<Background, ValueMarker> for Background {
    fn resolve_themed(self) -> Background {
        self
    }
}

impl<F: FnOnce(&Theme) -> Color> IntoThemed<Background, FnMarker> for F {
    fn resolve_themed(self) -> Background {
        Background::Color(CURRENT_THEME.with(|cell| self(&cell.borrow())))
    }
}

impl<T: Into<Length>> IntoThemed<Length, ValueMarker> for T {
    fn resolve_themed(self) -> Length {
        self.into()
    }
}

impl<F: FnOnce(&Theme) -> Length> IntoThemed<Length, FnMarker> for F {
    fn resolve_themed(self) -> Length {
        CURRENT_THEME.with(|cell| self(&cell.borrow()))
    }
}

impl<T: Into<Edges>> IntoThemed<Edges, ValueMarker> for T {
    fn resolve_themed(self) -> Edges {
        self.into()
    }
}

impl<F: FnOnce(&Theme) -> Edges> IntoThemed<Edges, FnMarker> for F {
    fn resolve_themed(self) -> Edges {
        CURRENT_THEME.with(|cell| self(&cell.borrow()))
    }
}

impl IntoThemed<Border, ValueMarker> for Border {
    fn resolve_themed(self) -> Border {
        self
    }
}

impl<F: FnOnce(&Theme) -> Border> IntoThemed<Border, FnMarker> for F {
    fn resolve_themed(self) -> Border {
        CURRENT_THEME.with(|cell| self(&cell.borrow()))
    }
}

impl IntoThemed<StyleValue<Outline>, ValueMarker> for Outline {
    fn resolve_themed(self) -> StyleValue<Outline> {
        StyleValue::Value(self)
    }
}

impl IntoThemed<StyleValue<Outline>, ValueMarker> for StyleValue<Outline> {
    fn resolve_themed(self) -> StyleValue<Outline> {
        self
    }
}

impl<F: FnOnce(&Theme) -> Outline> IntoThemed<StyleValue<Outline>, FnMarker> for F {
    fn resolve_themed(self) -> StyleValue<Outline> {
        StyleValue::Value(CURRENT_THEME.with(|cell| self(&cell.borrow())))
    }
}

impl IntoThemed<f32, ValueMarker> for f32 {
    fn resolve_themed(self) -> f32 {
        self
    }
}

impl<F: FnOnce(&Theme) -> f32> IntoThemed<f32, FnMarker> for F {
    fn resolve_themed(self) -> f32 {
        CURRENT_THEME.with(|cell| self(&cell.borrow()))
    }
}