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
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
//! Conversation list: new, switch, rename, archive, delete, and search.
//!
//! The application owns the threads (grouped into sections such as Today /
//! Yesterday / Earlier) and the active thread ID; the list owns only its
//! native search input, the "show archived" toggle, which row holds keyboard
//! focus, and the flattened snapshot it virtualizes. Every intent — selecting,
//! creating, renaming, archiving, deleting — is a typed [`ThreadListEvent`]
//! keyed by stable ID.
//!
//! Two constraints shape the rendering. Construction is bounded by the visible
//! window: section headers and surviving threads are flattened into
//! `ThreadRow`s once, in the setters that can change them, and `gpui::list`
//! asks for rows by index — a ten-thousand-thread snapshot builds a screenful.
//! And rows are uniform: Rename / Archive / Delete live in an upstream popup
//! menu anchored to the row's ellipsis rather than in a toolbar revealed
//! inside the row, so opening one never changes a row's height or shifts the
//! conversations below it, and dismissal plus focus return come from upstream.
use crate::cues::{self, Cue};
use crate::scrolling::PolicyScrollbarExt as _;
use crate::{
control::composed_button,
resolved_layout::ResolvedLayoutKey,
surface::{eyebrow, icon_button},
theme::SemanticStyledExt as _,
};
use gpui::{
Anchor, AnyElement, App, AppContext as _, ClickEvent, Context, DismissEvent, ElementId, Entity,
EventEmitter, FocusHandle, Focusable, InteractiveElement as _, IntoElement, KeyDownEvent,
ListAlignment, ListOffset, ListState, ParentElement as _, Pixels, Render, RenderOnce, Role,
SharedString, StatefulInteractiveElement as _, Styled as _, Subscription, WeakEntity, Window,
div, list, prelude::FluentBuilder as _,
};
use gpui_base::StyledExt as _;
use gpui_component::{
ActiveTheme as _, IconName, h_flex,
input::{Input, InputEvent, InputState},
menu::{PopupMenu, PopupMenuItem},
popover::Popover,
v_flex,
};
use std::{rc::Rc, sync::Arc};
/// One conversation with stable identity.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreadItem {
id: SharedString,
title: SharedString,
subtitle: Option<SharedString>,
archived: bool,
}
impl ThreadItem {
/// Creates a thread with a stable identifier and visible title.
pub fn new(id: impl Into<SharedString>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
subtitle: None,
archived: false,
}
}
/// Adds secondary text (a timestamp or the last message).
pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
/// Marks the thread archived; archived threads hide until requested.
pub fn archived(mut self, archived: bool) -> Self {
self.archived = archived;
self
}
/// Returns the stable thread identifier.
pub fn id(&self) -> &SharedString {
&self.id
}
/// Returns the visible title.
pub fn title(&self) -> &SharedString {
&self.title
}
/// Returns the secondary text, when present.
pub fn subtitle_text(&self) -> Option<&SharedString> {
self.subtitle.as_ref()
}
/// Whether the thread is archived.
pub fn is_archived(&self) -> bool {
self.archived
}
fn matches(&self, query: &str) -> bool {
if query.is_empty() {
return true;
}
let needle = query.to_lowercase();
self.title.to_lowercase().contains(&needle)
|| self
.subtitle
.as_ref()
.is_some_and(|subtitle| subtitle.to_lowercase().contains(&needle))
}
}
/// A labeled group of threads (for example "Today").
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThreadSection {
id: SharedString,
label: SharedString,
items: Vec<ThreadItem>,
}
impl ThreadSection {
/// Creates a section with a stable identifier and visible label.
pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
items: Vec::new(),
}
}
/// Sets the section's threads, in display order.
pub fn items(mut self, items: impl IntoIterator<Item = ThreadItem>) -> Self {
self.items = items.into_iter().collect();
self
}
/// Returns the stable section identifier.
pub fn id(&self) -> &SharedString {
&self.id
}
/// Returns the visible label.
pub fn label(&self) -> &SharedString {
&self.label
}
/// Returns the section's threads.
pub fn thread_items(&self) -> &[ThreadItem] {
&self.items
}
}
/// An interaction emitted by [`ThreadList`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ThreadListEvent {
/// The user chose a thread.
Selected {
/// Stable thread identifier.
id: SharedString,
},
/// The user asked for a new conversation.
NewRequested,
/// The user wants to rename a thread.
RenameRequested {
/// Stable thread identifier.
id: SharedString,
},
/// The user wants to archive a thread.
ArchiveRequested {
/// Stable thread identifier.
id: SharedString,
},
/// The user wants to restore an archived thread.
UnarchiveRequested {
/// Stable thread identifier.
id: SharedString,
},
/// The user wants to delete a thread.
DeleteRequested {
/// Stable thread identifier.
id: SharedString,
},
/// The search query changed.
QueryChanged {
/// The complete current query.
query: SharedString,
},
}
fn sections_are_well_formed(sections: &[ThreadSection]) -> bool {
let mut section_ids = std::collections::HashSet::new();
let mut item_ids = std::collections::HashSet::new();
sections.iter().all(|section| {
section_ids.insert(§ion.id)
&& section.items.iter().all(|item| item_ids.insert(&item.id))
})
}
/// One row of the flattened snapshot the virtual list indexes into.
///
/// Sections are flattened rather than nested because a list that virtualizes
/// sections still builds every thread inside a visible section. Headers travel
/// as rows of their own so one list owns the whole document and a header
/// scrolls away with the threads it labels.
#[derive(Clone, Debug, PartialEq, Eq)]
enum ThreadRow {
/// A section eyebrow.
Header {
/// Stable section identifier, used to re-find the row after remeasure.
section_id: SharedString,
/// The section's visible label.
label: SharedString,
},
/// A conversation and its 1-based position among the visible threads.
Thread {
/// The thread itself; cloning is three refcount bumps and a bool.
item: ThreadItem,
/// 1-based position across the whole listbox, for `aria-posinset`.
position: usize,
},
}
impl ThreadRow {
/// The stable thread ID, for rows that are threads.
fn thread_id(&self) -> Option<&SharedString> {
match self {
Self::Header { .. } => None,
Self::Thread { item, .. } => Some(&item.id),
}
}
/// Identity that survives a rebuild, so a scroll anchor can be re-found.
///
/// Section and thread identifiers live in separate namespaces and may
/// legitimately collide, so the kind travels with the ID.
fn anchor(&self) -> (bool, SharedString) {
match self {
Self::Header { section_id, .. } => (true, section_id.clone()),
Self::Thread { item, .. } => (false, item.id.clone()),
}
}
}
/// Row construction counts for one draw.
///
/// The whole point of virtualizing is that a draw builds a window, not a
/// snapshot, so the bound has to be observable. Counting is test-only:
/// production builds carry neither the field nor the branch.
#[cfg(test)]
#[derive(Default, Clone)]
struct ThreadConstructionCounts {
rows: std::rc::Rc<std::cell::Cell<usize>>,
threads: std::rc::Rc<std::cell::Cell<usize>>,
}
#[cfg(test)]
impl ThreadConstructionCounts {
/// Opens a draw. Counts describe one draw, never a running total.
fn start_draw(&self) {
self.rows.set(0);
self.threads.set(0);
}
fn count_row(&self) {
self.rows.set(self.rows.get() + 1);
}
fn count_thread(&self) {
self.threads.set(self.threads.get() + 1);
}
}
/// Everything a row needs that does not come from the row itself.
///
/// The virtual list hands its item renderer an `&mut App`, not this entity's
/// context, so intents travel back through a weak handle rather than through
/// `cx.listener`. Values are snapshotted per draw: changing the active or
/// focused thread notifies, which rebuilds this and re-renders the window.
#[derive(Clone)]
struct RowRenderer {
component: SharedString,
owner: WeakEntity<ThreadList>,
/// The listbox's single focus handle; the popup menu returns focus here.
list_focus: FocusHandle,
active: Option<SharedString>,
focused: Option<SharedString>,
visible_threads: usize,
/// The list's glide-hover state when the gliding highlight is on;
/// rows feed it their bounds and hover instead of painting hover.
glide: Option<gpui::Entity<crate::glide::GlideHover>>,
#[cfg(test)]
construction: ThreadConstructionCounts,
}
/// The inset every rectangle in the list shares — the new-chat control,
/// the search field, and each row — measured from the panel's edge.
fn row_gutter(cx: &App) -> gpui::Pixels {
cx.theme().semantic_tokens().spacing.sm
}
/// The inset from a row's own edge to its text. Section labels stand
/// inside the same gutter and add it too, so headings and row titles
/// share one left edge.
fn row_text_inset(cx: &App) -> gpui::Pixels {
cx.theme().semantic_tokens().spacing.xs
}
impl RowRenderer {
fn render(&self, row: &ThreadRow, window: &mut Window, cx: &mut App) -> AnyElement {
#[cfg(test)]
self.construction.count_row();
match row {
ThreadRow::Header { section_id, label } => self.render_header(section_id, label, cx),
ThreadRow::Thread { item, position } => self.render_thread(item, *position, window, cx),
}
}
fn render_header(
&self,
section_id: &SharedString,
label: &SharedString,
cx: &mut App,
) -> AnyElement {
let tokens = cx.theme().semantic_tokens();
// A section label sits on the same left edge as the row titles it
// names: both stand inside the list's gutter and add the row's own
// text inset.
let header_key = section_id.clone();
let header_label_key = section_id.clone();
let section_id =
ElementId::from((ElementId::from(self.component.clone()), section_id.clone()));
div()
.id((section_id, "header"))
.debug_selector(move || format!("thread-section-{header_key}"))
// Flattening costs the containment a nested group would give, so
// the header keeps an accessible node of its own: the boundary is
// still announced, it just no longer wraps its options.
.role(Role::Label)
.aria_label(label.clone())
.w_full()
.min_w_0()
.border_1()
.border_color(cx.theme().transparent)
.px(row_text_inset(cx))
.pt(tokens.spacing.sm)
.pb(tokens.spacing.xxs)
.child({
div()
.debug_selector(move || format!("thread-section-label-{header_label_key}"))
.child(eyebrow(label.clone(), cx))
})
.into_any_element()
}
fn render_thread(
&self,
item: &ThreadItem,
position: usize,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
#[cfg(test)]
self.construction.count_thread();
let tokens = cx.theme().semantic_tokens();
let root_id = ElementId::from(self.component.clone());
let item_id = ElementId::from((root_id.clone(), item.id.clone()));
let selected = self.active.as_ref() == Some(&item.id);
// The marker a mounted list starts with is placed settled; a row the
// user activates fades its surface in once at the quick tempo. Only
// constructed rows reach here, so offscreen rows spend nothing, and
// a row scrolled out and back keeps its acknowledged state.
let acknowledged = crate::motion::acknowledged_state(
ElementId::from((item_id.clone(), "active-ack")),
selected as u64,
window,
cx,
);
let focused = self.focused.as_ref() == Some(&item.id);
let accessibility_label: SharedString = if item.archived {
format!("{}, archived", item.title).into()
} else {
item.title.clone()
};
let debug_id = item.id.clone();
let select_id = item.id.clone();
let owner = self.owner.clone();
// The whole row — trailing actions included — is one selection
// surface: an inset fill at the nesting-rule radius against the
// list's panel, hover covering everything inside it, and the
// acknowledged fade playing on the fill. The select control keeps
// the semantics and the keyboard focus ring; it paints nothing.
// With the gliding highlight on, the row paints no hover of its
// own and reports its geometry and hover to the shared state.
let row_debug_id = item.id.clone();
let base = div()
.id((item_id.clone(), "row"))
.debug_selector(move || format!("thread-row-{row_debug_id}"));
crate::surface::selection_surface(
base,
selected,
tokens.radius.lg,
tokens.spacing.xs,
self.glide.as_ref().map(|glide| (&item.id, glide)),
cx,
)
// The acknowledged fade plays on the fill alone — the row's
// content never dims — by re-stating the selected background
// at the acknowledgment's alpha over the surface's own.
.when(selected, |row| {
row.bg(cx.theme().list_active.opacity(acknowledged))
})
// One rect is the row: selection fills it, the keyboard ring
// outlines it, and the hover highlight covers it — trailing
// actions included. A ring around the label alone stopped
// before the ellipsis and made focus look unlike selection.
.border_1()
.border_color(if focused {
cx.theme().ring
} else {
cx.theme().transparent
})
.w_full()
.min_w_0()
.flex()
.items_center()
.gap(tokens.spacing.xxs)
.mb(tokens.spacing.xxs)
.pr(tokens.spacing.xxs)
.child(
composed_button((item_id.clone(), "select"), accessibility_label)
.debug_selector(move || format!("thread-{debug_id}"))
.role(Role::ListBoxOption)
.selected(selected)
.aria_selected(selected)
.aria_position_in_set(position)
.aria_size_of_set(self.visible_threads)
// Keyboard focus stays on the listbox and travels as an
// active descendant, so a row scrolling out of the window
// cannot take the focus with it.
.when(focused, |button| button.aria_active_descendant())
.tab_stop(false)
.flex_1()
.min_w_0()
.justify_start()
.px(row_text_inset(cx))
.py(tokens.spacing.xs)
.child(
v_flex()
.w_full()
.min_w_0()
.items_start()
.child({
let title_debug = item.id.clone();
div()
.debug_selector(move || format!("thread-title-{title_debug}"))
.w_full()
.truncate()
.text_token(tokens.typography.sm)
.text_color(cx.theme().foreground)
.child(item.title.clone())
})
.when_some(item.subtitle.clone(), |this, subtitle| {
this.child(crate::surface::hint(subtitle, cx).w_full().truncate())
}),
)
.on_click(move |_, window, cx| {
let _ = owner.update(cx, |this, cx| {
this.focus_thread(select_id.clone(), window, cx);
cx.emit(ThreadListEvent::Selected {
id: select_id.clone(),
});
cues::emit(cx, Cue::ThreadSelected);
});
}),
)
.child(self.render_actions_menu(item, item_id, window, cx))
.into_any_element()
}
/// The row's ellipsis and the popup menu it opens.
///
/// Upstream owns the popup: `Popover` owns the open lifecycle, placement,
/// and outside dismissal, and `PopupMenu` owns the items, their keyboard
/// model, Escape, and the return of focus to the listbox. The row keeps no
/// "which row is open" state, so no row can change height.
fn render_actions_menu(
&self,
item: &ThreadItem,
item_id: ElementId,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let tokens = cx.theme().semantic_tokens();
let more_debug_id = item.id.clone();
let list_focus = self.list_focus.clone();
let owner = self.owner.clone();
let action_id = item.id.clone();
let archived = item.archived;
// The menu is rebuilt on each open, so the builder clones rather than
// moves: it must be able to run again.
ThreadActionsMenu {
id: ElementId::from((item_id.clone(), "more-menu")),
trigger: icon_button(
(item_id, "more"),
IconName::Ellipsis,
format!("More actions for {}", item.title),
window,
cx,
)
.debug_selector(move || format!("thread-more-{more_debug_id}"))
.rounded(tokens.radius.sm),
build: Rc::new(move |menu, _, _| {
let rename_id = action_id.clone();
let archive_id = action_id.clone();
let delete_id = action_id.clone();
let rename_owner = owner.clone();
let archive_owner = owner.clone();
let delete_owner = owner.clone();
menu.action_context(list_focus.clone())
.item(PopupMenuItem::new("Rename").on_click(move |_, _, cx| {
let _ = rename_owner.update(cx, |_, cx| {
cx.emit(ThreadListEvent::RenameRequested {
id: rename_id.clone(),
});
});
}))
.item(
PopupMenuItem::new(if archived { "Unarchive" } else { "Archive" })
.on_click(move |_, _, cx| {
let _ = archive_owner.update(cx, |_, cx| {
cx.emit(if archived {
ThreadListEvent::UnarchiveRequested {
id: archive_id.clone(),
}
} else {
ThreadListEvent::ArchiveRequested {
id: archive_id.clone(),
}
});
});
}),
)
.item(PopupMenuItem::new("Delete").on_click(move |_, _, cx| {
let _ = delete_owner.update(cx, |_, cx| {
cx.emit(ThreadListEvent::DeleteRequested {
id: delete_id.clone(),
});
});
}))
}),
}
}
}
/// The menu entity a row keeps between the frame that opens it and the frame
/// that dismisses it.
#[derive(Default)]
struct ThreadActionsMenuState {
open: bool,
menu: Option<Entity<PopupMenu>>,
}
/// Fills a freshly built menu with a row's actions.
///
/// Called on every open rather than once, so an item's label and handler are
/// always the ones the current snapshot implies — Archive and Unarchive are
/// the same item under two names.
type ThreadMenuBuilder = dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu;
/// An ellipsis trigger and the popup menu it opens.
///
/// This composes upstream `Popover` and `PopupMenu` by hand rather than using
/// the `DropdownMenu` trait, which is implemented only for upstream's own
/// `Button` — and that button takes its accessible name from a visible label,
/// which an icon-only control does not have. Everything else, including
/// rebuilding the menu on each open so item state is never stale, follows
/// `DropdownMenuPopover`.
#[derive(IntoElement)]
struct ThreadActionsMenu {
id: ElementId,
trigger: gpui_base::Button,
build: Rc<ThreadMenuBuilder>,
}
impl RenderOnce for ThreadActionsMenu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let build = self.build.clone();
let state = window.use_keyed_state((self.id.clone(), "menu"), cx, |_, _| {
ThreadActionsMenuState::default()
});
let open = state.read(cx).open;
let trigger_state = state.clone();
let trigger = self.trigger.on_click(move |event, _, cx| {
if matches!(event, ClickEvent::Keyboard(_)) {
trigger_state.update(cx, |state, cx| {
state.open = !state.open;
if !state.open {
state.menu = None;
}
cx.notify();
});
}
});
let open_state = state.clone();
Popover::new(self.id)
// The menu paints its own surface, and dismisses its own outside
// presses, so the popover must add neither.
.appearance(false)
.overlay_closable(false)
// The menu's top-right corner meets the trigger's, so it drops
// downward flush with the row's trailing edge.
.anchor(Anchor::TopRight)
.open(open)
.on_open_change(move |open, _, cx| {
open_state.update(cx, |state, cx| {
state.open = *open;
if !open {
state.menu = None;
}
cx.notify();
});
})
.trigger(trigger)
.content(move |_, window, cx| {
if let Some(menu) = state.read(cx).menu.clone() {
return div()
.debug_selector(|| "thread-actions-menu".into())
.child(menu);
}
let build = build.clone();
let menu =
PopupMenu::build(window, cx, move |menu, window, cx| build(menu, window, cx));
state.update(cx, |this, _| this.menu = Some(menu.clone()));
menu.focus_handle(cx).focus(window, cx);
window
.subscribe(&menu, cx, {
let state = state.clone();
move |_, _: &DismissEvent, _, cx| {
state.update(cx, |state, cx| {
state.open = false;
state.menu = None;
cx.notify();
});
}
})
.detach();
div()
.debug_selector(|| "thread-actions-menu".into())
.child(menu)
})
}
}
/// A controlled conversation list with native search.
///
/// # Example
///
/// ```no_run
/// # use gpui_ai::prelude::*;
/// # use gpui::AppContext;
/// # fn example(window: &mut gpui::Window, cx: &mut gpui::App) {
/// let threads = cx.new(|cx| ThreadList::new("threads", window, cx));
/// threads.update(cx, |threads, cx| {
/// threads.set_sections(
/// [ThreadSection::new("today", "Today").items([
/// ThreadItem::new("t-1", "Supplier pricing").subtitle("2 min ago"),
/// ])],
/// cx,
/// );
/// threads.set_active(Some("t-1"), cx);
/// });
/// # }
/// ```
pub struct ThreadList {
/// 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,
sections: Arc<[ThreadSection]>,
active: Option<SharedString>,
query: SharedString,
input: Entity<InputState>,
show_archived: bool,
/// The flattened visible snapshot; rebuilt in setters, never in `Render`.
rows: Arc<[ThreadRow]>,
/// Threads in `rows`, for `aria-setsize`.
visible_threads: usize,
/// Threads that survive the archive filter, whatever the query — the
/// difference between "no conversations yet" and "nothing matches".
candidates: usize,
/// The row holding keyboard focus, by stable ID. Distinct from `active`,
/// which the application owns: moving focus does not select.
focused_thread: Option<SharedString>,
/// The listbox's single tab stop; options are not focusable themselves.
list_focus: FocusHandle,
list_state: ListState,
/// One highlight glides between rows instead of per-row hover fills.
/// Default on; [`Self::set_hover_glide`] restores per-row hover.
hover_glide: bool,
/// Rem size the retained row heights were measured against.
resolved_layout: ResolvedLayoutKey,
#[cfg(test)]
construction: ThreadConstructionCounts,
_input_subscription: Subscription,
}
impl ThreadList {
/// Chooses between the gliding hover highlight (the default) and
/// classic per-row hover fills.
pub fn set_hover_glide(&mut self, enabled: bool, cx: &mut Context<Self>) {
if self.hover_glide != enabled {
self.hover_glide = enabled;
cx.notify();
}
}
/// Creates an empty list around a native search input.
pub fn new(id: impl Into<SharedString>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let input = cx.new(|cx| InputState::new(window, cx).placeholder("Search conversations"));
let subscription =
cx.subscribe_in(&input, window, |this, input, event: &InputEvent, _, cx| {
if let InputEvent::Change = event {
let query: SharedString = input.read(cx).value().to_string().into();
this.apply_query(query, cx);
}
});
Self {
style: gpui::StyleRefinement::default(),
id: id.into(),
sections: Arc::from([]),
active: None,
query: "".into(),
input,
show_archived: false,
rows: Arc::from([]),
visible_threads: 0,
candidates: 0,
focused_thread: None,
list_focus: cx.focus_handle(),
list_state: ListState::new(0, ListAlignment::Top, Pixels::ZERO),
hover_glide: true,
resolved_layout: ResolvedLayoutKey::default(),
#[cfg(test)]
construction: ThreadConstructionCounts::default(),
_input_subscription: subscription,
}
}
/// Replaces the controlled sections. Snapshots with duplicate section or
/// thread IDs are ignored so identities can never alias.
pub fn set_sections(
&mut self,
sections: impl IntoIterator<Item = ThreadSection>,
cx: &mut Context<Self>,
) {
let sections: Arc<[ThreadSection]> = sections.into_iter().collect();
if !sections_are_well_formed(§ions) || self.sections == sections {
return;
}
self.sections = sections;
self.rebuild_rows();
cx.notify();
}
/// Sets the active thread by stable ID (or none).
pub fn set_active(&mut self, id: Option<impl Into<SharedString>>, cx: &mut Context<Self>) {
let id = id.map(Into::into);
if self.active != id {
self.active = id;
// Selection repaints a row; it never changes its height, so the
// measured snapshot and the reader's scroll position both stand.
cx.notify();
}
}
/// Returns the active thread ID.
pub fn active_id(&self) -> Option<&SharedString> {
self.active.as_ref()
}
/// Shows or hides archived threads.
pub fn set_show_archived(&mut self, show: bool, cx: &mut Context<Self>) {
if self.show_archived != show {
self.show_archived = show;
self.rebuild_rows();
cx.notify();
}
}
/// Whether archived threads are shown.
pub fn shows_archived(&self) -> bool {
self.show_archived
}
/// Sets the search query programmatically.
pub fn set_query(
&mut self,
query: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let query = query.into();
if self.query == query {
return;
}
self.input.update(cx, |input, cx| {
input.set_value(query.to_string(), window, cx)
});
// InputState suppresses Change for programmatic values.
self.apply_query(query, cx);
}
/// Returns the current search query.
pub fn query(&self) -> &SharedString {
&self.query
}
/// Moves keyboard focus into the search input.
pub fn focus_search(&self, window: &mut Window, cx: &mut App) {
self.input.read(cx).focus_handle(cx).focus(window, cx);
}
/// Scrolls the list to its end (the oldest visible thread).
pub fn scroll_to_end(&mut self, cx: &mut Context<Self>) {
self.list_state.scroll_to_end();
cx.notify();
}
fn apply_query(&mut self, query: SharedString, cx: &mut Context<Self>) {
if self.query == query {
return;
}
self.query = query.clone();
self.rebuild_rows();
cx.emit(ThreadListEvent::QueryChanged { query });
cx.notify();
}
/// Rebuilds the flattened row snapshot the virtual list indexes into.
///
/// Called from every setter that can change what is visible, and never
/// from `Render`: the list asks for rows by index during layout, so a
/// snapshot rebuilt per frame would cost exactly what virtualizing saves.
fn rebuild_rows(&mut self) {
let sections = self.sections.clone();
let mut rows: Vec<ThreadRow> = Vec::new();
let mut visible_threads = 0usize;
let mut candidates = 0usize;
for section in sections.iter() {
let mut header_written = false;
for item in section.items.iter() {
if !self.show_archived && item.archived {
continue;
}
candidates += 1;
if !item.matches(&self.query) {
continue;
}
if !header_written {
rows.push(ThreadRow::Header {
section_id: section.id.clone(),
label: section.label.clone(),
});
header_written = true;
}
visible_threads += 1;
rows.push(ThreadRow::Thread {
item: item.clone(),
position: visible_threads,
});
}
}
self.visible_threads = visible_threads;
self.candidates = candidates;
// Keyboard focus is an ID, not an index: it survives a reorder or a
// narrowing query as long as the thread it names is still visible.
if !self
.focused_thread
.as_ref()
.is_some_and(|id| rows.iter().any(|row| row.thread_id() == Some(id)))
{
self.focused_thread = None;
}
self.rows = rows.into();
self.list_state.reset(self.rows.len());
}
/// Re-measures the rows after the window's rem size changed.
///
/// Row heights cache text laid out at the previous rem, and neither a
/// snapshot nor a filter reports a zoom change. The row that was first on
/// screen stays first.
fn resolve_layout(&mut self, rem_size: Pixels, cx: &mut Context<Self>) {
if !self.resolved_layout.observe(rem_size) {
return;
}
let offset = self.list_state.logical_scroll_top();
let anchor = self
.rows
.get(offset.item_ix)
.map(|row| (row.anchor(), offset.offset_in_item));
self.list_state.remeasure();
if let Some((anchor, offset_in_item)) = anchor
&& let Some(item_ix) = self.rows.iter().position(|row| row.anchor() == anchor)
{
self.list_state.scroll_to(ListOffset {
item_ix,
offset_in_item,
});
}
cx.notify();
}
/// Flattened index of the row for `id`, when it is visible.
fn row_index_of(&self, id: &SharedString) -> Option<usize> {
self.rows.iter().position(|row| row.thread_id() == Some(id))
}
/// Moves the roving focus onto `id` and keeps that row on screen.
fn focus_thread(&mut self, id: SharedString, window: &mut Window, cx: &mut Context<Self>) {
if let Some(index) = self.row_index_of(&id) {
self.list_state.scroll_to_reveal_item(index);
}
self.focused_thread = Some(id);
self.list_focus.focus(window, cx);
cx.notify();
}
/// The stable IDs of the visible threads, in display order.
fn focusable_ids(&self) -> Vec<SharedString> {
self.rows
.iter()
.filter_map(|row| row.thread_id().cloned())
.collect()
}
/// Moves the roving focus by `delta` options, clamped at both ends.
///
/// Section headers and threads hidden by the archive filter or the query
/// are not options, so they are never landed on: only rows that are in the
/// snapshot can hold focus. Reaching an end stays there rather than
/// wrapping, so Home and End remain the only way to jump the list.
fn move_focus(&mut self, delta: isize, window: &mut Window, cx: &mut Context<Self>) {
let ids = self.focusable_ids();
if ids.is_empty() {
return;
}
let last = ids.len() - 1;
let next = match self
.focused_thread
.as_ref()
.and_then(|id| ids.iter().position(|candidate| candidate == id))
{
Some(current) => current.saturating_add_signed(delta).min(last),
None if delta < 0 => last,
None => 0,
};
let id = ids[next].clone();
self.focus_thread(id, window, cx);
}
/// Moves the roving focus to the first or last option.
fn focus_bound(&mut self, last: bool, window: &mut Window, cx: &mut Context<Self>) {
let ids = self.focusable_ids();
let Some(id) = (if last { ids.last() } else { ids.first() }) else {
return;
};
let id = id.clone();
self.focus_thread(id, window, cx);
}
/// Selects the focused option, the way clicking it would.
fn activate_focused(&mut self, cx: &mut Context<Self>) {
let Some(id) = self.focused_thread.clone() else {
return;
};
cx.emit(ThreadListEvent::Selected { id });
cues::emit(cx, Cue::ThreadSelected);
}
/// The listbox's keyboard model, on stable IDs.
///
/// Only reaches here when focus is inside the listbox, so the search
/// input's own caret keys are untouched. Escape is deliberately absent:
/// while a row's menu is open the menu holds focus and closes itself
/// first, and with no menu open Escape belongs to the application.
fn on_key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
match event.keystroke.key.as_str() {
"up" => self.move_focus(-1, window, cx),
"down" => self.move_focus(1, window, cx),
"home" => self.focus_bound(false, window, cx),
"end" => self.focus_bound(true, window, cx),
"enter" | "space" => self.activate_focused(cx),
_ => return,
}
cx.stop_propagation();
}
}
impl EventEmitter<ThreadListEvent> for ThreadList {}
impl Focusable for ThreadList {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.input.read(cx).focus_handle(cx)
}
}
impl gpui::Styled for ThreadList {
fn style(&mut self) -> &mut gpui::StyleRefinement {
&mut self.style
}
}
impl Render for ThreadList {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Measured row heights are only valid for the rem they were laid out
// at. Reading it here mutates nothing; the reaction is deferred so
// that render never notifies.
let rem_size = window.rem_size();
if !self.resolved_layout.matches(rem_size) {
cx.defer_in(window, move |this, _, cx| {
this.resolve_layout(rem_size, cx);
});
}
#[cfg(test)]
self.construction.start_draw();
let tokens = cx.theme().semantic_tokens();
let root_id = ElementId::from(self.id.clone());
let show_archived = self.show_archived;
let empty_message: Option<SharedString> = if self.candidates == 0 {
Some("No conversations yet".into())
} else if self.rows.is_empty() {
Some(format!("No conversations match “{}”", self.query).into())
} else {
None
};
let rows = self.rows.clone();
let glide = self
.hover_glide
.then(|| crate::glide::glide_hover_state(root_id.clone(), window, cx));
let renderer = RowRenderer {
component: self.id.clone(),
owner: cx.weak_entity(),
list_focus: self.list_focus.clone(),
active: self.active.clone(),
focused: self.focused_thread.clone(),
visible_threads: self.visible_threads,
glide: glide.clone(),
#[cfg(test)]
construction: self.construction.clone(),
};
let list_state = self.list_state.clone();
v_flex()
.id(root_id.clone())
.role(Role::Group)
.aria_label("Conversations")
.size_full()
.min_h_0()
.min_w_0()
.gap(tokens.spacing.sm)
.child(
h_flex()
.w_full()
.items_center()
.px(row_gutter(cx))
.gap(tokens.spacing.xs)
.child(
div().debug_selector(|| "thread-list-new".into()).child(
crate::control::outlined_control_with_icon(
(root_id.clone(), "new"),
"New chat",
IconName::Plus,
"New chat",
window,
cx,
)
.accessibility_id(format!("{}-new", self.id))
.on_click(cx.listener(|_, _, _, cx| {
cx.emit(ThreadListEvent::NewRequested);
})),
),
)
.child(div().flex_1())
.child(
icon_button(
(root_id.clone(), "archived"),
IconName::Inbox,
if show_archived {
"Hide archived conversations"
} else {
"Show archived conversations"
},
window,
cx,
)
.debug_selector(|| "thread-list-archived-toggle".into())
.selected(show_archived)
.aria_expanded(show_archived)
.on_click(cx.listener(|this, _, _, cx| {
let show = !this.show_archived;
this.set_show_archived(show, cx);
})),
),
)
.child(
h_flex()
.debug_selector(|| "thread-list-search".into())
.w_full()
.items_center()
.px(row_gutter(cx))
.child(
div()
.debug_selector(|| "thread-list-field".into())
.flex_1()
.min_w_0()
.child(Input::new(&self.input).prefix(IconName::Search)),
),
)
.child(
// The outer frame owns the flex constraint; the virtual list
// fills it, so a long snapshot scrolls a window of rows
// instead of growing the pane or building the whole document.
{
let frame = div()
.id((root_id.clone(), "list-frame"))
.debug_selector(|| "thread-list-scroll".into())
.relative()
.flex_1()
.min_h_0()
.w_full()
.overflow_hidden()
.policy_vertical_scrollbar(&list_state, cx);
// The gliding highlight paints beneath the rows; the
// frame's bounds anchor its coordinates.
match &glide {
Some(glide) => crate::glide::glide_frame(frame, glide).child(
crate::glide::glide_highlight(
root_id.clone(),
glide,
crate::surface::nested_radius(
tokens.radius.lg,
tokens.spacing.xs,
tokens.radius.sm,
),
"thread-list-glide",
window,
cx,
),
),
None => frame,
}
}
.child(
div()
.id((root_id.clone(), "list"))
.role(Role::ListBox)
.aria_label("Conversation list")
// The gutter every row stands on. A virtual list
// positions its items itself, so the inset is the
// scroll region's, not each row's.
.pl(row_gutter(cx))
// And the trailing side keeps the overlaid bar's lane
// clear, because these rows carry a menu at that edge
// and a bar drawn over it is a target nobody can hit.
.pr(row_gutter(cx).max(crate::scrolling::control_lane(cx)))
.track_focus(&self.list_focus)
.tab_stop(true)
.on_key_down(cx.listener(Self::on_key_down))
.size_full()
.min_h_0()
.when(!rows.is_empty(), |this| {
this.child(
list(list_state, move |index, window, cx| {
rows.get(index)
.map(|row| renderer.render(row, window, cx))
.unwrap_or_else(|| div().hidden().into_any_element())
})
.size_full(),
)
})
.when_some(empty_message, |this, message| {
this.child(
div()
.id((root_id, "empty"))
.debug_selector(|| "thread-list-empty".into())
.role(Role::Status)
.aria_label(message.clone())
.child(crate::surface::empty_state(
IconName::Inbox,
message.clone(),
Some("Start a chat to see it here".into()),
cx,
)),
)
}),
),
)
.refine_style(&self.style)
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{
Bounds, KeyUpEvent, Keystroke, Modifiers, Pixels, TestAppContext, VisualTestContext, px,
size,
};
use std::cell::Cell;
struct ThreadActionsMenuProbe {
activations: Rc<Cell<usize>>,
trigger_focus: FocusHandle,
}
impl Render for ThreadActionsMenuProbe {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let activations = self.activations.clone();
div().tab_group().child(ThreadActionsMenu {
id: "keyboard-actions".into(),
trigger: icon_button(
"keyboard-actions-trigger",
IconName::Ellipsis,
"More actions",
window,
cx,
)
.track_focus(&self.trigger_focus)
.debug_selector(|| "keyboard-actions-trigger".into()),
build: Rc::new(move |menu, _, _| {
let activations = activations.clone();
menu.item(PopupMenuItem::new("Rename").on_click(move |_, _, _| {
activations.set(activations.get() + 1);
}))
}),
})
}
}
#[test]
fn matching_is_case_insensitive_over_title_and_subtitle() {
let item = ThreadItem::new("t", "Supplier pricing").subtitle("Compared Alpenrose");
assert!(item.matches(""));
assert!(item.matches("PRICING"));
assert!(item.matches("alpenrose"));
assert!(!item.matches("delivery"));
}
#[test]
fn malformed_snapshots_are_rejected() {
let duplicate_items = [ThreadSection::new("a", "A")
.items([ThreadItem::new("t", "One"), ThreadItem::new("t", "Two")])];
assert!(!sections_are_well_formed(&duplicate_items));
let duplicate_sections = [
ThreadSection::new("a", "A"),
ThreadSection::new("a", "Again"),
];
assert!(!sections_are_well_formed(&duplicate_sections));
let fine = [
ThreadSection::new("a", "A").items([ThreadItem::new("t-1", "One")]),
ThreadSection::new("b", "B").items([ThreadItem::new("t-2", "Two")]),
];
assert!(sections_are_well_formed(&fine));
}
#[gpui::test]
fn row_action_menu_opens_and_activates_from_the_keyboard(cx: &mut TestAppContext) {
cx.update(crate::init);
let activations = Rc::new(Cell::new(0));
let (view, cx) = cx.add_window_view({
let activations = activations.clone();
move |_, cx| ThreadActionsMenuProbe {
activations,
trigger_focus: cx.focus_handle(),
}
});
let cx: &mut VisualTestContext = cx;
cx.update(|window, cx| window.draw(cx).clear(cx));
cx.update(|window, cx| {
let trigger_focus = view.read(cx).trigger_focus.clone();
trigger_focus.focus(window, cx);
window.draw(cx).clear(cx);
});
let enter = Keystroke::parse("enter").expect("Enter is a valid keystroke");
cx.simulate_event(KeyDownEvent {
keystroke: enter.clone(),
is_held: false,
prefer_character_input: false,
});
cx.simulate_event(KeyUpEvent { keystroke: enter });
cx.update(|window, cx| window.draw(cx).clear(cx));
assert!(
cx.debug_bounds("thread-actions-menu").is_some(),
"Enter on the named ellipsis button must open the popup"
);
cx.simulate_keystrokes("down enter");
cx.update(|window, cx| window.draw(cx).clear(cx));
assert_eq!(activations.get(), 1);
assert!(cx.debug_bounds("thread-actions-menu").is_none());
}
/// Rows tall enough that a 480pt viewport holds a couple of dozen, so the
/// window is unambiguously smaller than the snapshot.
const MEASURED_VIEWPORT: (f32, f32) = (320., 480.);
/// Threads per section in the large snapshot; ten sections of a thousand.
const LARGE_SECTION_SIZE: usize = 1_000;
const LARGE_SECTIONS: usize = 10;
#[gpui::test]
fn activating_a_thread_acknowledges_once_and_a_mounted_marker_is_settled(
cx: &mut TestAppContext,
) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| {
threads.set_sections(
[ThreadSection::new("today", "Today")
.items([ThreadItem::new("t-1", "One"), ThreadItem::new("t-2", "Two")])],
cx,
);
threads.set_active(Some("t-1"), cx);
});
});
redraw(&threads, cx);
cx.executor()
.advance_clock(std::time::Duration::from_secs(2));
redraw(&threads, cx);
crate::motion::take_reveal_frame_requests();
redraw(&threads, cx);
assert_eq!(
crate::motion::take_reveal_frame_requests(),
0,
"the marker the list mounts with is placed settled"
);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_active(Some("t-2"), cx));
});
redraw(&threads, cx);
assert!(
crate::motion::take_reveal_frame_requests() > 0,
"activating a thread must acknowledge the new marker"
);
}
fn large_sections() -> Vec<ThreadSection> {
(0..LARGE_SECTIONS)
.map(|section| {
ThreadSection::new(format!("s-{section}"), format!("Section {section}")).items(
(0..LARGE_SECTION_SIZE).map(|row| {
let index = section * LARGE_SECTION_SIZE + row;
ThreadItem::new(format!("t-{index:05}"), format!("Conversation {index}"))
.subtitle(format!("{index} min ago"))
}),
)
})
.collect()
}
/// Every rectangle in the list stands on one left edge, and every
/// label — row titles and the section names above them — stands on
/// one more. The feel review found four edges here instead of two.
#[gpui::test]
fn the_list_stands_on_one_left_edge(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(glide_sections(), cx));
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
let new_chat = bounds_of(cx, "thread-list-new".to_owned()).expect("new chat renders");
let field = bounds_of(cx, "thread-list-field".to_owned()).expect("search renders");
let row = bounds_of(cx, "thread-row-one".to_owned()).expect("a row renders");
assert_eq!(
(new_chat.left(), row.left()),
(field.left(), field.left()),
"the control, the field, and the rows share one gutter"
);
let header = bounds_of(cx, "thread-section-label-recent".to_owned())
.expect("the section label renders");
let title = bounds_of(cx, "thread-title-one".to_owned()).expect("a row title renders");
assert_eq!(
header.left(),
title.left(),
"a section label and the titles it names share one text edge"
);
}
/// Selection, keyboard focus, and hover all describe the same
/// rectangle. A ring drawn around the label alone stopped short of the
/// row's trailing actions and made focus look unlike selection.
#[gpui::test]
fn selection_focus_and_hover_describe_one_row_rect(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| {
threads.set_sections(glide_sections(), cx);
threads.set_active(Some(SharedString::from("one")), cx);
});
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
let row = bounds_of(cx, "thread-row-one".to_owned()).expect("the row renders");
let select = bounds_of(cx, "thread-one".to_owned()).expect("the select control renders");
assert!(
row.right() > select.right(),
"the row rect reaches past its label into the trailing actions: row {row:?}, label {select:?}"
);
cx.simulate_mouse_move(row.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
let highlight =
bounds_of(cx, "thread-list-glide".to_owned()).expect("hovering highlights the row");
assert_eq!(
highlight.size.width,
row.size.width - px(2.),
"the hover highlight covers the row inside its own border"
);
}
fn glide_sections() -> Vec<ThreadSection> {
vec![ThreadSection::new("recent", "Recent").items([
ThreadItem::new("one", "One"),
ThreadItem::new("two", "Two"),
ThreadItem::new("three", "Three"),
])]
}
/// A row's trailing control never sits under the scrollbar.
///
/// The bar is sixteen pixels and overlaid by default, taking no layout
/// space - which is right for prose, and wrong for the menu at a row's
/// trailing edge. The row's gutter was eight, so the bar reached half its
/// width into the row and covered a target nobody could then hit. On a
/// list whose bar is always shown, that is the resting state.
///
/// Measured against the scroll frame rather than the row, because the bar
/// is positioned from the frame's trailing edge and that is the only edge
/// the arithmetic can be anchored to.
#[gpui::test]
fn a_row_keeps_its_controls_clear_of_the_scrollbar(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(glide_sections(), cx));
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
let frame = bounds_of(cx, "thread-list-scroll".to_owned()).expect("the list scrolls");
let row = bounds_of(cx, "thread-row-one".to_owned()).expect("a row renders");
let bar = gpui_base::Scrollbar::width();
let gutter = cx.update(|_, cx| super::row_gutter(cx));
assert!(
frame.right() - row.right() >= bar,
"the bar covers this row's trailing edge: row ends {:?} from the frame, bar is {bar:?}",
frame.right() - row.right()
);
// And the reading column keeps what it had: the lane comes off the
// trailing side only.
assert_eq!(
row.left() - frame.left(),
gutter,
"the leading gutter is unchanged"
);
}
/// The lag probe: a fresh hover seats the highlight on its row with no
/// glide from stale geometry, the next hover departs from the previous
/// row and travels between the two, and the highlight arrives exactly
/// on the hovered row once the glide tempo has elapsed.
#[gpui::test]
fn glide_highlight_appears_on_hover_and_chases_the_next_row(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(glide_sections(), cx));
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
assert!(
bounds_of(cx, "thread-list-glide".to_owned()).is_none(),
"no highlight before anything is hovered"
);
let row_one = bounds_of(cx, "thread-row-one".to_owned()).expect("row one renders");
cx.simulate_mouse_move(row_one.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
let highlight = bounds_of(cx, "thread-list-glide".to_owned())
.expect("hovering a row mounts the highlight");
assert_eq!(
highlight.size.height,
row_one.size.height - px(2.),
"a fresh hover seats the highlight on its row, inside its border"
);
assert_eq!(highlight.origin.y, row_one.origin.y + px(1.));
let row_three = bounds_of(cx, "thread-row-three".to_owned()).expect("row three renders");
cx.simulate_mouse_move(row_three.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
let start = bounds_of(cx, "thread-list-glide".to_owned()).expect("highlight persists");
assert_eq!(
start.origin.y,
row_one.origin.y + px(1.),
"the glide departs from the previously hovered row"
);
cx.executor()
.advance_clock(std::time::Duration::from_millis(10));
cx.update(|window, cx| window.draw(cx).clear(cx));
let moving = bounds_of(cx, "thread-list-glide".to_owned()).expect("highlight persists");
assert!(
moving.top() > row_one.top() && moving.top() < row_three.top(),
"mid-glide the highlight sits between the rows: {moving:?}"
);
cx.executor()
.advance_clock(std::time::Duration::from_secs(1));
cx.update(|window, cx| window.draw(cx).clear(cx));
let settled = bounds_of(cx, "thread-list-glide".to_owned()).expect("highlight persists");
assert_eq!(
settled.origin.y,
row_three.origin.y + px(1.),
"the highlight arrives on the hovered row"
);
// Settled means settled: nothing moves on further draws.
cx.update(|window, cx| window.draw(cx).clear(cx));
assert_eq!(
bounds_of(cx, "thread-list-glide".to_owned()),
Some(settled),
"a settled highlight holds its row"
);
}
/// A hovered row that leaves the list takes its highlight with it.
///
/// Hover is cleared by the row's own listener, and an unmounted row has
/// no listener to fire: filtering the list under a stationary pointer
/// used to leave the highlight painting the departed row's last
/// geometry over whatever moved into that space.
#[gpui::test]
fn a_filtered_away_row_takes_its_highlight_with_it(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(glide_sections(), cx));
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
let row = bounds_of(cx, "thread-row-three".to_owned()).expect("row three renders");
cx.simulate_mouse_move(row.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
assert!(
bounds_of(cx, "thread-list-glide".to_owned()).is_some(),
"the hovered row is highlighted"
);
// Filter it away without moving the pointer.
cx.update(|window, cx| {
threads.update(cx, |threads, cx| threads.set_query("One", window, cx));
window.draw(cx).clear(cx);
});
cx.update(|window, cx| window.draw(cx).clear(cx));
assert!(
bounds_of(cx, "thread-row-three".to_owned()).is_none(),
"the filter really removed the row"
);
assert!(
bounds_of(cx, "thread-list-glide".to_owned()).is_none(),
"and its highlight left with it, rather than painting over what took its place"
);
}
/// Any reduced preference snaps the highlight between rows: the move
/// lands on the very next draw with no clock advanced.
#[gpui::test]
fn glide_snaps_between_rows_under_a_reduced_preference(cx: &mut TestAppContext) {
cx.update(|cx| {
crate::motion::MotionTokens::default()
.with_preference(crate::motion::MotionPreference::Crossfade)
.set(cx);
crate::init(cx);
});
let (threads, cx) =
cx.add_window_view(|window, cx| ThreadList::new("measured", window, cx));
let cx: &mut VisualTestContext = cx;
cx.simulate_resize(size(px(MEASURED_VIEWPORT.0), px(MEASURED_VIEWPORT.1)));
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(glide_sections(), cx));
});
let _ = redraw(&threads, cx);
let _ = redraw(&threads, cx);
let row_one = bounds_of(cx, "thread-row-one".to_owned()).expect("row one renders");
cx.simulate_mouse_move(row_one.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
let row_three = bounds_of(cx, "thread-row-three".to_owned()).expect("row three renders");
cx.simulate_mouse_move(row_three.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
let snapped = bounds_of(cx, "thread-list-glide".to_owned())
.expect("a reduced preference still highlights the hovered row");
assert_eq!(
snapped.origin.y,
row_three.origin.y + px(1.),
"a reduced preference snaps the highlight to the hovered row"
);
}
/// Per-row hover is the builder escape: with the glide disabled the
/// highlight never mounts.
#[gpui::test]
fn disabling_the_glide_restores_per_row_hover(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| {
threads.set_sections(glide_sections(), cx);
threads.set_hover_glide(false, cx);
});
});
let _ = redraw(&threads, cx);
let row_one = bounds_of(cx, "thread-row-one".to_owned()).expect("row one renders");
cx.simulate_mouse_move(row_one.center(), None, Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
assert!(
bounds_of(cx, "thread-list-glide".to_owned()).is_none(),
"no gliding highlight when the builder disables it"
);
}
fn measured_list(cx: &mut TestAppContext) -> (Entity<ThreadList>, &mut VisualTestContext) {
cx.update(crate::init);
let (threads, cx) =
cx.add_window_view(|window, cx| ThreadList::new("measured", window, cx));
let cx: &mut VisualTestContext = cx;
cx.simulate_resize(size(px(MEASURED_VIEWPORT.0), px(MEASURED_VIEWPORT.1)));
(threads, cx)
}
/// Redraws and reports `(rows, threads)` built by that one draw.
fn redraw(threads: &Entity<ThreadList>, cx: &mut VisualTestContext) -> (usize, usize) {
cx.update(|window, cx| {
threads.update(cx, |_, cx| cx.notify());
window.draw(cx).clear(cx);
});
threads.read_with(cx, |threads, _| {
(
threads.construction.rows.get(),
threads.construction.threads.get(),
)
})
}
fn bounds_of(cx: &mut VisualTestContext, selector: String) -> Option<Bounds<Pixels>> {
cx.debug_bounds(Box::leak(selector.into_boxed_str()))
}
/// The bound is only a bound if construction tracks the window rather than
/// the snapshot. Ten thousand conversations must build a screenful on the
/// first draw and a screenful again after scrolling a page, and the rows
/// that were not built must be absent from the frame rather than merely
/// invisible.
#[gpui::test]
fn ten_thousand_threads_build_only_the_visible_window(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(large_sections(), cx));
});
let total = LARGE_SECTIONS * LARGE_SECTION_SIZE;
let (rows, built) = redraw(&threads, cx);
assert!(built > 0, "the first screen must build some threads");
assert!(
rows <= 32,
"a {total}-thread snapshot built {rows} rows on the first draw"
);
assert!(
bounds_of(cx, "thread-t-00000".to_owned()).is_some(),
"the first thread should be on screen"
);
for far in ["thread-t-00500", "thread-t-05000", "thread-t-09999"] {
assert!(
bounds_of(cx, far.to_owned()).is_none(),
"{far} is far outside the window and must not be built"
);
}
// One page down: still a window, and a different one.
let page = threads.read_with(cx, |threads, _| {
threads.list_state.logical_scroll_top().item_ix + rows
});
cx.update(|_, cx| {
threads.update(cx, |threads, _| {
threads.list_state.scroll_to(ListOffset {
item_ix: page,
offset_in_item: Pixels::ZERO,
});
});
});
let (scrolled_rows, scrolled_threads) = redraw(&threads, cx);
assert!(scrolled_threads > 0, "the second page must build threads");
assert!(
scrolled_rows <= 32,
"one page down built {scrolled_rows} rows of {total}"
);
assert!(
bounds_of(cx, "thread-t-00000".to_owned()).is_none(),
"the first thread scrolled out and must no longer be built"
);
// Counts describe one draw, never a running total.
let (again, _) = redraw(&threads, cx);
assert_eq!(again, scrolled_rows, "construction must not accumulate");
}
/// The snapshot is derived once per change, so the flattening itself is
/// the contract: headers only for sections that kept a thread, positions
/// numbered across the whole listbox, and no row for a hidden thread.
#[gpui::test]
fn the_flattened_snapshot_carries_headers_and_listbox_positions(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| {
threads.set_sections(
[
ThreadSection::new("today", "Today").items([
ThreadItem::new("a", "Alpha"),
ThreadItem::new("b", "Beta").archived(true),
]),
ThreadSection::new("earlier", "Earlier")
.items([ThreadItem::new("c", "Gamma")]),
ThreadSection::new("empty", "Empty")
.items([ThreadItem::new("d", "Delta").archived(true)]),
],
cx,
);
});
});
threads.read_with(cx, |threads, _| {
assert_eq!(
threads.rows.as_ref(),
&[
ThreadRow::Header {
section_id: "today".into(),
label: "Today".into()
},
ThreadRow::Thread {
item: ThreadItem::new("a", "Alpha"),
position: 1
},
ThreadRow::Header {
section_id: "earlier".into(),
label: "Earlier".into()
},
ThreadRow::Thread {
item: ThreadItem::new("c", "Gamma"),
position: 2
},
],
"a section whose every thread is archived contributes no header"
);
assert_eq!(threads.visible_threads, 2);
assert_eq!(threads.candidates, 2);
});
}
/// Zoom invalidates measured heights, and the row that was first on screen
/// must still be first afterwards.
#[gpui::test]
fn a_changed_rem_remeasures_and_keeps_the_first_visible_row(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| threads.set_sections(large_sections(), cx));
});
redraw(&threads, cx);
cx.update(|_, cx| {
threads.update(cx, |threads, _| {
threads.list_state.scroll_to(ListOffset {
item_ix: 24,
offset_in_item: Pixels::ZERO,
});
});
});
redraw(&threads, cx);
let before = threads.read_with(cx, |threads, _| {
threads.rows[threads.list_state.logical_scroll_top().item_ix].anchor()
});
cx.update(|window, cx| {
window.set_rem_size(px(24.));
window.draw(cx).clear(cx);
});
cx.update(|window, cx| window.draw(cx).clear(cx));
let after = threads.read_with(cx, |threads, _| {
threads.rows[threads.list_state.logical_scroll_top().item_ix].anchor()
});
assert_eq!(before, after, "zooming must not move the reader");
assert!(
threads.read_with(cx, |threads, _| threads.resolved_layout.matches(px(24.))),
"the new rem must be recorded so the next draw does not remeasure again"
);
}
/// Clicking a row takes the roving focus with it, so the keyboard picks up
/// where the pointer left off.
#[gpui::test]
fn clicking_a_row_moves_the_roving_focus(cx: &mut TestAppContext) {
let (threads, cx) = measured_list(cx);
cx.update(|_, cx| {
threads.update(cx, |threads, cx| {
threads.set_sections(
[ThreadSection::new("today", "Today")
.items([ThreadItem::new("a", "Alpha"), ThreadItem::new("b", "Beta")])],
cx,
);
});
});
redraw(&threads, cx);
let row = bounds_of(cx, "thread-b".to_owned()).expect("the row should render");
cx.simulate_click(row.center(), Modifiers::default());
cx.update(|window, cx| window.draw(cx).clear(cx));
threads.read_with(cx, |threads, _| {
assert_eq!(threads.focused_thread.as_deref(), Some("b"));
});
}
}