u0ui 0.3.0

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
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
//! Button: shadcn `Button` → Carbon `Button` / `IconButton` / `Link`.
//!
//! The reference component: every later component copies this layout.
//!
//! - **Behavior** is Kit's: [`gpui_kit::base::Button`] owns focus (tab stop),
//!   Enter/Space and pointer activation, the disabled state and accessibility.
//! - **Appearance** is Carbon's, computed per frame from a small [`Look`]
//!   (pure function of kind × state × theme, unit-tested against `bundle.css`)
//!   and [`Geometry`] (pure function of size × density × icon placement).
//! - **Motion**: background, border and text colors animate between states with
//!   [`Motion::HOVER`] (`.cds--btn { transition: background 70ms cubic-bezier(0, 0, 0.38, 0.9) }`).
//!
//! ```ignore
//! Button::new("create", "Create project")
//!     .icon(CarbonIcon::Add)
//!     .on_click(|_, _, _| println!("clicked"))
//! Button::new("delete", "Delete").variant(ButtonVariant::Destructive).size(ButtonSize::Md)
//! Button::new("add", "Add item").icon(CarbonIcon::Add).icon_only(true)
//! ```
use crate::{
    density::{self, Density},
    icons::{Icon, IconSize},
    motion::{self, Motion},
    theme::color,
    tokens::{ColorRole, TypeStyle},
    typography::TypeStyled as _,
};
use gpui_kit::{
    AnyElement, App, ClickEvent, CursorStyle, ElementId, FocusHandle, Hsla, InteractiveElement,
    IntoElement, MouseButton, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement,
    Styled, Window, div, prelude::FluentBuilder as _, px,
};
use std::{rc::Rc, time::Duration};

/// The variants of `index.d.ts` `ButtonProps.variant`: shadcn names plus Carbon's.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonVariant {
    /// shadcn `default` = Carbon `primary`.
    #[default]
    Default,
    /// Carbon `primary`.
    Primary,
    /// `secondary`.
    Secondary,
    /// shadcn `outline` = Carbon `tertiary`.
    Outline,
    /// Carbon `tertiary`.
    Tertiary,
    /// `ghost`.
    Ghost,
    /// shadcn `destructive` = Carbon `danger`.
    Destructive,
    /// Carbon `danger`.
    Danger,
    /// Carbon `danger-tertiary`.
    DangerTertiary,
    /// Carbon `danger-ghost`.
    DangerGhost,
    /// shadcn `link` = Carbon `Link`.
    Link,
}

impl ButtonVariant {
    /// Every variant, in `index.d.ts` order.
    pub const ALL: [ButtonVariant; 11] = [
        Self::Default,
        Self::Primary,
        Self::Secondary,
        Self::Outline,
        Self::Tertiary,
        Self::Ghost,
        Self::Destructive,
        Self::Danger,
        Self::DangerTertiary,
        Self::DangerGhost,
        Self::Link,
    ];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Primary => "primary",
            Self::Secondary => "secondary",
            Self::Outline => "outline",
            Self::Tertiary => "tertiary",
            Self::Ghost => "ghost",
            Self::Destructive => "destructive",
            Self::Danger => "danger",
            Self::DangerTertiary => "danger-tertiary",
            Self::DangerGhost => "danger-ghost",
            Self::Link => "link",
        }
    }

    /// The Carbon kind this variant draws (`KIND` in the reference `index.jsx`).
    pub const fn kind(self) -> ButtonKind {
        match self {
            Self::Default | Self::Primary => ButtonKind::Primary,
            Self::Secondary => ButtonKind::Secondary,
            Self::Outline | Self::Tertiary => ButtonKind::Tertiary,
            Self::Ghost => ButtonKind::Ghost,
            Self::Destructive | Self::Danger => ButtonKind::Danger,
            Self::DangerTertiary => ButtonKind::DangerTertiary,
            Self::DangerGhost => ButtonKind::DangerGhost,
            Self::Link => ButtonKind::Link,
        }
    }
}

/// Carbon button kinds (`.cds--btn--<kind>`), plus `Link` (`.cds--link`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ButtonKind {
    /// `.cds--btn--primary`
    Primary,
    /// `.cds--btn--secondary`
    Secondary,
    /// `.cds--btn--tertiary`
    Tertiary,
    /// `.cds--btn--ghost`
    Ghost,
    /// `.cds--btn--danger`
    Danger,
    /// `.cds--btn--danger--tertiary`
    DangerTertiary,
    /// `.cds--btn--danger--ghost`
    DangerGhost,
    /// `.cds--link`
    Link,
}

impl ButtonKind {
    /// The seven Carbon button kinds (without `Link`).
    pub const BUTTONS: [ButtonKind; 7] = [
        Self::Primary,
        Self::Secondary,
        Self::Tertiary,
        Self::Ghost,
        Self::Danger,
        Self::DangerTertiary,
        Self::DangerGhost,
    ];

    /// Filled kinds get the 1px `focus-inset` line inside the focus ring.
    const fn is_ghost(self) -> bool {
        matches!(self, Self::Ghost | Self::DangerGhost)
    }
}

/// `index.d.ts` `ButtonProps.size`; heights from `.cds--layout--size-*`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonSize {
    /// `sm`: 32px (`size-sm`).
    Sm,
    /// `md`: 40px (`size-md`).
    Md,
    /// `lg`: 48px (`size-lg`), the default.
    #[default]
    Lg,
    /// `xl`: 64px (`size-xl`).
    Xl,
    /// `2xl`: 80px (`size-2xl`).
    Xxl,
}

impl ButtonSize {
    /// Every size.
    pub const ALL: [ButtonSize; 5] = [Self::Sm, Self::Md, Self::Lg, Self::Xl, Self::Xxl];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Sm => "sm",
            Self::Md => "md",
            Self::Lg => "lg",
            Self::Xl => "xl",
            Self::Xxl => "2xl",
        }
    }

    /// Carbon height in px (`--layout-size-height-*`).
    pub const fn carbon_height(self) -> f32 {
        use crate::tokens::{SIZE_2XL, SIZE_LG, SIZE_MD, SIZE_SM, SIZE_XL};
        match self {
            Self::Sm => SIZE_SM.px_value(),
            Self::Md => SIZE_MD.px_value(),
            Self::Lg => SIZE_LG.px_value(),
            Self::Xl => SIZE_XL.px_value(),
            Self::Xxl => SIZE_2XL.px_value(),
        }
    }

    /// Height under `density`: compact shrinks every size but sm/xl/2xl to
    /// `density-field-md` (`[data-density="compact"] .cds--btn:not(.cds--btn--sm):not(.cds--btn--xl):not(.cds--btn--2xl)`).
    pub const fn height(self, density: Density) -> f32 {
        match (density, self) {
            (Density::Compact, Self::Md | Self::Lg) => density_field_md(),
            _ => self.carbon_height(),
        }
    }
}

const fn density_field_md() -> f32 {
    crate::tokens::DENSITY_FIELD_MD.px_value()
}

/// The interaction state a look is computed for.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ButtonState {
    /// Pointer over the button.
    pub hovered: bool,
    /// Pointer pressed on the button (`:active`).
    pub pressed: bool,
    /// Keyboard focus is on the button (`:focus-visible`): a button
    /// clicked keeps its focus but shows no ring.
    pub focused: bool,
    /// Disabled (or loading).
    pub disabled: bool,
}

/// The colors a button paints in one state.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Look {
    /// Fill.
    pub background: Hsla,
    /// 1px border (`border-width: 1px` on every kind).
    pub border: Hsla,
    /// Label color.
    pub text: Hsla,
    /// Icon color.
    pub icon: Hsla,
    /// Inner 1px focus line (`box-shadow: inset 0 0 0 1px focus`), if focused.
    pub focus_ring: Option<Hsla>,
    /// Innermost 1px line (`inset 0 0 0 2px background`; the `focus-inset` token).
    pub focus_inset: Option<Hsla>,
    /// Underline the label (Link hover/focus).
    pub underline: bool,
}

impl Look {
    /// The look of a selected button, which is also the trigger of an open
    /// popover or menu (Kit's `Selectable`). An icon-only button, and a ghost
    /// one, fill with `background-selected` (`.cds--btn--icon-only
    /// .cds--btn--selected`) while not hovered or pressed; Carbon gives a
    /// labelled filled or outlined button no selected state, so it holds its
    /// `:active` look while selected. A disabled button is never selected.
    pub fn selected(kind: ButtonKind, state: ButtonState, icon_only: bool) -> Look {
        if state.disabled {
            return Look::of(kind, state, icon_only);
        }
        if icon_only || kind == ButtonKind::Ghost {
            let mut look = Look::of(kind, state, icon_only);
            if !state.hovered && !state.pressed {
                look.background = color(ColorRole::BackgroundSelected);
            }
            return look;
        }
        Look::of(
            kind,
            ButtonState {
                pressed: true,
                ..state
            },
            icon_only,
        )
    }

    /// The Carbon look of `kind` in `state` on the active theme. Each value
    /// records the `bundle.css` rule it comes from.
    pub fn of(kind: ButtonKind, state: ButtonState, icon_only: bool) -> Look {
        use ColorRole::*;
        let clear = Hsla::transparent_black();
        let c = color;
        let mut look = match kind {
            // `.cds--btn--primary` etc.: filled, transparent border, text-on-color.
            ButtonKind::Primary | ButtonKind::Secondary | ButtonKind::Danger => {
                let (rest, hover, active) = match kind {
                    ButtonKind::Primary => (ButtonPrimary, ButtonPrimaryHover, ButtonPrimaryActive),
                    ButtonKind::Secondary => {
                        (ButtonSecondary, ButtonSecondaryHover, ButtonSecondaryActive)
                    }
                    _ => (ButtonDangerPrimary, ButtonDangerHover, ButtonDangerActive),
                };
                let background = if state.pressed {
                    c(active)
                } else if state.hovered {
                    c(hover)
                } else {
                    c(rest)
                };
                Look::plain(background, clear, c(TextOnColor))
            }
            // `.cds--btn--tertiary`: outlined in button-tertiary; hover fills
            // button-tertiary-hover; focus fills button-tertiary; active fills
            // button-tertiary-active with a transparent border; text-inverse on fills.
            ButtonKind::Tertiary => {
                if state.pressed {
                    Look::plain(c(ButtonTertiaryActive), clear, c(TextInverse))
                } else if state.focused {
                    Look::plain(c(ButtonTertiary), c(ButtonTertiary), c(TextInverse))
                } else if state.hovered {
                    Look::plain(c(ButtonTertiaryHover), c(ButtonTertiary), c(TextInverse))
                } else {
                    Look::plain(clear, c(ButtonTertiary), c(ButtonTertiary))
                }
            }
            // `.cds--btn--danger--tertiary`.
            ButtonKind::DangerTertiary => {
                if state.pressed {
                    Look::plain(c(ButtonDangerActive), c(ButtonDangerActive), c(TextOnColor))
                } else if state.focused {
                    Look::plain(
                        c(ButtonDangerPrimary),
                        c(ButtonDangerSecondary),
                        c(TextOnColor),
                    )
                } else if state.hovered {
                    Look::plain(c(ButtonDangerHover), c(ButtonDangerHover), c(TextOnColor))
                } else {
                    Look::plain(clear, c(ButtonDangerSecondary), c(ButtonDangerSecondary))
                }
            }
            // `.cds--btn--ghost`: link-primary text, background-hover/active
            // washes, link-primary-hover text on hover/press.
            //
            // The icon of a text + icon ghost follows the text: its paths take
            // `.cds--btn--ghost .cds--btn__icon path { fill: currentColor }`,
            // which beats the `svg` fill they would inherit. An icon-only ghost
            // draws its svg without `.cds--btn__icon`, so its paths inherit
            // `.cds--btn--ghost:not([disabled]) svg { fill: icon-primary }`
            // (reference render: text + icon #0f62fe, icon-only #161616).
            ButtonKind::Ghost => {
                let mut look = if state.pressed {
                    Look::plain(c(BackgroundActive), clear, c(LinkPrimaryHover))
                } else if state.hovered {
                    Look::plain(c(BackgroundHover), clear, c(LinkPrimaryHover))
                } else {
                    Look::plain(clear, clear, c(LinkPrimary))
                };
                if icon_only {
                    look.icon = c(IconPrimary);
                }
                look
            }
            // `.cds--btn--danger--ghost`.
            ButtonKind::DangerGhost => {
                if state.pressed {
                    Look::plain(c(ButtonDangerActive), clear, c(TextOnColor))
                } else if state.hovered {
                    Look::plain(c(ButtonDangerHover), clear, c(TextOnColor))
                } else {
                    Look::plain(clear, clear, c(ButtonDangerSecondary))
                }
            }
            // `.cds--link`: link-primary; hover link-primary-hover + underline;
            // focus 1px focus outline + underline.
            ButtonKind::Link => {
                let text = if state.hovered && !state.pressed {
                    c(LinkPrimaryHover)
                } else {
                    c(LinkPrimary)
                };
                let mut look = Look::plain(clear, clear, text);
                look.underline = state.hovered || state.focused || state.pressed;
                if state.focused || state.pressed {
                    look.border = c(Focus);
                }
                look
            }
        };

        if state.disabled {
            look = match kind {
                // `t3.css` global craft pass (gemini-A, "disabled buttons stay
                // legible"): `.cds--btn:disabled:not(.cds--btn--ghost)
                // :not(.cds--btn--icon-only) { background: layer-01; color:
                // text-secondary; box-shadow: inset 0 0 0 1px border-subtle-01;
                // border-color: transparent }`. It outranks the per-kind
                // `:disabled` rules, `.cds--btn--danger--ghost` included (that
                // class is not `.cds--btn--ghost`); the inset line is drawn as
                // the 1px border. Primary, secondary and danger are taken back
                // by the later r2-gal rule below.
                ButtonKind::Tertiary | ButtonKind::DangerTertiary | ButtonKind::DangerGhost
                    if !icon_only =>
                {
                    Look::plain(c(Layer01), c(BorderSubtle01), c(TextSecondary))
                }
                // `.cds--btn--tertiary:disabled`, `.cds--btn--danger--tertiary:disabled`.
                ButtonKind::Tertiary | ButtonKind::DangerTertiary => {
                    Look::plain(clear, c(ButtonDisabled), c(TextDisabled))
                }
                ButtonKind::Ghost | ButtonKind::DangerGhost | ButtonKind::Link => {
                    let mut look = Look::plain(clear, clear, c(TextDisabled));
                    if icon_only {
                        // `.cds--btn--ghost.cds--btn--icon-only[disabled] .cds--btn__icon`.
                        look.icon = c(IconOnColorDisabled);
                    }
                    look
                }
                // `.cds--btn:disabled`: button-disabled fill and border,
                // text-on-color-disabled. For text buttons this is the r2-gal
                // rule that follows the craft pass in `t3.css`: "disabled filled
                // buttons (primary, secondary, danger) use Carbon's disabled fill
                // so they never read as a tertiary or ghost" (`.cds--btn.cds--btn--
                // primary:disabled:not(.cds--btn--icon-only), … { background:
                // button-disabled; color: text-on-color-disabled; box-shadow:
                // none }`).
                _ => Look::plain(c(ButtonDisabled), c(ButtonDisabled), c(TextOnColorDisabled)),
            };
            return look;
        }

        if state.focused && kind != ButtonKind::Link {
            // `.cds--btn--<kind>:focus { border-color: focus; box-shadow: inset 0 0 0 1px
            // focus, inset 0 0 0 2px background }`; icon-only ghost drops the second line.
            look.border = c(Focus);
            look.focus_ring = Some(c(Focus));
            if !(icon_only && kind == ButtonKind::Ghost) {
                look.focus_inset = Some(c(FocusInset));
            }
        }
        look
    }

    fn plain(background: Hsla, border: Hsla, text: Hsla) -> Look {
        Look {
            background,
            border,
            text,
            icon: text,
            focus_ring: None,
            focus_inset: None,
            underline: false,
        }
    }
}

/// Box geometry in px at 100% zoom (converted to rems when drawn).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Geometry {
    /// Border-box height (`min-block-size: --layout-size-height-local`).
    pub height: f32,
    /// Fixed width (icon-only squares), else `None` (content width).
    pub width: Option<f32>,
    /// `padding-block-start`: `min((height − 1lh) / 2 − 1px, (48 − 18) / 2 − 1)`.
    pub padding_top: f32,
    /// `padding-inline-start`: `spacing-05 − 1px` = 15px.
    pub padding_left: f32,
    /// `padding-inline-end`: `3 × 16 + 16 − 1` = 63px when the button carries
    /// an icon, 16px when it is text-only (see [`TEXT_ONLY_END_PADDING`]),
    /// ghost 15px.
    pub padding_right: f32,
    /// Absolute icon offset from the padding box top (`inset-block-start` + margin),
    /// `None` when the icon flows inline (ghost, icon-only).
    pub icon_top: Option<f32>,
    /// Absolute icon offset from the padding box right (`inset-inline-end: 16px`).
    pub icon_right: f32,
}

/// body-compact-01 line box: 14px × 1.28572 = 18px.
const LINE: f32 = 18.;
/// `--layout-density-padding-inline-local`: spacing-05.
const PAD: f32 = 16.;
/// `--temp-padding-block-max`: (size-lg − 1lh) / 2 − 1px.
const PAD_BLOCK_MAX: f32 = (48. - LINE) / 2. - 1.;

/// `padding-inline-end` of a text-only, non-ghost button: `spacing-05`
/// (`t3.css` global craft pass: `.cds--btn:not(.cds--btn--icon-only)
/// :not(.cds--btn--ghost):not(:has(.cds--btn__icon)) { padding-inline-end:
/// var(--spacing-05) }`). Text-only buttons don't reserve Carbon's 63px icon slot.
pub const TEXT_ONLY_END_PADDING: f32 = PAD;

/// One turn of the loading spinner (`cu-spin 0.69s linear infinite`, as
/// `.t3-spin svg`); under reduced motion it pulses instead (see
/// [`crate::icons::spin_now`]).
pub const LOADING_SPIN_PERIOD: Duration = Duration::from_millis(690);

impl Geometry {
    /// Geometry for `size` under `density` of a button that carries an icon
    /// (Carbon's 63px end slot). Use [`Self::for_content`] for the geometry a
    /// [`Button`] actually draws.
    pub fn of(size: ButtonSize, density: Density, kind: ButtonKind, icon_only: bool) -> Self {
        let height = size.height(density);
        if icon_only {
            // `.cds--btn--icon-only { padding: 0; inline-size: block-size }`.
            return Self {
                height,
                width: Some(height),
                padding_top: 0.,
                padding_left: 0.,
                padding_right: 0.,
                icon_top: None,
                icon_right: 0.,
            };
        }
        if kind == ButtonKind::Link {
            return Self {
                height: LINE,
                width: None,
                padding_top: 0.,
                padding_left: 0.,
                padding_right: 0.,
                icon_top: None,
                icon_right: 0.,
            };
        }
        let padding_top = ((height - LINE) / 2. - 1.).min(PAD_BLOCK_MAX);
        // `.cds--btn--sm/md:not(.cds--btn--icon-only) .cds--btn__icon { margin-block-start: 0 }`.
        let icon_margin = if matches!(size, ButtonSize::Sm | ButtonSize::Md) {
            0.
        } else {
            1.
        };
        Self {
            height,
            width: None,
            padding_top,
            padding_left: PAD - 1.,
            padding_right: if kind.is_ghost() {
                PAD - 1.
            } else {
                PAD * 3. + 16. - 1.
            },
            // `inset-block-start: min((var(--layout-size-height-local) - 1rem) / 2
            // - 0.0625rem, var(--temp-padding-block-max))`. Compact density
            // shrinks `block-size` but not `--layout-size-height-local`, so the
            // icon keeps the Carbon size's offset: 12px into a compact 32px md
            // button, 16px into a compact lg one (the reference computes both).
            icon_top: (!kind.is_ghost())
                .then(|| ((size.carbon_height() - 16.) / 2. - 1.).min(PAD_BLOCK_MAX) + icon_margin),
            icon_right: PAD,
        }
    }

    /// The used flex base size of a `flex: <n>` (zero-basis) button: its
    /// inline padding plus the two 1px borders, since `box-sizing:
    /// border-box` cannot shrink the content box below zero.
    pub fn flex_basis(&self) -> f32 {
        self.padding_left + self.padding_right + 2.
    }

    /// Geometry of a button with or without an icon: a text-only button that
    /// is neither ghost nor icon-only ends at [`TEXT_ONLY_END_PADDING`]. The
    /// craft-pass rule excludes `.cds--btn--ghost` only, so a text-only
    /// danger ghost takes it too.
    pub fn for_content(
        size: ButtonSize,
        density: Density,
        kind: ButtonKind,
        icon_only: bool,
        has_icon: bool,
    ) -> Self {
        let mut geometry = Self::of(size, density, kind, icon_only);
        if !icon_only && !has_icon && !matches!(kind, ButtonKind::Ghost | ButtonKind::Link) {
            geometry.padding_right = TEXT_ONLY_END_PADDING;
        }
        geometry
    }
}

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

/// A Carbon button. See the module docs.
#[derive(IntoElement)]
pub struct Button {
    id: ElementId,
    label: SharedString,
    variant: ButtonVariant,
    size: ButtonSize,
    density: Option<Density>,
    icon: Option<Icon>,
    icon_color: Option<Hsla>,
    fill: Option<Hsla>,
    icon_only: bool,
    disabled: bool,
    loading: bool,
    selected: bool,
    full_width: bool,
    flex_share: Option<f32>,
    label_top: bool,
    wrap_label: bool,
    icon_size: IconSize,
    tooltip: bool,
    tooltip_align: super::floating::Align,
    tab_index: isize,
    focus_handle: Option<FocusHandle>,
    on_click: Option<ClickHandler>,
    trailing: Vec<AnyElement>,
}

impl Button {
    /// A button with a stable `id` and a `label` (the visible text, or the
    /// accessible name and tooltip when [`Self::icon_only`]).
    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            variant: ButtonVariant::Default,
            size: ButtonSize::Lg,
            density: None,
            icon: None,
            icon_color: None,
            fill: None,
            icon_only: false,
            disabled: false,
            loading: false,
            selected: false,
            full_width: false,
            flex_share: None,
            label_top: false,
            wrap_label: false,
            icon_size: IconSize::S16,
            tooltip: true,
            tooltip_align: super::floating::Align::Top,
            tab_index: 0,
            focus_handle: None,
            on_click: None,
            trailing: Vec::new(),
        }
    }

    /// Does nothing: text-only, non-ghost buttons end at
    /// [`TEXT_ONLY_END_PADDING`] by default. Kept one cycle for downstream
    /// callers; drop the call.
    #[doc(hidden)]
    #[deprecated(note = "no-op: text-only buttons end at TEXT_ONLY_END_PADDING by default")]
    pub fn balanced(self, _balanced: bool) -> Self {
        self
    }

    /// Content after the label inside the button, such as a shortcut key
    /// cap (`<Button>Approve<Kbd>⇧⌘↵</Kbd></Button>`).
    pub fn trailing(mut self, element: impl IntoElement) -> Self {
        self.trailing.push(element.into_any_element());
        self
    }

    /// The variant (shadcn or Carbon name).
    pub fn variant(mut self, variant: ButtonVariant) -> Self {
        self.variant = variant;
        self
    }

    /// The size (default `lg`).
    pub fn size(mut self, size: ButtonSize) -> Self {
        self.size = size;
        self
    }

    /// Override the app density for this button.
    pub fn density(mut self, density: Density) -> Self {
        self.density = Some(density);
        self
    }

    /// Paint the icon in `ink` while the button is enabled, for a context
    /// rule such as `.g-ptree-row .cds--btn--ghost svg { fill:
    /// var(--icon-secondary) }`. A disabled button keeps its disabled ink.
    pub fn icon_color(mut self, ink: Hsla) -> Self {
        self.icon_color = Some(ink);
        self
    }

    /// Paint the background `fill` at rest, hover and press alike while the
    /// button is enabled, for a context rule that outranks the kind's state
    /// fills, such as `body:has(.t3-approval--pending) .t3-gitctl
    /// .cds--btn.cds--btn--secondary { background: transparent }`. The focus
    /// ring is kept; a disabled button keeps its disabled fill.
    pub fn fill(mut self, fill: Hsla) -> Self {
        self.fill = Some(fill);
        self
    }

    /// An icon, drawn at the right edge (`.cds--btn__icon`).
    pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Draw only the icon in a square button; the label becomes the tooltip and
    /// accessible name. A `default` variant icon-only button is ghost
    /// (`index.jsx`: `kind = variant === 'default' ? 'ghost' : KIND[variant]`).
    pub fn icon_only(mut self, icon_only: bool) -> Self {
        self.icon_only = icon_only;
        self
    }

    /// Disable activation (`cursor: not-allowed`, disabled colors).
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Show a spinner in the icon slot and ignore activation.
    pub fn loading(mut self, loading: bool) -> Self {
        self.loading = loading;
        self
    }

    /// Selected, or the trigger of an open popover ([`Look::selected`]):
    /// an icon-only or ghost button fills with `background-selected`
    /// (`.cds--btn--icon-only.cds--btn--selected`), any other holds its
    /// pressed look.
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Fill the parent's width instead of hugging the label (`max-inline-size:
    /// none`, as in `.cds--modal-footer .cds--btn`).
    pub fn full_width(mut self, full_width: bool) -> Self {
        self.full_width = full_width;
        self
    }

    /// Share a flex row as CSS `flex: <weight>` on the button itself
    /// (`style: {flex: 1, maxWidth: 'none'}` in `.cu-pframe-foot`, the
    /// filter panel's Reset / Apply): grow by `weight` from a zero basis.
    /// A zero `flex-basis` on a `border-box` button still floors at its
    /// padding and border, so each button's share is that padding plus an
    /// equal part of the free space (see [`Geometry::flex_basis`]).
    pub fn flex_share(mut self, weight: f32) -> Self {
        self.flex_share = Some(weight);
        self
    }

    /// Drop the block padding so the label's line sits at the top of the
    /// box, under the 1px border (computed
    /// `.cds--actionable-notification__action-button`: `padding-block: 0`
    /// in a 32px box). Off by default: the label is centred by its size's
    /// block padding.
    pub fn label_top(mut self, top: bool) -> Self {
        self.label_top = top;
        self
    }

    /// The icon's size (default 16px). Carbon sizes an `IconButton`'s icon
    /// by the child the caller passes, such as `<Search size={20} />` in
    /// the UI shell header.
    pub fn icon_size(mut self, size: IconSize) -> Self {
        self.icon_size = size;
        self
    }

    /// Let a label that does not fit wrap onto more lines, the button growing
    /// from its size's height (`.cds--btn` sets `min-block-size` and no
    /// `white-space: nowrap`; a narrow `flex: 1` button in Carbon wraps).
    /// Off by default: one line, as wide as the label.
    pub fn wrap_label(mut self, wrap: bool) -> Self {
        self.wrap_label = wrap;
        self
    }

    /// Show the label as a Carbon tooltip when icon-only (default true; turn
    /// it off when the caller wraps the button in its own `Tooltip`).
    pub fn tooltip(mut self, tooltip: bool) -> Self {
        self.tooltip = tooltip;
        self
    }

    /// Where the icon-only tooltip sits (Carbon `IconButton` `align`, default
    /// `top`; header actions use `bottom`).
    pub fn tooltip_align(mut self, align: super::floating::Align) -> Self {
        self.tooltip_align = align;
        self
    }

    /// Focus traversal index (default 0).
    pub fn tab_index(mut self, tab_index: isize) -> Self {
        self.tab_index = tab_index;
        self
    }

    /// Use a caller-owned focus handle (to focus the button programmatically).
    pub fn track_focus(mut self, handle: &FocusHandle) -> Self {
        self.focus_handle = Some(handle.clone());
        self
    }

    /// Activation handler (pointer, Enter, Space).
    pub fn on_click(
        mut self,
        handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_click = Some(Rc::new(handler));
        self
    }

    /// The focus handle the button will track: the caller's
    /// ([`Self::track_focus`]) or the one kept under its id.
    pub fn focus_handle(&self, window: &mut Window, cx: &mut App) -> FocusHandle {
        self.focus_handle.clone().unwrap_or_else(|| {
            window
                .use_keyed_state((self.id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
                .read(cx)
                .clone()
        })
    }

    /// Whether the button is disabled.
    pub fn is_disabled(&self) -> bool {
        self.disabled
    }

    /// The Carbon kind actually drawn.
    pub fn kind(&self) -> ButtonKind {
        if self.icon_only && self.variant == ButtonVariant::Default {
            ButtonKind::Ghost
        } else {
            self.variant.kind()
        }
    }
}

/// Pointer state kept across frames for one button.
#[derive(Default)]
struct Pointer {
    hovered: bool,
    pressed: bool,
}

fn rem(px_value: f32) -> gpui_kit::Rems {
    gpui_kit::rems(px_value / crate::tokens::REM_PX)
}

/// Kit's `Selectable`, so a button can be the trigger of a Kit `Popover` or
/// menu: it is marked selected while its popup is open.
impl gpui_kit::base::Selectable for Button {
    fn selected(self, selected: bool) -> Self {
        Button::selected(self, selected)
    }

    fn is_selected(&self) -> bool {
        self.selected
    }
}

impl RenderOnce for Button {
    fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let kind = self.kind();
        let density = self.density.unwrap_or_else(density::current);
        let mut geometry = Geometry::for_content(
            self.size,
            density,
            kind,
            self.icon_only,
            self.icon.is_some() || self.loading,
        );
        if self.label_top {
            geometry.padding_top = 0.;
        }
        let id = self.id.clone();
        let pointer =
            window.use_keyed_state((id.clone(), "pointer"), cx, |_, _| Pointer::default());
        let focus = self.focus_handle(window, cx);
        let inert = self.disabled || self.loading;
        let state = {
            let p = pointer.read(cx);
            ButtonState {
                hovered: p.hovered && !inert,
                pressed: p.pressed && !inert,
                focused: crate::u0::primitives::parts::focus_visible(&focus, window),
                disabled: self.disabled,
            }
        };
        let mut look = if self.selected {
            Look::selected(kind, state, self.icon_only)
        } else {
            Look::of(kind, state, self.icon_only)
        };
        if let Some(ink) = self.icon_color.filter(|_| !self.disabled) {
            look.icon = ink;
        }
        if let Some(fill) = self.fill.filter(|_| !self.disabled) {
            look.background = fill;
        }
        let animate = |channel: &'static str, target: Hsla, window: &mut Window, cx: &mut App| {
            motion::animate_color((id.clone(), channel), target, Motion::HOVER, window, cx)
        };
        let background = animate("bg", look.background, window, cx);
        let border = animate("border", look.border, window, cx);
        let text = animate("text", look.text, window, cx);
        let icon_color = animate("icon", look.icon, window, cx);

        let icon = if self.loading {
            Some(
                crate::icons::spin_now(
                    Icon::named("progress_activity")
                        .size(IconSize::S16)
                        .color(icon_color),
                    LOADING_SPIN_PERIOD,
                    window,
                    cx,
                )
                .into_any_element(),
            )
        } else {
            self.icon.map(|icon| {
                icon.size(self.icon_size)
                    .color(icon_color)
                    .into_any_element()
            })
        };

        let on_click = self.on_click.clone();
        let label = self.label.clone();
        let mut button = gpui_kit::base::Button::new(id.clone())
            .track_focus(&focus)
            .tab_index(self.tab_index)
            .disabled(inert)
            .accessibility_label(label.clone())
            .relative()
            .flex()
            .flex_row()
            .flex_shrink_0()
            .whitespace_nowrap()
            .type_style(TypeStyle::BodyCompact01)
            .text_color(text)
            .bg(background)
            .border_1()
            .border_color(border)
            .cursor(if self.disabled {
                CursorStyle::OperationNotAllowed
            } else if self.loading {
                CursorStyle::Arrow
            } else {
                CursorStyle::PointingHand
            })
            .on_hover({
                let pointer = pointer.clone();
                move |hovered, _, cx| {
                    pointer.update(cx, |p, cx| {
                        if p.hovered != *hovered {
                            p.hovered = *hovered;
                            if !hovered {
                                p.pressed = false;
                            }
                            cx.notify();
                        }
                    })
                }
            })
            .on_mouse_down(MouseButton::Left, {
                let pointer = pointer.clone();
                move |_, _, cx| {
                    pointer.update(cx, |p, cx| {
                        p.pressed = true;
                        cx.notify();
                    })
                }
            })
            .on_mouse_up(MouseButton::Left, {
                let pointer = pointer.clone();
                move |_, _, cx| {
                    pointer.update(cx, |p, cx| {
                        p.pressed = false;
                        cx.notify();
                    })
                }
            })
            .on_mouse_up_out(MouseButton::Left, {
                let pointer = pointer.clone();
                move |_, _, cx| {
                    pointer.update(cx, |p, cx| {
                        if p.pressed {
                            p.pressed = false;
                            cx.notify();
                        }
                    })
                }
            })
            // Always a click listener, even without a handler: GPUI exposes
            // the accessibility Press action only on elements that have one.
            // A trigger Button (a Dialog's, a Popover's, a menu's) carries no
            // handler of its own: its wrapper opens the surface on the
            // pointer press. Without the action, VoiceOver, Voice Control and
            // other accessibility clients could not press it (design review
            // FG3C: "Open live dialog" never opened); with it, GPUI turns the
            // Press into a pointer press at the button's centre, which the
            // wrapper sees.
            .on_click({
                let focus = focus.clone();
                move |event, window, cx| {
                    let Some(handler) = &on_click else {
                        return;
                    };
                    // A press that removes the button (a dialog's Cancel, an
                    // attachment's Remove) keeps the Tab starting point there.
                    crate::u0::primitives::parts::vacate_focus(&focus, window, cx);
                    handler(event, window, cx)
                }
            });

        if kind == ButtonKind::Link {
            // `.cds--link { border: 0 }`: inline text, icon after it, no box,
            // one 18px line tall.
            button = button
                .items_center()
                .gap(rem(8.))
                .border_0()
                .when(look.underline, |this| this.underline())
                .child(label.clone())
                .when_some(icon, |this, icon| this.child(icon));
            // Focus: 1px `focus` outline around the text (`.cds--link:focus
            // { outline: 1px solid }`), outside the box so it takes no space.
            return button
                .when(state.focused || state.pressed, |this| {
                    this.child(
                        div()
                            .absolute()
                            .top(px(-1.))
                            .left(px(-1.))
                            .right(px(-1.))
                            .bottom(px(-1.))
                            .border_1()
                            .border_color(border),
                    )
                })
                .into_any_element();
        }

        let wrap_label = self.wrap_label && geometry.width.is_none();
        button = if wrap_label {
            // `min-block-size` with the block padding on both sides: one line
            // is exactly `height`, each further line adds its line height.
            button
                .min_h(rem(geometry.height))
                .pb(rem(geometry.padding_top))
                .whitespace_normal()
        } else {
            button.h(rem(geometry.height))
        };
        if let Some(width) = geometry.width {
            button = button
                .w(rem(width))
                .items_center()
                .justify_center()
                .when_some(icon, |this, icon| this.child(icon));
        } else {
            button = button
                .map(|this| match self.flex_share {
                    // `flex: <weight>; min-width: 0; max-width: none`.
                    Some(weight) => this
                        .flex_grow(weight)
                        .flex_shrink(1.)
                        .flex_basis(rem(geometry.flex_basis()))
                        .min_w_0(),
                    None if self.full_width => this.w_full(),
                    None => this.max_w(rem(320.)),
                })
                // Base centers its content; Carbon's label starts at the padding box.
                .justify_start()
                .items_start()
                .pt(rem(geometry.padding_top))
                .pl(rem(geometry.padding_left))
                .pr(rem(geometry.padding_right))
                .map(|this| {
                    if wrap_label {
                        this.min_w_0()
                            .child(div().flex_1().min_w_0().child(label.clone()))
                    } else {
                        this.child(label.clone())
                    }
                })
                .children(std::mem::take(&mut self.trailing));
            button = match (icon, geometry.icon_top) {
                // `.cds--btn .cds--btn__icon { position: absolute; inset-inline-end: 16px }`.
                (Some(icon), Some(top)) => button.child(
                    div()
                        .absolute()
                        .top(rem(top))
                        .right(rem(geometry.icon_right))
                        .size(rem(16.))
                        .child(icon),
                ),
                // `.cds--btn--ghost .cds--btn__icon { position: static; align-self: center; margin-inline-start: 0.5rem }`.
                (Some(icon), None) => button.child(
                    div()
                        .self_center()
                        .ml(rem(8.))
                        .mt(rem(-geometry.padding_top))
                        .size(rem(16.))
                        .child(icon),
                ),
                (None, _) => button,
            };
        }
        // Focus: inset 1px `focus`, then 1px `focus-inset` (box-shadow has no
        // inset form in GPUI, so two absolutely positioned outlines).
        let focus_layers: Vec<AnyElement> = [
            look.focus_ring.map(|c| (0., c)),
            look.focus_inset.map(|c| (1., c)),
        ]
        .into_iter()
        .flatten()
        .map(|(inset, c)| {
            div()
                .absolute()
                .top(px(inset))
                .left(px(inset))
                .right(px(inset))
                .bottom(px(inset))
                .border_1()
                .border_color(c)
                .into_any_element()
        })
        .collect();
        let button = button.children(focus_layers).into_any_element();
        if self.icon_only && self.tooltip {
            // `.cds--icon-tooltip`: the label as a Carbon tooltip (above by
            // default). Carbon `IconButton` defaults `closeOnActivation` to
            // true: a press hides it.
            return super::tooltip::Tooltip::new((id, "tooltip"), button)
                .content(label)
                .close_on_activation(true)
                .align(self.tooltip_align)
                .into_any_element();
        }
        button
    }
}

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

    #[test]
    fn wrapping_and_icon_size_are_opt_in() {
        let b = Button::new("b", "Apply filters");
        assert!(!b.wrap_label);
        assert_eq!(b.icon_size, IconSize::S16);
        let b = b.wrap_label(true).icon_size(IconSize::S20);
        assert!(b.wrap_label);
        assert_eq!(b.icon_size, IconSize::S20);
        // A wrapping `sm` button: one line is exactly the size's height (the
        // block padding on both sides), two lines add one line height:
        // Carbon's 50px "Apply filters" in a 94px `flex: 1` slot.
        let g = Geometry::for_content(
            ButtonSize::Sm,
            Density::Compact,
            ButtonKind::Primary,
            false,
            false,
        );
        assert_eq!(g.height, 32.);
        assert_eq!(2. * g.padding_top + LINE + 2., g.height);
        assert_eq!(2. * g.padding_top + 2. * LINE + 2., 50.);
    }

    #[test]
    fn the_loading_spinner_pulses_under_reduced_motion() {
        use crate::t3::base::spin_frame;
        assert_eq!(LOADING_SPIN_PERIOD, Duration::from_millis(690));
        // Turning: a quarter turn a quarter period in, fully opaque.
        let f = spin_frame(Duration::from_micros(172_500), LOADING_SPIN_PERIOD, false);
        assert!((f.turn - 0.25).abs() < 1e-3 && f.opacity == 1.);
        // Reduced: no rotation, `g-pulse 1.4s` 1 → 0.35 → 1.
        let f = spin_frame(Duration::from_millis(700), LOADING_SPIN_PERIOD, true);
        assert_eq!(f.turn, 0.);
        assert!((f.opacity - 0.35).abs() < 1e-4);
        let f = spin_frame(Duration::from_millis(1400), LOADING_SPIN_PERIOD, true);
        assert!((f.opacity - 1.).abs() < 1e-4);
    }

    fn hex(c: Hsla) -> String {
        let c = c.to_rgb();
        let b = |v: f32| (v * 255.).round() as u8;
        format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
    }

    #[test]
    fn variants_map_to_carbon_kinds() {
        assert_eq!(ButtonVariant::Default.kind(), ButtonKind::Primary);
        assert_eq!(ButtonVariant::Outline.kind(), ButtonKind::Tertiary);
        assert_eq!(ButtonVariant::Destructive.kind(), ButtonKind::Danger);
        assert_eq!(ButtonVariant::Link.kind(), ButtonKind::Link);
        let icon = Button::new("x", "Add").icon_only(true);
        assert_eq!(icon.kind(), ButtonKind::Ghost);
    }

    #[test]
    fn a_selected_button_reads_as_open() {
        use gpui_kit::base::Selectable as _;
        crate::theme::tests_set(CarbonTheme::White);
        let rest = ButtonState::default();
        let selected_fill = color(ColorRole::BackgroundSelected);
        assert_eq!(
            Look::selected(ButtonKind::Ghost, rest, true).background,
            selected_fill
        );
        assert_eq!(
            Look::selected(ButtonKind::Ghost, rest, false).background,
            selected_fill
        );
        // Hover wins over the selected fill, as before.
        let hover = ButtonState {
            hovered: true,
            ..rest
        };
        assert_eq!(
            Look::selected(ButtonKind::Ghost, hover, true).background,
            Look::of(ButtonKind::Ghost, hover, true).background
        );
        // A labelled primary holds its `:active` fill.
        assert_eq!(
            hex(Look::selected(ButtonKind::Primary, rest, false).background),
            "#002d9c"
        );
        let disabled = ButtonState {
            disabled: true,
            ..rest
        };
        assert_eq!(
            Look::selected(ButtonKind::Primary, disabled, false),
            Look::of(ButtonKind::Primary, disabled, false)
        );
        // Kit's `Selectable`: a popover marks its trigger open.
        let b = Button::new("b", "Add").open(true);
        assert!(b.is_open() && b.is_selected());
        assert!(!Button::new("b", "Add").is_selected());
    }

    #[test]
    fn primary_state_colors_follow_bundle_css() {
        crate::theme::tests_set(CarbonTheme::White);
        let rest = Look::of(ButtonKind::Primary, ButtonState::default(), false);
        assert_eq!(hex(rest.background), "#0f62fe");
        assert_eq!(hex(rest.text), "#ffffff");
        assert_eq!(rest.border.a, 0.);
        let hover = Look::of(
            ButtonKind::Primary,
            ButtonState {
                hovered: true,
                ..Default::default()
            },
            false,
        );
        assert_eq!(hex(hover.background), "#0050e6");
        let active = Look::of(
            ButtonKind::Primary,
            ButtonState {
                hovered: true,
                pressed: true,
                ..Default::default()
            },
            false,
        );
        assert_eq!(hex(active.background), "#002d9c");
        let focus = Look::of(
            ButtonKind::Primary,
            ButtonState {
                focused: true,
                ..Default::default()
            },
            false,
        );
        assert_eq!(hex(focus.border), "#0f62fe");
        assert_eq!(focus.focus_ring.map(hex).as_deref(), Some("#0f62fe"));
        assert_eq!(focus.focus_inset.map(hex).as_deref(), Some("#ffffff"));
        let disabled = Look::of(
            ButtonKind::Primary,
            ButtonState {
                disabled: true,
                focused: true,
                ..Default::default()
            },
            false,
        );
        // `t3.css` r2-gal: filled kinds keep Carbon's disabled fill.
        assert_eq!(hex(disabled.background), "#c6c6c6");
        assert_eq!(hex(disabled.text), "#8d8d8d");
        assert!(disabled.focus_ring.is_none());
    }

    #[test]
    fn disabled_outlined_kinds_follow_the_craft_pass() {
        let disabled = ButtonState {
            disabled: true,
            ..Default::default()
        };
        crate::theme::tests_set(CarbonTheme::G100);
        for kind in [
            ButtonKind::Tertiary,
            ButtonKind::DangerTertiary,
            ButtonKind::DangerGhost,
        ] {
            let look = Look::of(kind, disabled, false);
            // G100 layer-01 / text-secondary / border-subtle-01.
            assert_eq!(hex(look.background), "#262626", "{kind:?}");
            assert_eq!(hex(look.text), "#c6c6c6", "{kind:?}");
            assert_eq!(hex(look.border), "#525252", "{kind:?}");
        }
        // r2-gal: primary, secondary and danger keep button-disabled.
        for kind in [
            ButtonKind::Primary,
            ButtonKind::Secondary,
            ButtonKind::Danger,
        ] {
            let look = Look::of(kind, disabled, false);
            assert_eq!(
                look.background,
                color(ColorRole::ButtonDisabled),
                "{kind:?}"
            );
            assert_eq!(look.text, color(ColorRole::TextOnColorDisabled), "{kind:?}");
        }
        crate::theme::tests_set(CarbonTheme::White);
        let ghost = Look::of(ButtonKind::Ghost, disabled, false);
        assert_eq!(ghost.background.a, 0.);
        assert_eq!(hex(ghost.text), "#161616");
        assert!(ghost.text.a < 1.);
        // Icon-only filled buttons keep `.cds--btn:disabled`.
        let icon = Look::of(ButtonKind::Primary, disabled, true);
        assert_eq!(hex(icon.background), "#c6c6c6");
    }

    #[test]
    fn tertiary_and_ghost_states() {
        crate::theme::tests_set(CarbonTheme::White);
        let rest = Look::of(ButtonKind::Tertiary, ButtonState::default(), false);
        assert_eq!(rest.background.a, 0.);
        assert_eq!(hex(rest.border), "#0f62fe");
        assert_eq!(hex(rest.text), "#0f62fe");
        let hover = Look::of(
            ButtonKind::Tertiary,
            ButtonState {
                hovered: true,
                ..Default::default()
            },
            false,
        );
        assert_eq!(hex(hover.text), "#ffffff");
        let ghost = Look::of(ButtonKind::Ghost, ButtonState::default(), false);
        assert_eq!(hex(ghost.text), "#0f62fe");
        let focused_icon_ghost = Look::of(
            ButtonKind::Ghost,
            ButtonState {
                focused: true,
                ..Default::default()
            },
            true,
        );
        assert!(focused_icon_ghost.focus_ring.is_some());
        assert!(focused_icon_ghost.focus_inset.is_none());
    }

    /// Reference render (ScriptRow, ThemeEditor, Button): a text + icon ghost
    /// paints its icon in the text color, link-primary at rest and
    /// link-primary-hover on hover; an icon-only ghost keeps icon-primary.
    #[test]
    fn ghost_icon_follows_text_unless_icon_only() {
        let hovered = ButtonState {
            hovered: true,
            ..Default::default()
        };
        for (theme, link, link_hover, icon) in [
            (CarbonTheme::White, "#0f62fe", "#0043ce", "#161616"),
            (CarbonTheme::G100, "#78a9ff", "#a6c8ff", "#f4f4f4"),
        ] {
            crate::theme::tests_set(theme);
            let rest = Look::of(ButtonKind::Ghost, ButtonState::default(), false);
            assert_eq!(hex(rest.icon), link, "{theme:?}");
            assert_eq!(
                hex(Look::of(ButtonKind::Ghost, hovered, false).icon),
                link_hover
            );
            let only = Look::of(ButtonKind::Ghost, ButtonState::default(), true);
            assert_eq!(hex(only.icon), icon, "{theme:?}");
            assert_eq!(hex(Look::of(ButtonKind::Ghost, hovered, true).icon), icon);
        }
        // Every other kind is unchanged: its icon is its text color.
        crate::theme::tests_set(CarbonTheme::White);
        for kind in ButtonKind::BUTTONS.into_iter().chain([ButtonKind::Link]) {
            if kind == ButtonKind::Ghost {
                continue;
            }
            let look = Look::of(kind, ButtonState::default(), false);
            assert_eq!(look.icon, look.text, "{kind:?}");
        }
    }

    #[test]
    fn geometry_follows_bundle_css() {
        let lg = Geometry::of(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::Primary,
            false,
        );
        assert_eq!(lg.height, 48.);
        assert_eq!(
            (lg.padding_top, lg.padding_left, lg.padding_right),
            (14., 15., 63.)
        );
        assert_eq!(lg.icon_top, Some(15.));
        let sm = Geometry::of(
            ButtonSize::Sm,
            Density::Comfortable,
            ButtonKind::Primary,
            false,
        );
        assert_eq!(
            (sm.height, sm.padding_top, sm.icon_top),
            (32., 6., Some(7.))
        );
        let xl = Geometry::of(
            ButtonSize::Xl,
            Density::Comfortable,
            ButtonKind::Primary,
            false,
        );
        assert_eq!((xl.height, xl.padding_top), (64., 14.));
        let ghost = Geometry::of(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::Ghost,
            false,
        );
        assert_eq!((ghost.padding_right, ghost.icon_top), (15., None));
        let compact = Geometry::of(ButtonSize::Lg, Density::Compact, ButtonKind::Primary, false);
        assert_eq!((compact.height, compact.padding_top), (32., 6.));
        // The icon keeps the Carbon size's offset under compact density.
        assert_eq!(compact.icon_top, Some(15.));
        let compact_md = Geometry::of(ButtonSize::Md, Density::Compact, ButtonKind::Primary, false);
        assert_eq!((compact_md.height, compact_md.icon_top), (32., Some(11.)));
        let compact_xl = Geometry::of(ButtonSize::Xl, Density::Compact, ButtonKind::Primary, false);
        assert_eq!(compact_xl.height, 64.);
        // `t3.css` craft pass: text-only, non-ghost buttons end at spacing-05.
        let text_only = Geometry::for_content(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::Primary,
            false,
            false,
        );
        assert_eq!(text_only.padding_right, 16.);
        let with_icon = Geometry::for_content(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::Secondary,
            false,
            true,
        );
        assert_eq!(with_icon.padding_right, 63.);
        let ghost_text = Geometry::for_content(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::Ghost,
            false,
            false,
        );
        assert_eq!(ghost_text.padding_right, 15.);
        let danger_ghost_text = Geometry::for_content(
            ButtonSize::Lg,
            Density::Comfortable,
            ButtonKind::DangerGhost,
            false,
            false,
        );
        assert_eq!(danger_ghost_text.padding_right, 16.);
        let icon = Geometry::of(
            ButtonSize::Md,
            Density::Comfortable,
            ButtonKind::Ghost,
            true,
        );
        assert_eq!((icon.height, icon.width), (40., Some(40.)));
    }
}