u0ui 0.3.6

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
//! ProcessTree: every process the app started, as a tree with CPU and memory
//! (`ProcessTree` in `src/ghost/admin.jsx`, `.g-ptree` in `src/ghost.css`).
//!
//! - **Behavior**: branches start open; each chevron collapses or expands
//!   its children (the chevron turns 90° in 110ms). Every row has its own
//!   Stop button.
//! - **Appearance**: a `density-row-md` header on `layer-accent-01`
//!   (Process · PID · CPU · Memory · Stop), columns `minmax(0, 1fr) 4rem
//!   7.5rem 8.5rem 2.5rem`; rows indent 16px per level from 8px; agent
//!   processes show their provider mark, the rest a terminal glyph; CPU and
//!   memory pair a 2.5rem bar (`charts-1`, `support-error` past 70%) with
//!   the figure.
use crate::{
    components::{Button, ButtonSize, ButtonVariant},
    icons::{CarbonIcon, Icon},
    motion::{self, Motion},
    t3::{
        IdentitySize, ProviderIcon,
        craft::{mono, rem, sans},
    },
    theme::color,
    tokens::{self, ColorRole},
    u0::primitives::parts::{focus_ring, focus_visible, keyed_focus},
};
use gpui_kit::{
    AnyElement, App, CursorStyle, Div, ElementId, Hsla, InteractiveElement as _, IntoElement,
    ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
    prelude::FluentBuilder as _, px, relative,
};
use std::{collections::HashSet, rc::Rc};

/// One process (`{pid, name, kind, cpu, mem, children}`).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Process {
    /// `pid`.
    pub pid: u32,
    /// `name`.
    pub name: SharedString,
    /// `kind`: the agent behind it (draws its `ProviderIcon`).
    pub kind: Option<SharedString>,
    /// `cpu` in percent.
    pub cpu: f32,
    /// `mem` in MB.
    pub mem: f32,
    /// `children`.
    pub children: Option<Vec<Process>>,
}

impl Process {
    /// A leaf process.
    pub fn new(pid: u32, name: impl Into<SharedString>, cpu: f32, mem: f32) -> Self {
        Self {
            pid,
            name: name.into(),
            kind: None,
            cpu,
            mem,
            children: None,
        }
    }

    /// The agent behind it (`kind`).
    pub fn kind(mut self, kind: impl Into<SharedString>) -> Self {
        self.kind = Some(kind.into());
        self
    }

    /// Its children (makes it a branch).
    pub fn children(mut self, children: impl IntoIterator<Item = Process>) -> Self {
        self.children = Some(children.into_iter().collect());
        self
    }
}

/// The visible rows with their depth: children of open branches only.
pub fn visible_rows(processes: &[Process], open: &HashSet<u32>) -> Vec<(Process, usize)> {
    fn walk(ps: &[Process], d: usize, open: &HashSet<u32>, out: &mut Vec<(Process, usize)>) {
        for p in ps {
            out.push((p.clone(), d));
            if let Some(c) = &p.children
                && open.contains(&p.pid)
            {
                walk(c, d + 1, open, out);
            }
        }
    }
    let mut out = Vec::new();
    walk(processes, 0, open, &mut out);
    out
}

/// Every branch's pid (the initial open set).
pub fn branch_pids(processes: &[Process]) -> HashSet<u32> {
    fn walk(ps: &[Process], s: &mut HashSet<u32>) {
        for p in ps {
            if let Some(c) = &p.children {
                s.insert(p.pid);
                walk(c, s);
            }
        }
    }
    let mut s = HashSet::new();
    walk(processes, &mut s);
    s
}

/// `mem >= 1024 ? (mem / 1024).toFixed(1) + ' GB' : mem + ' MB'`.
pub fn format_mem(mem: f32) -> String {
    if mem >= 1024. {
        format!("{:.1} GB", mem / 1024.)
    } else {
        format!("{} MB", fmt_num(mem))
    }
}

/// A number as JavaScript prints it (no trailing `.0`).
fn fmt_num(n: f32) -> String {
    if n.fract() == 0. {
        format!("{}", n as i64)
    } else {
        format!("{n}")
    }
}

/// The bar's fill fraction and ink: `min(100, v / max × 100)%`,
/// `.g-ptree-bar i { background: var(--charts-1) }`, `.g-hot { background:
/// var(--support-error) }` past 70%.
pub fn bar_look(v: f32, max: f32) -> (f32, Hsla) {
    let f = v / max;
    (
        f.clamp(0., 1.),
        color(if f > 0.7 {
            ColorRole::SupportError
        } else {
            ColorRole::Charts1
        }),
    )
}

/// `.g-ptree-head, .g-ptree-row { grid-template-columns: minmax(0, 1fr) 4rem
/// 7.5rem 8.5rem 2.5rem }`: the fixed columns in px.
pub const COLUMNS: [f32; 4] = [64., 120., 136., 40.];

/// A toggle's name for (open, process name).
pub type ToggleText = Rc<dyn Fn(bool, &str) -> SharedString>;

/// The tree's user-facing strings (the reference's English defaults).
#[derive(Clone)]
pub struct ProcessTreeTexts {
    /// The headers: Process, PID, CPU, Memory, and the stop column's `Stop`.
    pub headers: [SharedString; 5],
    /// A toggle's name: `"Expand {name}"` / `"Collapse {name}"`.
    pub toggle: ToggleText,
    /// A stop button's name: `"Stop {name}"`.
    pub stop: Rc<dyn Fn(&str) -> SharedString>,
}

impl Default for ProcessTreeTexts {
    fn default() -> Self {
        Self {
            headers: [
                "Process".into(),
                "PID".into(),
                "CPU".into(),
                "Memory".into(),
                "Stop".into(),
            ],
            toggle: Rc::new(|open, n| {
                format!("{} {n}", if open { "Collapse" } else { "Expand" }).into()
            }),
            stop: Rc::new(|n| format!("Stop {n}").into()),
        }
    }
}

type StopHandler = Rc<dyn Fn(u32, &mut Window, &mut App)>;

/// The process tree. See the module docs.
#[derive(IntoElement)]
pub struct ProcessTree {
    id: ElementId,
    processes: Vec<Process>,
    texts: ProcessTreeTexts,
    on_stop: Option<StopHandler>,
}

impl ProcessTree {
    /// A tree of `processes` (`processes`).
    pub fn new(id: impl Into<ElementId>, processes: impl IntoIterator<Item = Process>) -> Self {
        Self {
            id: id.into(),
            processes: processes.into_iter().collect(),
            texts: ProcessTreeTexts::default(),
            on_stop: None,
        }
    }

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

    /// A Stop button was pressed, with its pid (the reference leaves it
    /// unwired; confirm before stopping).
    pub fn on_stop(mut self, f: impl Fn(u32, &mut Window, &mut App) + 'static) -> Self {
        self.on_stop = Some(Rc::new(f));
        self
    }
}

/// A grid row's five cells: the flexible name column, then the fixed ones.
fn grid(cells: [AnyElement; 5]) -> Div {
    let [a, b, c, d, e] = cells;
    let fixed = |el: AnyElement, w: f32| div().flex_none().w(rem(w)).min_w_0().child(el);
    div()
        .flex()
        .items_center()
        .w_full()
        .h(crate::density::current().row())
        .child(div().flex_1().min_w_0().child(a))
        .child(fixed(b, COLUMNS[0]))
        .child(fixed(c, COLUMNS[1]))
        .child(fixed(d, COLUMNS[2]))
        .child(fixed(e, COLUMNS[3]))
}

/// `.g-ptree-head > span, .g-ptree-row > span { padding: 0 var(--spacing-04) }`.
fn cell() -> Div {
    div()
        .px(tokens::SPACING_04)
        .overflow_hidden()
        .whitespace_nowrap()
}

/// A figure with its bar (`.g-ptree-row .g-tabnum { display: flex;
/// align-items: center; gap: var(--spacing-03); font-size: 0.75rem }`).
fn metric(v: f32, max: f32, label: String) -> AnyElement {
    let (f, ink) = bar_look(v, max);
    sans(cell(), 12., 400)
        .flex()
        .items_center()
        .gap(tokens::SPACING_03)
        .child(
            // `.g-ptree-bar { width: 2.5rem; height: 4px; background:
            // var(--layer-accent-01); flex: none }`.
            div()
                .flex_none()
                .w(rem(40.))
                .h(px(4.))
                .bg(color(ColorRole::LayerAccent01))
                .child(div().h_full().w(relative(f)).bg(ink)),
        )
        .child(label)
        .into_any_element()
}

impl RenderOnce for ProcessTree {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let seed = branch_pids(&self.processes);
        let open = window.use_keyed_state((id.clone(), "open"), cx, |_, _| seed);
        let opened = open.read(cx).clone();
        let rows = visible_rows(&self.processes, &opened);
        let texts = self.texts.clone();

        let header = {
            let h = |t: &SharedString| cell().child(t.clone()).into_any_element();
            // `.g-ptree-head { background: var(--layer-accent-01); font-weight:
            // 600; font-size: 0.75rem }`; the last header is centered with no
            // padding (`::before { content: 'Stop' }`).
            sans(
                grid([
                    h(&texts.headers[0]),
                    h(&texts.headers[1]),
                    h(&texts.headers[2]),
                    h(&texts.headers[3]),
                    div()
                        .flex()
                        .justify_center()
                        .child(texts.headers[4].clone())
                        .into_any_element(),
                ]),
                12.,
                600,
            )
            .bg(color(ColorRole::LayerAccent01))
            .text_color(color(ColorRole::TextPrimary))
        };

        let body = rows.into_iter().map(|(p, d)| {
            let pid = p.pid;
            let hover = window.use_keyed_state(
                (id.clone(), SharedString::from(format!("hover-{pid}"))),
                cx,
                |_, _| false,
            );
            let hovered = *hover.read(cx);
            let is_open = opened.contains(&pid);
            let toggle = if p.children.is_some() {
                let chevron = motion::animate(
                    (id.clone(), SharedString::from(format!("chev-{pid}"))),
                    if is_open {
                        std::f32::consts::FRAC_PI_2
                    } else {
                        0.
                    },
                    Motion::TOGGLE,
                    window,
                    cx,
                );
                let open = open.clone();
                let focus = keyed_focus(
                    (id.clone(), SharedString::from(format!("tog-focus-{pid}"))),
                    window,
                    cx,
                );
                // `.g-ttable-tog:focus-visible { outline: 2px solid var(--focus);
                // outline-offset: -2px }`.
                let ring = focus_visible(&focus, window);
                // `.g-ttable-tog { width/height: 1.25rem }` with the 14px
                // `.t3-chev` (`icon-secondary`, rotate 90° open).
                gpui_kit::base::Button::new((id.clone(), SharedString::from(format!("tog-{pid}"))))
                    .track_focus(&focus)
                    .accessibility_label((texts.toggle)(is_open, &p.name))
                    .relative()
                    .flex()
                    .flex_none()
                    .items_center()
                    .justify_center()
                    .size(rem(20.))
                    .cursor(CursorStyle::PointingHand)
                    .child(
                        Icon::carbon(CarbonIcon::ChevronRight)
                            .size_length(px(14.))
                            .color(color(ColorRole::IconSecondary))
                            .rotate(gpui_kit::radians(chevron)),
                    )
                    .when(ring, |b| b.child(focus_ring()))
                    .on_click(move |_, _, cx| {
                        open.update(cx, |s, cx| {
                            if !s.remove(&pid) {
                                s.insert(pid);
                            }
                            cx.notify();
                        })
                    })
                    .into_any_element()
            } else {
                // `.g-trow-sp { width: 14px }`.
                div().flex_none().w(px(14.)).into_any_element()
            };
            let mark = match &p.kind {
                Some(k) => ProviderIcon::new(k.clone())
                    .id((id.clone(), SharedString::from(format!("mark-{pid}"))))
                    .size(IdentitySize::Xs)
                    .into_any_element(),
                None => Icon::carbon(CarbonIcon::Terminal)
                    .size_length(px(14.))
                    .color(color(ColorRole::IconSecondary))
                    .into_any_element(),
            };
            // `.g-ptree-name { display: flex; align-items: center; gap:
            // var(--spacing-02) }`, `paddingLeft: 8 + d * 16`.
            let name = div()
                .flex()
                .items_center()
                .gap(tokens::SPACING_02)
                .pl(px(8. + d as f32 * 16.))
                .pr(tokens::SPACING_04)
                .overflow_hidden()
                .whitespace_nowrap()
                .child(toggle)
                .child(div().flex_none().child(mark))
                .child(mono(div(), 12., 400).child(p.name.clone()));
            let stop = {
                let f = self.on_stop.clone();
                Button::new(
                    (id.clone(), SharedString::from(format!("stop-{pid}"))),
                    (texts.stop)(&p.name),
                )
                .variant(ButtonVariant::Ghost)
                .size(ButtonSize::Sm)
                .icon(CarbonIcon::StopFilled)
                // `.g-ptree-row .cds--btn--ghost svg { fill: var(--icon-secondary) }`.
                .icon_color(color(ColorRole::IconSecondary))
                .icon_only(true)
                .on_click(move |_, w, cx| {
                    if let Some(f) = &f {
                        f(pid, w, cx);
                    }
                })
            };
            // `.g-ptree-row { height: var(--density-row-md); background:
            // var(--layer-01); border-bottom: 1px solid var(--border-subtle-01) }`,
            // `:hover { background: var(--layer-hover-01) }`.
            sans(
                grid([
                    name.into_any_element(),
                    mono(cell(), 12., 400)
                        .text_color(color(ColorRole::TextSecondary))
                        .child(format!("{pid}"))
                        .into_any_element(),
                    metric(p.cpu, 100., format!("{:.1}%", p.cpu)),
                    metric(p.mem, 2048., format_mem(p.mem)),
                    div().flex().justify_center().child(stop).into_any_element(),
                ]),
                14.,
                400,
            )
            .id((id.clone(), SharedString::from(format!("row-{pid}"))))
            .bg(color(if hovered {
                ColorRole::LayerHover01
            } else {
                ColorRole::Layer01
            }))
            .border_b_1()
            .border_color(color(ColorRole::BorderSubtle01))
            .text_color(color(ColorRole::TextPrimary))
            .on_hover(move |h, _, cx| {
                hover.update(cx, |v, cx| {
                    *v = *h;
                    cx.notify();
                })
            })
        });

        div()
            .flex()
            .flex_col()
            .w_full()
            .child(header)
            .children(body)
    }
}

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

    fn sample() -> Vec<Process> {
        vec![
            Process::new(4120, "u0-server", 3.2, 180.).children([
                Process::new(4188, "claude", 42.5, 610.)
                    .kind("Claude")
                    .children([Process::new(4301, "pnpm test", 88.1, 1540.)]),
                Process::new(4190, "codex", 0.4, 220.).kind("Codex"),
            ]),
            Process::new(5011, "cloudflared", 0.8, 38.),
        ]
    }

    #[test]
    fn branches_start_open_and_collapse() {
        let open = branch_pids(&sample());
        assert_eq!(open, HashSet::from([4120, 4188]));
        let rows = visible_rows(&sample(), &open);
        let flat: Vec<_> = rows.iter().map(|(p, d)| (p.pid, *d)).collect();
        assert_eq!(
            flat,
            vec![(4120, 0), (4188, 1), (4301, 2), (4190, 1), (5011, 0)]
        );
        let rows = visible_rows(&sample(), &HashSet::from([4120]));
        assert_eq!(rows.len(), 4);
    }

    #[test]
    fn memory_and_bars_follow_the_reference() {
        crate::theme::tests_set(CarbonTheme::White);
        assert_eq!(format_mem(610.), "610 MB");
        assert_eq!(format_mem(1540.), "1.5 GB");
        assert_eq!(bar_look(88.1, 100.).1, color(ColorRole::SupportError));
        assert_eq!(bar_look(42.5, 100.).1, color(ColorRole::Charts1));
        // 1.4 GB of 2048 MB is the 70% line.
        assert_eq!(bar_look(1540., 2048.).1, color(ColorRole::SupportError));
        assert_eq!(bar_look(3000., 2048.).0, 1.);
        assert_eq!(COLUMNS, [64., 120., 136., 40.]);
    }
}