gpui-box-kit 0.1.0

GPUI Box Kit design-system components and interaction primitives
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Controls that report a choice: checkbox, radio, and switch.
//!
//! All three read their state from the caller and report an intent. They never
//! hold the answer themselves, so a host that refuses a change simply does not
//! apply it, and the control keeps showing what is actually true.

use std::rc::Rc;

use gpui::{
    AnyElement, App, Hsla, InteractiveElement, IntoElement, ParentElement, RenderOnce,
    SharedString, Styled, Window, div, point, prelude::FluentBuilder, px,
};
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{ActiveTheme, ControlSize, Radius, Theme, TypeScale};

use crate::foundation::{
    Disableable, FocusRing, Ident, Pressable, Selectable, Sizable, StyledExt,
    text as foundation_text,
};
use crate::motion::{self, Interpolate};

type ToggleHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
type ActionHandler = Rc<dyn Fn(&mut Window, &mut App)>;

/// A box that reports one of on, off, or partly on.
///
/// The mixed state is for a parent whose children disagree; it is reported as
/// mixed rather than guessed into on or off.
#[derive(IntoElement)]
pub struct Checkbox {
    ident: Ident,
    label: Option<SharedString>,
    description: Option<SharedString>,
    checked: Option<bool>,
    disabled: bool,
    size: ControlSize,
    on_change: Option<ToggleHandler>,
}

impl std::fmt::Debug for Checkbox {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Checkbox")
            .field("ident", &self.ident)
            .field("label", &self.label)
            .field("checked", &self.checked)
            .field("disabled", &self.disabled)
            .field("has_handler", &self.on_change.is_some())
            .finish()
    }
}

impl Checkbox {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            label: None,
            description: None,
            checked: Some(false),
            disabled: false,
            size: ControlSize::Md,
            on_change: None,
        }
    }

    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Secondary text under the label, for a consequence the typist should
    /// know before choosing.
    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn checked(mut self, checked: bool) -> Self {
        self.checked = Some(checked);
        self
    }

    /// Neither on nor off, because the things this box stands for disagree.
    pub fn mixed(mut self) -> Self {
        self.checked = None;
        self
    }

    pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
        self.on_change = Some(Rc::new(handler));
        self
    }

    fn actionable(&self) -> bool {
        !self.disabled && self.on_change.is_some()
    }
}

impl Disableable for Checkbox {
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

impl Sizable for Checkbox {
    fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }
}

impl RenderOnce for Checkbox {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let metrics = theme.control.get(self.size);
        let actionable = self.actionable();
        let side = px(metrics.icon_size);
        let on = self.checked.unwrap_or(false);
        let mixed = self.checked.is_none();

        // The two marks are tracked separately so mixed and checked can cross
        // over each other: the bar shrinks while the check draws, and the box
        // is never momentarily empty between the two states.
        let drawn = motion::tracked(
            &self.ident.semantic_id(),
            point(f32::from(u8::from(on)), f32::from(u8::from(mixed))),
            motion::state_change(&theme),
            window,
            cx,
        );
        let filled = drawn.x.max(drawn.y);

        let mark = div()
            .size(side)
            .flex()
            .items_center()
            .justify_center()
            .flex_none()
            .relative()
            .radius(&theme, Radius::Small)
            .border(px(theme.borders.hairline))
            .border_color(
                theme
                    .colors
                    .hairline_strong
                    .lerp(theme.colors.accent, filled),
            )
            .bg(theme.colors.accent.opacity(filled))
            .when(drawn.y > 0.0, |element| {
                element.child(
                    div()
                        .absolute()
                        .w(side * 0.5 * drawn.y)
                        .h(px(theme.borders.thick))
                        .bg(theme.colors.text_on_accent),
                )
            })
            .when(drawn.x > 0.0, |element| {
                element.child(
                    div().absolute().opacity(drawn.x).child(
                        gpui_kit_assets::icon(gpui_kit_assets::Icon::Check)
                            .size(side * (0.4 + 0.4 * drawn.x))
                            .text_color(theme.colors.text_on_accent),
                    ),
                )
            });

        let next = !on;
        choice_row(
            &theme,
            cx,
            self.ident.clone(),
            mark.into_any_element(),
            self.label.clone(),
            self.description.clone(),
            metrics.font_size,
            self.disabled,
            actionable,
            self.on_change.clone().map(move |handler| {
                Rc::new(move |window: &mut Window, cx: &mut App| handler(next, window, cx))
                    as ActionHandler
            }),
        )
        .semantic_in(
            cx,
            spec(
                &self.ident,
                Role::Checkbox,
                self.label.clone(),
                self.disabled,
            )
            .tristate(self.checked),
        )
    }
}

/// One option in a set where exactly one can hold.
#[derive(IntoElement)]
pub struct Radio {
    ident: Ident,
    label: Option<SharedString>,
    description: Option<SharedString>,
    selected: bool,
    disabled: bool,
    size: ControlSize,
    on_select: Option<ActionHandler>,
}

impl std::fmt::Debug for Radio {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Radio")
            .field("ident", &self.ident)
            .field("label", &self.label)
            .field("selected", &self.selected)
            .field("disabled", &self.disabled)
            .field("has_handler", &self.on_select.is_some())
            .finish()
    }
}

impl Radio {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            label: None,
            description: None,
            selected: false,
            disabled: false,
            size: ControlSize::Md,
            on_select: None,
        }
    }

    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn on_select(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_select = Some(Rc::new(handler));
        self
    }
}

impl Disableable for Radio {
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

impl Selectable for Radio {
    fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }
}

impl Sizable for Radio {
    fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }
}

impl RenderOnce for Radio {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let metrics = theme.control.get(self.size);
        let actionable = !self.disabled && self.on_select.is_some();
        let side = px(metrics.icon_size);

        let drawn = motion::tracked(
            &self.ident.semantic_id(),
            f32::from(u8::from(self.selected)),
            motion::state_change(&theme),
            window,
            cx,
        );

        let mark = div()
            .size(side)
            .flex()
            .items_center()
            .justify_center()
            .flex_none()
            .rounded_full()
            .border(px(theme.borders.hairline))
            .border_color(
                theme
                    .colors
                    .hairline_strong
                    .lerp(theme.colors.accent, drawn),
            )
            .when(drawn > 0.0, |element| {
                element.child(
                    div()
                        .size(side * 0.5 * drawn)
                        .rounded_full()
                        .bg(theme.colors.accent),
                )
            });

        choice_row(
            &theme,
            cx,
            self.ident.clone(),
            mark.into_any_element(),
            self.label.clone(),
            self.description.clone(),
            metrics.font_size,
            self.disabled,
            actionable,
            self.on_select.clone(),
        )
        .semantic_in(
            cx,
            spec(&self.ident, Role::Radio, self.label.clone(), self.disabled)
                .checked(self.selected),
        )
    }
}

/// A control that takes effect the moment it is flipped.
///
/// Use a switch for something that applies immediately, and a checkbox for
/// something that applies when a form is submitted.
#[derive(IntoElement)]
pub struct Switch {
    ident: Ident,
    label: Option<SharedString>,
    /// A name for a reader who has only the tree, when the words on screen
    /// belong to something else.
    name: Option<SharedString>,
    description: Option<SharedString>,
    on: bool,
    disabled: bool,
    size: ControlSize,
    on_change: Option<ToggleHandler>,
}

impl std::fmt::Debug for Switch {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Switch")
            .field("ident", &self.ident)
            .field("label", &self.label)
            .field("on", &self.on)
            .field("disabled", &self.disabled)
            .field("has_handler", &self.on_change.is_some())
            .finish()
    }
}

impl Switch {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            label: None,
            name: None,
            description: None,
            on: false,
            disabled: false,
            size: ControlSize::Md,
            on_change: None,
        }
    }

    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Names the switch without drawing the name.
    ///
    /// A switch at the right edge of a settings row is named by the row, and
    /// repeating those words beside the track would say everything twice. The
    /// name still has to reach the tree, because a control nobody can name is
    /// a control nobody can operate without looking at it.
    pub fn named(mut self, name: impl Into<SharedString>) -> Self {
        self.name = Some(name.into());
        self
    }

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

    pub fn on_change(mut self, handler: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
        self.on_change = Some(Rc::new(handler));
        self
    }
}

impl Disableable for Switch {
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

impl Sizable for Switch {
    fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }
}

impl RenderOnce for Switch {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let metrics = theme.control.get(self.size);
        let actionable = !self.disabled && self.on_change.is_some();
        let height = px(metrics.icon_size);
        let width = height * 1.8;
        let knob = height - px(4.0);

        // The knob is placed by margin rather than by a relative offset, so
        // the switch is the same size at every point of the slide.
        let drawn = motion::tracked(
            &self.ident.semantic_id(),
            f32::from(u8::from(self.on)),
            motion::state_change(&theme),
            window,
            cx,
        );

        let track = div()
            .w(width)
            .h(height)
            .flex_none()
            .flex()
            .items_center()
            .rounded_full()
            .p(px(2.0))
            .bg(theme
                .colors
                .hairline_strong
                .lerp(theme.colors.accent, drawn))
            .child(
                div()
                    .size(knob)
                    .rounded_full()
                    .bg(theme.colors.text_on_accent)
                    .ml((width - knob - px(4.0)) * drawn),
            );

        let next = !self.on;
        choice_row(
            &theme,
            cx,
            self.ident.clone(),
            track.into_any_element(),
            self.label.clone(),
            self.description.clone(),
            metrics.font_size,
            self.disabled,
            actionable,
            self.on_change.clone().map(move |handler| {
                Rc::new(move |window: &mut Window, cx: &mut App| handler(next, window, cx))
                    as ActionHandler
            }),
        )
        .semantic_in(
            cx,
            spec(
                &self.ident,
                Role::Switch,
                self.label.clone().or_else(|| self.name.clone()),
                self.disabled,
            )
            .checked(self.on),
        )
    }
}

fn spec(ident: &Ident, role: Role, label: Option<SharedString>, disabled: bool) -> NodeSpec {
    let mut spec = NodeSpec::new(ident.semantic_id(), role).disabled(disabled);
    if let Some(label) = label {
        spec = spec.text(label);
    }
    spec
}

/// The shared frame: a mark, a label, and the whole row as the target.
///
/// The row is the hit area rather than the mark alone, because a small square
/// is a hard thing to hit and the label means the same thing.
#[allow(clippy::too_many_arguments)]
fn choice_row(
    theme: &Theme,
    cx: &App,
    ident: Ident,
    mark: AnyElement,
    label: Option<SharedString>,
    description: Option<SharedString>,
    font_size: f32,
    disabled: bool,
    actionable: bool,
    handler: Option<ActionHandler>,
) -> gpui::Stateful<gpui::Div> {
    let text_color: Hsla = if disabled {
        theme.colors.text_faint
    } else {
        theme.colors.text
    };

    let mut row = div()
        .id(ident.element_id())
        .flex()
        .flex_row()
        .items_start()
        .gap(px(theme.space(gpui_kit_theme::Space::Sm)))
        .when(disabled, |element| element.opacity(theme.opacity.disabled))
        .when(actionable, |element| {
            element
                .cursor_pointer()
                .tab_index(0)
                .focus_ring(theme)
                .pressable(cx)
        })
        .child(div().mt(px(1.0)).child(mark))
        .when_some(label, |element, label| {
            element.child(
                div()
                    .flex()
                    .flex_col()
                    .gap(px(2.0))
                    .child(
                        foundation_text(theme, TypeScale::Label, label)
                            .text_size(px(font_size))
                            .text_color(text_color),
                    )
                    .when_some(description, |element, description| {
                        element.child(
                            foundation_text(theme, TypeScale::Caption, description)
                                .text_size(px(font_size * 0.9))
                                .text_tone(theme, gpui_kit_theme::TextTone::Muted),
                        )
                    }),
            )
        });

    if let (true, Some(handler)) = (actionable, handler) {
        let click = Rc::clone(&handler);
        row.interactivity()
            .on_click(move |_, window, cx| click(window, cx));
        row.interactivity().on_key_down(move |event, window, cx| {
            if matches!(event.keystroke.key.as_str(), "enter" | "space") {
                handler(window, cx);
                cx.stop_propagation();
            }
        });
    }

    row
}