gpui-ai 0.8.0

AI-native UI components for GPUI applications, built on gpui-component.
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
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
//! Controlled fine-tune inspector for design properties.

use gpui_base::StyledExt as _;
use std::sync::Arc;

use gpui::Hsla;
use gpui::{
    AccessibleAction, AppContext as _, Axis, Context, Entity, EventEmitter, FocusHandle,
    Focusable as _, InteractiveElement as _, IntoElement, MouseButton, Orientation,
    ParentElement as _, Render, Role, ScrollHandle, SharedString, StatefulInteractiveElement as _,
    Styled as _, Subscription, Window, div, prelude::FluentBuilder as _, relative,
};
use gpui_base::{
    Decrement, Increment, SliderIndicator, SliderThumb, SliderTrack, StepAction, step_value,
};
use gpui_component::{
    ActiveTheme as _, Colorize as _, Disableable as _, Sizable as _,
    button::Button,
    color_picker::{ColorPicker, ColorPickerEvent, ColorPickerState},
    h_flex,
    input::{Input, InputEvent, InputState},
    label::Label,
    menu::{DropdownMenu as _, PopupMenuItem},
    scroll::ScrollableMask,
    slider::{SliderEvent, SliderState},
    v_flex,
};

use crate::{
    control::{outlined_control, outlined_control_with_label},
    theme::SemanticStyledExt as _,
};

const MIN_DIMENSION: f64 = 1.;
const MAX_DIMENSION: f64 = 4_096.;
const MIN_RADIUS: f64 = 0.;
const MAX_RADIUS: f64 = 2_048.;

fn finite_clamp(value: f64, min: f64, max: f64) -> f64 {
    if value.is_finite() {
        value.clamp(min, max)
    } else {
        min
    }
}

fn normalize_opacity(opacity: f32) -> f32 {
    if opacity.is_finite() {
        opacity.clamp(0., 1.)
    } else {
        0.
    }
}

/// Consumer-owned design values displayed by a [`FineTuneCard`].
#[derive(Clone, Debug, PartialEq)]
pub struct FineTuneValues {
    width: f64,
    height: f64,
    radius: f64,
    opacity: f32,
    typeface_id: SharedString,
    accent: Option<Hsla>,
}

impl FineTuneValues {
    /// Create a normalized snapshot without an accent color.
    ///
    /// Dimensions clamp to `1..=4096`, radius to `0..=2048`, and opacity to
    /// the normalized `0..=1` range.
    pub fn new(
        width: f64,
        height: f64,
        radius: f64,
        opacity: f32,
        typeface_id: impl Into<SharedString>,
    ) -> Self {
        Self {
            width: finite_clamp(width, MIN_DIMENSION, MAX_DIMENSION),
            height: finite_clamp(height, MIN_DIMENSION, MAX_DIMENSION),
            radius: finite_clamp(radius, MIN_RADIUS, MAX_RADIUS),
            opacity: normalize_opacity(opacity),
            typeface_id: typeface_id.into(),
            accent: None,
        }
    }

    /// Set the optional accent color.
    pub fn accent(mut self, accent: Hsla) -> Self {
        self.accent = Some(accent);
        self
    }

    /// Return a snapshot with a different normalized width.
    pub fn with_width(mut self, width: f64) -> Self {
        self.width = finite_clamp(width, MIN_DIMENSION, MAX_DIMENSION);
        self
    }

    /// Return a snapshot with a different normalized height.
    pub fn with_height(mut self, height: f64) -> Self {
        self.height = finite_clamp(height, MIN_DIMENSION, MAX_DIMENSION);
        self
    }

    /// Return a snapshot with a different normalized radius.
    pub fn with_radius(mut self, radius: f64) -> Self {
        self.radius = finite_clamp(radius, MIN_RADIUS, MAX_RADIUS);
        self
    }

    /// Return a snapshot with a different normalized opacity.
    pub fn with_opacity(mut self, opacity: f32) -> Self {
        self.opacity = normalize_opacity(opacity);
        self
    }

    /// Return a snapshot with a different stable typeface identity.
    pub fn with_typeface(mut self, typeface_id: impl Into<SharedString>) -> Self {
        self.typeface_id = typeface_id.into();
        self
    }

    /// Return a snapshot with a different optional accent color.
    pub fn with_accent(mut self, accent: Option<Hsla>) -> Self {
        self.accent = accent;
        self
    }

    /// Return the clamped width in logical pixels.
    pub fn width(&self) -> f64 {
        self.width
    }

    /// Return the clamped height in logical pixels.
    pub fn height(&self) -> f64 {
        self.height
    }

    /// Return the clamped corner radius in logical pixels.
    pub fn radius(&self) -> f64 {
        self.radius
    }

    /// Return normalized opacity in `0..=1`.
    pub fn opacity(&self) -> f32 {
        self.opacity
    }

    /// Return the stable selected typeface identity.
    pub fn typeface_id(&self) -> &SharedString {
        &self.typeface_id
    }

    /// Return the optional accent color.
    pub fn accent_color(&self) -> Option<Hsla> {
        self.accent
    }
}

/// One application-owned typeface choice.
///
/// IDs must be stable and unique in a snapshot. Labels may be duplicated.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FineTuneTypeface {
    id: SharedString,
    label: SharedString,
}

impl FineTuneTypeface {
    /// Create a typeface choice with stable identity and a visible label.
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
        }
    }

    /// Return the stable application identity.
    pub fn id(&self) -> &SharedString {
        &self.id
    }

    /// Return the visible label.
    pub fn label(&self) -> &SharedString {
        &self.label
    }
}

/// A typed application intent emitted by [`FineTuneCard`].
#[derive(Clone, Debug, PartialEq)]
pub enum FineTuneEvent {
    /// The width editor proposed a different clamped value.
    WidthChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// Width in logical pixels.
        width: f64,
    },
    /// The height editor proposed a different clamped value.
    HeightChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// Height in logical pixels.
        height: f64,
    },
    /// The radius editor proposed a different clamped value.
    RadiusChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// Corner radius in logical pixels.
        radius: f64,
    },
    /// The opacity editor or slider proposed a normalized value.
    OpacityChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// Normalized opacity in `0..=1`.
        opacity: f32,
    },
    /// The typeface menu proposed a different stable typeface identity.
    TypefaceChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// Stable application typeface identity.
        typeface_id: SharedString,
    },
    /// The accent picker proposed a different optional color.
    AccentChanged {
        /// Fine-tune card identity.
        id: SharedString,
        /// The selected accent, or `None` when cleared.
        accent: Option<Hsla>,
    },
    /// The named Reset control was activated.
    ResetRequested {
        /// Fine-tune card identity.
        id: SharedString,
    },
    /// The named Apply control was activated.
    ApplyRequested {
        /// Fine-tune card identity.
        id: SharedString,
    },
}

impl FineTuneEvent {
    /// Return the stable identity of the card that emitted this intent.
    pub fn id(&self) -> &SharedString {
        match self {
            Self::WidthChanged { id, .. }
            | Self::HeightChanged { id, .. }
            | Self::RadiusChanged { id, .. }
            | Self::OpacityChanged { id, .. }
            | Self::TypefaceChanged { id, .. }
            | Self::AccentChanged { id, .. }
            | Self::ResetRequested { id }
            | Self::ApplyRequested { id } => id,
        }
    }
}

#[derive(Clone, Copy)]
enum NumericProperty {
    Width,
    Height,
    Radius,
    Opacity,
}

/// A consumer-controlled design-property inspector.
pub struct FineTuneCard {
    /// Styles the caller put on this component, applied to its own frame.
    ///
    /// Last, so a caller outranks the component's defaults - the same rule the
    /// builder components follow. A wrapper `div` cannot stand in for this:
    /// a background, a border, or an ink set on a wrapper paints around the
    /// component rather than on it.
    style: gpui::StyleRefinement,
    id: SharedString,
    values: FineTuneValues,
    typefaces: Arc<[FineTuneTypeface]>,
    /// Scroll position of the card body when its height is constrained.
    scroll_handle: ScrollHandle,
    width_input: Entity<InputState>,
    height_input: Entity<InputState>,
    radius_input: Entity<InputState>,
    opacity_input: Entity<InputState>,
    opacity_slider: Entity<SliderState>,
    opacity_slider_focus: FocusHandle,
    accent_picker: Entity<ColorPickerState>,
    local_width: f64,
    local_height: f64,
    local_radius: f64,
    local_opacity: f32,
    local_typeface_id: SharedString,
    local_accent: Option<Hsla>,
    _subscriptions: Vec<Subscription>,
}

impl FineTuneCard {
    /// Create a controlled card from a value snapshot and typeface catalog.
    pub fn new<I>(
        id: impl Into<SharedString>,
        values: FineTuneValues,
        typefaces: I,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self
    where
        I: IntoIterator<Item = FineTuneTypeface>,
    {
        let typefaces = valid_typefaces(typefaces).unwrap_or_default();
        let width_input = cx.new(|cx| {
            InputState::new(window, cx)
                .default_value(values.width.to_string())
                .step(1.)
                .min(MIN_DIMENSION)
                .max(MAX_DIMENSION)
        });
        let height_input = cx.new(|cx| {
            InputState::new(window, cx)
                .default_value(values.height.to_string())
                .step(1.)
                .min(MIN_DIMENSION)
                .max(MAX_DIMENSION)
        });
        let radius_input = cx.new(|cx| {
            InputState::new(window, cx)
                .default_value(values.radius.to_string())
                .step(1.)
                .min(MIN_RADIUS)
                .max(MAX_RADIUS)
        });
        let opacity_input = cx.new(|cx| {
            InputState::new(window, cx)
                .default_value((values.opacity * 100.).to_string())
                .step(1.)
                .min(0.)
                .max(100.)
        });
        let opacity_slider = cx.new(|_| {
            SliderState::new()
                .min(0.)
                .max(100.)
                .step(1.)
                .default_value(values.opacity * 100.)
        });
        let opacity_slider_focus = cx.focus_handle();
        let accent_picker = cx.new(|cx| {
            let state = ColorPickerState::new(window, cx);
            if let Some(accent) = values.accent {
                state.default_value(accent)
            } else {
                state
            }
        });
        let width_subscription =
            cx.subscribe_in(&width_input, window, |this, input, event, window, cx| {
                this.on_numeric_input_event(NumericProperty::Width, input, event, window, cx)
            });
        let height_subscription =
            cx.subscribe_in(&height_input, window, |this, input, event, window, cx| {
                this.on_numeric_input_event(NumericProperty::Height, input, event, window, cx)
            });
        let radius_subscription =
            cx.subscribe_in(&radius_input, window, |this, input, event, window, cx| {
                this.on_numeric_input_event(NumericProperty::Radius, input, event, window, cx)
            });
        let opacity_subscription =
            cx.subscribe_in(&opacity_input, window, |this, input, event, window, cx| {
                this.on_numeric_input_event(NumericProperty::Opacity, input, event, window, cx)
            });
        let slider_subscription = cx.subscribe_in(
            &opacity_slider,
            window,
            |this, _, event: &SliderEvent, window, cx| {
                if let SliderEvent::Change(value) = event {
                    this.change_opacity(value.end() / 100., window, cx);
                }
            },
        );
        let accent_subscription = cx.subscribe_in(
            &accent_picker,
            window,
            |this, _, event: &ColorPickerEvent, window, cx| match event {
                ColorPickerEvent::Change(accent) => {
                    this.change_accent(*accent, window, cx);
                }
            },
        );
        Self {
            style: gpui::StyleRefinement::default(),
            id: id.into(),
            local_width: values.width,
            local_height: values.height,
            local_radius: values.radius,
            local_opacity: values.opacity,
            local_typeface_id: values.typeface_id.clone(),
            local_accent: values.accent,
            values,
            typefaces,
            width_input,
            height_input,
            radius_input,
            opacity_input,
            opacity_slider,
            opacity_slider_focus,
            accent_picker,
            scroll_handle: ScrollHandle::new(),
            _subscriptions: vec![
                width_subscription,
                height_subscription,
                radius_subscription,
                opacity_subscription,
                slider_subscription,
                accent_subscription,
            ],
        }
    }

    /// Replace the consumer-controlled value snapshot without rebuilding editors.
    ///
    /// A numeric editor keeps its exact text when that text is valid and
    /// semantically equal to the incoming value. This preserves focused caret
    /// and undo state while other editors synchronize to the new snapshot.
    pub fn set_values(
        &mut self,
        values: FineTuneValues,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let changed = self.values != values
            || self.local_width != values.width
            || self.local_height != values.height
            || self.local_radius != values.radius
            || self.local_opacity != values.opacity
            || self.local_typeface_id != values.typeface_id
            || self.local_accent != values.accent;
        self.local_width = values.width;
        self.local_height = values.height;
        self.local_radius = values.radius;
        self.local_opacity = values.opacity;
        self.local_typeface_id = values.typeface_id.clone();
        self.local_accent = values.accent;

        Self::sync_numeric_input(
            &self.width_input,
            NumericProperty::Width,
            values.width,
            window,
            cx,
        );
        Self::sync_numeric_input(
            &self.height_input,
            NumericProperty::Height,
            values.height,
            window,
            cx,
        );
        Self::sync_numeric_input(
            &self.radius_input,
            NumericProperty::Radius,
            values.radius,
            window,
            cx,
        );
        Self::sync_numeric_input(
            &self.opacity_input,
            NumericProperty::Opacity,
            f64::from(values.opacity * 100.),
            window,
            cx,
        );
        let slider_value = values.opacity * 100.;
        if self.opacity_slider.read(cx).value().end() != slider_value {
            self.opacity_slider
                .update(cx, |slider, cx| slider.set_value(slider_value, window, cx));
        }
        if self.accent_picker.read(cx).value() != values.accent {
            self.accent_picker.update(cx, |picker, cx| {
                if let Some(accent) = values.accent {
                    picker.set_value(accent, window, cx);
                } else {
                    picker.clear_value(window, cx);
                }
            });
        }

        self.values = values;
        if changed {
            cx.notify();
        }
    }

    /// Replace the application typeface catalog without rebuilding editor state.
    ///
    /// Catalogs containing an empty or duplicate stable ID are ignored as one
    /// malformed snapshot. Duplicate visible labels are supported.
    pub fn set_typefaces<I>(&mut self, typefaces: I, cx: &mut Context<Self>)
    where
        I: IntoIterator<Item = FineTuneTypeface>,
    {
        let Some(typefaces) = valid_typefaces(typefaces) else {
            return;
        };
        if self.typefaces != typefaces {
            self.typefaces = typefaces;
            cx.notify();
        }
    }

    fn sync_numeric_input(
        input: &Entity<InputState>,
        property: NumericProperty,
        target: f64,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let text = input.read(cx).value();
        let current = text.trim().parse::<f64>().ok().filter(|value| {
            value.is_finite()
                && match property {
                    NumericProperty::Width | NumericProperty::Height => {
                        (MIN_DIMENSION..=MAX_DIMENSION).contains(value)
                    }
                    NumericProperty::Radius => (MIN_RADIUS..=MAX_RADIUS).contains(value),
                    NumericProperty::Opacity => (0.0..=100.0).contains(value),
                }
        });
        // Preserving every valid semantically equal textual form also covers
        // focused editors without needing to disturb their caret or undo state.
        if current == Some(target) {
            return;
        }
        input.update(cx, |input, cx| {
            input.set_value(target.to_string(), window, cx)
        });
    }

    fn on_numeric_input_event(
        &mut self,
        property: NumericProperty,
        input: &Entity<InputState>,
        event: &InputEvent,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        match event {
            InputEvent::Change => {
                let text = input.read(cx).value();
                let Ok(value) = text.trim().parse::<f64>() else {
                    return;
                };
                self.change_numeric(property, value, window, cx);
            }
            InputEvent::Blur if input.read(cx).value().trim().parse::<f64>().is_err() => {
                let value = match property {
                    NumericProperty::Width => self.values.width,
                    NumericProperty::Height => self.values.height,
                    NumericProperty::Radius => self.values.radius,
                    NumericProperty::Opacity => f64::from(self.values.opacity * 100.),
                };
                input.update(cx, |input, cx| {
                    input.set_value(value.to_string(), window, cx)
                });
                match property {
                    NumericProperty::Width => self.local_width = self.values.width,
                    NumericProperty::Height => self.local_height = self.values.height,
                    NumericProperty::Radius => self.local_radius = self.values.radius,
                    NumericProperty::Opacity => self.local_opacity = self.values.opacity,
                }
                cx.notify();
            }
            InputEvent::PressEnter { .. } | InputEvent::Focus | InputEvent::Blur => {}
        }
    }

    fn change_numeric(
        &mut self,
        property: NumericProperty,
        value: f64,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        match property {
            NumericProperty::Width => {
                let width = finite_clamp(value, MIN_DIMENSION, MAX_DIMENSION);
                if self.local_width != width {
                    self.local_width = width;
                    cx.notify();
                    cx.emit(FineTuneEvent::WidthChanged {
                        id: self.id.clone(),
                        width,
                    });
                }
            }
            NumericProperty::Height => {
                let height = finite_clamp(value, MIN_DIMENSION, MAX_DIMENSION);
                if self.local_height != height {
                    self.local_height = height;
                    cx.notify();
                    cx.emit(FineTuneEvent::HeightChanged {
                        id: self.id.clone(),
                        height,
                    });
                }
            }
            NumericProperty::Radius => {
                let radius = finite_clamp(value, MIN_RADIUS, MAX_RADIUS);
                if self.local_radius != radius {
                    self.local_radius = radius;
                    cx.notify();
                    cx.emit(FineTuneEvent::RadiusChanged {
                        id: self.id.clone(),
                        radius,
                    });
                }
            }
            NumericProperty::Opacity => {
                self.change_opacity((value / 100.) as f32, window, cx);
            }
        }
    }

    fn change_opacity(&mut self, opacity: f32, window: &mut Window, cx: &mut Context<Self>) {
        let opacity = normalize_opacity(opacity);
        if self.local_opacity == opacity {
            return;
        }
        self.local_opacity = opacity;
        let percentage = opacity * 100.;
        let input_percentage = self
            .opacity_input
            .read(cx)
            .value()
            .trim()
            .parse::<f32>()
            .ok();
        if input_percentage != Some(percentage) {
            self.opacity_input.update(cx, |input, cx| {
                input.set_value(percentage.to_string(), window, cx)
            });
        }
        if self.opacity_slider.read(cx).value().end() != percentage {
            self.opacity_slider
                .update(cx, |slider, cx| slider.set_value(percentage, window, cx));
        }
        cx.notify();
        cx.emit(FineTuneEvent::OpacityChanged {
            id: self.id.clone(),
            opacity,
        });
    }

    fn step_opacity(&mut self, step: f32, window: &mut Window, cx: &mut Context<Self>) {
        let percentage = (self.local_opacity * 100. + step).clamp(0., 100.);
        self.change_opacity(percentage / 100., window, cx);
    }

    fn select_typeface(&mut self, typeface_id: impl Into<SharedString>, cx: &mut Context<Self>) {
        let typeface_id = typeface_id.into();
        if self.local_typeface_id == typeface_id
            || !self.typefaces.iter().any(|item| item.id == typeface_id)
        {
            return;
        }
        self.local_typeface_id = typeface_id.clone();
        cx.notify();
        cx.emit(FineTuneEvent::TypefaceChanged {
            id: self.id.clone(),
            typeface_id,
        });
    }

    fn change_accent(&mut self, accent: Option<Hsla>, window: &mut Window, cx: &mut Context<Self>) {
        if self.local_accent == accent {
            return;
        }
        self.local_accent = accent;
        if self.accent_picker.read(cx).value() != accent {
            self.accent_picker.update(cx, |picker, cx| {
                if let Some(accent) = accent {
                    picker.set_value(accent, window, cx);
                } else {
                    picker.clear_value(window, cx);
                }
            });
        }
        cx.notify();
        cx.emit(FineTuneEvent::AccentChanged {
            id: self.id.clone(),
            accent,
        });
    }
}

fn valid_typefaces<I>(typefaces: I) -> Option<Arc<[FineTuneTypeface]>>
where
    I: IntoIterator<Item = FineTuneTypeface>,
{
    let mut validated = Vec::new();
    for typeface in typefaces {
        if typeface.id.is_empty()
            || validated
                .iter()
                .any(|item: &FineTuneTypeface| item.id == typeface.id)
        {
            return None;
        }
        validated.push(typeface);
    }
    Some(validated.into())
}

impl EventEmitter<FineTuneEvent> for FineTuneCard {}

#[derive(Clone, Copy)]
struct NumericSemantics {
    value: f64,
    min: f64,
    max: f64,
}

fn step_numeric_editor(
    input: &Entity<InputState>,
    action: StepAction,
    semantics: NumericSemantics,
    window: &mut Window,
    cx: &mut gpui::App,
) {
    let text = input.read(cx).value();
    let Some(next) = step_value(&text, action, 1., Some(semantics.min), Some(semantics.max)) else {
        return;
    };
    input.update(cx, |input, cx| input.replace_all(next, window, cx));
}

fn named_number_input(
    card_id: &SharedString,
    key: &'static str,
    label: &'static str,
    semantics: NumericSemantics,
    input: &Entity<InputState>,
    suffix: &'static str,
    cx: &mut gpui::App,
) -> gpui::Stateful<gpui::Div> {
    let tokens = cx.theme().semantic_tokens();
    let focus = input.focus_handle(cx);
    let decrement_input = input.clone();
    let increment_input = input.clone();
    let accessible_decrement_input = input.clone();
    let accessible_increment_input = input.clone();
    let accessible_value_input = input.clone();
    div()
        .id((gpui::ElementId::from(card_id.clone()), key))
        .accessibility_id(format!("fine-tune.{card_id}.{key}"))
        .debug_selector(move || format!("fine-tune-{key}-editor"))
        .role(Role::SpinButton)
        .aria_label(label)
        .aria_numeric_value(semantics.value)
        .aria_min_numeric_value(semantics.min)
        .aria_max_numeric_value(semantics.max)
        .aria_numeric_value_step(1.)
        .aria_value(format!("{} {suffix}", semantics.value))
        .track_focus(&focus)
        .key_context("NumberInput")
        .on_action(move |_: &Decrement, window, cx| {
            step_numeric_editor(
                &decrement_input,
                StepAction::Decrement,
                semantics,
                window,
                cx,
            );
        })
        .on_action(move |_: &Increment, window, cx| {
            step_numeric_editor(
                &increment_input,
                StepAction::Increment,
                semantics,
                window,
                cx,
            );
        })
        .on_a11y_action(AccessibleAction::Decrement, move |_, window, cx| {
            step_numeric_editor(
                &accessible_decrement_input,
                StepAction::Decrement,
                semantics,
                window,
                cx,
            );
        })
        .on_a11y_action(AccessibleAction::Increment, move |_, window, cx| {
            step_numeric_editor(
                &accessible_increment_input,
                StepAction::Increment,
                semantics,
                window,
                cx,
            );
        })
        .on_a11y_action(AccessibleAction::SetValue, move |data, window, cx| {
            let Some(gpui::accesskit::ActionData::Value(value)) = data else {
                return;
            };
            accessible_value_input.update(cx, |input, cx| {
                input.replace_all(value.to_string(), window, cx)
            });
        })
        .rounded(tokens.radius.sm)
        .border_1()
        .border_color(cx.theme().transparent)
        .focus_visible(|style| style.border_color(cx.theme().ring))
        .child(
            Input::new(input)
                .small()
                .role(None::<Role>)
                .suffix(crate::surface::field_unit(suffix, cx)),
        )
}

fn opacity_slider_control(
    card_id: &SharedString,
    value: f32,
    state: &Entity<SliderState>,
    focus: &FocusHandle,
    weak_card: gpui::WeakEntity<FineTuneCard>,
    cx: &mut gpui::App,
) -> gpui::Stateful<gpui::Div> {
    let tokens = cx.theme().semantic_tokens();
    let percentage = state.read(cx).percentage().end;
    let mouse_focus = focus.clone();
    let keyboard_card = weak_card.clone();
    let decrement_card = weak_card.clone();
    let increment_card = weak_card.clone();
    let set_value_card = weak_card;
    div()
        .id((gpui::ElementId::from(card_id.clone()), "opacity-slider"))
        .debug_selector(|| "fine-tune-opacity-slider".to_owned())
        .accessibility_id(format!("fine-tune.{card_id}.opacity-slider"))
        .role(Role::Slider)
        .aria_label("Opacity slider")
        .aria_numeric_value(f64::from(value * 100.))
        .aria_min_numeric_value(0.)
        .aria_max_numeric_value(100.)
        .aria_numeric_value_step(1.)
        .aria_value(format!("{} percent", value * 100.))
        .aria_orientation(Orientation::Horizontal)
        .track_focus(focus)
        .tab_index(0)
        .on_mouse_down(MouseButton::Left, move |_, window, cx| {
            mouse_focus.focus(window, cx);
        })
        .on_key_down(move |event, window, cx| {
            let step = match event.keystroke.key.as_str() {
                "left" | "down" => -1.,
                "right" | "up" => 1.,
                _ => return,
            };
            let _ = keyboard_card.update(cx, |card, cx| {
                card.step_opacity(step, window, cx);
            });
            cx.stop_propagation();
        })
        .on_a11y_action(AccessibleAction::Decrement, move |_, window, cx| {
            let _ = decrement_card.update(cx, |card, cx| {
                card.step_opacity(-1., window, cx);
            });
        })
        .on_a11y_action(AccessibleAction::Increment, move |_, window, cx| {
            let _ = increment_card.update(cx, |card, cx| {
                card.step_opacity(1., window, cx);
            });
        })
        .on_a11y_action(AccessibleAction::SetValue, move |data, window, cx| {
            let Some(gpui::accesskit::ActionData::Value(value)) = data else {
                return;
            };
            let Ok(value) = value.parse::<f32>() else {
                return;
            };
            let _ = set_value_card.update(cx, |card, cx| {
                card.change_opacity(value / 100., window, cx);
            });
        })
        .relative()
        .w_full()
        .py(tokens.spacing.xs)
        .border_1()
        .border_color(cx.theme().transparent)
        .rounded(tokens.radius.sm)
        .focus_visible(|style| style.border_color(cx.theme().ring))
        .child(
            SliderTrack::new(state)
                .relative()
                .w_full()
                .h(tokens.spacing.sm)
                .child(
                    SliderIndicator::new(state)
                        .absolute()
                        .top(tokens.spacing.xs)
                        .left_0()
                        .w_full()
                        .h(tokens.spacing.xxs)
                        .rounded(tokens.radius.sm)
                        .bg(cx.theme().muted)
                        .child(
                            div()
                                .absolute()
                                .top_0()
                                .bottom_0()
                                .left_0()
                                .right(relative(1. - percentage))
                                .rounded(tokens.radius.sm)
                                .bg(cx.theme().primary),
                        ),
                )
                .child(
                    SliderThumb::new(state)
                        .absolute()
                        .top_0()
                        .left(relative(percentage))
                        .ml(-tokens.spacing.xs)
                        .size(tokens.spacing.sm)
                        // A thumb is a dot, and dots are circles here.
                        .rounded(tokens.radius.full)
                        .border_1()
                        .border_color(cx.theme().primary)
                        .bg(cx.theme().background),
                ),
        )
}

fn numeric_field(
    card_id: &SharedString,
    key: &'static str,
    label: &'static str,
    semantics: NumericSemantics,
    input: &Entity<InputState>,
    suffix: &'static str,
    cx: &mut gpui::App,
) -> gpui::Stateful<gpui::Div> {
    let tokens = cx.theme().semantic_tokens();
    v_flex()
        .id((gpui::ElementId::from(card_id.clone()), key))
        .debug_selector(move || format!("fine-tune-{key}-input"))
        .role(Role::Group)
        .aria_label(format!("{label} property"))
        .flex_1()
        .min_w_0()
        .gap(tokens.spacing.xs)
        .child(Label::new(label).text_token(tokens.typography.sm))
        .child(named_number_input(
            card_id, key, label, semantics, input, suffix, cx,
        ))
}

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

impl Render for FineTuneCard {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let tokens = cx.theme().semantic_tokens();
        let root_id = self.id.clone();
        let selected_typeface = self
            .typefaces
            .iter()
            .find(|typeface| typeface.id == self.local_typeface_id)
            .map(|typeface| typeface.label.clone());
        let typefaces_empty = self.typefaces.is_empty();
        let selected_typeface_value: SharedString =
            selected_typeface.clone().unwrap_or_else(|| {
                if typefaces_empty {
                    "No typefaces available".into()
                } else {
                    format!("Unavailable typeface: {}", self.local_typeface_id).into()
                }
            });
        let selected_typeface =
            selected_typeface.unwrap_or_else(|| selected_typeface_value.clone());
        let typefaces = self.typefaces.clone();
        let selected_typeface_id = self.local_typeface_id.clone();
        let weak_card = cx.weak_entity();
        let opacity_weak_card = cx.weak_entity();
        let typeface_button =
            Button::new((gpui::ElementId::from(self.id.clone()), "typeface-trigger"))
                .label(selected_typeface)
                .small()
                .outline()
                .disabled(typefaces_empty)
                .debug_selector(|| "fine-tune-typeface".to_owned())
                .dropdown_caret(!typefaces_empty);
        let typeface_control = if typefaces_empty {
            typeface_button.into_any_element()
        } else {
            typeface_button
                .dropdown_menu(move |menu, _, _| {
                    typefaces.iter().fold(menu, |menu, typeface| {
                        let typeface_id = typeface.id.clone();
                        let callback_id = typeface_id.clone();
                        let weak_card = weak_card.clone();
                        menu.item(
                            PopupMenuItem::new(typeface.label.clone())
                                .checked(typeface_id == selected_typeface_id)
                                .on_click(move |_, _, cx| {
                                    let _ = weak_card.update(cx, |card, cx| {
                                        card.select_typeface(callback_id.clone(), cx);
                                    });
                                }),
                        )
                    })
                })
                .into_any_element()
        };
        let accent_text: SharedString = self
            .local_accent
            .map(|accent| accent.to_hex().into())
            .unwrap_or_else(|| "No accent selected".into());
        let has_accent = self.local_accent.is_some();
        let card = v_flex()
            .id(root_id.clone())
            .debug_selector(move || format!("fine-tune-card-{root_id}"))
            .role(Role::Group)
            .aria_label("Fine-tune design properties")
            .min_h_0()
            .h_full()
            .max_h_full()
            .overflow_y_scroll()
            .track_scroll(&self.scroll_handle)
            .gap(tokens.spacing.md)
            .p(tokens.spacing.md)
            .rounded(tokens.radius.lg)
            .border_1()
            .border_color(cx.theme().border)
            .bg(tokens.colors.surface)
            .child(
                h_flex()
                    .w_full()
                    .gap(tokens.spacing.sm)
                    .child(numeric_field(
                        &self.id,
                        "width",
                        "Width",
                        NumericSemantics {
                            value: self.local_width,
                            min: MIN_DIMENSION,
                            max: MAX_DIMENSION,
                        },
                        &self.width_input,
                        "px",
                        cx,
                    ))
                    .child(numeric_field(
                        &self.id,
                        "height",
                        "Height",
                        NumericSemantics {
                            value: self.local_height,
                            min: MIN_DIMENSION,
                            max: MAX_DIMENSION,
                        },
                        &self.height_input,
                        "px",
                        cx,
                    )),
            )
            .child(numeric_field(
                &self.id,
                "radius",
                "Radius",
                NumericSemantics {
                    value: self.local_radius,
                    min: MIN_RADIUS,
                    max: MAX_RADIUS,
                },
                &self.radius_input,
                "px",
                cx,
            ))
            .child(
                v_flex()
                    .id((gpui::ElementId::from(self.id.clone()), "opacity"))
                    .role(Role::Group)
                    .aria_label("Opacity property")
                    .gap(tokens.spacing.xs)
                    .child(
                        h_flex()
                            .items_end()
                            .gap(tokens.spacing.sm)
                            .child(
                                Label::new("Opacity")
                                    .text_token(tokens.typography.sm)
                                    .flex_1(),
                            )
                            .child(div().w(tokens.spacing.xxl * 2.).child(named_number_input(
                                &self.id,
                                "opacity-input",
                                "Opacity",
                                NumericSemantics {
                                    value: f64::from(self.local_opacity * 100.),
                                    min: 0.,
                                    max: 100.,
                                },
                                &self.opacity_input,
                                "%",
                                cx,
                            ))),
                    )
                    .child(opacity_slider_control(
                        &self.id,
                        self.local_opacity,
                        &self.opacity_slider,
                        &self.opacity_slider_focus,
                        opacity_weak_card,
                        cx,
                    )),
            )
            .child(
                v_flex()
                    .id((gpui::ElementId::from(self.id.clone()), "typeface"))
                    .role(Role::Group)
                    .aria_label("Typeface selection")
                    .aria_value(selected_typeface_value)
                    .gap(tokens.spacing.xs)
                    .child(Label::new("Typeface").text_token(tokens.typography.sm))
                    .child(typeface_control),
            )
            .child(
                v_flex()
                    .id((gpui::ElementId::from(self.id.clone()), "accent"))
                    .role(Role::Group)
                    .aria_label("Accent color")
                    .aria_value(accent_text.clone())
                    .gap(tokens.spacing.xs)
                    .child(Label::new("Accent").text_token(tokens.typography.sm))
                    .child(
                        h_flex()
                            .w_full()
                            .flex_wrap()
                            .gap(tokens.spacing.sm)
                            .items_center()
                            .child(
                                ColorPicker::new(&self.accent_picker)
                                    .small()
                                    .label("Accent color"),
                            )
                            .child(
                                Label::new(accent_text)
                                    .text_token(tokens.typography.sm)
                                    .flex_1(),
                            )
                            .when(has_accent, |this| {
                                this.child(
                                    outlined_control(
                                        (gpui::ElementId::from(self.id.clone()), "clear-accent"),
                                        "Remove accent color",
                                        window,
                                        cx,
                                    )
                                    .debug_selector(|| "fine-tune-clear-accent".to_owned())
                                    .on_click(cx.listener(
                                        |card, _, window, cx| {
                                            card.change_accent(None, window, cx);
                                        },
                                    )),
                                )
                            }),
                    ),
            )
            .child(
                h_flex()
                    .w_full()
                    .flex_wrap()
                    .justify_end()
                    .gap(tokens.spacing.xs)
                    .child(
                        outlined_control_with_label(
                            (gpui::ElementId::from(self.id.clone()), "reset"),
                            "Reset fine-tune values",
                            "Reset",
                            window,
                            cx,
                        )
                        .debug_selector(|| "fine-tune-reset".to_owned())
                        .on_click(cx.listener(|this, _, _, cx| {
                            cx.emit(FineTuneEvent::ResetRequested {
                                id: this.id.clone(),
                            });
                        })),
                    )
                    .child(
                        outlined_control_with_label(
                            (gpui::ElementId::from(self.id.clone()), "apply"),
                            "Apply fine-tune values",
                            "Apply",
                            window,
                            cx,
                        )
                        .debug_selector(|| "fine-tune-apply".to_owned())
                        .on_click(cx.listener(|this, _, _, cx| {
                            cx.emit(FineTuneEvent::ApplyRequested {
                                id: this.id.clone(),
                            });
                        })),
                    ),
            )
            .child(div().hidden());

        div()
            .relative()
            .size_full()
            .child(card)
            .child(
                ScrollableMask::new(Axis::Vertical, &self.scroll_handle).id((
                    gpui::ElementId::from(self.id.clone()),
                    "content-scroll-mask",
                )),
            )
            .refine_style(&self.style)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use gpui::{Element as _, TestAppContext, accesskit, canvas};
    use std::{
        cell::{Cell, RefCell},
        rc::Rc,
        sync::{Arc, Mutex},
    };

    type CapturedSemanticNode = Arc<Mutex<Option<(Option<Role>, accesskit::Node)>>>;

    struct NumericSemanticProbe {
        input: Entity<InputState>,
        captured: CapturedSemanticNode,
    }

    struct SliderSemanticProbe {
        card: Entity<FineTuneCard>,
        captured: CapturedSemanticNode,
    }

    impl SliderSemanticProbe {
        fn new(
            captured: CapturedSemanticNode,
            window: &mut Window,
            cx: &mut Context<Self>,
        ) -> Self {
            let card = cx.new(|cx| {
                FineTuneCard::new(
                    "fine-tune",
                    FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                    [FineTuneTypeface::new("inter", "Inter")],
                    window,
                    cx,
                )
            });
            Self { card, captured }
        }
    }

    impl Render for SliderSemanticProbe {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let card = self.card.clone();
            let captured = self.captured.clone();
            canvas(
                move |_, _, cx| {
                    let (id, value, state, focus) = card.read_with(cx, |card, _| {
                        (
                            card.id.clone(),
                            card.local_opacity,
                            card.opacity_slider.clone(),
                            card.opacity_slider_focus.clone(),
                        )
                    });
                    let slider =
                        opacity_slider_control(&id, value, &state, &focus, card.downgrade(), cx)
                            .into_element();
                    let role = slider.a11y_role();
                    let mut node = accesskit::Node::new(Role::Unknown);
                    slider.write_a11y_info(&mut node);
                    *captured.lock().expect("capture mutex should be available") =
                        Some((role, node));
                },
                |_, _, _, _| {},
            )
        }
    }

    impl NumericSemanticProbe {
        fn new(
            captured: CapturedSemanticNode,
            window: &mut Window,
            cx: &mut Context<Self>,
        ) -> Self {
            let input = cx.new(|cx| InputState::new(window, cx).default_value("320"));
            Self { input, captured }
        }
    }

    impl Render for NumericSemanticProbe {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let input = self.input.clone();
            let captured = self.captured.clone();
            canvas(
                move |_, _, cx| {
                    let field = named_number_input(
                        &"fine-tune".into(),
                        "width",
                        "Width",
                        NumericSemantics {
                            value: 320.,
                            min: MIN_DIMENSION,
                            max: MAX_DIMENSION,
                        },
                        &input,
                        "px",
                        cx,
                    )
                    .into_element();
                    let role = field.a11y_role();
                    let mut node = accesskit::Node::new(Role::Unknown);
                    field.write_a11y_info(&mut node);
                    *captured.lock().expect("capture mutex should be available") =
                        Some((role, node));
                },
                |_, _, _, _| {},
            )
        }
    }

    #[test]
    fn controlled_values_clamp_numeric_properties_and_normalize_opacity() {
        let values = FineTuneValues::new(9_000., -20., 3_000., 1.4, "inter");

        assert_eq!(values.width(), 4_096.);
        assert_eq!(values.height(), 1.);
        assert_eq!(values.radius(), 2_048.);
        assert_eq!(values.opacity(), 1.);
        assert_eq!(values.typeface_id(), "inter");
    }

    #[gpui::test]
    fn numeric_editor_exposes_name_value_bounds_step_and_actions(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let captured = Arc::new(Mutex::new(None));
        let result = captured.clone();
        let (_, cx) =
            cx.add_window_view(move |window, cx| NumericSemanticProbe::new(captured, window, cx));
        cx.update(|window, cx| window.draw(cx).clear(cx));

        let (role, node) = result
            .lock()
            .expect("capture mutex should be available")
            .take()
            .expect("numeric property semantics should be captured");
        assert_eq!(role, Some(Role::SpinButton));
        assert_eq!(node.label(), Some("Width"));
        assert_eq!(node.value(), Some("320 px"));
        assert_eq!(node.numeric_value(), Some(320.));
        assert_eq!(node.min_numeric_value(), Some(MIN_DIMENSION));
        assert_eq!(node.max_numeric_value(), Some(MAX_DIMENSION));
        assert_eq!(node.numeric_value_step(), Some(1.));
        assert!(node.supports_action(AccessibleAction::Increment));
        assert!(node.supports_action(AccessibleAction::Decrement));
        assert!(node.supports_action(AccessibleAction::SetValue));
    }

    #[gpui::test]
    fn opacity_slider_exposes_name_value_bounds_step_and_actions(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let captured = Arc::new(Mutex::new(None));
        let result = captured.clone();
        let (probe, cx) =
            cx.add_window_view(move |window, cx| SliderSemanticProbe::new(captured, window, cx));
        cx.update(|window, cx| window.draw(cx).clear(cx));

        let (role, node) = result
            .lock()
            .expect("capture mutex should be available")
            .take()
            .expect("opacity slider semantics should be captured");
        assert_eq!(role, Some(Role::Slider));
        assert_eq!(node.label(), Some("Opacity slider"));
        assert_eq!(node.value(), Some("72 percent"));
        assert_eq!(node.numeric_value(), Some(72.));
        assert_eq!(node.min_numeric_value(), Some(0.));
        assert_eq!(node.max_numeric_value(), Some(100.));
        assert_eq!(node.numeric_value_step(), Some(1.));
        assert!(node.supports_action(AccessibleAction::Increment));
        assert!(node.supports_action(AccessibleAction::Decrement));
        assert!(node.supports_action(AccessibleAction::SetValue));

        let card = probe.read_with(cx, |probe, _| probe.card.clone());
        cx.update(|window, cx| {
            card.update(cx, |card, cx| card.step_opacity(1., window, cx));
            let (id, value, state, focus) = card.read_with(cx, |card, _| {
                (
                    card.id.clone(),
                    card.local_opacity,
                    card.opacity_slider.clone(),
                    card.opacity_slider_focus.clone(),
                )
            });
            let slider = opacity_slider_control(&id, value, &state, &focus, card.downgrade(), cx)
                .into_element();
            let role = slider.a11y_role();
            let mut node = accesskit::Node::new(Role::Unknown);
            slider.write_a11y_info(&mut node);
            *result.lock().expect("capture mutex should be available") = Some((role, node));
        });
        let (_, node) = result
            .lock()
            .expect("capture mutex should be available")
            .take()
            .expect("locally edited opacity semantics should be captured before consumer echo");
        assert_eq!(node.value(), Some("73 percent"));
        assert_eq!(node.numeric_value(), Some(73.));
        assert_eq!(card.read_with(cx, |card, _| card.values.opacity), 0.72);
    }

    #[gpui::test]
    fn invalid_intermediate_number_does_not_emit_a_semantic_change(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                [FineTuneTypeface::new("inter", "Inter")],
                window,
                cx,
            )
        });
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let _subscription = cx.update(|_, cx| {
            cx.subscribe(&card, move |_, event, _| {
                captured.borrow_mut().push(event.clone());
            })
        });
        let input = card.read_with(cx, |card, _| card.width_input.clone());

        cx.update(|window, cx| {
            input.update(cx, |input, cx| input.replace_all("-", window, cx));
        });
        cx.run_until_parked();

        assert!(events.borrow().is_empty());
    }

    #[gpui::test]
    fn numeric_editor_events_are_clamped_typed_and_card_identified(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                [FineTuneTypeface::new("inter", "Inter")],
                window,
                cx,
            )
        });
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let _subscription = cx.update(|_, cx| {
            cx.subscribe(&card, move |_, event, _| {
                captured.borrow_mut().push(event.clone());
            })
        });
        let (width, height, radius, opacity) = card.read_with(cx, |card, _| {
            (
                card.width_input.clone(),
                card.height_input.clone(),
                card.radius_input.clone(),
                card.opacity_input.clone(),
            )
        });

        cx.update(|window, cx| {
            width.update(cx, |input, cx| input.replace_all("9000", window, cx));
            height.update(cx, |input, cx| input.replace_all("-20", window, cx));
            radius.update(cx, |input, cx| input.replace_all("3000", window, cx));
            opacity.update(cx, |input, cx| input.replace_all("125", window, cx));
        });
        cx.run_until_parked();

        assert_eq!(
            events.borrow().as_slice(),
            [
                FineTuneEvent::WidthChanged {
                    id: "fine-tune".into(),
                    width: 4_096.,
                },
                FineTuneEvent::HeightChanged {
                    id: "fine-tune".into(),
                    height: 1.,
                },
                FineTuneEvent::RadiusChanged {
                    id: "fine-tune".into(),
                    radius: 2_048.,
                },
                FineTuneEvent::OpacityChanged {
                    id: "fine-tune".into(),
                    opacity: 1.,
                },
            ]
        );
    }

    #[gpui::test]
    fn controlled_replacement_preserves_valid_editor_text_and_identity(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                [FineTuneTypeface::new("inter", "Inter")],
                window,
                cx,
            )
        });
        let (width, width_id, height_id, radius_id, opacity_id) = card.read_with(cx, |card, _| {
            (
                card.width_input.clone(),
                card.width_input.entity_id(),
                card.height_input.entity_id(),
                card.radius_input.entity_id(),
                card.opacity_input.entity_id(),
            )
        });
        cx.update(|window, cx| {
            width.update(cx, |input, cx| {
                input.replace_all("480.", window, cx);
            });
            card.update(cx, |card, cx| {
                card.set_values(
                    FineTuneValues::new(480., 240., 12., 0.5, "inter"),
                    window,
                    cx,
                );
            });
        });

        card.read_with(cx, |card, cx| {
            assert_eq!(card.width_input.entity_id(), width_id);
            assert_eq!(card.height_input.entity_id(), height_id);
            assert_eq!(card.radius_input.entity_id(), radius_id);
            assert_eq!(card.opacity_input.entity_id(), opacity_id);
            assert_eq!(card.width_input.read(cx).value(), "480.");
            assert_eq!(card.height_input.read(cx).value(), "240");
            assert_eq!(card.radius_input.read(cx).value(), "12");
            assert_eq!(card.opacity_input.read(cx).value(), "50");
            assert_eq!(card.opacity_slider.read(cx).value().end(), 50.);
        });

        cx.update(|window, cx| {
            width.update(cx, |input, cx| input.replace_all("9000", window, cx));
            card.update(cx, |card, cx| {
                card.set_values(
                    FineTuneValues::new(4_096., 240., 12., 0.5, "inter"),
                    window,
                    cx,
                );
            });
        });
        assert_eq!(width.read_with(cx, |input, _| input.value()), "4096");
    }

    #[gpui::test]
    fn typeface_and_accent_changes_emit_stable_identity_once(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter-regular"),
                [
                    FineTuneTypeface::new("inter-regular", "Inter"),
                    FineTuneTypeface::new("inter-display", "Inter"),
                ],
                window,
                cx,
            )
        });
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let _subscription = cx.update(|_, cx| {
            cx.subscribe(&card, move |_, event, _| {
                captured.borrow_mut().push(event.clone());
            })
        });
        let accent = gpui::hsla(0.58, 0.75, 0.52, 1.);

        cx.update(|window, cx| {
            card.update(cx, |card, cx| {
                card.select_typeface("inter-display", cx);
                card.select_typeface("inter-display", cx);
                card.change_accent(Some(accent), window, cx);
                card.change_accent(Some(accent), window, cx);
            });
        });

        assert_eq!(
            events.borrow().as_slice(),
            [
                FineTuneEvent::TypefaceChanged {
                    id: "fine-tune".into(),
                    typeface_id: "inter-display".into(),
                },
                FineTuneEvent::AccentChanged {
                    id: "fine-tune".into(),
                    accent: Some(accent),
                },
            ]
        );
    }

    #[gpui::test]
    fn unchanged_consumer_snapshot_rejects_local_typeface_and_notifies(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let snapshot = FineTuneValues::new(320., 180., 24., 0.72, "inter-regular");
        let (card, cx) = cx.add_window_view({
            let snapshot = snapshot.clone();
            move |window, cx| {
                FineTuneCard::new(
                    "fine-tune",
                    snapshot,
                    [
                        FineTuneTypeface::new("inter-regular", "Inter Regular"),
                        FineTuneTypeface::new("inter-display", "Inter Display"),
                    ],
                    window,
                    cx,
                )
            }
        });
        let notifications = Rc::new(Cell::new(0));
        let captured = notifications.clone();
        let _observation =
            cx.update(|_, cx| cx.observe(&card, move |_, _| captured.set(captured.get() + 1)));

        cx.update(|_, cx| {
            card.update(cx, |card, cx| card.select_typeface("inter-display", cx));
        });
        cx.run_until_parked();
        let before_rejection = notifications.get();

        cx.update(|window, cx| {
            card.update(cx, |card, cx| card.set_values(snapshot, window, cx));
        });
        cx.run_until_parked();

        assert_eq!(
            card.read_with(cx, |card, _| card.local_typeface_id.clone()),
            "inter-regular"
        );
        assert!(notifications.get() > before_rejection);
    }

    #[gpui::test]
    fn retained_slider_and_picker_subscriptions_emit_typed_changes_once(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                [FineTuneTypeface::new("inter", "Inter")],
                window,
                cx,
            )
        });
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let _subscription = cx.update(|_, cx| {
            cx.subscribe(&card, move |_, event, _| {
                captured.borrow_mut().push(event.clone());
            })
        });
        let (slider, picker) = card.read_with(cx, |card, _| {
            (card.opacity_slider.clone(), card.accent_picker.clone())
        });
        let accent = gpui::hsla(0.08, 0.82, 0.56, 1.);

        cx.update(|_, cx| {
            slider.update(cx, |_, cx| cx.emit(SliderEvent::Change(75_f32.into())));
            slider.update(cx, |_, cx| cx.emit(SliderEvent::Release(75_f32.into())));
            picker.update(cx, |_, cx| cx.emit(ColorPickerEvent::Change(Some(accent))));
        });

        assert_eq!(
            events.borrow().as_slice(),
            [
                FineTuneEvent::OpacityChanged {
                    id: "fine-tune".into(),
                    opacity: 0.75,
                },
                FineTuneEvent::AccentChanged {
                    id: "fine-tune".into(),
                    accent: Some(accent),
                },
            ]
        );
    }

    #[gpui::test]
    fn malformed_typeface_replacement_is_ignored(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (card, cx) = cx.add_window_view(|window, cx| {
            FineTuneCard::new(
                "fine-tune",
                FineTuneValues::new(320., 180., 24., 0.72, "inter"),
                [FineTuneTypeface::new("inter", "Inter")],
                window,
                cx,
            )
        });

        cx.update(|_, cx| {
            card.update(cx, |card, cx| {
                card.set_typefaces(
                    [
                        FineTuneTypeface::new("duplicate", "Inter"),
                        FineTuneTypeface::new("duplicate", "Display"),
                    ],
                    cx,
                );
            });
        });

        card.read_with(cx, |card, _| {
            assert_eq!(
                card.typefaces.as_ref(),
                [FineTuneTypeface::new("inter", "Inter")]
            );
        });
    }
}