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
//! [`ComposerAttachmentStrip`] (`.t3-attstrip`, "Attachments" in `t3.css`):
//! image thumbnails and file cards above the composer input.
//!
//! - **Thumbnail** (`.t3-thumb`): 4rem square on `layer-accent-01`, the image
//!   cover-fit or a colored placeholder with a 20px image glyph; while
//!   uploading a 50% scrim holds a white [`Spin`]; the remove × sits in the
//!   corner on `background-inverse` (always shown, d-fix).
//! - **Failed upload** (`.t3-attfail`, r2-gal): a 32px readable chip with the
//!   error icon, name, message, Retry and ×, on
//!   `notification-background-error` framed by `support-error`.
//! - **Files**: [`ContextChip`] bubbles with their size.
//! - **Opening** ([`ComposerAttachmentStrip::on_open`]): a done image's
//!   thumbnail is a button that opens it, named by `texts.open`.
//! - **In the composer** ([`ComposerAttachmentStrip::in_composer`], gemini-A
//!   and r2-gal): 2rem thumbs with a hairline, the × hung off the corner,
//!   a 55% upload scrim, and 32px file chips.
use super::chip::{ContextChip, ContextChipKind, ContextChipShape};
use crate::t3::base::Spin;
use crate::t3::press::{self, rem};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::{
    AnyElement, App, BoxShadow, CursorStyle, ElementId, Hsla, ImageSource, InteractiveElement as _,
    IntoElement, ObjectFit, ParentElement, RenderOnce, SharedString,
    StatefulInteractiveElement as _, Styled, StyledImage as _, Window, div, img,
    prelude::FluentBuilder as _, px, rgba,
};
use std::rc::Rc;

/// An image's upload state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AttachmentImageState {
    /// Uploaded.
    #[default]
    Done,
    /// Uploading: scrim and spinner.
    Uploading,
    /// Failed: the readable error chip.
    Error,
}

/// An image attachment (`{ name, src | color, state, error, onRetry }`).
#[derive(Clone)]
pub struct AttachmentImage {
    /// `name`: the key, tooltip and remove label.
    pub name: SharedString,
    /// `src`.
    pub source: Option<ImageSource>,
    /// `color`: the placeholder fill without `src`.
    pub color: Option<Hsla>,
    /// `state`.
    pub state: AttachmentImageState,
    /// `error`: the failure message (default "Upload failed").
    pub error: Option<SharedString>,
}

impl AttachmentImage {
    /// An image named `name`.
    pub fn new(name: impl Into<SharedString>) -> Self {
        Self {
            name: name.into(),
            source: None,
            color: None,
            state: AttachmentImageState::Done,
            error: None,
        }
    }

    /// `src`.
    pub fn source(mut self, source: impl Into<ImageSource>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// `color`: placeholder fill.
    pub fn color(mut self, color: impl Into<Hsla>) -> Self {
        self.color = Some(color.into());
        self
    }

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

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

/// A file attachment (`{ name, kind, size }`).
#[derive(Clone)]
pub struct AttachmentFile {
    /// `name`.
    pub name: SharedString,
    /// `kind` (default `file`).
    pub kind: ContextChipKind,
    /// `size`, shown as the chip's meta.
    pub size: Option<SharedString>,
}

impl AttachmentFile {
    /// A file named `name`.
    pub fn new(name: impl Into<SharedString>) -> Self {
        Self {
            name: name.into(),
            kind: ContextChipKind::File,
            size: None,
        }
    }

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

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

/// The strip's strings (the reference's English defaults).
#[derive(Clone)]
pub struct AttachmentTexts {
    /// The failed chip's message without `error`.
    pub upload_failed: SharedString,
    /// Retry.
    pub retry: SharedString,
    /// The spinner's hidden label.
    pub uploading: SharedString,
    /// The remove ×'s name: `"Remove {name}"`.
    pub remove: Rc<dyn Fn(&str) -> SharedString>,
    /// An openable thumbnail's name: `"Open {name}"`.
    pub open: Rc<dyn Fn(&str) -> SharedString>,
}

impl Default for AttachmentTexts {
    fn default() -> Self {
        Self {
            upload_failed: "Upload failed".into(),
            retry: "Retry".into(),
            uploading: "Uploading".into(),
            remove: Rc::new(|name| format!("Remove {name}").into()),
            open: Rc::new(|name| format!("Open {name}").into()),
        }
    }
}

/// Thumbnail geometry in px: (side, × offset from the corner, × padding).
pub const fn thumb_geometry(in_composer: bool) -> (f32, f32, f32) {
    if in_composer {
        // `.t3-composer-ctx .t3-thumb { width: 2rem; height: 2rem }`,
        // `.t3-thumb-x { top: -6px; right: -6px; padding: 1px }` (r2-gal).
        (32., -6., 1.)
    } else {
        // `.t3-thumb { width: 4rem; height: 4rem }`, `.t3-thumb-x { top: 2px; right: 2px; padding: 2px }`.
        (64., 2., 2.)
    }
}

/// The upload scrim: `rgba(22, 22, 22, 0.5)`, `.55` in the composer.
pub fn upload_scrim(in_composer: bool) -> Hsla {
    rgba(if in_composer { 0x1616168c } else { 0x16161680 }).into()
}

type NameHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;

/// The attachment strip. See the module docs.
#[derive(IntoElement)]
pub struct ComposerAttachmentStrip {
    id: ElementId,
    images: Vec<AttachmentImage>,
    files: Vec<AttachmentFile>,
    in_composer: bool,
    texts: AttachmentTexts,
    on_remove: Option<NameHandler>,
    on_retry: Option<NameHandler>,
    on_open: Option<NameHandler>,
}

impl ComposerAttachmentStrip {
    /// A strip of `images` then `files`.
    pub fn new(
        id: impl Into<ElementId>,
        images: Vec<AttachmentImage>,
        files: Vec<AttachmentFile>,
    ) -> Self {
        Self {
            id: id.into(),
            images,
            files,
            in_composer: false,
            texts: AttachmentTexts::default(),
            on_remove: None,
            on_retry: None,
            on_open: None,
        }
    }

    /// Drawn inside the composer's context row (`.t3-composer-ctx`).
    pub fn in_composer(mut self, in_composer: bool) -> Self {
        self.in_composer = in_composer;
        self
    }

    /// Replace the strings.
    pub fn texts(mut self, texts: AttachmentTexts) -> Self {
        self.texts = texts;
        self
    }

    /// `onRemove(name)`: adds the × to files (images always have one).
    pub fn on_remove(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
        self.on_remove = Some(Rc::new(f));
        self
    }

    /// A done image's thumbnail opened (`onOpen(name)`): the thumbnail is a
    /// button, the × above it still removes.
    pub fn on_open(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
        self.on_open = Some(Rc::new(f));
        self
    }

    /// A failed image's Retry (`image.onRetry(name)`).
    pub fn on_retry(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
        self.on_retry = Some(Rc::new(f));
        self
    }
}

/// How a bare button fills: on hover and/or under keyboard focus.
#[derive(Clone, Copy)]
struct Fill {
    hover: Option<Hsla>,
    focus: Option<Hsla>,
}

/// A bare button with its fills and the focus ring; returns it and whether
/// the pointer is over it.
fn bare_button(
    id: ElementId,
    label: SharedString,
    fill: Fill,
    content: impl FnOnce(bool) -> AnyElement,
    on_click: Option<press::Handler>,
    window: &mut Window,
    cx: &mut App,
) -> gpui_kit::base::Button {
    let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
    let (focus, ring) = press::focus((id.clone(), "focus"), window, cx);
    let bg = if ring {
        fill.focus.or(fill.hover.filter(|_| hovered))
    } else {
        fill.hover.filter(|_| hovered)
    };
    gpui_kit::base::Button::new(id)
        .track_focus(&focus)
        .accessibility_label(label)
        .relative()
        .flex()
        .flex_none()
        .items_center()
        .justify_center()
        .cursor(CursorStyle::PointingHand)
        .on_hover(press::track_hover(hover))
        .when_some(bg, |this, bg| this.bg(bg))
        .when_some(on_click, |this, f| {
            // A press that removes the attachment keeps the Tab starting point.
            let focus = focus.clone();
            this.on_click(move |_, w, cx| {
                crate::u0::primitives::parts::vacate_focus(&focus, w, cx);
                f(w, cx)
            })
        })
        .child(content(hovered))
        .when(ring, |this| this.child(press::ring()))
}

impl RenderOnce for ComposerAttachmentStrip {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let texts = self.texts.clone();
        let (side, x_at, x_pad) = thumb_geometry(self.in_composer);
        let mut children: Vec<AnyElement> = Vec::new();
        for image in self.images {
            // Keyed by name (`key={im.name}`), not position: removing one
            // never hands its focus or hover to the attachment after it.
            let name = image.name.clone();
            let key = |part: &str| -> ElementId {
                (id.clone(), SharedString::from(format!("{part}-{name}"))).into()
            };
            let remove_name = (texts.remove)(&image.name);
            let remove = self.on_remove.clone().map(|f| {
                let name = image.name.clone();
                Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
                    as Rc<dyn Fn(&mut Window, &mut App)>
            });
            if image.state == AttachmentImageState::Error {
                // `.t3-attfail { display: inline-flex; align-items: center; gap: spacing-03; height: 2rem;
                // padding: 0 spacing-02 0 spacing-03; background: notification-background-error;
                // box-shadow: inset 0 0 0 1px support-error; font-size: 0.875rem; white-space: nowrap }`.
                let retry = self.on_retry.clone().map(|f| {
                    let name = image.name.clone();
                    Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
                        as Rc<dyn Fn(&mut Window, &mut App)>
                });
                children.push(
                    div()
                        .flex()
                        .flex_row()
                        .flex_none()
                        .items_center()
                        .gap(rem(8.))
                        .h(rem(32.))
                        .pl(rem(8.))
                        .pr(rem(4.))
                        .bg(color(ColorRole::NotificationBackgroundError))
                        .shadow(press::inset_frame(color(ColorRole::SupportError)))
                        .text_size(rem(14.))
                        .line_height(rem(18.))
                        .whitespace_nowrap()
                        .child(press::icon(
                            CarbonIcon::ErrorFilled,
                            16.,
                            color(ColorRole::SupportError),
                        ))
                        // `.t3-attfail-name { color: text-primary }`.
                        .child(
                            div()
                                .text_color(color(ColorRole::TextPrimary))
                                .child(image.name.clone()),
                        )
                        // `.t3-attfail-msg { color: text-error; font-size: 0.75rem }`.
                        .child(
                            div()
                                .text_size(rem(12.))
                                .text_color(color(ColorRole::TextError))
                                .child(image.error.clone().unwrap_or(texts.upload_failed.clone())),
                        )
                        // `.t3-attfail-retry { padding: 0 spacing-02; height: 1.5rem; color: link-primary;
                        // font-size: 0.875rem }`, `:hover { text-decoration: underline }`,
                        // `:focus-visible { background: layer-hover-01; outline: 2px solid focus }`.
                        .child(
                            bare_button(
                                key("retry"),
                                texts.retry.clone(),
                                Fill {
                                    hover: None,
                                    focus: Some(color(ColorRole::LayerHover01)),
                                },
                                |hovered| {
                                    div()
                                        .text_color(color(ColorRole::LinkPrimary))
                                        .when(hovered, |this| this.underline())
                                        .child(texts.retry.clone())
                                        .into_any_element()
                                },
                                retry,
                                window,
                                cx,
                            )
                            .h(rem(24.))
                            .px(rem(4.)),
                        )
                        // `.t3-attfail-x { width: 1.5rem; height: 1.5rem; color: icon-primary }`,
                        // `:hover { background: layer-hover-01 }`.
                        .child(
                            bare_button(
                                key("x"),
                                remove_name,
                                Fill {
                                    hover: Some(color(ColorRole::LayerHover01)),
                                    focus: Some(color(ColorRole::LayerHover01)),
                                },
                                |_| {
                                    press::icon(
                                        CarbonIcon::Close,
                                        12.,
                                        color(ColorRole::IconPrimary),
                                    )
                                    .into_any_element()
                                },
                                remove,
                                window,
                                cx,
                            )
                            .size(rem(24.)),
                        )
                        .into_any_element(),
                );
                continue;
            }
            let uploading = image.state == AttachmentImageState::Uploading;
            let open = self.on_open.clone().filter(|_| !uploading).map(|f| {
                let name = image.name.clone();
                Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
                    as Rc<dyn Fn(&mut Window, &mut App)>
            });
            let picture: AnyElement = match image.source.clone() {
                Some(source) => img(source)
                    .size_full()
                    .object_fit(ObjectFit::Cover)
                    .into_any_element(),
                // `.t3-thumb-ph { background: <color> }`, `svg { fill: text-on-color; opacity: .8 }`.
                None => div()
                    .size_full()
                    .flex()
                    .items_center()
                    .justify_center()
                    .when_some(image.color, |this, c| this.bg(c))
                    .child(press::icon(
                        CarbonIcon::Image,
                        20.,
                        color(ColorRole::TextOnColor).opacity(0.8),
                    ))
                    .into_any_element(),
            };
            // `.t3-thumb-x { padding: 2px; background: background-inverse; color: text-inverse }`;
            // in the composer `box-shadow: 0 0 0 1px layer-01`.
            let x = bare_button(
                key("thumb-x"),
                remove_name,
                Fill {
                    hover: None,
                    focus: None,
                },
                |_| {
                    press::icon(CarbonIcon::Close, 12., color(ColorRole::TextInverse))
                        .into_any_element()
                },
                remove,
                window,
                cx,
            )
            .p(px(x_pad))
            .bg(color(ColorRole::BackgroundInverse))
            .when(self.in_composer, |this| {
                this.shadow(vec![
                    BoxShadow::new(px(0.), px(0.), color(ColorRole::Layer01)).spread_radius(px(1.)),
                ])
            });
            children.push(
                div()
                    .relative()
                    .flex_none()
                    .size(rem(side))
                    .bg(color(ColorRole::LayerAccent01))
                    // `.t3-thumb { overflow: hidden }`; the composer's thumb lets the × hang out.
                    .child(
                        div()
                            .absolute()
                            .inset_0()
                            .overflow_hidden()
                            .child(match open {
                                Some(f) => bare_button(
                                    key("open"),
                                    (texts.open)(&image.name),
                                    Fill {
                                        hover: None,
                                        focus: None,
                                    },
                                    |_| picture,
                                    Some(f),
                                    window,
                                    cx,
                                )
                                .size_full()
                                .into_any_element(),
                                None => picture,
                            })
                            .when(uploading, |this| {
                                // `.t3-thumb-over { background: rgba(22,22,22,.5) }` with a white spinner
                                // on a 30% white track (`.t3-thumb-over .t3-spin`).
                                this.child(
                                    div()
                                        .absolute()
                                        .inset_0()
                                        .flex()
                                        .items_center()
                                        .justify_center()
                                        .bg(upload_scrim(self.in_composer))
                                        .child(
                                            Spin::new(key("spin"))
                                                .label(texts.uploading.clone())
                                                .color(gpui_kit::white())
                                                .track(gpui_kit::white().opacity(0.3)),
                                        ),
                                )
                            }),
                    )
                    // `.t3-composer-ctx .t3-thumb { box-shadow: inset 0 0 0 1px border-subtle-01 }`.
                    .when(self.in_composer, |this| {
                        this.child(
                            div()
                                .absolute()
                                .inset_0()
                                .border_1()
                                .border_color(color(ColorRole::BorderSubtle01)),
                        )
                    })
                    // The × blocks the pointer, so pressing it never opens the thumbnail under it.
                    .child(
                        div()
                            .absolute()
                            .top(px(x_at))
                            .right(px(x_at))
                            .occlude()
                            .child(x),
                    )
                    .into_any_element(),
            );
        }
        for file in self.files {
            let mut chip = ContextChip::new(
                (
                    id.clone(),
                    SharedString::from(format!("file-{}", file.name)),
                ),
                file.kind,
                file.name.clone(),
            )
            .shape(ContextChipShape::Bubble)
            .in_composer(self.in_composer)
            .remove_label((texts.remove)(&file.name));
            if let Some(size) = file.size {
                chip = chip.meta(size);
            }
            // `onRemove={() => onRemove && onRemove(f.name)}`: every file chip
            // has its ×, with or without a handler (as the image thumbs do).
            let remove = self.on_remove.clone();
            let name = file.name.clone();
            chip = chip.on_remove(move |w, cx| {
                if let Some(f) = remove.as_ref() {
                    f(name.clone(), w, cx)
                }
            });
            children.push(chip.into_any_element());
        }
        // `.t3-attstrip { display: flex; flex-wrap: wrap; gap: spacing-03 }`; in the composer
        // `align-items: center`.
        div()
            .flex()
            .flex_row()
            .flex_wrap()
            .gap(rem(8.))
            .when(self.in_composer, |this| this.items_center())
            .children(children)
    }
}

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

    #[test]
    fn composer_thumbs_are_32px_with_a_hanging_x() {
        assert_eq!(thumb_geometry(false), (64., 2., 2.));
        assert_eq!(thumb_geometry(true), (32., -6., 1.));
    }

    /// What a strip's handlers were called with, in order.
    struct Strip {
        heard: Rc<std::cell::RefCell<Vec<String>>>,
    }

    impl gpui_kit::Render for Strip {
        fn render(&mut self, _: &mut Window, _: &mut gpui_kit::Context<Self>) -> impl IntoElement {
            let (opened, removed) = (self.heard.clone(), self.heard.clone());
            div().size_full().child(
                ComposerAttachmentStrip::new("strip", vec![AttachmentImage::new("a.png")], vec![])
                    .on_open(move |name, _, _| opened.borrow_mut().push(format!("open {name}")))
                    .on_remove(move |name, _, _| {
                        removed.borrow_mut().push(format!("remove {name}"));
                    }),
            )
        }
    }

    #[gpui_kit::test]
    fn a_thumbnail_opens_and_its_x_only_removes(cx: &mut gpui_kit::TestAppContext) {
        let heard = Rc::new(std::cell::RefCell::new(Vec::new()));
        let (_, window) = cx.add_window_view({
            let heard = heard.clone();
            |_, _| Strip { heard }
        });
        // The 64px thumb at the origin; its × 2px in from the top right corner.
        window.simulate_click(
            gpui_kit::point(px(16.), px(48.)),
            gpui_kit::Modifiers::none(),
        );
        window.simulate_click(
            gpui_kit::point(px(56.), px(8.)),
            gpui_kit::Modifiers::none(),
        );
        assert_eq!(*heard.borrow(), ["open a.png", "remove a.png"]);
    }

    #[test]
    fn upload_scrims_follow_the_css() {
        assert!((upload_scrim(false).a - 0.5).abs() < 0.01);
        assert!((upload_scrim(true).a - 0.55).abs() < 0.01);
    }
}