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
//! Ported from Arc (MIT) © 2026 Elia Kuratli
//! Reference: registry/blocks/changelog-feed/changelog-feed.tsx.
//! 月份按钮滚动定位并按容器滚动同步高亮;订阅控件保留三态流程。
use super::{child_id, text};
use crate::{Button, ButtonSize, ButtonVariant};
use arc_motion::{
ArcPresence, LayoutSnapshot, arc_spring, layout::layout_scope_key, sample_shared_layout,
};
use arc_tokens::{
ActiveArcTheme as _, BlockTokens, Radii, Shadows, Spacing, Typography, oklab_mix,
};
use gpui_kit::base::ElementExt as _;
use gpui_kit::base::StyledExt as _;
use gpui_kit::component::input::{Input as KitInput, InputState};
use gpui_kit::{
App, BorderStyle, Context, Edges, ElementId, Entity, InteractiveElement as _, IntoElement,
KeyDownEvent, ParentElement, RenderOnce, Role, ScrollHandle, SharedString,
StatefulInteractiveElement as _, Styled, TestSupportExt as _, Transformation, Window,
assets::IconName, bounds, canvas, div, point, prelude::*, px, quad, svg,
};
use std::{
cell::{Cell, RefCell},
collections::HashMap,
rc::Rc,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChangelogKind {
New,
Improved,
Fixed,
}
impl ChangelogKind {
fn label(self) -> &'static str {
match self {
Self::New => "New",
Self::Improved => "Improved",
Self::Fixed => "Fixed",
}
}
}
/// 条目附件:本地或远程图片,或一段只读代码。
#[derive(Clone, Debug)]
pub enum ChangelogMedia {
Photo {
src: SharedString,
alt: SharedString,
caption: SharedString,
},
Code {
file: SharedString,
code: SharedString,
},
}
#[derive(Clone, Debug)]
pub struct ChangelogEntry {
pub id: SharedString,
pub month: SharedString,
pub date: SharedString,
pub version: SharedString,
pub kind: ChangelogKind,
pub title: SharedString,
pub summary: SharedString,
pub details: Vec<SharedString>,
pub media: Option<ChangelogMedia>,
}
impl ChangelogEntry {
pub fn new(
id: impl Into<SharedString>,
month: impl Into<SharedString>,
title: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
month: month.into(),
date: "".into(),
version: "".into(),
kind: ChangelogKind::New,
title: title.into(),
summary: "".into(),
details: vec![],
media: None,
}
}
pub fn summary(mut self, v: impl Into<SharedString>) -> Self {
self.summary = v.into();
self
}
pub fn date(mut self, v: impl Into<SharedString>) -> Self {
self.date = v.into();
self
}
pub fn version(mut self, v: impl Into<SharedString>) -> Self {
self.version = v.into();
self
}
pub fn kind(mut self, v: ChangelogKind) -> Self {
self.kind = v;
self
}
pub fn detail(mut self, v: impl Into<SharedString>) -> Self {
self.details.push(v.into());
self
}
pub fn media(mut self, media: ChangelogMedia) -> Self {
self.media = Some(media);
self
}
}
/// 与上游 `emailPattern` 等价:单 @、无空白、域名带至少两位的顶级域。
pub fn is_valid_email(value: &str) -> bool {
let value = value.trim();
let Some((local, domain)) = value.split_once('@') else {
return false;
};
!local.is_empty()
&& !domain.is_empty()
&& domain.contains('.')
&& !value.chars().any(char::is_whitespace)
&& domain.split('.').all(|part| !part.is_empty())
&& domain
.rsplit('.')
.next()
.is_some_and(|tld| tld.chars().count() >= 2)
}
type FilterChange = Rc<dyn Fn(Option<ChangelogKind>, &mut Window, &mut App)>;
type SubscribeHandler = Rc<dyn Fn(&SharedString, &mut Window, &mut App)>;
/// 月份高亮的越线阈值:分组顶部进入视口顶部 28px 内即视为到达。
const MONTH_STICK_PX: f32 = 28.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SubscribePhase {
Idle,
Editing,
Done,
}
/// 与上游滚动同步一致的纯决策函数:按滚动距离取最近越线的月份;
/// 滚到最底部时强制最后一个月,避免短尾月份高亮滞留在前一组。
fn sync_active_month(
order: &[SharedString],
tops: &HashMap<SharedString, f32>,
distance: f32,
viewport_h: f32,
content_bottom: f32,
) -> Option<SharedString> {
let first = order.first().cloned();
let last = order.last().cloned();
let at_bottom = viewport_h > 0.0
&& content_bottom > viewport_h + 1.0
&& distance + viewport_h >= content_bottom - 2.0;
if at_bottom {
return last.or(first);
}
let mut active = first;
for month in order {
if tops
.get(month)
.is_some_and(|y| *y - distance <= MONTH_STICK_PX)
{
active = Some(month.clone());
}
}
active
}
struct State {
filter: Vec<ChangelogKind>,
open: Vec<SharedString>,
month: Option<SharedString>,
email: Entity<InputState>,
phase: SubscribePhase,
subscribe_error: bool,
on_subscribe: Option<SubscribeHandler>,
scroll: ScrollHandle,
/// 各月份分组相对列表内容顶部的偏移,用于点击跳转与滚动同步。
tops: Rc<RefCell<HashMap<SharedString, f32>>>,
/// 各月份分组的内容底部偏移,用于判断是否滚到最底部。
bottoms: Rc<RefCell<HashMap<SharedString, f32>>>,
/// 各月份导航按钮的实测边界,供选中 pill 做共享布局滑行。
month_bounds: Rc<RefCell<HashMap<SharedString, gpui_kit::Bounds<gpui_kit::Pixels>>>>,
list_top: Rc<Cell<f32>>,
list_height: Rc<Cell<f32>>,
}
impl State {
fn new(window: &mut Window, cx: &mut Context<Self>, default_open: Vec<SharedString>) -> Self {
Self {
filter: vec![],
open: default_open,
month: None,
email: cx.new(|cx| InputState::new(window, cx)),
phase: SubscribePhase::Idle,
subscribe_error: false,
on_subscribe: None,
scroll: ScrollHandle::new(),
tops: Rc::new(RefCell::new(HashMap::new())),
bottoms: Rc::new(RefCell::new(HashMap::new())),
month_bounds: Rc::new(RefCell::new(HashMap::new())),
list_top: Rc::new(Cell::new(0.0)),
list_height: Rc::new(Cell::new(0.0)),
}
}
}
#[derive(IntoElement)]
pub struct ChangelogFeed {
id: ElementId,
title: SharedString,
subtitle: Option<SharedString>,
entries: Vec<ChangelogEntry>,
default_open: Option<Vec<SharedString>>,
end_note: Option<SharedString>,
on_filter: Option<FilterChange>,
on_subscribe: Option<SubscribeHandler>,
}
impl ChangelogFeed {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
title: "Changelog".into(),
subtitle: None,
entries: vec![],
default_open: None,
end_note: None,
on_filter: None,
on_subscribe: None,
}
}
pub fn title(mut self, v: impl Into<SharedString>) -> Self {
self.title = v.into();
self
}
pub fn subtitle(mut self, v: impl Into<SharedString>) -> Self {
self.subtitle = Some(v.into());
self
}
pub fn entries(mut self, v: impl IntoIterator<Item = ChangelogEntry>) -> Self {
self.entries = v.into_iter().collect();
self
}
pub fn default_open(mut self, v: impl IntoIterator<Item = SharedString>) -> Self {
self.default_open = Some(v.into_iter().collect());
self
}
pub fn end_note(mut self, value: impl Into<SharedString>) -> Self {
self.end_note = Some(value.into());
self
}
pub fn on_filter(
mut self,
f: impl Fn(Option<ChangelogKind>, &mut Window, &mut App) + 'static,
) -> Self {
self.on_filter = Some(Rc::new(f));
self
}
/// 校验通过的邮箱;组件不发起网络请求。
pub fn on_subscribe(
mut self,
f: impl Fn(&SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_subscribe = Some(Rc::new(f));
self
}
}
impl RenderOnce for ChangelogFeed {
fn render(self, w: &mut Window, cx: &mut App) -> impl IntoElement {
let colors = cx.arc_theme().colors;
let default_open = self.default_open.clone().unwrap_or_else(|| {
self.entries
.first()
.map(|e| vec![e.id.clone()])
.unwrap_or_default()
});
let state = w.use_keyed_state(child_id(&self.id, "state"), cx, |w, cx| {
State::new(w, cx, default_open)
});
state.update(cx, |s, _| s.on_subscribe = self.on_subscribe.clone());
let filter = state.read(cx).filter.clone();
let active_month = state.read(cx).month.clone();
let phase = state.read(cx).phase;
let subscribe_error = state.read(cx).subscribe_error;
let entry_heights = w.use_keyed_state(child_id(&self.id, "detail-heights"), cx, |_, _| {
Rc::new(RefCell::new(HashMap::<SharedString, f32>::new()))
});
let mut root = div()
.id(self.id.clone())
.role(Role::Region)
.aria_label(self.title.clone())
.w_full()
.max_w(BlockTokens::FEED_WIDTH)
.flex()
.flex_col()
.overflow_hidden()
.rounded(Radii::PANEL)
.border_1()
.border_color(colors.border)
.bg(colors.surface)
.text_color(colors.foreground);
let mut header = div()
.px(px(28.0))
.pt(px(32.0))
.pb(px(20.0))
.flex()
.flex_wrap()
.justify_between()
.items_start()
.gap(Spacing::S3)
.child(
div()
.flex()
.flex_col()
.gap(px(8.0))
.min_w(px(0.0))
.child(
div()
.text_size(Typography::TEXT_3XL)
.child(self.title.clone()),
)
.when_some(self.subtitle.clone(), |d, v| {
d.child(text(v, cx).text_size(Typography::TEXT_SM))
}),
);
let email = state.read(cx).email.clone();
let subscribe = self.on_subscribe.clone();
// 上游订阅控件:一个 pill 在三态间 morph 外壳——idle 黑底(Bell+Subscribe)、
// editing surface 底输入框 + 28px 圆形提交钮、done surface-muted + 划线
// check + Undo;错误时边框转 danger。
let pill_bg = match (phase, subscribe_error) {
(_, true) => colors.surface,
(SubscribePhase::Idle, false) => colors.foreground,
(SubscribePhase::Editing, false) => colors.surface,
(SubscribePhase::Done, false) => colors.surface_muted,
};
let pill_fg = match (phase, subscribe_error) {
(SubscribePhase::Idle, false) => colors.background,
_ => colors.foreground,
};
let pill_border = match (phase, subscribe_error) {
(_, true) => colors.danger,
(SubscribePhase::Idle, false) => colors.foreground,
(SubscribePhase::Editing, false) => colors.border_strong,
(SubscribePhase::Done, false) => colors.border,
};
let pill_content = match phase {
SubscribePhase::Idle => {
let st = state.clone();
let input = email.clone();
div()
.id(child_id(&self.id, "subscribe"))
.test_support()
.role(Role::Button)
.aria_label("Subscribe to release notes")
.cursor_pointer()
.h(px(38.0))
.px(px(15.0))
.pl(px(17.0))
.flex()
.items_center()
.gap(px(7.0))
.text_size(Typography::TEXT_SM)
.font_medium()
.whitespace_nowrap()
.text_color(pill_fg)
.hover(move |s| s.opacity(0.88))
.on_click(move |_, w, cx| {
let input = input.clone();
st.update(cx, |s, _| {
s.phase = SubscribePhase::Editing;
s.subscribe_error = false;
});
input.update(cx, |i, cx| i.focus(w, cx));
w.refresh();
})
.child(
svg()
.path(IconName::Bell.path())
.size(px(14.0))
.text_color(pill_fg),
)
.child("Subscribe")
.into_any_element()
}
SubscribePhase::Editing => {
let st_submit = state.clone();
let submit_subscribe = subscribe.clone();
let close_state = state.clone();
let input = email.clone();
div()
.id(child_id(&self.id, "subscribe-form"))
.h(px(38.0))
.pl(px(15.0))
.pr(px(5.0))
.flex()
.items_center()
.gap(px(6.0))
.on_key_down(move |event: &KeyDownEvent, w, cx| {
if event.keystroke.key == "escape" {
close_state.update(cx, |s, _| {
s.phase = SubscribePhase::Idle;
s.subscribe_error = false;
});
w.refresh();
}
})
.child(
KitInput::new(&input)
.id(child_id(&self.id, "subscribe-email"))
.aria_label("Email address")
.w(px(188.0)),
)
.child(
div()
.id(child_id(&self.id, "subscribe-submit"))
.test_support()
.role(Role::Button)
.aria_label("Subscribe to release notes")
.cursor_pointer()
.size(px(28.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded_full()
.bg(colors.foreground)
.text_color(colors.background)
.hover(move |s| s.opacity(0.88))
.on_click(move |_, w, cx| {
let value =
st_submit.read(cx).email.read(cx).value().trim().to_string();
if !is_valid_email(&value) {
st_submit.update(cx, |s, _| s.subscribe_error = true);
w.refresh();
return;
}
st_submit.update(cx, |s, _| {
s.subscribe_error = false;
s.phase = SubscribePhase::Done;
});
if let Some(cb) = &submit_subscribe {
cb(&value.into(), w, cx)
}
w.refresh();
})
.child(
svg()
.path(IconName::ArrowRight.path())
.size(px(14.0))
.text_color(colors.background),
),
)
.into_any_element()
}
SubscribePhase::Done => {
let undo_state = state.clone();
div()
.id(child_id(&self.id, "subscribe-done"))
.test_support()
.h(px(38.0))
.pl(px(13.0))
.pr(px(5.0))
.flex()
.items_center()
.gap(px(7.0))
.text_size(Typography::TEXT_SM)
.font_medium()
.text_color(pill_fg)
.whitespace_nowrap()
.child(
svg()
.path(IconName::Check.path())
.size(px(15.0))
.text_color(colors.success),
)
.child("Subscribed")
.child(
Button::new(child_id(&self.id, "subscribe-undo"))
.label("Undo")
.accessibility_label("Undo subscription")
.size(ButtonSize::Sm)
.variant(ButtonVariant::Ghost)
.on_click(move |_, w, cx| {
let email = undo_state.read(cx).email.clone();
undo_state.update(cx, |s, _| {
s.phase = SubscribePhase::Idle;
s.subscribe_error = false;
});
email.update(cx, |i, cx| i.set_value("", w, cx));
w.refresh();
}),
)
.into_any_element()
}
};
let control = div()
.id(child_id(&self.id, "subscribe-pill"))
.flex_none()
.overflow_hidden()
.rounded_full()
.border_1()
.border_color(pill_border)
.bg(pill_bg)
.child(pill_content);
header = header.child(control);
root = root.child(header);
if subscribe_error {
root = root.child(
div()
.id(child_id(&self.id, "subscribe-note"))
.test_support()
.role(Role::Status)
.text_color(colors.danger)
.child("Enter a valid email address"),
);
}
let toolbar_id = child_id(&self.id, "filters");
let mut toolbar = div()
.id(toolbar_id)
.role(Role::Group)
.flex()
.flex_wrap()
.items_center()
.px(px(28.0))
.pt(px(6.0))
.pb(px(20.0))
.gap(Spacing::S2);
for kind in [
ChangelogKind::New,
ChangelogKind::Improved,
ChangelogKind::Fixed,
] {
let selected = filter.contains(&kind);
let count = self.entries.iter().filter(|e| e.kind == kind).count();
let st = state.clone();
let cb = self.on_filter.clone();
toolbar = toolbar.child(
div()
.id(child_id(&self.id, format!("filter-{}", kind.label())))
.test_support()
.role(Role::Button)
.aria_label(format!("{} {count}", kind.label()))
.aria_selected(selected)
.cursor_pointer()
.h(px(34.0))
.px(px(12.0))
.rounded_full()
.border_1()
.border_color(if selected {
colors.foreground
} else {
colors.border
})
.bg(if selected {
colors.foreground
} else {
gpui_kit::transparent_black()
})
.text_color(if selected {
colors.background
} else {
colors.text_secondary
})
.text_size(Typography::TEXT_SM)
.font_medium()
.flex()
.items_center()
.gap(px(8.0))
.child(div().size(px(7.0)).rounded_full().bg(match kind {
ChangelogKind::New => colors.accent,
ChangelogKind::Improved => {
if selected {
colors.background
} else {
colors.text_muted
}
}
ChangelogKind::Fixed => colors.success,
}))
.child(kind.label())
.child(
div()
.text_size(Typography::TEXT_XS)
.child(count.to_string()),
)
.on_click(move |_, w, cx| {
let next = if selected { None } else { Some(kind) };
st.update(cx, |s, _| {
if selected {
s.filter.retain(|k| *k != kind);
} else {
s.filter.push(kind);
}
});
if let Some(cb) = &cb {
cb(next, w, cx)
}
w.refresh();
}),
);
}
if !filter.is_empty() {
let st = state.clone();
toolbar = toolbar.child(
Button::new(child_id(&self.id, "filter-clear"))
.label("Show all")
.size(ButtonSize::Sm)
.variant(ButtonVariant::Ghost)
.on_click(move |_, w, cx| {
st.update(cx, |s, _| s.filter.clear());
w.refresh();
}),
);
}
let total = self.entries.len();
let visible_total = self
.entries
.iter()
.filter(|e| filter.is_empty() || filter.contains(&e.kind))
.count();
root = root.child(
div()
.flex()
.items_center()
.justify_between()
.child(toolbar)
.child(
div()
.id(child_id(&self.id, "status"))
.role(Role::Status)
.pr(px(28.0))
.text_size(Typography::TEXT_SM)
.text_color(colors.text_muted)
.child(if filter.is_empty() {
format!("{total} updates")
} else {
format!("Showing {visible_total} of {total} updates")
}),
),
);
// 按首次出现顺序分组;月份导航只列出当前筛选下仍有内容的月份。
let mut groups: Vec<(SharedString, Vec<ChangelogEntry>)> = Vec::new();
for entry in &self.entries {
if let Some(group) = groups.iter_mut().find(|(month, _)| *month == entry.month) {
group.1.push(entry.clone());
} else {
groups.push((entry.month.clone(), vec![entry.clone()]));
}
}
let nav_months: Vec<SharedString> = groups
.iter()
.filter(|(_, rows)| {
rows.iter()
.any(|e| filter.is_empty() || filter.contains(&e.kind))
})
.map(|(month, _)| month.clone())
.collect();
let active = active_month.or_else(|| nav_months.first().cloned());
let scroll = state.read(cx).scroll.clone();
let tops = state.read(cx).tops.clone();
let bottoms = state.read(cx).bottoms.clone();
// 筛选变化后清掉不再可见月份的过期几何,避免跳转与底部判定被旧值干扰。
{
let mut tops = tops.borrow_mut();
tops.retain(|month, _| nav_months.contains(month));
}
{
let mut bottoms = bottoms.borrow_mut();
bottoms.retain(|month, _| nav_months.contains(month));
}
// 上游月份条:上下边框、min-height 60px;右侧 pill 容器内选中月份
// 以共享布局滑行(motion layoutId + spring.morph)。
let month_bounds = state.read(cx).month_bounds.clone();
let morph = cx.arc_theme().motion.spring.morph;
let pill_scope = layout_scope_key(&child_id(&self.id, "month-nav"));
let active_month_key = active.clone();
let pill_bg = colors.surface;
let pill_border = colors.border;
let pill_shadows = Shadows::resting(cx.arc_theme().mode);
let month_pill = canvas(
|_, _, _| {},
move |nav_rect, _, window, cx| {
let Some(key) = active_month_key.clone() else {
return;
};
let Some(btn) = month_bounds.borrow().get(&key).copied() else {
window.request_animation_frame();
return;
};
let target = LayoutSnapshot::uniform_radius(
bounds(btn.origin - nav_rect.origin, btn.size),
Radii::PILL,
);
let flip = sample_shared_layout(
pill_scope.clone(),
"month-pill",
target,
Some(morph),
window,
cx,
);
let paint = bounds(nav_rect.origin + flip.bounds.origin, flip.bounds.size);
window.paint_drop_shadows(paint, flip.corner_radii, &pill_shadows);
window.paint_quad(quad(
paint,
flip.corner_radii,
pill_bg,
Edges::all(px(1.0)),
pill_border,
BorderStyle::Solid,
));
},
)
.absolute()
.inset_0();
let mut month_bar = div()
.flex()
.flex_wrap()
.items_center()
.justify_between()
.gap(Spacing::S4)
.min_h(BlockTokens::FEED_MONTH_MIN_H)
.px(px(28.0))
.py(px(10.0))
.border_t_1()
.border_b_1()
.border_color(colors.border);
// 左侧:当前月份 label + 条目数(上游 monthLabel)。
let current_count = nav_months
.iter()
.find(|m| Some(*m) == active.as_ref())
.and_then(|m| groups.iter().find(|(g, _)| g == m))
.map(|(_, rows)| rows.len())
.unwrap_or(0);
month_bar = month_bar.child(
div()
.flex()
.items_baseline()
.gap(Spacing::S3)
.min_w(px(0.0))
.child(
div()
.text_size(Typography::TEXT_LG)
.font_medium()
.whitespace_nowrap()
.child(active.clone().unwrap_or_else(|| "No updates".into())),
)
.child(
text(format!("{current_count} updates"), cx)
.text_color(colors.text_muted)
.text_size(Typography::TEXT_SM),
),
);
// 右侧:pill 容器 + 每个月份按钮(第一个子元素是滑行 pill canvas)。
let mut month_nav = div()
.id(child_id(&self.id, "month-nav"))
.test_support()
.role(Role::Group)
.aria_label("Jump to month")
.relative()
.flex()
.flex_none()
.gap(px(2.0))
.p(px(3.0))
.rounded_full()
.bg(colors.surface_muted)
.child(month_pill);
for month in &nav_months {
let st = state.clone();
let next = month.clone();
let jump_scroll = scroll.clone();
let jump_tops = tops.clone();
let measured = state.read(cx).month_bounds.clone();
let measure_key = month.clone();
let probe = canvas(
move |rect, _, _| {
measured.borrow_mut().insert(measure_key.clone(), rect);
},
|_, _, _, _| {},
)
.absolute()
.inset_0();
let selected = active.as_ref() == Some(month);
month_nav = month_nav.child(
div()
.id(child_id(&self.id, format!("month-{month}")))
.test_support()
.role(Role::Button)
.aria_label(format!("Jump to {month}"))
.aria_selected(selected)
.cursor_pointer()
.relative()
.min_w(px(42.0))
.h(px(28.0))
.px(px(10.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded_full()
.text_size(Typography::TEXT_XS)
.font_medium()
.whitespace_nowrap()
.text_color(if selected {
colors.foreground
} else {
colors.text_secondary
})
.hover(move |s| s.text_color(colors.foreground))
.on_click(move |_, w, cx| {
if let Some(y) = jump_tops.borrow().get(&next).copied() {
jump_scroll.set_offset(point(px(0.0), px(MONTH_STICK_PX - y)));
}
st.update(cx, |s, _| s.month = Some(next.clone()));
w.refresh();
})
.child(probe)
.child(month.clone()),
);
}
month_bar = month_bar.child(month_nav);
root = root.child(month_bar);
let list_top = state.read(cx).list_top.clone();
let list_height = state.read(cx).list_height.clone();
let tops = state.read(cx).tops.clone();
let bottoms = state.read(cx).bottoms.clone();
let scroll = state.read(cx).scroll.clone();
let order = nav_months.clone();
let scroll_sync = scroll.clone();
let tops_sync = tops.clone();
let bottoms_sync = bottoms.clone();
let order_sync = order.clone();
let list_h_sync = list_height.clone();
let sync_state = state.clone();
let mut list = div()
.id(child_id(&self.id, "list"))
.role(Role::List)
.aria_label("Release notes")
.test_support()
.h(BlockTokens::FEED_HEIGHT)
.flex()
.flex_col()
.track_scroll(&scroll)
.on_prepaint({
let list_top = list_top.clone();
let list_height = list_height.clone();
move |bounds, _, _| {
list_top.set(f32::from(bounds.origin.y));
list_height.set(f32::from(bounds.size.height));
}
})
.on_scroll(move |_, w, cx| {
let distance = -f32::from(scroll_sync.offset().y);
let viewport_h = list_h_sync.get();
let tops = tops_sync.borrow();
let content_bottom = bottoms_sync
.borrow()
.values()
.copied()
.fold(0.0f32, f32::max);
if let Some(active) =
sync_active_month(&order_sync, &tops, distance, viewport_h, content_bottom)
{
sync_state.update(cx, |s, _| {
if s.month.as_ref() != Some(&active) {
s.month = Some(active);
}
});
}
w.refresh();
})
.overflow_y_scroll();
for (group_index, (month, rows)) in groups.into_iter().enumerate() {
if !rows
.iter()
.any(|e| filter.is_empty() || filter.contains(&e.kind))
{
continue;
}
let group_id = child_id(&self.id, format!("month-group-{month}"));
let group_tops = tops.clone();
let group_bottoms = bottoms.clone();
let group_list_top = list_top.clone();
let group_scroll = scroll.clone();
let month_key = month.clone();
let mut group = div()
.id(group_id)
.role(Role::Group)
.w_full()
.flex_none()
.flex()
.flex_col()
.on_prepaint(move |bounds, _, _| {
let base = group_list_top.get();
if base > 0.0 {
let shift = -f32::from(group_scroll.offset().y);
let y = f32::from(bounds.origin.y) - base + shift;
group_tops.borrow_mut().insert(month_key.clone(), y);
group_bottoms
.borrow_mut()
.insert(month_key.clone(), y + f32::from(bounds.size.height));
}
})
.when(group_index > 0, |d| {
d.child(
div()
.px(px(28.0))
.pt(px(30.0))
.pb(px(10.0))
.border_b_1()
.border_color(colors.border_subtle)
.text_size(Typography::TEXT_SM)
.text_color(colors.text_muted)
.child(month.clone()),
)
});
for entry in rows {
let visible = filter.is_empty() || filter.contains(&entry.kind);
let open = state.read(cx).open.contains(&entry.id);
let st = state.clone();
let key = entry.id.clone();
let id = child_id(&self.id, format!("entry-{}", entry.id));
let presence = ArcPresence::new((id.clone(), "entry"), visible)
.animate_initial(false)
.sample(w, cx);
if !presence.should_render() {
continue;
}
// 上游 kind 圆点色:new→accent、improved→text-muted、fixed→success。
let kind_color = match entry.kind {
ChangelogKind::New => colors.accent,
ChangelogKind::Improved => colors.text_muted,
ChangelogKind::Fixed => colors.success,
};
// chevron 旋转随 snappy 弹簧,展开 180°(上游 rotate 动画)。
let angle = arc_spring(
(child_id(&id, "chev"), "angle"),
if open { 180.0 } else { 0.0 },
cx.arc_theme().motion.spring.snappy,
w,
cx,
);
let toggle_label = if open { "Collapse" } else { "Details" };
// 上游 .row:整行 button,栅格 104px meta|body|20px chevron,
// gap 24px、padding 22px 28px;aria-expanded 标注展开态。
let mut entry_box = div()
.w_full()
.flex_none()
.flex()
.flex_col()
.border_b_1()
.border_color(colors.border_subtle)
.overflow_hidden()
.opacity(presence.progress);
if presence.progress < 0.999 {
entry_box = entry_box.max_h(px(1200.0 * presence.progress));
}
let row = div()
.id(id.clone())
.test_support()
.role(Role::ListItem)
.aria_expanded(open)
.w_full()
.flex_none()
.bg(colors.surface)
.hover(move |s| {
s.bg(oklab_mix(
colors.surface_muted,
gpui_kit::transparent_black(),
0.6,
))
})
.cursor_pointer()
.on_click(move |_, w, cx| {
st.update(cx, |s, _| {
if let Some(pos) = s.open.iter().position(|i| i == &key) {
s.open.remove(pos);
} else {
s.open.push(key.clone());
}
});
w.refresh();
})
.p(Spacing::S4)
.pt(px(22.0))
.pb(px(22.0))
.px(px(28.0))
.flex()
.items_start()
.gap(BlockTokens::FEED_ROW_GAP)
// meta 列:日期 + mono 版本徽章(上游 104px 列)。
.child(
div()
.w(BlockTokens::FEED_META_COL)
.flex_none()
.flex()
.flex_col()
.items_start()
.gap(px(8.0))
.pt(px(1.0))
.child(
text(entry.date.clone(), cx)
.text_size(Typography::TEXT_SM)
.text_color(colors.text_secondary),
)
.when(open && !entry.version.is_empty(), |meta| {
meta.child(
div()
.px(px(7.0))
.py(px(1.0))
.rounded(px(7.0))
.border_1()
.border_color(colors.border)
.text_size(Typography::TEXT_XS)
.font_family("Consolas")
.text_color(colors.text_secondary)
.child(format!("v{}", entry.version)),
)
}),
)
.child(
div()
.id(child_id(&id, "body"))
.test_support()
.flex_1()
.min_w(px(0.0))
.flex()
.flex_col()
.gap(px(6.0))
.when(open, |body| {
body.child(
// 收起只保留时间、标题和副标题;类型标记放在展开态。
div()
.flex()
.items_center()
.gap(px(7.0))
.text_size(Typography::TEXT_XS)
.font_medium()
.text_color(colors.text_secondary)
.child(div().size(px(7.0)).rounded_full().bg(kind_color))
.child(entry.kind.label()),
)
})
.child(
div()
.text_size(Typography::TEXT_BASE)
.font_medium()
.text_color(colors.foreground)
.child(entry.title.clone()),
)
.child(
text(entry.summary.clone(), cx)
.max_w(px(560.0))
.text_color(colors.text_secondary),
),
)
.child(
div()
.id(child_id(&id, "toggle"))
.test_support()
.role(Role::Button)
.aria_label(toggle_label)
.size(BlockTokens::FEED_CHEV_COL)
.flex_none()
.flex()
.items_center()
.justify_center()
.text_color(if open {
colors.foreground
} else {
colors.text_muted
})
.child(
svg()
.path(IconName::ChevronDown.path())
.size(px(16.0))
.with_transformation(Transformation::rotate(
gpui_kit::Radians(angle.to_radians()),
)),
),
);
entry_box = entry_box.child(row);
let height = arc_spring(
(id.clone(), "details"),
if open { 1.0 } else { 0.0 },
cx.arc_theme().motion.spring.smooth,
w,
cx,
)
.clamp(0.0, 1.0);
if height > 0.001 {
// 详情是摘要行下方的独立面板,不得作为横向 row 的第四列。
// 记录自然高度,仅在开合途中裁切;展开后不再固定为 160px。
let measured = entry_heights.read(cx).clone();
let measure_key = entry.id.clone();
let mut detail = div()
.id(child_id(&id, "details"))
.test_support()
.flex_none()
.on_prepaint(move |bounds, _, _| {
measured
.borrow_mut()
.insert(measure_key.clone(), f32::from(bounds.size.height));
})
.ml(px(28.0) + BlockTokens::FEED_META_COL + BlockTokens::FEED_ROW_GAP)
.mr(px(72.0))
.pb(px(28.0))
.flex()
.flex_col()
.gap(px(6.0));
for value in entry.details {
// 上游 details:6×1px 短横线引导,不是圆点。
detail = detail.child(
div()
.relative()
.pl(px(16.0))
.text_color(colors.text_secondary)
.child(
div()
.absolute()
.left(px(1.0))
.top(px(9.0))
.w(px(6.0))
.h(px(1.0))
.bg(colors.text_muted),
)
.child(text(value, cx)),
);
}
if let Some(media) = entry.media.clone() {
detail = detail.child(match media {
ChangelogMedia::Photo { src, alt, caption } => div()
.id(child_id(&id, "media-photo"))
.test_support()
.role(Role::Figure)
.aria_label(alt)
.flex()
.flex_col()
.gap(Spacing::S1)
.mt(px(18.0))
.child(
gpui_kit::img(src)
.w_full()
.max_h(px(180.0))
.rounded(px(16.0)),
)
.child(text(caption, cx)),
ChangelogMedia::Code { file, code } => div()
.id(child_id(&id, "media-code"))
.test_support()
.role(Role::Region)
.aria_label(format!("Code sample: {file}"))
.mt(px(18.0))
.rounded(px(16.0))
.border_1()
.border_color(colors.border)
.bg(colors.surface_muted)
.flex()
.flex_col()
.child(
div()
.h(px(40.0))
.px(px(14.0))
.flex()
.items_center()
.text_size(Typography::TEXT_XS)
.text_color(colors.text_muted)
.border_b_1()
.border_color(colors.border)
.child(file),
)
.child(
div()
.id(child_id(&id, "code-scroll"))
.overflow_x_scroll()
.p(px(16.0))
.text_size(Typography::TEXT_XS)
.font_family("Consolas")
.text_color(colors.foreground)
.flex()
.flex_col()
.children(code.lines().map(|line| {
div().whitespace_nowrap().child(
if line.is_empty() { " " } else { line }.to_owned(),
)
})),
),
});
}
let natural_height = entry_heights.read(cx).borrow().get(&entry.id).copied();
let mut panel = div()
.id(child_id(&id, "panel"))
.test_support()
.w_full()
.flex_none()
.overflow_hidden()
.opacity(height);
if height < 0.999
&& let Some(natural_height) = natural_height
{
panel = panel.h(px(natural_height * height));
}
entry_box = entry_box.child(panel.child(detail));
}
group = group.child(entry_box);
}
list = list.child(group);
}
if let Some(note) = self.end_note {
list = list.child(
text(note, cx)
.px(px(28.0))
.pt(px(32.0))
.pb(px(56.0))
.text_center(),
);
}
root.child(list)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn order() -> Vec<SharedString> {
vec!["November".into(), "October".into(), "September".into()]
}
fn tops() -> HashMap<SharedString, f32> {
HashMap::from([
("November".into(), 0.0),
("October".into(), 320.0),
("September".into(), 640.0),
])
}
#[test]
fn kinds_have_labels() {
assert_eq!(ChangelogKind::New.label(), "New");
assert_eq!(ChangelogKind::Fixed.label(), "Fixed");
assert_ne!(ChangelogKind::New, ChangelogKind::Fixed);
}
#[test]
fn email_validation_matches_upstream_pattern() {
assert!(is_valid_email("you@example.com"));
assert!(is_valid_email(" first.last@sub.example.co "));
assert!(!is_valid_email("you@example"));
assert!(!is_valid_email("you@example.c"));
assert!(!is_valid_email("you example.com"));
assert!(!is_valid_email("@example.com"));
assert!(!is_valid_email("you@"));
}
#[test]
fn scroll_sync_starts_at_first_month() {
assert_eq!(
sync_active_month(&order(), &tops(), 0.0, 400.0, 960.0),
Some("November".into())
);
}
#[test]
fn scroll_sync_follows_the_latest_crossed_month() {
// 350px 滚动后 October 顶部(320)已越过 28px 阈值线。
assert_eq!(
sync_active_month(&order(), &tops(), 350.0, 400.0, 960.0),
Some("October".into())
);
}
#[test]
fn scroll_sync_forces_last_month_at_the_bottom() {
// 到达底部:distance + viewport >= content_bottom;尾部月份很短,
// 其顶部尚未越线也必须高亮最后一组。
assert_eq!(
sync_active_month(&order(), &tops(), 700.0, 300.0, 990.0),
Some("September".into())
);
}
#[test]
fn scroll_sync_ignores_bottom_when_content_fits_without_scroll() {
// 内容不满一屏:不发生真实滚动,不强制末月。
assert_eq!(
sync_active_month(&order(), &tops(), 0.0, 400.0, 360.0),
Some("November".into())
);
}
#[test]
fn scroll_sync_leaves_state_untouched_without_months() {
assert_eq!(
sync_active_month(&[], &HashMap::new(), 40.0, 400.0, 800.0),
None
);
}
}