gpui-component 0.6.0

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI components for GPUI.
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
use std::rc::Rc;

use gpui::{
    AnyElement, App, Axis, ClickEvent, ElementId, ImageSource, InteractiveElement as _,
    IntoElement, MouseButton, ObjectFit, ParentElement, RenderOnce, SharedString,
    StatefulInteractiveElement as _, StyleRefinement, Styled, StyledImage as _, Window, div, img,
    prelude::FluentBuilder as _, relative, rems,
};

use crate::{
    ActiveTheme as _, InteractiveElementExt as _, Sizable, Size, StyledExt as _, h_flex,
    shimmer::{ShimmerStyle, ShimmerText},
    v_flex,
};

/// The lifecycle status of an attachment.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum AttachmentStatus {
    /// The attachment has been selected and is waiting to be uploaded.
    Pending,
    /// The attachment is currently being uploaded.
    Uploading,
    /// The upload has completed and the attachment is being processed.
    Processing,
    /// The attachment failed to upload or process.
    Failed,
    /// The attachment is ready.
    #[default]
    Complete,
}

impl AttachmentStatus {
    /// Returns whether the attachment is waiting to start.
    pub fn is_pending(self) -> bool {
        matches!(self, Self::Pending)
    }

    /// Returns whether the attachment is being uploaded.
    pub fn is_uploading(self) -> bool {
        matches!(self, Self::Uploading)
    }

    /// Returns whether the attachment is being processed.
    pub fn is_processing(self) -> bool {
        matches!(self, Self::Processing)
    }

    /// Returns whether the attachment has failed.
    pub fn is_failed(self) -> bool {
        matches!(self, Self::Failed)
    }

    /// Returns whether the attachment is ready.
    pub fn is_complete(self) -> bool {
        matches!(self, Self::Complete)
    }

    /// Returns whether the attachment is in an in-progress state.
    pub fn is_in_progress(self) -> bool {
        matches!(self, Self::Uploading | Self::Processing)
    }
}

/// A file or image attachment composed from media, content, and actions slots.
#[derive(IntoElement)]
pub struct Attachment {
    id: Option<ElementId>,
    style: StyleRefinement,
    status: AttachmentStatus,
    size: Size,
    axis: Axis,
    media: Option<AttachmentMedia>,
    content: Option<AttachmentContent>,
    actions: Option<AttachmentActions>,
    on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
}

impl Attachment {
    /// Create an attachment in the [`AttachmentStatus::Complete`] state.
    pub fn new() -> Self {
        Self {
            id: None,
            style: StyleRefinement::default(),
            status: AttachmentStatus::Complete,
            size: Size::Medium,
            axis: Axis::Horizontal,
            media: None,
            content: None,
            actions: None,
            on_click: None,
        }
    }

    /// Set a stable identity for the whole-card click layer.
    pub fn id(mut self, id: impl Into<ElementId>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Make the whole card clickable, e.g. to open a preview.
    ///
    /// The click layer is painted below the actions slot, so action buttons
    /// stay independently clickable. Click state needs a stable identity, so
    /// the handler takes effect only together with [`Self::id`].
    pub fn on_click(
        mut self,
        handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_click = Some(Rc::new(handler));
        self
    }

    /// Set the attachment lifecycle status.
    pub fn status(mut self, status: AttachmentStatus) -> Self {
        self.status = status;
        self
    }

    /// Set the attachment layout axis.
    pub fn axis(mut self, axis: Axis) -> Self {
        self.axis = axis;
        self
    }

    /// Set the media slot.
    pub fn media(mut self, media: AttachmentMedia) -> Self {
        self.media = Some(media);
        self
    }

    /// Set the metadata content slot.
    pub fn content(mut self, content: AttachmentContent) -> Self {
        self.content = Some(content);
        self
    }

    /// Set the actions slot.
    pub fn actions(mut self, actions: AttachmentActions) -> Self {
        self.actions = Some(actions);
        self
    }
}

impl Default for Attachment {
    fn default() -> Self {
        Self::new()
    }
}

impl Sizable for Attachment {
    fn with_size(mut self, size: impl Into<Size>) -> Self {
        self.size = size.into();
        self
    }
}

impl Styled for Attachment {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Attachment {
    fn layout_slots(&mut self) {
        let size = self.size;
        let axis = self.axis;
        let status = self.status;

        self.media = self
            .media
            .take()
            .map(|media| media.layout(size, status, axis));
        self.content = self
            .content
            .take()
            .map(|content| content.layout(axis, status));
        self.actions = self
            .actions
            .take()
            .map(|actions| actions.layout_for_axis(axis));
    }
}

impl RenderOnce for Attachment {
    fn render(mut self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();
        let size = self.size;
        let axis = self.axis;
        let status = self.status;
        let has_media = self.media.is_some();
        let has_content = self.content.is_some();
        let clickable = self.id.is_some() && self.on_click.is_some();

        self.layout_slots();

        div()
            .relative()
            .flex()
            .flex_none()
            .max_w_full()
            .min_w_0()
            .rounded(if size == Size::XSmall {
                tokens.radius.xl
            } else {
                cx.theme().radius_2xl()
            })
            .border_1()
            .border_color(if status.is_failed() {
                tokens.colors.destructive.opacity(0.3)
            } else {
                tokens.colors.border
            })
            .when(status.is_pending(), |this| this.border_dashed())
            .bg(tokens.colors.background)
            .text_color(tokens.colors.foreground)
            // Register `hover` unconditionally: a conditionally registered
            // hover style stays cached when the condition later flips off.
            .hover(move |style| {
                if clickable {
                    style.bg(tokens.colors.muted.opacity(0.5))
                } else {
                    style
                }
            })
            .line_height(relative(1.25))
            .map(|this| attachment_size_style(this, size, has_media, has_content))
            .map(|this| match axis {
                Axis::Horizontal => this.min_w_40().items_center(),
                Axis::Vertical => this
                    .when(has_content, |this| this.w(rems(7.5)))
                    .when(!has_content, |this| this.w_24())
                    .flex_col()
                    .items_start(),
            })
            .when_some(self.media, |this, media| this.child(media))
            .when_some(self.content, |this, content| this.child(content))
            .when_some(self.id.zip(self.on_click), |this, (id, on_click)| {
                // The click layer is painted before the actions slot, so the
                // actions' hitboxes stay on top and their buttons keep working.
                this.child(
                    div()
                        .id(id)
                        .absolute()
                        .inset_0()
                        .on_click(move |event, window, cx| on_click(event, window, cx)),
                )
            })
            .when_some(self.actions, |this, actions| this.child(actions))
            .refine_style(&self.style)
    }
}

/// The media slot for an attachment.
///
/// Add an icon or another element as a child for an icon-style preview. Use
/// [`Self::src`] when the attachment has an image preview.
#[derive(IntoElement)]
pub struct AttachmentMedia {
    style: StyleRefinement,
    size: Option<Size>,
    status: AttachmentStatus,
    axis: Axis,
    source: Option<ImageSource>,
    children: Vec<AnyElement>,
}

impl AttachmentMedia {
    /// Create an empty media slot.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            size: None,
            status: AttachmentStatus::Complete,
            axis: Axis::Horizontal,
            source: None,
            children: Vec::new(),
        }
    }

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

    /// Add centered content above the preview without dimming it during loading.
    pub fn overlay(mut self, overlay: impl IntoElement) -> Self {
        self.children.push(
            div()
                .absolute()
                .inset_0()
                .flex()
                .items_center()
                .justify_center()
                .child(overlay)
                .into_any_element(),
        );
        self
    }

    fn layout(mut self, size: Size, status: AttachmentStatus, axis: Axis) -> Self {
        if self.size.is_none() {
            self.size = Some(size);
        }
        self.status = status;
        self.axis = axis;
        self
    }
}

impl Default for AttachmentMedia {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for AttachmentMedia {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Sizable for AttachmentMedia {
    fn with_size(mut self, size: impl Into<Size>) -> Self {
        self.size = Some(size.into());
        self
    }
}

impl Styled for AttachmentMedia {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentMedia {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();
        let resolved_size = self.size.unwrap_or_default();
        let radius = match resolved_size {
            Size::XSmall => tokens.radius.sm,
            Size::Small | Size::Medium | Size::Large | Size::Size(_) => tokens.radius.md,
        };
        let source = self.source;
        let has_source = source.is_some();
        let failed_media = self.status.is_failed() && !has_source;
        let dimmed_image = has_source
            && !matches!(
                self.status,
                AttachmentStatus::Pending | AttachmentStatus::Complete
            );
        let children = self.children;

        div()
            .relative()
            .flex()
            .flex_shrink_0()
            .items_center()
            .justify_center()
            .overflow_hidden()
            .when(self.axis == Axis::Horizontal, |this| match resolved_size {
                Size::XSmall => this.size_7(),
                Size::Small => this.size_8(),
                Size::Medium => this.size_10(),
                Size::Large => this.size_12(),
                Size::Size(size) => this.size(size),
            })
            .when(self.axis == Axis::Vertical, |this| {
                this.w_full().aspect_ratio(1.)
            })
            .rounded(radius)
            .bg(if failed_media {
                tokens.colors.destructive.opacity(0.1)
            } else {
                tokens.colors.muted
            })
            .text_color(if failed_media {
                tokens.colors.destructive
            } else {
                tokens.colors.foreground
            })
            .when_some(source, |this, source| {
                this.child(
                    img(source)
                        .absolute()
                        .inset_0()
                        .size_full()
                        .object_fit(ObjectFit::Cover)
                        .when(dimmed_image, |this| this.opacity(0.6)),
                )
            })
            .children(children)
            .refine_style(&self.style)
    }
}

/// The metadata slot for an attachment.
#[derive(IntoElement)]
pub struct AttachmentContent {
    style: StyleRefinement,
    vertical_layout: bool,
    children: Vec<AttachmentContentChild>,
}

enum AttachmentContentChild {
    Title(AttachmentTitle),
    Description(AttachmentDescription),
    Element(AnyElement),
}

impl AttachmentContent {
    /// Create an empty metadata slot.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            vertical_layout: false,
            children: Vec::new(),
        }
    }

    /// Add a title that automatically inherits the attachment lifecycle status.
    pub fn title(mut self, title: AttachmentTitle) -> Self {
        self.children.push(AttachmentContentChild::Title(title));
        self
    }

    /// Add a description that automatically inherits the attachment lifecycle status.
    pub fn description(mut self, description: AttachmentDescription) -> Self {
        self.children
            .push(AttachmentContentChild::Description(description));
        self
    }

    fn layout(mut self, axis: Axis, status: AttachmentStatus) -> Self {
        self.vertical_layout = axis == Axis::Vertical;

        for child in &mut self.children {
            match child {
                AttachmentContentChild::Title(title) => {
                    if title.status.is_none() {
                        title.status = Some(status);
                    }
                }
                AttachmentContentChild::Description(description) => {
                    if description.status.is_none() {
                        description.status = Some(status);
                    }
                }
                AttachmentContentChild::Element(_) => {}
            }
        }

        self
    }
}

impl Default for AttachmentContent {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for AttachmentContent {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children
            .extend(elements.into_iter().map(AttachmentContentChild::Element));
    }
}

impl Styled for AttachmentContent {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentContent {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        v_flex()
            .max_w_full()
            .min_w_0()
            .flex_1()
            .gap_0p5()
            .line_height(relative(1.25))
            .when(self.vertical_layout, |this| this.w_full().px_1())
            .children(self.children.into_iter().map(|child| match child {
                AttachmentContentChild::Title(title) => title.into_any_element(),
                AttachmentContentChild::Description(description) => description.into_any_element(),
                AttachmentContentChild::Element(element) => element,
            }))
            .refine_style(&self.style)
    }
}

/// A single-line attachment title.
#[derive(IntoElement)]
pub struct AttachmentTitle {
    style: StyleRefinement,
    text: SharedString,
    status: Option<AttachmentStatus>,
    shimmer_style: Option<ShimmerStyle>,
}

impl AttachmentTitle {
    /// Create an attachment title.
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self {
            style: StyleRefinement::default(),
            text: text.into(),
            status: None,
            shimmer_style: None,
        }
    }

    /// Override the attachment lifecycle status used for the loading shimmer.
    pub fn status(mut self, status: AttachmentStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Customize the shimmer used while this attachment is uploading or processing.
    pub fn with_shimmer_style(mut self, style: ShimmerStyle) -> Self {
        self.shimmer_style = Some(style);
        self
    }
}

impl Styled for AttachmentTitle {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentTitle {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let loading = self.status.is_some_and(AttachmentStatus::is_in_progress);

        div()
            .max_w_full()
            .min_w_0()
            .truncate()
            .font_medium()
            .map(|this| {
                if loading {
                    this.child(
                        ShimmerText::new(self.text).when_some(self.shimmer_style, |this, style| {
                            this.with_shimmer_style(style)
                        }),
                    )
                } else {
                    this.child(self.text)
                }
            })
            .refine_style(&self.style)
    }
}

/// A single-line attachment description or status message.
#[derive(IntoElement)]
pub struct AttachmentDescription {
    style: StyleRefinement,
    text: SharedString,
    status: Option<AttachmentStatus>,
}

impl AttachmentDescription {
    /// Create an attachment description.
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self {
            style: StyleRefinement::default(),
            text: text.into(),
            status: None,
        }
    }

    /// Set the status used for the semantic description color.
    pub fn status(mut self, status: AttachmentStatus) -> Self {
        self.status = Some(status);
        self
    }
}

impl Styled for AttachmentDescription {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentDescription {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();
        let color = self
            .status
            .is_some_and(AttachmentStatus::is_failed)
            .then(|| tokens.colors.destructive.opacity(0.8))
            .unwrap_or(tokens.colors.muted_foreground);

        div()
            .max_w_full()
            .min_w_0()
            .truncate()
            .text_xs()
            .line_height(relative(1.25))
            .text_color(color)
            .child(self.text)
            .refine_style(&self.style)
    }
}

/// A composition slot for attachment actions.
///
/// Add existing [`crate::button::Button`] or other controls as children. A
/// separate attachment-specific action wrapper is intentionally unnecessary.
#[derive(IntoElement)]
pub struct AttachmentActions {
    style: StyleRefinement,
    vertical_layout: bool,
    children: Vec<AnyElement>,
}

impl AttachmentActions {
    /// Create an empty actions slot.
    pub fn new() -> Self {
        Self {
            style: StyleRefinement::default(),
            vertical_layout: false,
            children: Vec::new(),
        }
    }

    fn layout_for_axis(mut self, axis: Axis) -> Self {
        self.vertical_layout = axis == Axis::Vertical;
        self
    }
}

impl Default for AttachmentActions {
    fn default() -> Self {
        Self::new()
    }
}

impl ParentElement for AttachmentActions {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for AttachmentActions {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentActions {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .relative()
            .flex()
            .flex_shrink_0()
            .items_center()
            .gap_1()
            .when(self.vertical_layout, |this| {
                this.absolute().top_3().right_3()
            })
            // The actions cluster owns its presses: an action (or the gap
            // between actions) must not also arm the whole-card click layer
            // below, mirroring the shadcn stacking where actions sit above
            // the trigger. Buttons run first in the bubble phase, so they
            // are unaffected.
            .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
            .children(self.children)
            .refine_style(&self.style)
    }
}

fn attachment_size_style<T: Styled + gpui::prelude::FluentBuilder>(
    element: T,
    size: Size,
    has_media: bool,
    has_content: bool,
) -> T {
    match size {
        Size::XSmall => element
            .gap_1p5()
            .text_xs()
            .when(has_content, |this| this.px_1p5().py_1())
            .when(has_media, |this| this.p_1()),
        Size::Small => element
            .gap_2p5()
            .text_xs()
            .when(has_content, |this| this.px_2().py_1p5())
            .when(has_media, |this| this.p_1p5()),
        Size::Medium => element
            .gap_2()
            .text_sm()
            .when(has_content, |this| this.px_2p5().py_2())
            .when(has_media, |this| this.p_2()),
        Size::Large => element
            .gap_3()
            .text_base()
            .when(has_content, |this| this.px_4().py_3())
            .when(has_media, |this| this.p_3()),
        Size::Size(value) => element
            .gap_1()
            .text_size(value * 0.875)
            .when(has_content || has_media, |this| this.p(value * 0.25)),
    }
}

/// A horizontally scrollable row of attachments.
#[derive(IntoElement)]
pub struct AttachmentGroup {
    id: ElementId,
    style: StyleRefinement,
    children: Vec<AnyElement>,
}

impl AttachmentGroup {
    /// Create an empty attachment group with a stable scroll identifier.
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            style: StyleRefinement::default(),
            children: Vec::new(),
        }
    }
}

impl ParentElement for AttachmentGroup {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Styled for AttachmentGroup {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for AttachmentGroup {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        h_flex()
            .id(self.id)
            .w_full()
            .min_w_0()
            .gap_3()
            .py_1()
            .overflow_x_scroll()
            .lock_scroll_axis()
            .refine_style(&self.style)
            .children(self.children)
    }
}

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

    #[test]
    fn test_attachment_builder() {
        let mut attachment = Attachment::new()
            .status(AttachmentStatus::Uploading)
            .axis(Axis::Vertical)
            .with_size(Size::Small)
            .media(AttachmentMedia::new().src("preview.png"))
            .content(
                AttachmentContent::new()
                    .title(AttachmentTitle::new("report.pdf"))
                    .description(AttachmentDescription::new("Uploading")),
            )
            .actions(AttachmentActions::new().child("Cancel"));

        assert_eq!(attachment.status, AttachmentStatus::Uploading);
        assert_eq!(attachment.axis, Axis::Vertical);
        assert_eq!(attachment.size, Size::Small);
        assert!(attachment.media.is_some());
        assert!(attachment.content.is_some());
        assert!(attachment.actions.is_some());

        attachment.layout_slots();
        assert_eq!(attachment.media.as_ref().unwrap().size, Some(Size::Small));
        assert_eq!(
            attachment.media.as_ref().unwrap().status,
            AttachmentStatus::Uploading
        );
        assert!(attachment.content.as_ref().unwrap().vertical_layout);
        assert!(attachment.actions.as_ref().unwrap().vertical_layout);
    }

    #[test]
    fn test_attachment_whole_card_click_builder() {
        assert!(Attachment::new().id.is_none());
        assert!(Attachment::new().on_click.is_none());

        let clickable = Attachment::new()
            .id("report-attachment")
            .on_click(|_, _, _| {});
        assert_eq!(clickable.id, Some("report-attachment".into()));
        assert!(clickable.on_click.is_some());
    }

    #[test]
    fn test_attachment_defaults_and_status_helpers() {
        assert_eq!(Attachment::new().status, AttachmentStatus::Complete);
        assert_eq!(AttachmentStatus::default(), AttachmentStatus::Complete);
        assert!(AttachmentStatus::Pending.is_pending());
        assert!(AttachmentStatus::Uploading.is_in_progress());
        assert!(AttachmentStatus::Processing.is_processing());
        assert!(AttachmentStatus::Failed.is_failed());
        assert!(AttachmentStatus::Complete.is_complete());
        assert!(!AttachmentStatus::Complete.is_in_progress());
    }

    #[test]
    fn test_attachment_slots_are_composable() {
        let media = AttachmentMedia::new().child("icon");
        assert_eq!(media.children.len(), 1);

        let content = AttachmentContent::new()
            .title(AttachmentTitle::new("name"))
            .description(AttachmentDescription::new("Details"))
            .child("Custom progress");
        assert_eq!(content.children.len(), 3);
        assert!(matches!(
            content.children[0],
            AttachmentContentChild::Title(_)
        ));
        assert!(matches!(
            content.children[1],
            AttachmentContentChild::Description(_)
        ));
        assert!(matches!(
            content.children[2],
            AttachmentContentChild::Element(_)
        ));

        let legacy = AttachmentContent::new().child(AttachmentTitle::new("legacy"));
        assert!(matches!(
            legacy.children[0],
            AttachmentContentChild::Element(_)
        ));

        let actions = AttachmentActions::new().child("remove");
        assert_eq!(actions.children.len(), 1);
    }

    #[test]
    fn test_attachment_typed_content_inherits_status() {
        let mut attachment = Attachment::new()
            .status(AttachmentStatus::Uploading)
            .content(
                AttachmentContent::new()
                    .title(AttachmentTitle::new("report.pdf"))
                    .description(AttachmentDescription::new("Uploading")),
            );

        attachment.layout_slots();

        let content = attachment.content.unwrap();
        let AttachmentContentChild::Title(title) = &content.children[0] else {
            panic!("expected the typed title slot");
        };
        assert_eq!(title.status, Some(AttachmentStatus::Uploading));

        let AttachmentContentChild::Description(description) = &content.children[1] else {
            panic!("expected the typed description slot");
        };
        assert_eq!(description.status, Some(AttachmentStatus::Uploading));
    }

    #[test]
    fn test_attachment_explicit_child_status_overrides_parent() {
        let mut attachment = Attachment::new().status(AttachmentStatus::Failed).content(
            AttachmentContent::new()
                .title(AttachmentTitle::new("report.pdf").status(AttachmentStatus::Processing))
                .description(
                    AttachmentDescription::new("Previous upload completed")
                        .status(AttachmentStatus::Complete),
                ),
        );

        attachment.layout_slots();

        let content = attachment.content.unwrap();
        let AttachmentContentChild::Title(title) = &content.children[0] else {
            panic!("expected the typed title slot");
        };
        assert_eq!(title.status, Some(AttachmentStatus::Processing));

        let AttachmentContentChild::Description(description) = &content.children[1] else {
            panic!("expected the typed description slot");
        };
        assert_eq!(description.status, Some(AttachmentStatus::Complete));
    }

    #[test]
    fn test_attachment_title_keeps_custom_shimmer_style() {
        let mut attachment = Attachment::new()
            .status(AttachmentStatus::Processing)
            .content(
                AttachmentContent::new().title(
                    AttachmentTitle::new("report.pdf")
                        .with_shimmer_style(ShimmerStyle::new().spread(0.45).reverse(true)),
                ),
            );

        attachment.layout_slots();

        let content = attachment.content.unwrap();
        let AttachmentContentChild::Title(title) = &content.children[0] else {
            panic!("expected the typed title slot");
        };
        assert_eq!(title.status, Some(AttachmentStatus::Processing));
        assert!(title.shimmer_style.is_some());
    }

    #[test]
    fn test_attachment_media_preview_keeps_children_and_overlays() {
        let media = AttachmentMedia::new()
            .src("preview.png")
            .child("Existing overlay")
            .overlay("Centered overlay");

        assert!(media.source.is_some());
        assert_eq!(media.children.len(), 2);
    }

    #[test]
    fn test_attachment_media_size_inherits_root_unless_explicit() {
        let inherited =
            AttachmentMedia::new().layout(Size::Small, AttachmentStatus::Complete, Axis::Vertical);
        assert_eq!(inherited.size, Some(Size::Small));
        assert_eq!(inherited.axis, Axis::Vertical);

        let explicit = AttachmentMedia::new().with_size(Size::XSmall).layout(
            Size::Large,
            AttachmentStatus::Failed,
            Axis::Horizontal,
        );
        assert_eq!(explicit.size, Some(Size::XSmall));
        assert_eq!(explicit.status, AttachmentStatus::Failed);
    }

    #[test]
    fn test_attachment_group_builder() {
        let group = AttachmentGroup::new("attachments")
            .child("First")
            .child("Second");

        assert_eq!(group.children.len(), 2);
    }

    mod click_dispatch {
        use std::{cell::Cell, rc::Rc};

        use gpui::{Context, Modifiers, Render, TestAppContext, point, px};

        use super::super::*;
        use crate::button::Button;

        struct AttachmentClickHarness {
            card_clicks: Rc<Cell<usize>>,
            action_clicks: Rc<Cell<usize>>,
        }

        impl Render for AttachmentClickHarness {
            fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
                let card_clicks = self.card_clicks.clone();
                let action_clicks = self.action_clicks.clone();

                Attachment::new()
                    .id("attachment")
                    .w(px(200.))
                    .h(px(60.))
                    .on_click(move |_, _, _| card_clicks.set(card_clicks.get() + 1))
                    .actions(
                        AttachmentActions::new().child(
                            Button::new("open")
                                .w(px(40.))
                                .h(px(40.))
                                .on_click(move |_, _, _| {
                                    action_clicks.set(action_clicks.get() + 1)
                                }),
                        ),
                    )
            }
        }

        #[gpui::test]
        fn whole_card_click_stays_below_the_actions(cx: &mut TestAppContext) {
            cx.update(crate::init);
            let card_clicks = Rc::new(Cell::new(0));
            let action_clicks = Rc::new(Cell::new(0));
            let (_, cx) = cx.add_window_view({
                let card_clicks = card_clicks.clone();
                let action_clicks = action_clicks.clone();
                move |_, _| AttachmentClickHarness {
                    card_clicks,
                    action_clicks,
                }
            });
            cx.update(|window, cx| window.draw(cx).clear(cx));

            // A click on an action must not also fire the whole-card handler.
            cx.simulate_click(point(px(20.), px(30.)), Modifiers::default());
            assert_eq!(action_clicks.get(), 1);
            assert_eq!(card_clicks.get(), 0);

            // A click elsewhere on the card fires the whole-card handler.
            cx.simulate_click(point(px(150.), px(30.)), Modifiers::default());
            assert_eq!(action_clicks.get(), 1);
            assert_eq!(card_clicks.get(), 1);
        }
    }
}