u0ui 0.3.17

Carbon UI: IBM Carbon v11 visuals with a shadcn-style API for GPUI Kit
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
//! `ProgressIndicator` / `.cds--progress`: Carbon's step indicator, the
//! wizard header of progress dialogs.
//!
//! - **Anatomy**: per step a 2px line along the top, a 16px status icon
//!   (complete `CheckmarkOutline`, current `Incomplete`, incomplete
//!   `CircleDash`, invalid `Warning`) and the label, with an optional
//!   secondary label under it.
//! - **Behavior**: with [`ProgressIndicator::on_change`] every step but the
//!   current one is a button (Kit base button: focus, Enter / Space);
//!   without it the steps are static (`.cds--progress-step-button--unclickable`).
//!
//! ```ignore
//! ProgressIndicator::new("wizard")
//!     .current_index(1)
//!     .space_equally(true)
//!     .step(ProgressStep::new("Folder"))
//!     .step(ProgressStep::new("Agents"))
//!     .step(ProgressStep::new("Review"))
//! ```
use crate::{
    components::form::{self, rem},
    icons::{CarbonIcon, Icon, IconSize},
    theme::color,
    tokens::{ColorRole, TypeStyle},
    typography::TypeStyled as _,
};
use gpui_kit::{
    App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement,
    RenderOnce, SharedString, Styled, Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;

/// Where a step stands relative to the current index.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StepStatus {
    /// Before the current step (`.cds--progress-step--complete`).
    Complete,
    /// The current step (`.cds--progress-step--current`).
    Current,
    /// After it (`.cds--progress-step--incomplete`).
    Incomplete,
}

impl StepStatus {
    /// The status of step `index` when `current` is the current index
    /// (Carbon: `index < currentIndex` complete, `===` current, else incomplete).
    pub fn of(index: usize, current: usize) -> Self {
        match index.cmp(&current) {
            std::cmp::Ordering::Less => Self::Complete,
            std::cmp::Ordering::Equal => Self::Current,
            std::cmp::Ordering::Greater => Self::Incomplete,
        }
    }
}

/// The colors and icon of one step.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StepLook {
    /// The status icon.
    pub icon: CarbonIcon,
    /// `.cds--progress-step svg { fill: var(--interactive) }`; incomplete
    /// `icon-primary`; disabled `icon-disabled`; invalid `support-error`.
    pub icon_color: Hsla,
    /// `.cds--progress-step--current/--complete .cds--progress-line {
    /// background-color: var(--interactive) }`; incomplete `border-subtle`.
    pub line: Hsla,
    /// `.cds--progress-label { color: var(--text-primary) }`; disabled
    /// `text-disabled`; hover on a clickable step `link-primary-hover`.
    pub label: Hsla,
    /// `.cds--progress-label:hover { color: var(--link-primary-hover) }`.
    pub label_hover: Hsla,
}

impl StepLook {
    /// The look of a step.
    pub fn of(status: StepStatus, disabled: bool, invalid: bool) -> Self {
        let icon = if invalid {
            CarbonIcon::Warning
        } else {
            match status {
                StepStatus::Complete => CarbonIcon::CheckmarkOutline,
                StepStatus::Current => CarbonIcon::Incomplete,
                StepStatus::Incomplete => CarbonIcon::CircleDash,
            }
        };
        let icon_color = color(if disabled {
            ColorRole::IconDisabled
        } else if invalid {
            ColorRole::SupportError
        } else if status == StepStatus::Incomplete {
            ColorRole::IconPrimary
        } else {
            ColorRole::Interactive
        });
        let line = color(match status {
            StepStatus::Incomplete => ColorRole::BorderSubtle01,
            _ => ColorRole::Interactive,
        });
        let label = color(if disabled {
            ColorRole::TextDisabled
        } else {
            ColorRole::TextPrimary
        });
        Self {
            icon,
            icon_color,
            line,
            label,
            label_hover: color(ColorRole::LinkPrimaryHover),
        }
    }
}

/// The indicator's measurements, in px at 100% zoom.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ProgressGeometry {
    /// `.cds--progress-step { inline-size: 8rem }`.
    pub step_width: f32,
    /// `.cds--progress-step { min-inline-size: 7rem }`.
    pub step_min: f32,
    /// `.cds--progress--space-equal .cds--progress-step { min-inline-size: 8rem }`.
    pub step_min_equal: f32,
    /// `.cds--progress-line { block-size: 2px }`.
    pub line: f32,
    /// `.cds--progress-step svg { block-size: 1rem; margin-block-start: 0.625rem;
    /// margin-inline-end: 0.5rem }`: side.
    pub icon: f32,
    /// … top margin.
    pub icon_top: f32,
    /// … end margin.
    pub icon_gap: f32,
    /// `.cds--progress-label { margin: 0.5rem 0 0 0 }`.
    pub label_top: f32,
    /// `.cds--progress-label { line-height: 1.45 }` at 14px.
    pub label_line: f32,
    /// `.cds--progress-label { max-inline-size: 5.5rem }`.
    pub label_max: f32,
    /// `.cds--progress--space-equal .cds--progress-label { margin-inline-end: 0.75rem }`.
    pub label_end: f32,
}

impl ProgressGeometry {
    /// The reference geometry.
    pub const REFERENCE: Self = Self {
        step_width: 128.,
        step_min: 112.,
        step_min_equal: 128.,
        line: 2.,
        icon: 16.,
        icon_top: 10.,
        icon_gap: 8.,
        label_top: 8.,
        label_line: 14. * 1.45,
        label_max: 88.,
        label_end: 12.,
    };
}

/// One step (`ProgressStep`).
#[derive(Clone)]
pub struct ProgressStep {
    label: SharedString,
    secondary_label: Option<SharedString>,
    disabled: bool,
    invalid: bool,
}

impl ProgressStep {
    /// A step reading `label`.
    pub fn new(label: impl Into<SharedString>) -> Self {
        Self {
            label: label.into(),
            secondary_label: None,
            disabled: false,
            invalid: false,
        }
    }

    /// `secondaryLabel` (`.cds--progress-optional`: `label-01`, `text-secondary`).
    pub fn secondary_label(mut self, text: impl Into<SharedString>) -> Self {
        self.secondary_label = Some(text.into());
        self
    }

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

    /// `invalid`: the warning icon.
    pub fn invalid(mut self, invalid: bool) -> Self {
        self.invalid = invalid;
        self
    }
}

type ChangeHandler = Rc<dyn Fn(usize, &mut Window, &mut App)>;

/// A Carbon progress indicator. See the module docs.
#[derive(IntoElement)]
pub struct ProgressIndicator {
    id: ElementId,
    current: usize,
    space_equally: bool,
    steps: Vec<ProgressStep>,
    on_change: Option<ChangeHandler>,
}

impl ProgressIndicator {
    /// An indicator with a stable `id`, the first step current.
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            current: 0,
            space_equally: false,
            steps: Vec::new(),
            on_change: None,
        }
    }

    /// `currentIndex`.
    pub fn current_index(mut self, index: usize) -> Self {
        self.current = index;
        self
    }

    /// `spaceEqually`: steps share the width (`flex-grow: 1`).
    pub fn space_equally(mut self, equal: bool) -> Self {
        self.space_equally = equal;
        self
    }

    /// Append a step.
    pub fn step(mut self, step: ProgressStep) -> Self {
        self.steps.push(step);
        self
    }

    /// `onChange`: makes the other steps clickable; called with the index.
    pub fn on_change(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_change = Some(Rc::new(f));
        self
    }
}

/// Whether step `index` is a button: there is a handler, it is not the
/// current step and not disabled.
pub fn clickable(has_handler: bool, index: usize, current: usize, disabled: bool) -> bool {
    has_handler && index != current && !disabled
}

impl RenderOnce for ProgressIndicator {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let g = ProgressGeometry::REFERENCE;
        let id = self.id;
        let equal = self.space_equally;
        let current = self.current;
        let steps = self.steps.into_iter().enumerate().map(|(i, step)| {
            let status = StepStatus::of(i, current);
            let look = StepLook::of(status, step.disabled, step.invalid);
            let can_click = clickable(self.on_change.is_some(), i, current, step.disabled);
            let focus = window
                .use_keyed_state((id.clone(), format!("focus{i}")), cx, |_, cx| {
                    cx.focus_handle()
                })
                .read(cx)
                .clone();
            let focused =
                can_click && focus.is_focused(window) && crate::focus::focus_visible_mode(window);
            let hovered_id: ElementId = (id.clone(), format!("label{i}")).into();
            let label = div()
                .id(hovered_id)
                .relative()
                .mt(rem(g.label_top))
                .overflow_hidden()
                .whitespace_nowrap()
                .text_ellipsis()
                .line_height(rem(g.label_line))
                .text_color(if focused {
                    color(ColorRole::Focus)
                } else {
                    look.label
                })
                .map(|d| {
                    if equal {
                        d.mr(rem(g.label_end))
                    } else {
                        d.max_w(rem(g.label_max))
                    }
                })
                // `:hover { box-shadow: 0 0.0625rem var(--link-primary-hover); color: … }`.
                .when(can_click, |d| {
                    let hover = look.label_hover;
                    d.hover(move |s| s.text_color(hover).underline())
                })
                // `:focus-visible .cds--progress-label { outline: 0.0625rem solid var(--focus) }`.
                .when(focused, |d| {
                    d.child(form::inset_outline(1., color(ColorRole::Focus)))
                })
                .child(step.label);
            let text = div()
                .flex()
                .flex_col()
                .min_w_0()
                .flex_1()
                .child(label)
                .when_some(step.secondary_label, |d, text| {
                    d.child(
                        div()
                            .type_style(TypeStyle::Label01)
                            .text_color(color(ColorRole::TextSecondary))
                            .child(text),
                    )
                });
            let on_change = self.on_change.clone();
            let body = gpui_kit::base::Button::new((id.clone(), format!("step{i}")))
                .track_focus(&focus)
                .disabled(!can_click)
                .flex()
                .flex_row()
                .w_full()
                .min_w_0()
                .cursor(if step.disabled {
                    CursorStyle::OperationNotAllowed
                } else if can_click {
                    CursorStyle::PointingHand
                } else {
                    CursorStyle::Arrow
                })
                .child(
                    div()
                        .flex_none()
                        .mt(rem(g.icon_top))
                        .mr(rem(g.icon_gap))
                        .child(
                            Icon::carbon(look.icon)
                                .size(IconSize::S16)
                                .color(look.icon_color),
                        ),
                )
                .child(text)
                .on_click(move |_, window, cx| {
                    if let Some(f) = &on_change {
                        f(i, window, cx)
                    }
                });
            div()
                .relative()
                .flex()
                .flex_row()
                .min_w_0()
                .map(|d| {
                    if equal {
                        d.flex_1().min_w(rem(g.step_min_equal))
                    } else {
                        d.w(rem(g.step_width)).min_w(rem(g.step_min))
                    }
                })
                .child(
                    div()
                        .absolute()
                        .top_0()
                        .left_0()
                        .right_0()
                        .h(rem(g.line))
                        .bg(look.line),
                )
                .child(body)
        });
        div()
            .id(id.clone())
            .flex()
            .flex_row()
            .w_full()
            .type_style(TypeStyle::BodyCompact01)
            .children(steps)
    }
}

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

    #[test]
    fn status_follows_the_current_index() {
        assert_eq!(StepStatus::of(0, 1), StepStatus::Complete);
        assert_eq!(StepStatus::of(1, 1), StepStatus::Current);
        assert_eq!(StepStatus::of(2, 1), StepStatus::Incomplete);
    }

    #[test]
    fn look_matches_the_css() {
        let done = StepLook::of(StepStatus::Complete, false, false);
        assert_eq!(done.icon, CarbonIcon::CheckmarkOutline);
        assert_eq!(done.icon_color, color(ColorRole::Interactive));
        assert_eq!(done.line, color(ColorRole::Interactive));
        let now = StepLook::of(StepStatus::Current, false, false);
        assert_eq!(now.icon, CarbonIcon::Incomplete);
        let next = StepLook::of(StepStatus::Incomplete, false, false);
        assert_eq!(next.icon, CarbonIcon::CircleDash);
        assert_eq!(next.icon_color, color(ColorRole::IconPrimary));
        assert_eq!(next.line, color(ColorRole::BorderSubtle01));
        let off = StepLook::of(StepStatus::Incomplete, true, false);
        assert_eq!(off.icon_color, color(ColorRole::IconDisabled));
        assert_eq!(off.label, color(ColorRole::TextDisabled));
        let bad = StepLook::of(StepStatus::Complete, false, true);
        assert_eq!(bad.icon, CarbonIcon::Warning);
        assert_eq!(bad.icon_color, color(ColorRole::SupportError));
    }

    #[test]
    fn geometry_matches_the_css() {
        let g = ProgressGeometry::REFERENCE;
        assert_eq!((g.step_width, g.line, g.icon), (128., 2., 16.));
        assert_eq!((g.icon_top, g.icon_gap, g.label_top), (10., 8., 8.));
        assert_eq!((g.label_max, g.label_end), (88., 12.));
    }

    #[test]
    fn only_other_enabled_steps_click() {
        assert!(clickable(true, 0, 1, false));
        assert!(!clickable(true, 1, 1, false));
        assert!(!clickable(true, 2, 1, true));
        assert!(!clickable(false, 0, 1, false));
    }
}