u0ui 0.3.2

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
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
//! T3 · Mobile chrome: [`PhoneFrame`], [`MobileAppBar`], [`MobileListSection`]
//! / [`ListRow`], [`MobileSheet`] and [`MobileTabDock`].
use super::{Handler, float_shadow, inset_frame};
use crate::{
    icons::{CarbonIcon, Icon},
    motion::{self, Curve, Motion, Speed},
    t3::craft::{self, focus_handle, focus_visible, press, rem, sans, sub},
    theme::color,
    tokens::ColorRole,
};
use gpui_kit::{
    AnyElement, App, AppContext as _, Bounds, BoxShadow, Element, ElementId, GlobalElementId,
    InspectorElementId, InteractiveElement, IntoElement, LayoutId, ParentElement, Pixels,
    RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window,
    component::{Sizable as _, Size, input::Input as KitInput, input::InputState},
    div,
    prelude::FluentBuilder as _,
    px, relative,
};
use std::{cell::Cell, rc::Rc};

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

// ── PhoneFrame ───────────────────────────────────────────────────────────

/// `.t3-phone { padding: 10px }`: the bezel width.
pub const PHONE_BEZEL: f32 = 10.;
/// `.t3-phone-screen { border-radius: 36px }`.
pub const PHONE_SCREEN_RADIUS: f32 = 36.;
/// `.t3-phone { border-radius: 2.75rem }`.
pub const PHONE_RADIUS: f32 = 44.;

/// A phone bezel for demos (`.t3-phone`): 22rem wide, 10px
/// `background-inverse` bezel with 44px corners, a black notch, and a
/// `background` screen with 36px corners and a 44px status-bar inset.
#[derive(IntoElement)]
pub struct PhoneFrame {
    height: f32,
    children: Vec<AnyElement>,
}

impl PhoneFrame {
    /// A frame `height` px tall (`height`, default 560).
    pub fn new() -> Self {
        Self {
            height: 560.,
            children: Vec::new(),
        }
    }

    /// The frame height in px.
    pub fn height(mut self, height: f32) -> Self {
        self.height = height;
        self
    }
}

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

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

thread_local! {
    /// How many [`PhoneFrame`] screens are laying out their children now.
    static SCREEN_DEPTH: Cell<u32> = const { Cell::new(0) };
}

/// Whether the component rendering now sits on a [`PhoneFrame`]'s screen.
/// Read it in `render` (GPUI renders components while their parent lays
/// out, so the frame marks its subtree then).
pub fn in_phone_screen() -> bool {
    SCREEN_DEPTH.with(|d| d.get() > 0)
}

fn with_phone_screen<R>(f: impl FnOnce() -> R) -> R {
    SCREEN_DEPTH.with(|d| d.set(d.get() + 1));
    struct Pop;
    impl Drop for Pop {
        fn drop(&mut self) {
            SCREEN_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
        }
    }
    let _pop = Pop;
    f()
}

/// The bottom corner radius a full-bleed child of the screen takes, in px:
/// [`PHONE_SCREEN_RADIUS`] on a [`PhoneFrame`]'s screen, else none.
///
/// `.t3-phone-screen { border-radius: 36px; overflow: hidden }` clips a
/// child to the screen's curve, but GPUI clips overflow to a rectangle, and
/// a rectangle's corner (10px in from the frame's) lies outside the bezel's
/// own 44px curve. A child that fills to the screen's bottom edge (a sheet,
/// a backdrop) rounds its own bottom corners to the screen's instead.
pub fn screen_bottom_radius() -> Option<f32> {
    in_phone_screen().then_some(PHONE_SCREEN_RADIUS)
}

/// The phone screen, laid out with [`in_phone_screen`] set.
struct ScreenScope(AnyElement);

impl Element for ScreenScope {
    type RequestLayoutState = ();
    type PrepaintState = ();

    fn id(&self) -> Option<ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, ()) {
        let child = &mut self.0;
        (with_phone_screen(|| child.request_layout(window, cx)), ())
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        _: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        let child = &mut self.0;
        with_phone_screen(|| child.prepaint(window, cx));
    }

    fn paint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        _: &mut (),
        _: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        let child = &mut self.0;
        with_phone_screen(|| child.paint(window, cx));
    }
}

impl RenderOnce for PhoneFrame {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .relative()
            .flex_none()
            .w(rem(352.))
            .h(rem(self.height))
            .p(px(PHONE_BEZEL))
            .rounded(rem(PHONE_RADIUS))
            .bg(color(ColorRole::BackgroundInverse))
            .shadow(float_shadow(2.))
            .child(ScreenScope(
                div()
                    .relative()
                    .flex()
                    .flex_col()
                    .size_full()
                    .overflow_hidden()
                    .pt(rem(44.))
                    .rounded(rem(PHONE_SCREEN_RADIUS))
                    .bg(color(ColorRole::Background))
                    .children(self.children)
                    .into_any_element(),
            ))
            // `.t3-phone-screen { border-radius: 36px; overflow: hidden }`:
            // GPUI clips overflow to a rectangle, so the screen's content (a
            // sheet's square corners) would paint over the rounded corners.
            // The bezel again, as a ring over the screen (inner edge: its
            // radius less the 10px border, within a pixel of the screen's
            // 36px curve), masks whatever lies between the two curves; what
            // lies past the bezel's outer curve, children keep clear of
            // themselves (see [`screen_bottom_radius`]).
            .child(
                div()
                    .absolute()
                    .top_0()
                    .left_0()
                    .size_full()
                    .border(px(PHONE_BEZEL))
                    .border_color(color(ColorRole::BackgroundInverse))
                    .rounded(rem(PHONE_RADIUS)),
            )
            // `.t3-phone-notch { top: 18px; 5.5rem × 1.5rem; radius 1rem; #000 }`.
            .child(
                div()
                    .absolute()
                    .top(px(18.))
                    .left_0()
                    .right_0()
                    .flex()
                    .justify_center()
                    .child(
                        div()
                            .w(rem(88.))
                            .h(rem(24.))
                            .rounded(rem(16.))
                            .bg(gpui_kit::black()),
                    ),
            )
    }
}

// ── MobileAppBar ─────────────────────────────────────────────────────────

/// The app bar's leading control (`leading`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Leading {
    /// `back`, the default: a chevron.
    #[default]
    Back,
    /// `menu`: a hamburger.
    Menu,
    /// Anything else: a 44px spacer.
    None,
}

/// A 44px round icon button in `link-primary` (`.t3-mbtn`); pressing
/// paints `background-hover`.
fn mbtn(
    id: ElementId,
    label: SharedString,
    icon: CarbonIcon,
    on_press: Option<Handler>,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    let focus = focus_handle(&id, window, cx);
    let ring = focus_visible(&focus, window);
    press(id, &focus)
        .accessibility_label(label)
        .flex_none()
        .justify_center()
        .size(rem(44.))
        .rounded_full()
        .active(|s| s.bg(color(ColorRole::BackgroundHover)))
        .when_some(on_press, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
        .child(
            Icon::carbon(icon)
                .size_length(rem(20.))
                .color(color(ColorRole::LinkPrimary)),
        )
        .when(ring, |this| this.child(craft::focus_ring()))
        .into_any_element()
}

/// One app bar action (`actions[]`).
#[derive(Clone)]
pub struct BarAction {
    /// `label` (the accessible name).
    pub label: SharedString,
    /// `icon`.
    pub icon: CarbonIcon,
    on_press: Option<Handler>,
}

impl BarAction {
    /// An action.
    pub fn new(label: impl Into<SharedString>, icon: CarbonIcon) -> Self {
        Self {
            label: label.into(),
            icon,
            on_press: None,
        }
    }

    /// Pressed.
    pub fn on_press(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_press = Some(Rc::new(f));
        self
    }
}

/// Side slot width of a phone nav bar with a leading button (`r3-mobB`):
/// 88px, or 52px with at most one action, so the title stays on the axis.
pub fn nav_slot(actions: usize) -> f32 {
    if actions > 1 { 88. } else { 52. }
}

/// The mobile top bar (`.t3-mbar`): a 44px row with Back / Menu, a centred
/// title and subtitle and icon actions; `large` moves the title to a 32px
/// heading under the row; `searchable` adds a search field.
#[derive(IntoElement)]
pub struct MobileAppBar {
    id: ElementId,
    title: SharedString,
    subtitle: Option<SharedString>,
    leading: Leading,
    actions: Vec<BarAction>,
    large: bool,
    searchable: bool,
    phone: bool,
    on_leading: Option<Handler>,
    back_label: SharedString,
    menu_label: SharedString,
    search_label: SharedString,
}

impl MobileAppBar {
    /// A bar titled `title`.
    pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            subtitle: None,
            leading: Leading::Back,
            actions: Vec::new(),
            large: false,
            searchable: false,
            phone: false,
            on_leading: None,
            back_label: "Back".into(),
            menu_label: "Menu".into(),
            search_label: "Search".into(),
        }
    }

    /// A subtitle under the title.
    pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// The leading control.
    pub fn leading(mut self, leading: Leading) -> Self {
        self.leading = leading;
        self
    }

    /// Trailing actions.
    pub fn actions(mut self, actions: impl IntoIterator<Item = BarAction>) -> Self {
        self.actions = actions.into_iter().collect();
        self
    }

    /// Large title (list pages).
    pub fn large(mut self, large: bool) -> Self {
        self.large = large;
        self
    }

    /// A search field under the title.
    pub fn searchable(mut self, searchable: bool) -> Self {
        self.searchable = searchable;
        self
    }

    /// The shared phone chrome (`[data-mb]`).
    pub fn phone(mut self, phone: bool) -> Self {
        self.phone = phone;
        self
    }

    /// Back / Menu pressed.
    pub fn on_leading(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_leading = Some(Rc::new(f));
        self
    }

    /// Accessible names of Back, Menu and the search field.
    pub fn labels(
        mut self,
        back: impl Into<SharedString>,
        menu: impl Into<SharedString>,
        search: impl Into<SharedString>,
    ) -> Self {
        self.back_label = back.into();
        self.menu_label = menu.into();
        self.search_label = search.into();
        self
    }
}

impl RenderOnce for MobileAppBar {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let lead = match self.leading {
            Leading::Back => Some((self.back_label.clone(), CarbonIcon::ChevronLeft)),
            Leading::Menu => Some((self.menu_label.clone(), CarbonIcon::Menu)),
            Leading::None => None,
        };
        let lead_el = match lead.clone() {
            Some((label, icon)) => mbtn(
                (id.clone(), "lead").into(),
                label,
                icon,
                self.on_leading.clone(),
                window,
                cx,
            ),
            None => div().flex_none().w(rem(44.)).into_any_element(),
        };
        let actions: Vec<AnyElement> = self
            .actions
            .iter()
            .enumerate()
            .map(|(i, a)| {
                mbtn(
                    sub(&id, "act", i),
                    a.label.clone(),
                    a.icon,
                    a.on_press.clone(),
                    window,
                    cx,
                )
            })
            .collect();
        let n_actions = actions.len();
        let title = (!self.large).then(|| {
            div()
                .flex()
                .flex_col()
                .flex_1()
                .min_w_0()
                .items_center()
                .text_center()
                .child(
                    sans(div(), 16., 600)
                        .max_w_full()
                        .when(self.phone, |d| {
                            d.overflow_hidden().whitespace_nowrap().text_ellipsis()
                        })
                        .text_color(color(ColorRole::TextPrimary))
                        .child(self.title.clone()),
                )
                .when_some(self.subtitle.clone(), |this, s| {
                    this.child(
                        craft::text(s, 12., ColorRole::TextSecondary)
                            .max_w_full()
                            .when(self.phone, |d| {
                                d.overflow_hidden().whitespace_nowrap().text_ellipsis()
                            }),
                    )
                })
        });
        let acts = div().flex().children(actions);
        let row = if self.phone && lead.is_some() {
            // `[data-mb] .t3-mbar-row:has(> .t3-mbtn:first-child) { grid: 88px 1fr 88px }`.
            let slot = nav_slot(n_actions);
            div()
                .flex()
                .items_center()
                .h(rem(44.))
                .child(div().flex_none().w(px(slot)).child(lead_el))
                .child(title.unwrap_or_else(|| div().flex_1()))
                .child(
                    div()
                        .flex()
                        .flex_none()
                        .w(px(slot))
                        .justify_end()
                        .child(acts),
                )
        } else {
            div()
                .flex()
                .items_center()
                .gap(rem(4.))
                .h(rem(44.))
                .child(lead_el)
                .child(title.unwrap_or_else(|| div().flex_1()))
                .child(acts)
        };
        let search = self.searchable.then(|| {
            let state = window
                .use_keyed_state((id.clone(), "search"), cx, |window, cx| {
                    let placeholder = self.search_label.clone();
                    cx.new(|cx| InputState::new(window, cx).placeholder(placeholder))
                })
                .read(cx)
                .clone();
            // `.t3-mbar-search { height: 36px; margin: 8px 8px 0; padding: 0 12px;
            // background: layer-accent-01 }`; `[data-mb]`: 40px, 12px top, `field-01`
            // with a `border-strong-01` rule.
            div()
                .flex()
                .items_center()
                .gap(rem(8.))
                .h(rem(if self.phone { 40. } else { 36. }))
                .mt(rem(if self.phone { 12. } else { 8. }))
                .mx(rem(8.))
                .px(rem(12.))
                .bg(color(if self.phone {
                    ColorRole::Field01
                } else {
                    ColorRole::LayerAccent01
                }))
                .when(self.phone, |d| {
                    d.border_b_1()
                        .border_color(color(ColorRole::BorderStrong01))
                })
                .child(
                    Icon::carbon(CarbonIcon::Search)
                        .size_length(rem(16.))
                        .color(color(ColorRole::IconSecondary)),
                )
                .child(
                    KitInput::new(&state)
                        .appearance(false)
                        .with_size(Size::XSmall)
                        .p_0()
                        .flex_1()
                        .min_w_0()
                        .text_size(rem(16.))
                        .text_color(color(ColorRole::TextPrimary)),
                )
        });
        div()
            .id(id)
            .flex()
            .flex_col()
            .px(rem(8.))
            .pb(rem(8.))
            .bg(color(ColorRole::Background))
            .child(row)
            .when(self.large, |this| {
                this.child(
                    sans(div(), 32., 600)
                        .px(rem(8.))
                        .when(self.phone, |d| d.line_height(rem(40.)))
                        .text_color(color(ColorRole::TextPrimary))
                        .child(self.title.clone()),
                )
            })
            .children(search)
    }
}

// ── MobileList ───────────────────────────────────────────────────────────

/// A list row's kind (`kind`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum RowKind {
    /// `navigation`, the default: value and chevron.
    #[default]
    Navigation,
    /// `toggle`: a switch.
    Toggle,
    /// `choice`: a check when chosen.
    Choice,
    /// `action`: link-colored text.
    Action,
}

impl RowKind {
    /// Every kind.
    pub const ALL: [RowKind; 4] = [Self::Navigation, Self::Toggle, Self::Choice, Self::Action];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Navigation => "navigation",
            Self::Toggle => "toggle",
            Self::Choice => "choice",
            Self::Action => "action",
        }
    }
}

/// Geometry of the mobile switch (`.t3-mswitch`): (track w, track h, knob,
/// inset, travel). Phone chrome uses Carbon toggle proportions (48×24).
pub fn switch_geometry(phone: bool) -> (f32, f32, f32, f32, f32) {
    if phone {
        (48., 24., 18., 3., 24.)
    } else {
        (48., 28., 24., 2., 20.)
    }
}

/// A grouped-list row (`.t3-mrowx`, at least 44px; 56px in phone chrome).
#[derive(IntoElement)]
pub struct ListRow {
    id: ElementId,
    icon: Option<CarbonIcon>,
    title: SharedString,
    subtitle: Option<SharedString>,
    value: Option<SharedString>,
    kind: RowKind,
    checked: bool,
    on: Option<bool>,
    default_on: bool,
    destructive: bool,
    on_press: Option<Handler>,
    on_toggle: Option<BoolHandler>,
    phone: bool,
    divider: Option<f32>,
}

impl ListRow {
    /// A navigation row titled `title`.
    pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            icon: None,
            title: title.into(),
            subtitle: None,
            value: None,
            kind: RowKind::Navigation,
            checked: false,
            on: None,
            default_on: false,
            destructive: false,
            on_press: None,
            on_toggle: None,
            phone: false,
            divider: None,
        }
    }

    /// A leading icon tile.
    pub fn icon(mut self, icon: CarbonIcon) -> Self {
        self.icon = Some(icon);
        self
    }

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

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

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

    /// Chosen (choice rows).
    pub fn checked(mut self, checked: bool) -> Self {
        self.checked = checked;
        self
    }

    /// Controlled switch state (toggle rows).
    pub fn on(mut self, on: bool) -> Self {
        self.on = Some(on);
        self
    }

    /// Initial switch state when uncontrolled (`defaultOn`).
    pub fn default_on(mut self, on: bool) -> Self {
        self.default_on = on;
        self
    }

    /// Destructive (`text-error`).
    pub fn destructive(mut self, destructive: bool) -> Self {
        self.destructive = destructive;
        self
    }

    /// Pressed (`onPress`; navigation, choice and action rows).
    pub fn on_press(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_press = Some(Rc::new(f));
        self
    }

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

    /// The shared phone chrome (`[data-mb]`).
    pub fn phone(mut self, phone: bool) -> Self {
        self.phone = phone;
        self
    }

    fn has_icon(&self) -> bool {
        self.icon.is_some()
    }
}

/// The start inset of the hairline above a row that follows another
/// (`.t3-mrowx + .t3-mrowx::before`): 52px after an icon tile, else 16px.
/// Phone chrome also counts an icon on the row above.
pub fn divider_inset(phone: bool, icon: bool, previous_icon: bool) -> f32 {
    if icon || (phone && previous_icon) {
        52.
    } else {
        16.
    }
}

impl RenderOnce for ListRow {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let ink = color(if self.destructive {
            ColorRole::TextError
        } else if self.kind == RowKind::Action {
            ColorRole::LinkPrimary
        } else {
            ColorRole::TextPrimary
        });
        let icon = self.icon.map(|icon| {
            // `.t3-mrowx-icon { 28×28; background: interactive; color: text-on-color }`;
            // phone: no tile, `icon-primary` (`text-error` when destructive), 20px wide.
            let (bg, fg) = if self.phone {
                (
                    None,
                    color(if self.destructive {
                        ColorRole::TextError
                    } else {
                        ColorRole::IconPrimary
                    }),
                )
            } else {
                (
                    Some(color(ColorRole::Interactive)),
                    color(ColorRole::TextOnColor),
                )
            };
            div()
                .flex()
                .flex_none()
                .items_center()
                .justify_center()
                .w(rem(if self.phone { 20. } else { 28. }))
                .h(rem(28.))
                .when_some(bg, |d, bg| d.bg(bg))
                .child(Icon::carbon(icon).size_length(rem(20.)).color(fg))
        });
        let text = div()
            .flex()
            .flex_col()
            .flex_1()
            .min_w_0()
            .when(self.phone, |d| d.gap(px(2.)))
            .child(self.title.clone())
            .when_some(self.subtitle.clone(), |d, s| {
                d.child(craft::text(s, 13., ColorRole::TextSecondary))
            });
        let value = |v: SharedString| craft::text(v, 16., ColorRole::TextSecondary);
        let focus = focus_handle(&id, window, cx);
        let ring = focus_visible(&focus, window);
        let row = sans(div(), 16., 400)
            .relative()
            .flex()
            .items_center()
            .gap(rem(12.))
            .w_full()
            .min_h(rem(if self.phone { 56. } else { 44. }))
            .px(rem(16.))
            .py(rem(8.))
            .text_color(ink)
            .children(icon)
            .child(text);
        let row = match self.kind {
            RowKind::Toggle => {
                let (on, store) = crate::components::form::controlled(
                    (id.clone(), "on"),
                    self.on,
                    self.default_on,
                    window,
                    cx,
                );
                let (w, h, knob, inset, travel) = switch_geometry(self.phone);
                // `.t3-mswitch { transition: background 110ms }`,
                // `span { transition: transform 110ms cubic-bezier(0.2,0,0.38,0.9) }`.
                let fade = Motion::new(Curve::ProductiveStandard, Speed::Fast02);
                let bg = motion::animate_color(
                    (id.clone(), "track"),
                    color(if on {
                        ColorRole::SupportSuccess
                    } else {
                        ColorRole::ToggleOff
                    }),
                    fade,
                    window,
                    cx,
                );
                let x = motion::animate(
                    (id.clone(), "knob"),
                    if on { travel } else { 0. },
                    fade.spatial(true),
                    window,
                    cx,
                );
                let on_toggle = self.on_toggle.clone();
                let phone = self.phone;
                row.child(
                    press(id.clone(), &focus)
                        .role(gpui_kit::Role::Switch)
                        .accessibility_label(self.title.clone())
                        .flex_none()
                        .w(rem(w))
                        .h(rem(h))
                        .rounded(rem(16.))
                        .bg(bg)
                        .on_click(move |_, window, cx| {
                            store.update(cx, |v, cx| {
                                *v = !on;
                                cx.notify();
                            });
                            if let Some(f) = &on_toggle {
                                f(!on, window, cx);
                            }
                        })
                        .child(
                            div()
                                .absolute()
                                .top(px(inset))
                                .left(px(inset + x))
                                .size(rem(knob))
                                .rounded_full()
                                .bg(gpui_kit::white())
                                // `box-shadow: 0 1px 2px rgba(0,0,0,.3)`; none in phone chrome.
                                .when(!phone, |d| {
                                    d.shadow(vec![
                                        BoxShadow::new(
                                            px(0.),
                                            px(1.),
                                            gpui_kit::black().opacity(0.3),
                                        )
                                        .blur_radius(px(2.)),
                                    ])
                                }),
                        )
                        .when(ring, |this| this.child(craft::focus_ring())),
                )
                .into_any_element()
            }
            kind => {
                let right: Option<AnyElement> = match kind {
                    RowKind::Navigation => Some(
                        div()
                            .flex()
                            .items_center()
                            .gap(rem(12.))
                            .children(self.value.clone().map(value))
                            .child(
                                Icon::carbon(CarbonIcon::ChevronRight)
                                    .size_length(rem(16.))
                                    .color(color(ColorRole::IconSecondary)),
                            )
                            .into_any_element(),
                    ),
                    RowKind::Choice => self.checked.then(|| {
                        Icon::carbon(CarbonIcon::Checkmark)
                            .size_length(rem(20.))
                            .color(color(ColorRole::Interactive))
                            .into_any_element()
                    }),
                    _ => self.value.clone().map(|v| value(v).into_any_element()),
                };
                let on_press = self.on_press.clone();
                press(id.clone(), &focus)
                    .when(kind == RowKind::Choice, |b| {
                        b.role(gpui_kit::Role::MenuItemRadio)
                    })
                    .accessibility_label(self.title.clone())
                    .w_full()
                    .active(|s| s.bg(color(ColorRole::LayerHover01)))
                    .when_some(on_press, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
                    .child(row.children(right))
                    .when(ring, |this| this.child(craft::focus_ring()))
                    .into_any_element()
            }
        };
        div()
            .relative()
            .w_full()
            .child(row)
            .when_some(self.divider, |this, left| {
                this.child(
                    div()
                        .absolute()
                        .top_0()
                        .left(rem(left))
                        .right_0()
                        .h(px(1.))
                        .bg(color(if self.phone {
                            ColorRole::BorderSubtle00
                        } else {
                            ColorRole::BorderSubtle01
                        })),
                )
            })
    }
}

/// A grouped list section (`.t3-mlist`): uppercase 12px heading, a square
/// `layer-01` card of [`ListRow`]s with hairlines between them, an optional
/// footer.
#[derive(IntoElement)]
pub struct MobileListSection {
    title: SharedString,
    footer: Option<SharedString>,
    rows: Vec<ListRow>,
    raised: bool,
    phone: bool,
}

impl MobileListSection {
    /// A section titled `title` (empty for none).
    pub fn new(title: impl Into<SharedString>) -> Self {
        Self {
            title: title.into(),
            footer: None,
            rows: Vec::new(),
            raised: false,
            phone: false,
        }
    }

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

    /// Add a row.
    pub fn row(mut self, row: ListRow) -> Self {
        self.rows.push(row);
        self
    }

    /// Add rows.
    pub fn rows(mut self, rows: impl IntoIterator<Item = ListRow>) -> Self {
        self.rows.extend(rows);
        self
    }

    /// Paint the card `layer-02` (inside a sheet: `.t3-msheet-body
    /// .t3-mlist-card`, or a `[data-grouped]` phone page).
    pub fn raised(mut self, raised: bool) -> Self {
        self.raised = raised;
        self
    }

    /// The shared phone chrome (`[data-mb]`).
    pub fn phone(mut self, phone: bool) -> Self {
        self.phone = phone;
        self
    }
}

impl RenderOnce for MobileListSection {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let phone = self.phone;
        let mut previous_icon = false;
        let rows: Vec<ListRow> = self
            .rows
            .into_iter()
            .enumerate()
            .map(|(i, mut row)| {
                row.phone = row.phone || phone;
                let icon = row.has_icon();
                if i > 0 {
                    row.divider = Some(divider_inset(row.phone, icon, previous_icon));
                }
                previous_icon = icon;
                row
            })
            .collect();
        div()
            .flex()
            .flex_col()
            .px(rem(16.))
            .when(!phone, |d| d.pb(rem(24.)))
            .child(if self.title.is_empty() {
                // An empty `.t3-mlist-h` has no line box: only its 8px
                // bottom padding stays (`height: 8px`).
                div().pb(rem(MLIST_HEADING_GAP))
            } else {
                // `.t3-mlist-h { padding: 0 0 8px; 12px uppercase text-secondary }`.
                craft::text(self.title.to_uppercase(), 12., ColorRole::TextSecondary)
                    .pb(rem(MLIST_HEADING_GAP))
            })
            .child(
                div()
                    .flex()
                    .flex_col()
                    .overflow_hidden()
                    .bg(color(if self.raised {
                        ColorRole::Layer02
                    } else {
                        ColorRole::Layer01
                    }))
                    .children(rows),
            )
            .when_some(self.footer, |this, f| {
                this.child(craft::text(f, 12., ColorRole::TextSecondary).pt(rem(8.)))
            })
    }
}

// ── MobileSheet ──────────────────────────────────────────────────────────

/// `.t3-mlist-h { padding-bottom: spacing-03 }` (also the whole height of
/// an empty heading).
pub const MLIST_HEADING_GAP: f32 = 8.;
/// `.t3-msheet-intro { padding-bottom: spacing-04 }`.
pub const SHEET_INTRO_GAP: f32 = 12.;
/// The intro `<p>`'s line height (`line-height: 1.5`, 21px at 14px).
pub const SHEET_INTRO_LINE: f32 = 1.5;
/// `.t3-msheet-grab { height: 5px; margin-top: spacing-03 }` plus the 44px
/// head: where the intro (or, without one, the body) starts.
pub const SHEET_HEAD_HEIGHT: f32 = 8. + 5. + 44.;

/// A bottom sheet (`.t3-msheet`, square per `gemini-MA`): grabber, a
/// three-column head (Close · title · primary), an optional centred intro
/// and the body (its list cards paint `layer-02`).
#[derive(IntoElement)]
pub struct MobileSheet {
    id: ElementId,
    title: SharedString,
    intro: Option<SharedString>,
    primary: Option<SharedString>,
    inline: bool,
    on_close: Option<Handler>,
    on_primary: Option<Handler>,
    close_label: SharedString,
    children: Vec<AnyElement>,
}

impl MobileSheet {
    /// A sheet titled `title`.
    pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            intro: None,
            primary: None,
            inline: true,
            on_close: None,
            on_primary: None,
            close_label: "Close".into(),
            children: Vec::new(),
        }
    }

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

    /// The primary action text (e.g. `Start`).
    pub fn primary(mut self, primary: impl Into<SharedString>) -> Self {
        self.primary = Some(primary.into());
        self
    }

    /// Sit at the bottom of its column (`inline`, default true: `margin-top: auto`).
    pub fn inline(mut self, inline: bool) -> Self {
        self.inline = inline;
        self
    }

    /// Close pressed.
    pub fn on_close(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_close = Some(Rc::new(f));
        self
    }

    /// Primary pressed.
    pub fn on_primary(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_primary = Some(Rc::new(f));
        self
    }

    /// The close button's name (`Close`).
    pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
        self.close_label = label.into();
        self
    }
}

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

impl RenderOnce for MobileSheet {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let close = mbtn(
            (id.clone(), "close").into(),
            self.close_label.clone(),
            CarbonIcon::Close,
            self.on_close.clone(),
            window,
            cx,
        );
        let primary = match self.primary.clone() {
            Some(label) => {
                let pid: ElementId = (id.clone(), "primary").into();
                let focus = focus_handle(&pid, window, cx);
                let ring = focus_visible(&focus, window);
                let on_primary = self.on_primary.clone();
                sans(press(pid, &focus), 16., 600)
                    .h(rem(44.))
                    .px(rem(12.))
                    .text_color(color(ColorRole::LinkPrimary))
                    .when_some(on_primary, |b, f| b.on_click(move |_, w, cx| f(w, cx)))
                    .child(label)
                    .when(ring, |b| b.child(craft::focus_ring()))
                    .into_any_element()
            }
            None => div().w(rem(44.)).into_any_element(),
        };
        div()
            .id(id)
            .flex()
            .flex_col()
            .w_full()
            .when(self.inline, |d| d.mt_auto())
            // Square (`gemini-MA`), but on a phone screen its bottom
            // corners follow the screen's curve.
            .when_some(screen_bottom_radius(), |d, r| d.rounded_b(rem(r)))
            .bg(color(ColorRole::Layer01))
            // `box-shadow: 0 -2px 6px var(--shadow)`.
            .shadow(float_shadow(-2.))
            .child(
                div()
                    .mt(rem(8.))
                    .mx_auto()
                    .w(rem(36.))
                    .h(px(5.))
                    .rounded(px(3.))
                    .bg(color(ColorRole::BorderStrong01)),
            )
            .child(
                // `grid-template-columns: 1fr auto 1fr`.
                div()
                    .flex()
                    .items_center()
                    .px(rem(8.))
                    .child(div().flex().flex_1().flex_basis(px(0.)).child(close))
                    .child(
                        sans(div(), 16., 600)
                            .flex_none()
                            .text_color(color(ColorRole::TextPrimary))
                            .child(self.title.clone()),
                    )
                    .child(
                        div()
                            .flex()
                            .flex_1()
                            .flex_basis(px(0.))
                            .justify_end()
                            .child(primary),
                    ),
            )
            .when_some(self.intro.clone(), |this, intro| {
                this.child(
                    // `.t3-msheet-intro { padding: 0 spacing-06 spacing-04 }`;
                    // a `<p>`, so `line-height: 1.5` (21px, computed).
                    craft::text(intro, 14., ColorRole::TextSecondary)
                        .line_height(relative(SHEET_INTRO_LINE))
                        .px(rem(24.))
                        .pb(rem(SHEET_INTRO_GAP))
                        .text_center(),
                )
            })
            .child(div().pb(rem(32.)).children(self.children))
    }
}

// ── MobileTabDock ────────────────────────────────────────────────────────

/// One dock tab (`tabs[]`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DockTab {
    /// `id`.
    pub id: SharedString,
    /// `label`.
    pub label: SharedString,
    /// `icon`.
    pub icon: CarbonIcon,
    /// `badge`: the needs-you count.
    pub badge: Option<usize>,
}

impl DockTab {
    /// A tab.
    pub fn new(
        id: impl Into<SharedString>,
        label: impl Into<SharedString>,
        icon: CarbonIcon,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon,
            badge: None,
        }
    }

    /// A count badge.
    pub fn badge(mut self, badge: usize) -> Self {
        self.badge = Some(badge);
        self
    }
}

/// The bottom tab dock (`.t3-dock`): a floating square bar on `layer-02`
/// with the floating shadow; the current tab is tinted `layer-selected-01`
/// in `link-primary`. Phone chrome makes it a flat full-width 56px bar with
/// a home indicator. Badges are the yellow needs-you count.
#[derive(IntoElement)]
pub struct MobileTabDock {
    id: ElementId,
    tabs: Vec<DockTab>,
    value: Option<SharedString>,
    on_change: Option<ValueHandler>,
    phone: bool,
}

impl MobileTabDock {
    /// A dock.
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            tabs: Vec::new(),
            value: None,
            on_change: None,
            phone: false,
        }
    }

    /// The tabs.
    pub fn tabs(mut self, tabs: impl IntoIterator<Item = DockTab>) -> Self {
        self.tabs = tabs.into_iter().collect();
        self
    }

    /// Controlled current tab id.
    pub fn value(mut self, value: impl Into<SharedString>) -> Self {
        self.value = Some(value.into());
        self
    }

    /// A tab was chosen (`onValueChange`).
    pub fn on_value_change(
        mut self,
        f: impl Fn(SharedString, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_change = Some(Rc::new(f));
        self
    }

    /// The shared phone chrome (`[data-mb]`).
    pub fn phone(mut self, phone: bool) -> Self {
        self.phone = phone;
        self
    }
}

/// The home indicator (`[data-mb] .mb-home`, `.t3-dock::after`): 134×5,
/// radius 3, `text-primary`, 8px above the bottom edge.
pub fn home_indicator() -> AnyElement {
    div()
        .absolute()
        .bottom(px(8.))
        .left_0()
        .right_0()
        .flex()
        .justify_center()
        .child(
            div()
                .w(px(134.))
                .h(px(5.))
                .rounded(px(3.))
                .bg(color(ColorRole::TextPrimary)),
        )
        .into_any_element()
}

impl RenderOnce for MobileTabDock {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let first = self.tabs.first().map(|t| t.id.clone()).unwrap_or_default();
        let (current, store) = crate::components::form::controlled(
            (id.clone(), "value"),
            self.value.clone(),
            first,
            window,
            cx,
        );
        let phone = self.phone;
        let tabs = self.tabs.iter().enumerate().map(|(i, t)| {
            let on = t.id == current;
            let tid = sub(&id, "tab", i);
            let focus = focus_handle(&tid, window, cx);
            let ring = focus_visible(&focus, window);
            let ink = color(if !on {
                ColorRole::TextSecondary
            } else if phone {
                ColorRole::Interactive
            } else {
                ColorRole::LinkPrimary
            });
            let value = t.id.clone();
            let store = store.clone();
            let on_change = self.on_change.clone();
            // Phone: `top: 3px; left: calc(50% + 6px)`, 18px tall, a 2px
            // `background` ring; card: `top: 2px; right: 0.9rem`, 16px.
            let badge = t.badge.map(|n| {
                let b = sans(
                    div(),
                    if phone { 11. } else { 10. },
                    if phone { 600 } else { 400 },
                )
                .absolute()
                .min_w(rem(if phone { 18. } else { 16. }))
                .px(px(if phone { 5. } else { 4. }))
                .h(rem(if phone { 18. } else { 16. }))
                .line_height(rem(if phone { 18. } else { 16. }))
                .rounded(rem(if phone { 9. } else { 8. }))
                .text_center()
                .bg(color(ColorRole::SupportWarning))
                .text_color(crate::t3::base::ink())
                .child(n.to_string());
                if phone {
                    b.top(px(3.)).left(relative(0.5)).ml(px(6.)).shadow(vec![
                        BoxShadow::new(px(0.), px(0.), color(ColorRole::Background))
                            .spread_radius(px(2.)),
                    ])
                } else {
                    b.top(px(2.)).right(rem(14.4))
                }
            });
            sans(
                press(tid, &focus),
                if phone { 12. } else { 11. },
                if on && phone { 600 } else { 400 },
            )
            .role(gpui_kit::Role::Tab)
            .accessibility_label(t.label.clone())
            .flex_col()
            .items_center()
            .justify_center()
            .gap(px(if phone { 4. } else { 2. }))
            .when(phone, |b| b.flex_1().min_w_0().h(px(56.)))
            .when(!phone, |b| {
                b.min_w(rem(64.))
                    .px(rem(12.))
                    .py(rem(4.))
                    .when(on, |b| b.bg(color(ColorRole::LayerSelected01)))
            })
            .text_color(ink)
            .on_click(move |_, window, cx| {
                store.update(cx, |v, cx| {
                    *v = value.clone();
                    cx.notify();
                });
                if let Some(f) = &on_change {
                    f(value.clone(), window, cx);
                }
            })
            .child(Icon::carbon(t.icon).size_length(rem(20.)).color(ink))
            .child(t.label.clone())
            .children(badge)
            .when(ring, |b| b.child(craft::focus_ring()))
            .into_any_element()
        });
        let tabs: Vec<AnyElement> = tabs.collect();
        if phone {
            // `[data-mb] .t3-dock { padding: 0 8px 34px; background; border-top }`.
            div()
                .id(id)
                .relative()
                .flex()
                .w_full()
                .px(rem(8.))
                .pb(px(34.))
                .bg(color(ColorRole::Background))
                .border_t_1()
                .border_color(color(ColorRole::BorderSubtle01))
                .children(tabs)
                .child(home_indicator())
        } else {
            div()
                .id(id)
                .relative()
                .flex()
                .self_center()
                .mx_auto()
                .mt(rem(8.))
                .mb(rem(16.))
                .gap(rem(2.))
                .p(rem(4.))
                .bg(color(ColorRole::Layer02))
                .shadow(float_shadow(2.))
                .children(tabs)
                .child(inset_frame(color(ColorRole::BorderSubtle01)))
        }
    }
}

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

    #[test]
    fn the_phone_screen_mask_stays_in_the_bezel() {
        use std::f32::consts::FRAC_1_SQRT_2;
        // `.t3-phone { padding: 10px; border-radius: 2.75rem }`,
        // `.t3-phone-screen { border-radius: 2.25rem }`.
        assert_eq!(
            (PHONE_BEZEL, PHONE_RADIUS, PHONE_SCREEN_RADIUS),
            (10., 44., 36.)
        );
        // Along the diagonal, where the curves differ most, the masking
        // ring's inner corner (radius 44 − 10, centred 44px in) sits within
        // a pixel of the screen's (radius 36, centred 46px in).
        let ring = PHONE_RADIUS - (PHONE_RADIUS - PHONE_BEZEL) * FRAC_1_SQRT_2;
        let screen = PHONE_BEZEL + PHONE_SCREEN_RADIUS * (1. - FRAC_1_SQRT_2);
        assert!((ring - screen).abs() < 1., "{ring} vs {screen}");
    }

    #[test]
    fn full_bleed_corners_stay_in_the_bezel() {
        // A square child's corner, where the screen's rectangle starts
        // (10, 10) in from the frame's, lies past the bezel's outer curve
        // (radius 44 about (44, 44)): the sliver the review saw.
        let centre = PHONE_RADIUS;
        let corner = (centre - PHONE_BEZEL) * std::f32::consts::SQRT_2;
        assert!(corner > PHONE_RADIUS, "{corner}");
        // Rounded to the screen's 36px, its farthest point from the
        // bezel's centre stays inside the outer curve.
        let r = screen_bottom_radius_px();
        let child_centre = PHONE_BEZEL + r;
        let farthest = (child_centre - centre) * std::f32::consts::SQRT_2 + r;
        assert!(farthest <= PHONE_RADIUS, "{farthest}");
        // Only a frame's screen asks for it.
        assert_eq!(screen_bottom_radius(), None);
        assert_eq!(
            with_phone_screen(screen_bottom_radius),
            Some(PHONE_SCREEN_RADIUS)
        );
        assert!(!in_phone_screen());
    }

    fn screen_bottom_radius_px() -> f32 {
        with_phone_screen(screen_bottom_radius).unwrap()
    }

    #[test]
    fn sheet_geometry_follows_the_reference() {
        // Grabber and head, intro (one 21px line and 12px), and an empty
        // `.t3-mlist-h` (8px): the list card is 98px under the sheet top.
        assert_eq!(SHEET_HEAD_HEIGHT, 57.);
        assert_eq!(14. * SHEET_INTRO_LINE, 21.);
        assert_eq!(
            SHEET_HEAD_HEIGHT + 14. * SHEET_INTRO_LINE + SHEET_INTRO_GAP + MLIST_HEADING_GAP,
            98.
        );
    }

    #[test]
    fn list_and_bar_geometry() {
        assert_eq!(divider_inset(false, true, false), 52.);
        assert_eq!(divider_inset(false, false, true), 16.);
        assert_eq!(divider_inset(true, false, true), 52.);
        assert_eq!(nav_slot(2), 88.);
        assert_eq!(nav_slot(1), 52.);
        assert_eq!(switch_geometry(false), (48., 28., 24., 2., 20.));
        assert_eq!(switch_geometry(true), (48., 24., 18., 3., 24.));
    }
}