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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! TaskRow: one scheduled task in a list (`TaskRow` in `src/ghost/admin.jsx`,
//! `.g-task` in `src/ghost.css`): the `Time` glyph, name, a meta line
//! (schedule · agent · project), the last run with its result and the next
//! run, "Run now" and an on/off switch.
//!
//! The switch keeps its own state seeded from [`TaskRow::enabled`] (the
//! reference's `useState(enabled)`), and follows `enabled` again whenever
//! the caller changes it. A paused task greys its name and shows "Paused"
//! instead of the next run.
//!
//! ```ignore
//! TaskRow::new("t-triage", "Morning triage")
//!     .schedule("Weekdays at 09:00")
//!     .agent("Claude")
//!     .project("carbon-ui")
//!     .last_run("today 09:00")
//!     .last_status(TaskStatus::Ok)
//!     .next_run("tomorrow 09:00")
//!     .on_toggle(|on, _, _| {})
//!     .on_run(|_, _| {})
//! ```
use super::row;
use crate::{
    components::{Button, ButtonSize, ButtonVariant, Switch, SwitchSize},
    icons::{CarbonIcon, Icon},
    t3::{
        IdentitySize, ProjectMonogram, ProviderIcon,
        base::visually_hidden,
        craft::{rem, sans},
    },
    theme::color,
    tokens::{self, ColorRole},
    u0::primitives::parts,
};
use gpui_kit::{
    AnyElement, App, ClickEvent, CursorStyle, ElementId, Hsla, IntoElement, ParentElement,
    RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
    prelude::FluentBuilder as _, px,
};
use std::rc::Rc;

/// The last run's result (`lastStatus`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TaskStatus {
    /// `ok`: succeeded.
    Ok,
    /// `failed`.
    Failed,
    /// `running`: currently in progress.
    Running,
}

impl TaskStatus {
    /// Every status, in `index.d.ts` order plus `running`.
    pub const ALL: [Self; 3] = [Self::Ok, Self::Failed, Self::Running];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Ok => "ok",
            Self::Failed => "failed",
            Self::Running => "running",
        }
    }

    /// `I.CheckmarkFilled` / `I.ErrorFilled` / `I.InProgress`.
    pub const fn icon(self) -> CarbonIcon {
        match self {
            Self::Ok => CarbonIcon::CheckmarkFilled,
            Self::Failed => CarbonIcon::ErrorFilled,
            Self::Running => CarbonIcon::InProgress,
        }
    }

    /// `svg.t3-okicon { fill: var(--support-success) }`, `svg.t3-err { fill:
    /// var(--support-error) }` (`src/ghost.css`), `interactive` when running.
    pub fn ink(self) -> Hsla {
        color(match self {
            Self::Ok => ColorRole::SupportSuccess,
            Self::Failed => ColorRole::SupportError,
            Self::Running => ColorRole::Interactive,
        })
    }
}

/// The row's user-facing strings (the reference's English defaults).
#[derive(Clone)]
pub struct TaskRowTexts {
    /// The last run: `"Last {lastRun}"`.
    pub last: Rc<dyn Fn(&str) -> SharedString>,
    /// The next run: `"Next {nextRun}"`.
    pub next: Rc<dyn Fn(&str) -> SharedString>,
    /// Instead of the next run while off: `"Paused"`.
    pub paused: SharedString,
    /// Hidden text after the `ok` glyph: `"Succeeded"`.
    pub succeeded: SharedString,
    /// Hidden text after the `failed` glyph: `"Failed"`.
    pub failed: SharedString,
    /// Hidden text after the `running` glyph: `"Running"`.
    pub running: SharedString,
    /// The run button's label (tooltip): `"Run now"`.
    pub run_now: SharedString,
    /// The switch's accessible name: `"{name} enabled"`.
    pub enabled: Rc<dyn Fn(&str) -> SharedString>,
}

impl Default for TaskRowTexts {
    fn default() -> Self {
        Self {
            last: Rc::new(|t| format!("Last {t}").into()),
            next: Rc::new(|t| format!("Next {t}").into()),
            paused: "Paused".into(),
            succeeded: "Succeeded".into(),
            failed: "Failed".into(),
            running: "Running".into(),
            run_now: "Run now".into(),
            enabled: Rc::new(|name| format!("{name} enabled").into()),
        }
    }
}

/// The switch state: the `enabled` prop it was last seeded from and the
/// current position.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EnabledState {
    prop: bool,
    on: bool,
}

impl EnabledState {
    /// Seeded from the `enabled` prop.
    pub const fn new(enabled: bool) -> Self {
        Self {
            prop: enabled,
            on: enabled,
        }
    }

    /// Re-seed when the caller's `enabled` changed since the last render;
    /// otherwise keep the user's position.
    pub fn sync(&mut self, enabled: bool) {
        if self.prop != enabled {
            *self = Self::new(enabled);
        }
    }

    /// The switch was flipped.
    pub fn set(&mut self, on: bool) {
        self.on = on;
    }

    /// On?
    pub const fn on(self) -> bool {
        self.on
    }
}

/// The name's ink: `text-primary`, `text-secondary` while off
/// (`.g-task--off .g-task-main b { color: var(--text-secondary) }`).
pub fn name_ink(on: bool) -> Hsla {
    color(if on {
        ColorRole::TextPrimary
    } else {
        ColorRole::TextSecondary
    })
}

/// Row surface and selection bar colors: `(background, left_bar)`.
pub fn row_look(selected: bool, hovered: bool) -> (Hsla, Option<Hsla>) {
    let bg = if selected {
        color(ColorRole::LayerSelected01)
    } else if hovered {
        color(ColorRole::LayerHover01)
    } else {
        color(ColorRole::Layer01)
    };
    let bar = selected.then(|| color(ColorRole::BorderInteractive));
    (bg, bar)
}

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

/// A scheduled-task row. See the module docs.
#[derive(IntoElement)]
pub struct TaskRow {
    id: ElementId,
    name: SharedString,
    icon: CarbonIcon,
    schedule: Option<SharedString>,
    agent: Option<SharedString>,
    agent_mark: Option<AnyElement>,
    project: Option<SharedString>,
    show_monograms: bool,
    last_run: Option<SharedString>,
    last_status: Option<TaskStatus>,
    next_run: Option<SharedString>,
    enabled: bool,
    selected: bool,
    texts: TaskRowTexts,
    on_click: Option<ClickHandler>,
    on_toggle: Option<ToggleHandler>,
    on_run: Option<Handler>,
}

impl TaskRow {
    /// A row for task `name` (`name`), enabled.
    pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            icon: CarbonIcon::Time,
            schedule: None,
            agent: None,
            agent_mark: None,
            project: None,
            show_monograms: true,
            last_run: None,
            last_status: None,
            next_run: None,
            enabled: true,
            selected: false,
            texts: TaskRowTexts::default(),
            on_click: None,
            on_toggle: None,
            on_run: None,
        }
    }

    /// Host-drawn agent mark (e.g. ACP registry SVG or Simple Icons mark)
    /// rendered before the agent name in place of [`ProviderIcon`].
    pub fn agent_mark(mut self, mark: impl IntoElement) -> Self {
        self.agent_mark = Some(mark.into_any_element());
        self
    }

    /// Leading icon (default [`CarbonIcon::Time`]).
    pub fn icon(mut self, icon: CarbonIcon) -> Self {
        self.icon = icon;
        self
    }

    /// Show the 14px agent/project monograms in the metadata line (default `true`).
    pub fn show_monograms(mut self, show: bool) -> Self {
        self.show_monograms = show;
        self
    }

    /// Highlight as the selected task (`layer-selected-01` + 3px `border-interactive` start bar).
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Row body pressed (does not intercept the Run button or Switch).
    pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
        self.on_click = Some(Rc::new(f));
        self
    }

    /// When it runs, in words (`schedule`, e.g. `ScheduleEditor`'s summary).
    pub fn schedule(mut self, schedule: impl Into<SharedString>) -> Self {
        self.schedule = Some(schedule.into());
        self
    }

    /// The agent that runs it (`agent`), drawn with its `ProviderIcon`.
    pub fn agent(mut self, agent: impl Into<SharedString>) -> Self {
        self.agent = Some(agent.into());
        self
    }

    /// The project (`project`), drawn with its `ProjectMonogram`.
    pub fn project(mut self, project: impl Into<SharedString>) -> Self {
        self.project = Some(project.into());
        self
    }

    /// When it last ran (`lastRun`).
    pub fn last_run(mut self, last_run: impl Into<SharedString>) -> Self {
        self.last_run = Some(last_run.into());
        self
    }

    /// How the last run ended (`lastStatus`); no glyph without one.
    pub fn last_status(mut self, status: TaskStatus) -> Self {
        self.last_status = Some(status);
        self
    }

    /// When it runs next (`nextRun`).
    pub fn next_run(mut self, next_run: impl Into<SharedString>) -> Self {
        self.next_run = Some(next_run.into());
        self
    }

    /// Whether the task is on (`enabled`, default `true`).
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// The user-facing strings.
    pub fn texts(mut self, texts: TaskRowTexts) -> Self {
        self.texts = texts;
        self
    }

    /// The switch was flipped (`onToggle`).
    pub fn on_toggle(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
        self.on_toggle = Some(Rc::new(f));
        self
    }

    /// "Run now" pressed (`onRun`).
    pub fn on_run(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_run = Some(Rc::new(f));
        self
    }

    /// The second line of the runs column: `on ? "Next {nextRun}" :
    /// "Paused"` (nothing while on without a next run).
    pub fn upcoming(&self, on: bool) -> Option<SharedString> {
        if on {
            self.next_run.as_deref().map(|t| (self.texts.next)(t))
        } else {
            Some(self.texts.paused.clone())
        }
    }

    /// The meta line: schedule, then agent and project with their marks.
    fn meta(&mut self) -> Vec<AnyElement> {
        let mut runs: Vec<AnyElement> = Vec::new();
        if let Some(s) = &self.schedule {
            runs.push(row::meta_text(s.clone()));
        }
        // `<ProviderIcon name={agent} size="xs" />{agent}` and
        // `<ProjectMonogram name={project} size="xs" />{project}`: mark and
        // name are siblings in the meta flex, so the 8px column gap parts them.
        if let Some(agent) = &self.agent {
            if let Some(mark) = self.agent_mark.take() {
                runs.push(mark_and_name(mark, agent.clone()));
            } else if self.show_monograms {
                runs.push(mark_and_name(
                    ProviderIcon::new(agent.clone())
                        .id((self.id.clone(), "agent"))
                        .size(IdentitySize::Xs)
                        .into_any_element(),
                    agent.clone(),
                ));
            } else {
                runs.push(row::meta_text(agent.clone()));
            }
        }
        if let Some(project) = &self.project {
            if self.show_monograms {
                runs.push(mark_and_name(
                    ProjectMonogram::new(project.clone())
                        .size(IdentitySize::Xs)
                        .into_any_element(),
                    project.clone(),
                ));
            } else {
                runs.push(row::meta_text(project.clone()));
            }
        }
        row::with_dots(runs)
    }
}

/// A mark and its name, `spacing-03` apart (the meta line's column gap).
fn mark_and_name(mark: AnyElement, name: SharedString) -> AnyElement {
    div()
        .flex()
        .items_center()
        .gap(tokens::SPACING_03)
        .child(mark)
        .child(row::meta_text(name))
        .into_any_element()
}

impl RenderOnce for TaskRow {
    fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let enabled = self.enabled;
        let store = window.use_keyed_state((self.id.clone(), "enabled"), cx, |_, _| {
            EnabledState::new(enabled)
        });
        let on = store.update(cx, |s, _| {
            s.sync(enabled);
            s.on()
        });
        let meta = self.meta();
        let upcoming = self.upcoming(on);
        let status = self.last_status.map(|s| {
            let hidden = match s {
                TaskStatus::Ok => self.texts.succeeded.clone(),
                TaskStatus::Failed => self.texts.failed.clone(),
                TaskStatus::Running => self.texts.running.clone(),
            };
            div()
                .relative()
                .flex()
                .flex_none()
                .child(Icon::carbon(s.icon()).size_length(rem(12.)).color(s.ink()))
                .child(visually_hidden(hidden))
        });
        let last = self.last_run.as_deref().map(|t| (self.texts.last)(t));
        let on_run = self.on_run.clone();
        let on_toggle = self.on_toggle.clone();
        let on_click = self.on_click.clone();
        let switch_label = (self.texts.enabled)(&self.name);
        let hovered_state = window.use_keyed_state((self.id.clone(), "hover"), cx, |_, _| false);
        let is_hovered = on_click.is_some() && *hovered_state.read(cx);
        let (bg, bar) = row_look(self.selected, is_hovered);

        // `.g-task-runs { display: flex; flex-direction: column;
        // align-items: flex-end; gap: 2px; flex: none; font-size:
        // 0.75rem; color: var(--text-secondary) }`; each span
        // `inline-flex; align-items: center; gap: var(--spacing-02)`.
        let runs_col = sans(div(), 12., 400)
            .flex()
            .flex_col()
            .items_end()
            .flex_none()
            .gap(px(2.))
            .text_color(color(ColorRole::TextSecondary))
            .when(status.is_some() || last.is_some(), |this| {
                this.child(
                    div()
                        .flex()
                        .items_center()
                        .gap(tokens::SPACING_02)
                        .whitespace_nowrap()
                        .children(status)
                        .children(last),
                )
            })
            .when_some(upcoming, |this, t| {
                this.child(div().whitespace_nowrap().child(t))
            });

        let run_button = div().flex_none().child(
            Button::new((self.id.clone(), "run"), self.texts.run_now.clone())
                .icon(CarbonIcon::Play)
                .icon_only(true)
                .variant(ButtonVariant::Ghost)
                .size(ButtonSize::Sm)
                .when_some(on_run, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
        );

        let switch = div().flex_none().child(
            Switch::new((self.id.clone(), "toggle"))
                .size(SwitchSize::Sm)
                .label(switch_label)
                .hide_label(true)
                .checked(on)
                .on_checked_change(move |v, w, cx| {
                    store.update(cx, |s, cx| {
                        s.set(v);
                        cx.notify();
                    });
                    if let Some(f) = &on_toggle {
                        f(v, w, cx);
                    }
                }),
        );

        let accent_bar = bar.map(|bar| {
            div()
                .absolute()
                .top_0()
                .bottom_0()
                .left_0()
                .w(px(3.))
                .bg(bar)
        });

        match on_click {
            Some(f) => {
                let focus = parts::keyed_focus((self.id.clone(), "body-focus"), window, cx);
                let ring = parts::focus_visible(&focus, window);
                let body = gpui_kit::base::Button::new((self.id.clone(), "body"))
                    .track_focus(&focus)
                    .accessibility_label(self.name.clone())
                    .relative()
                    .flex()
                    .items_center()
                    .justify_start()
                    .gap(tokens::SPACING_04)
                    .flex_1()
                    .min_w_0()
                    .self_stretch()
                    .pl(tokens::SPACING_05)
                    .pt(tokens::SPACING_02)
                    .pb(tokens::SPACING_02)
                    .text_left()
                    .cursor(CursorStyle::PointingHand)
                    .on_hover(move |h, _, cx| {
                        hovered_state.update(cx, |v, cx| {
                            if *v != *h {
                                *v = *h;
                                cx.notify();
                            }
                        })
                    })
                    .on_click(move |e, w, cx| f(e, w, cx))
                    .child(row::lead_icon(self.icon))
                    .child(row::main(self.name.clone(), name_ink(on), meta))
                    .child(runs_col)
                    .when(ring, |b| b.child(parts::focus_ring()));

                row::frame()
                    .pl(px(0.))
                    .pt(px(0.))
                    .pb(px(0.))
                    .bg(bg)
                    .child(body)
                    .child(
                        div()
                            .flex()
                            .flex_none()
                            .items_center()
                            .gap(tokens::SPACING_04)
                            .pt(tokens::SPACING_02)
                            .pb(tokens::SPACING_02)
                            .child(run_button)
                            .child(switch),
                    )
                    .children(accent_bar)
            }
            None => row::frame()
                .bg(bg)
                .child(row::lead_icon(self.icon))
                .child(row::main(self.name.clone(), name_ink(on), meta))
                .child(runs_col)
                .child(run_button)
                .child(switch)
                .children(accent_bar),
        }
    }
}

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

    #[test]
    fn status_glyphs_and_inks() {
        crate::theme::tests_set(CarbonTheme::G100);
        assert_eq!(TaskStatus::Ok.icon(), CarbonIcon::CheckmarkFilled);
        assert_eq!(TaskStatus::Failed.icon(), CarbonIcon::ErrorFilled);
        assert_eq!(TaskStatus::Running.icon(), CarbonIcon::InProgress);
        assert_eq!(TaskStatus::Ok.ink(), color(ColorRole::SupportSuccess));
        assert_eq!(TaskStatus::Failed.ink(), color(ColorRole::SupportError));
        assert_eq!(TaskStatus::Running.ink(), color(ColorRole::Interactive));
        assert_eq!(
            TaskStatus::ALL.map(TaskStatus::name),
            ["ok", "failed", "running"]
        );
    }

    #[test]
    fn selection_and_hover_colors_follow_carbon_layers() {
        crate::theme::tests_set(CarbonTheme::White);
        assert_eq!(row_look(false, false), (color(ColorRole::Layer01), None));
        assert_eq!(
            row_look(false, true),
            (color(ColorRole::LayerHover01), None)
        );
        assert_eq!(
            row_look(true, false),
            (
                color(ColorRole::LayerSelected01),
                Some(color(ColorRole::BorderInteractive)),
            )
        );
        let mut row = TaskRow::new("t", "Hook")
            .icon(CarbonIcon::Flow)
            .agent("Claude")
            .project("u0")
            .show_monograms(false)
            .selected(true);
        assert_eq!(row.icon, CarbonIcon::Flow);
        assert!(!row.show_monograms);
        assert!(row.selected);
        // 2 runs (`Claude` · `u0`) + 1 dot = 3 elements.
        assert_eq!(row.meta().len(), 3);
    }

    #[test]
    fn paused_greys_the_name() {
        crate::theme::tests_set(CarbonTheme::White);
        assert_eq!(name_ink(true), color(ColorRole::TextPrimary));
        assert_eq!(name_ink(false), color(ColorRole::TextSecondary));
    }

    #[test]
    fn upcoming_is_next_run_or_paused() {
        let row = TaskRow::new("t", "Morning triage").next_run("tomorrow 09:00");
        assert_eq!(row.upcoming(true), Some("Next tomorrow 09:00".into()));
        assert_eq!(row.upcoming(false), Some("Paused".into()));
        let row = TaskRow::new("t", "Release notes draft");
        assert_eq!(row.upcoming(true), None);
        assert_eq!(row.upcoming(false), Some("Paused".into()));
    }

    #[test]
    fn switch_follows_the_prop_but_keeps_the_users_flip() {
        let mut s = EnabledState::new(true);
        s.set(false);
        s.sync(true);
        assert!(!s.on(), "same prop: the user's flip stays");
        s.sync(false);
        assert!(!s.on());
        s.sync(true);
        assert!(s.on(), "prop changed: re-seeded");
    }
}