1use std::{
21 cell::RefCell,
22 collections::{HashMap, HashSet},
23 rc::Rc,
24};
25
26use arc_motion::{CubicBezier, PathPoint, PolylinePath, arc_spring, is_reduced_motion, motion_now};
27use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography, oklab_mix};
28use gpui_kit::{
29 AnyElement, App, Bounds, Corners, Edges, ElementId, FocusHandle, Hsla, IntoElement,
30 KeyDownEvent, MouseButton, ParentElement, PathBuilder, RenderOnce, Role, SharedString, Styled,
31 Window, assets::IconName, base::StyledExt as _, canvas, div, point, prelude::*, px, quad, svg,
32};
33
34use crate::text::animated_counter::AnimatedCounter;
35
36type CompletedChangeCallback = Rc<dyn Fn(&Vec<SharedString>, &mut Window, &mut App)>;
37type HiddenChangeCallback = Rc<dyn Fn(&bool, &mut Window, &mut App)>;
38type StepActionCallback =
39 Rc<dyn Fn(&SharedString, &mut Window, &mut App) -> Result<(), SharedString>>;
40
41pub const ONBOARDING_SETTLE_SECS: f64 = 0.72;
44
45#[derive(Clone)]
47pub struct OnboardingStep {
48 pub id: SharedString,
49 pub title: SharedString,
50 pub description: Option<SharedString>,
51 pub action_label: Option<SharedString>,
52 pub on_action: Option<StepActionCallback>,
53}
54
55impl OnboardingStep {
56 pub fn new(id: impl Into<SharedString>, title: impl Into<SharedString>) -> Self {
57 Self {
58 id: id.into(),
59 title: title.into(),
60 description: None,
61 action_label: None,
62 on_action: None,
63 }
64 }
65
66 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
67 self.description = Some(description.into());
68 self
69 }
70
71 pub fn action_label(mut self, label: impl Into<SharedString>) -> Self {
72 self.action_label = Some(label.into());
73 self
74 }
75
76 pub fn on_action(
79 mut self,
80 handler: impl Fn(&SharedString, &mut Window, &mut App) -> Result<(), SharedString> + 'static,
81 ) -> Self {
82 self.on_action = Some(Rc::new(handler));
83 self
84 }
85}
86
87pub fn onboarding_ordered_ids(
90 ids: &[SharedString],
91 step_ids: &[SharedString],
92) -> Vec<SharedString> {
93 let set: HashSet<&str> = ids.iter().map(SharedString::as_ref).collect();
94 let mut out = Vec::new();
95 for id in step_ids {
96 if set.contains(id.as_ref()) && !out.iter().any(|existing: &SharedString| existing == id) {
97 out.push(id.clone());
98 }
99 }
100 out
101}
102
103pub fn onboarding_next_expanded_id(
107 step_ids: &[SharedString],
108 done_ids: &[SharedString],
109 current_expanded: Option<&str>,
110 just_settled_id: &str,
111) -> Option<SharedString> {
112 if step_ids.is_empty() {
113 return None;
114 }
115 if current_expanded != Some(just_settled_id) {
116 return current_expanded.map(|s| SharedString::from(s.to_string()));
117 }
118 let done_set: HashSet<&str> = done_ids.iter().map(SharedString::as_ref).collect();
119 let start = step_ids
120 .iter()
121 .position(|id| id.as_ref() == just_settled_id)
122 .unwrap_or(0);
123 let len = step_ids.len();
124 for offset in 0..len {
125 let candidate = &step_ids[(start + offset + 1) % len];
126 if !done_set.contains(candidate.as_ref()) {
127 return Some(candidate.clone());
128 }
129 }
130 None
131}
132
133#[inline]
136pub fn onboarding_strike_progress(is_done: bool, elapsed_since_toggle: f64, reduced: bool) -> f32 {
137 if reduced {
138 return if is_done { 1.0 } else { 0.0 };
139 }
140 let ease_enter = CubicBezier::new(0.22, 1.0, 0.36, 1.0);
141 let ease_standard = CubicBezier::new(0.4, 0.0, 0.2, 1.0);
142 if is_done {
143 let delay = 0.08;
144 let duration = 0.32;
145 if elapsed_since_toggle <= delay {
146 0.0
147 } else {
148 let t = ((elapsed_since_toggle - delay) / duration).clamp(0.0, 1.0) as f32;
149 ease_enter.sample(t).clamp(0.0, 1.0)
150 }
151 } else {
152 let duration = 0.14;
153 let t = (elapsed_since_toggle / duration).clamp(0.0, 1.0) as f32;
154 (1.0_f32 - ease_standard.sample(t)).clamp(0.0, 1.0)
155 }
156}
157
158#[derive(Clone)]
159struct ChecklistInternalState {
160 done: Vec<SharedString>,
161 settled: Vec<SharedString>,
162 settle_deadlines: HashMap<SharedString, (f64, f64)>, uncheck_times: HashMap<SharedString, f64>,
164 expanded: Option<SharedString>,
165 pending: HashSet<SharedString>,
166 errors: HashMap<SharedString, SharedString>,
167 group_open: bool,
168 hidden: bool,
169 complete_since: Option<f64>,
170 focus_handle: FocusHandle,
171 focused_index: usize,
172 measured_panel_heights: Rc<RefCell<HashMap<SharedString, f32>>>,
173 measured_row_heights: Rc<RefCell<HashMap<SharedString, f32>>>,
174 measured_completed_height: Rc<RefCell<f32>>,
175 measured_body_height: Rc<RefCell<f32>>,
176}
177
178#[derive(IntoElement)]
180pub struct OnboardingChecklist {
181 id: ElementId,
182 title: SharedString,
183 done_title: SharedString,
184 steps: Vec<OnboardingStep>,
185 default_completed: Vec<SharedString>,
186 completed_override: Option<Vec<SharedString>>,
187 default_hidden: bool,
188 hidden_override: Option<bool>,
189 hide_label: SharedString,
190 show_label: SharedString,
191 completed_suffix: SharedString,
192 on_completed_change: Option<CompletedChangeCallback>,
193 on_hidden_change: Option<HiddenChangeCallback>,
194}
195
196impl OnboardingChecklist {
197 pub fn new(
198 id: impl Into<ElementId>,
199 title: impl Into<SharedString>,
200 steps: impl IntoIterator<Item = OnboardingStep>,
201 ) -> Self {
202 Self {
203 id: id.into(),
204 title: title.into(),
205 done_title: "You’re all set".into(),
206 steps: steps.into_iter().collect(),
207 default_completed: Vec::new(),
208 completed_override: None,
209 default_hidden: false,
210 hidden_override: None,
211 hide_label: "Hide".into(),
212 show_label: "Show".into(),
213 completed_suffix: "completed".into(),
214 on_completed_change: None,
215 on_hidden_change: None,
216 }
217 }
218
219 pub fn done_title(mut self, done_title: impl Into<SharedString>) -> Self {
220 self.done_title = done_title.into();
221 self
222 }
223
224 pub fn default_completed(
225 mut self,
226 completed: impl IntoIterator<Item = impl Into<SharedString>>,
227 ) -> Self {
228 self.default_completed = completed.into_iter().map(Into::into).collect();
229 self
230 }
231
232 pub fn completed(
233 mut self,
234 completed: impl IntoIterator<Item = impl Into<SharedString>>,
235 ) -> Self {
236 self.completed_override = Some(completed.into_iter().map(Into::into).collect());
237 self
238 }
239
240 pub fn default_hidden(mut self, hidden: bool) -> Self {
241 self.default_hidden = hidden;
242 self
243 }
244
245 pub fn hidden(mut self, hidden: bool) -> Self {
246 self.hidden_override = Some(hidden);
247 self
248 }
249
250 pub fn hide_show_labels(
251 mut self,
252 hide_label: impl Into<SharedString>,
253 show_label: impl Into<SharedString>,
254 ) -> Self {
255 self.hide_label = hide_label.into();
256 self.show_label = show_label.into();
257 self
258 }
259
260 pub fn completed_suffix(mut self, suffix: impl Into<SharedString>) -> Self {
261 self.completed_suffix = suffix.into();
262 self
263 }
264
265 pub fn on_completed_change(
266 mut self,
267 handler: impl Fn(&Vec<SharedString>, &mut Window, &mut App) + 'static,
268 ) -> Self {
269 self.on_completed_change = Some(Rc::new(handler));
270 self
271 }
272
273 pub fn on_hidden_change(
274 mut self,
275 handler: impl Fn(&bool, &mut Window, &mut App) + 'static,
276 ) -> Self {
277 self.on_hidden_change = Some(Rc::new(handler));
278 self
279 }
280}
281
282fn paint_progress_ring(
283 bounds: Bounds<gpui_kit::Pixels>,
284 ratio: f32,
285 tick_draw: f32,
286 track_color: Hsla,
287 fill_color: Hsla,
288 window: &mut Window,
289) {
290 let cx_px = bounds.origin.x + bounds.size.width * 0.5;
291 let cy_px = bounds.origin.y + bounds.size.height * 0.5;
292 let scale = (bounds.size.width / px(40.0)).min(bounds.size.height / px(40.0));
293 let r = 17.0 * scale;
294 let stroke_w = px(3.0 * scale);
295
296 let segments = 48;
298 let mut track_builder = PathBuilder::stroke(stroke_w);
299 for i in 0..=segments {
300 let angle = (i as f32 / segments as f32) * std::f32::consts::TAU;
301 let pt = point(cx_px + px(r * angle.cos()), cy_px + px(r * angle.sin()));
302 if i == 0 {
303 track_builder.move_to(pt);
304 } else {
305 track_builder.line_to(pt);
306 }
307 }
308 if let Ok(path) = track_builder.build() {
309 window.paint_path(path, track_color);
310 }
311
312 let clamped = ratio.clamp(0.0, 1.0);
314 if clamped > 0.005 {
315 let active_segs = ((segments as f32 * clamped).ceil() as usize).max(2);
316 let mut arc_builder = PathBuilder::stroke(stroke_w);
317 for i in 0..=active_segs {
318 let frac = (i as f32 / active_segs as f32) * clamped;
319 let angle = -std::f32::consts::FRAC_PI_2 + frac * std::f32::consts::TAU;
320 let pt = point(cx_px + px(r * angle.cos()), cy_px + px(r * angle.sin()));
321 if i == 0 {
322 arc_builder.move_to(pt);
323 } else {
324 arc_builder.line_to(pt);
325 }
326 }
327 if let Ok(path) = arc_builder.build() {
328 window.paint_path(path, fill_color);
329 }
330 }
331
332 if tick_draw > 0.01 {
334 let poly = PolylinePath::new(vec![
335 PathPoint::new(13.5, 20.5),
336 PathPoint::new(18.0, 25.0),
337 PathPoint::new(26.5, 15.5),
338 ]);
339 let sliced = poly.slice(0.0, tick_draw.clamp(0.0, 1.0));
340 if sliced.points.len() >= 2 {
341 let mut tick_builder = PathBuilder::stroke(px(2.25 * scale));
342 for (idx, pt) in sliced.points.iter().enumerate() {
343 let p = point(
344 bounds.origin.x + px(pt.x * scale),
345 bounds.origin.y + px(pt.y * scale),
346 );
347 if idx == 0 {
348 tick_builder.move_to(p);
349 } else {
350 tick_builder.line_to(p);
351 }
352 }
353 if let Ok(path) = tick_builder.build() {
354 window.paint_path(path, fill_color);
355 }
356 }
357 }
358}
359
360struct CheckboxDiscColors {
361 border: Hsla,
362 surface: Hsla,
363 accent: Hsla,
364 tick: Hsla,
365}
366
367fn paint_checkbox_disc(
368 bounds: Bounds<gpui_kit::Pixels>,
369 fill_scale: f32,
370 tick_draw: f32,
371 colors: CheckboxDiscColors,
372 window: &mut Window,
373) {
374 let CheckboxDiscColors {
375 border: border_color,
376 surface: surface_color,
377 accent: accent_color,
378 tick: tick_color,
379 } = colors;
380 let size = bounds.size.width.min(bounds.size.height);
381 let radius = size * 0.5;
382 window.paint_quad(quad(
383 bounds,
384 Corners::all(radius),
385 surface_color,
386 Edges::all(px(1.5)),
387 border_color,
388 gpui_kit::BorderStyle::Solid,
389 ));
390
391 let s = fill_scale.clamp(0.0, 1.0);
392 if s > 0.02 {
393 let fill_size = size * s;
394 let offset = (size - fill_size) * 0.5;
395 let fill_bounds = Bounds::new(
396 point(bounds.origin.x + offset, bounds.origin.y + offset),
397 gpui_kit::size(fill_size, fill_size),
398 );
399 window.paint_quad(quad(
400 fill_bounds,
401 Corners::all(fill_size * 0.5),
402 accent_color.opacity(s),
403 Edges::all(px(0.0)),
404 gpui_kit::transparent_black(),
405 gpui_kit::BorderStyle::Solid,
406 ));
407 }
408
409 if tick_draw > 0.01 {
411 let scale = (size / px(22.0)).max(0.5);
412 let poly = PolylinePath::new(vec![
413 PathPoint::new(6.75, 11.25),
414 PathPoint::new(9.75, 14.25),
415 PathPoint::new(15.25, 8.25),
416 ]);
417 let sliced = poly.slice(0.0, tick_draw.clamp(0.0, 1.0));
418 if sliced.points.len() >= 2 {
419 let mut builder = PathBuilder::stroke(px(1.85 * scale));
420 for (idx, pt) in sliced.points.iter().enumerate() {
421 let p = point(
422 bounds.origin.x + px(pt.x * scale),
423 bounds.origin.y + px(pt.y * scale),
424 );
425 if idx == 0 {
426 builder.move_to(p);
427 } else {
428 builder.line_to(p);
429 }
430 }
431 if let Ok(path) = builder.build() {
432 window.paint_path(path, tick_color);
433 }
434 }
435 }
436}
437
438impl RenderOnce for OnboardingChecklist {
439 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
440 let theme = cx.arc_theme();
441 let colors = theme.colors;
442 let motion = theme.motion;
443 let reduced = is_reduced_motion(cx);
444 let now = motion_now(cx);
445 let ease_enter = CubicBezier::new(0.22, 1.0, 0.36, 1.0);
446
447 let step_ids: Vec<SharedString> = self.steps.iter().map(|s| s.id.clone()).collect();
448 let init_done = onboarding_ordered_ids(&self.default_completed, &step_ids);
449 let init_expanded = step_ids.iter().find(|id| !init_done.contains(id)).cloned();
450 let init_hidden = self.default_hidden;
451
452 let state_cell = window
453 .use_keyed_state(
454 (self.id.clone(), "onboarding-checklist-state"),
455 cx,
456 |_, cx| {
457 Rc::new(RefCell::new(ChecklistInternalState {
458 done: init_done.clone(),
459 settled: init_done,
460 settle_deadlines: HashMap::new(),
461 uncheck_times: HashMap::new(),
462 expanded: init_expanded,
463 pending: HashSet::new(),
464 errors: HashMap::new(),
465 group_open: false,
466 hidden: init_hidden,
467 complete_since: None,
468 focus_handle: cx.focus_handle(),
469 focused_index: 0,
470 measured_panel_heights: Rc::new(RefCell::new(HashMap::new())),
471 measured_row_heights: Rc::new(RefCell::new(HashMap::new())),
472 measured_completed_height: Rc::new(RefCell::new(0.0)),
473 measured_body_height: Rc::new(RefCell::new(260.0)),
474 }))
475 },
476 )
477 .read(cx)
478 .clone();
479
480 let mut request_anim = false;
481 {
482 let mut st = state_cell.borrow_mut();
483 if let Some(ref ext_done) = self.completed_override {
484 let ordered = onboarding_ordered_ids(ext_done, &step_ids);
485 if st.done != ordered {
486 st.done = ordered.clone();
487 st.settled = ordered;
488 st.settle_deadlines.clear();
489 st.expanded = step_ids.iter().find(|id| !st.done.contains(id)).cloned();
490 }
491 }
492 if let Some(ext_hidden) = self.hidden_override {
493 st.hidden = ext_hidden;
494 }
495
496 let mut newly_settled = Vec::new();
498 for (id, &(_checked_at, deadline)) in st.settle_deadlines.iter() {
499 if now >= deadline {
500 newly_settled.push(id.clone());
501 } else {
502 request_anim = true;
503 }
504 }
505 for id in newly_settled {
506 st.settle_deadlines.remove(&id);
507 if st.done.contains(&id) && !st.settled.contains(&id) {
508 let next_exp = onboarding_next_expanded_id(
509 &step_ids,
510 &st.done,
511 st.expanded.as_deref(),
512 id.as_ref(),
513 );
514 let mut next_settled = st.settled.clone();
515 next_settled.push(id);
516 st.settled = onboarding_ordered_ids(&next_settled, &step_ids);
517 st.expanded = next_exp;
518 }
519 }
520
521 let all_done_now = !step_ids.is_empty() && st.done.len() == step_ids.len();
522 if all_done_now {
523 if st.complete_since.is_none() {
524 st.complete_since = Some(now);
525 }
526 if let Some(since) = st.complete_since
527 && now - since < 0.5
528 {
529 request_anim = true;
530 }
531 } else {
532 st.complete_since = None;
533 }
534 }
535
536 if request_anim {
537 window.request_animation_frame();
538 }
539
540 let snapshot = state_cell.borrow().clone();
541 let total = self.steps.len();
542 let done_count = snapshot.done.len();
543 let all_done = total > 0 && done_count == total;
544
545 let target_ratio = if total > 0 {
546 done_count as f32 / total as f32
547 } else {
548 0.0
549 };
550 let anim_ratio = if reduced {
551 target_ratio
552 } else {
553 arc_spring(
554 (self.id.clone(), "ring-ratio"),
555 target_ratio,
556 motion.spring.morph,
557 window,
558 cx,
559 )
560 };
561
562 let ring_tick_draw = if all_done {
563 if reduced {
564 1.0
565 } else if let Some(since) = snapshot.complete_since {
566 let delay = 0.12;
567 let duration = 0.24;
568 if now <= since + delay {
569 0.0
570 } else {
571 let t = ((now - since - delay) / duration).clamp(0.0, 1.0) as f32;
572 ease_enter.sample(t).clamp(0.0, 1.0)
573 }
574 } else {
575 1.0
576 }
577 } else {
578 0.0
579 };
580
581 let open_steps: Vec<OnboardingStep> = self
585 .steps
586 .iter()
587 .filter(|s| !snapshot.settled.contains(&s.id))
588 .cloned()
589 .collect();
590 let completed_steps: Vec<OnboardingStep> = self
591 .steps
592 .iter()
593 .filter(|s| snapshot.settled.contains(&s.id))
594 .cloned()
595 .collect();
596
597 let header_title = if all_done {
598 self.done_title.clone()
599 } else {
600 self.title.clone()
601 };
602 let hide_btn_text = if snapshot.hidden {
603 self.show_label.clone()
604 } else {
605 self.hide_label.clone()
606 };
607
608 let track_color = colors.border;
610 let fill_color = colors.accent;
611 let progress_ring = div().w(px(40.0)).h(px(40.0)).flex_shrink_0().child(
612 canvas(
613 move |_, _, _| {},
614 move |bounds, _, window, _| {
615 paint_progress_ring(
616 bounds,
617 anim_ratio,
618 ring_tick_draw,
619 track_color,
620 fill_color,
621 window,
622 );
623 },
624 )
625 .size_full(),
626 );
627
628 let count_subline = div()
630 .flex()
631 .items_center()
632 .gap(px(4.0))
633 .text_size(Typography::TEXT_SM)
634 .text_color(colors.text_muted)
635 .child(
636 AnimatedCounter::new((self.id.clone(), "done-counter"), done_count as f64)
637 .decimals(0)
638 .text_size(Typography::TEXT_SM),
639 )
640 .child(format!("of {total} done"));
641
642 let state_for_hide = state_cell.clone();
644 let on_hidden_cb = self.on_hidden_change.clone();
645 let hide_icon = if snapshot.hidden {
646 IconName::ChevronDown
647 } else {
648 IconName::ChevronUp
649 };
650 let hide_btn = div()
651 .id((self.id.clone(), "hide-toggle"))
652 .h(px(32.0))
653 .px(Spacing::S3)
654 .rounded(Radii::CONTROL)
655 .flex()
656 .items_center()
657 .gap(Spacing::S1)
658 .cursor_pointer()
659 .text_size(Typography::TEXT_SM)
660 .font_medium()
661 .text_color(colors.text_secondary)
662 .hover(move |s| s.bg(colors.surface_muted).text_color(colors.foreground))
663 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
664 let next_hidden = {
665 let mut st = state_for_hide.borrow_mut();
666 st.hidden = !st.hidden;
667 st.hidden
668 };
669 if let Some(ref cb) = on_hidden_cb {
670 cb(&next_hidden, window, cx);
671 }
672 window.refresh();
673 })
674 .child(hide_btn_text)
675 .child(
676 svg()
677 .path(hide_icon.path())
678 .size(px(14.0))
679 .text_color(colors.text_secondary),
680 );
681
682 let header = div()
683 .w_full()
684 .p(Spacing::S5)
685 .flex()
686 .items_center()
687 .justify_between()
688 .gap(Spacing::S3)
689 .child(
690 div()
691 .flex()
692 .items_center()
693 .gap(Spacing::S3)
694 .min_w(px(0.0))
695 .child(progress_ring)
696 .child(
697 div()
698 .flex()
699 .flex_col()
700 .gap(px(2.0))
701 .min_w(px(0.0))
702 .child(
703 div()
704 .text_size(Typography::TEXT_BASE)
705 .font_medium()
706 .text_color(colors.foreground)
707 .child(header_title),
708 )
709 .child(count_subline),
710 ),
711 )
712 .child(hide_btn);
713
714 let mut open_rows: Vec<AnyElement> = Vec::with_capacity(self.steps.len());
716 for step in &self.steps {
717 let step_id = step.id.clone();
718 let is_settled = snapshot.settled.contains(&step_id);
719 let is_done = snapshot.done.contains(&step_id);
720 let is_expanded = !is_settled && snapshot.expanded.as_ref() == Some(&step_id);
721 let step_err = snapshot.errors.get(&step_id).cloned();
722
723 let target_presence = if is_settled { 0.0_f32 } else { 1.0_f32 };
724 let row_presence = if reduced {
725 target_presence
726 } else {
727 arc_spring(
728 (self.id.clone(), format!("row-presence-{step_id}")),
729 target_presence,
730 motion.spring.smooth,
731 window,
732 cx,
733 )
734 };
735
736 let target_panel = if is_expanded { 1.0_f32 } else { 0.0_f32 };
737 let panel_prog = if reduced {
738 target_panel
739 } else {
740 arc_spring(
741 (self.id.clone(), format!("panel-open-{step_id}")),
742 target_panel,
743 motion.spring.smooth,
744 window,
745 cx,
746 )
747 };
748
749 if is_settled && row_presence < 0.015 {
750 continue;
751 }
752
753 let checked_elapsed = if is_done {
754 snapshot
755 .settle_deadlines
756 .get(&step_id)
757 .map(|&(checked_at, _)| (now - checked_at).max(0.0))
758 .unwrap_or(1.0)
759 } else {
760 snapshot
761 .uncheck_times
762 .get(&step_id)
763 .map(|&t| (now - t).max(0.0))
764 .unwrap_or(1.0)
765 };
766
767 let strike_prog = onboarding_strike_progress(is_done, checked_elapsed, reduced);
768 let target_check = if is_done { 1.0 } else { 0.0 };
769 let check_fill = if reduced {
770 target_check
771 } else {
772 arc_spring(
773 (self.id.clone(), format!("check-fill-{step_id}")),
774 target_check,
775 motion.spring.snappy,
776 window,
777 cx,
778 )
779 };
780
781 let check_tick = if is_done {
782 if reduced {
783 1.0
784 } else {
785 let delay = 0.05;
786 let duration = 0.22;
787 if checked_elapsed <= delay {
788 0.0
789 } else {
790 let t = ((checked_elapsed - delay) / duration).clamp(0.0, 1.0) as f32;
791 ease_enter.sample(t).clamp(0.0, 1.0)
792 }
793 }
794 } else {
795 0.0
796 };
797
798 let disc_border = if is_done {
799 colors.accent
800 } else {
801 colors.border_strong
802 };
803 let disc_surface = colors.surface;
804 let disc_accent = colors.accent;
805 let disc_tick_color = colors.accent_foreground;
806
807 let state_for_toggle = state_cell.clone();
808 let step_id_for_toggle = step_id.clone();
809 let all_ids_for_toggle = step_ids.clone();
810 let on_completed_cb = self.on_completed_change.clone();
811
812 let check_btn = div()
813 .id((self.id.clone(), format!("step-check-{step_id}")))
814 .w(px(40.0))
815 .h(px(40.0))
816 .flex_shrink_0()
817 .flex()
818 .items_center()
819 .justify_center()
820 .rounded_full()
821 .cursor_pointer()
822 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
823 let t_now = motion_now(cx);
824 let red_now = is_reduced_motion(cx);
825 let updated_done = {
826 let mut st = state_for_toggle.borrow_mut();
827 if st.done.contains(&step_id_for_toggle) {
828 st.done.retain(|id| id != &step_id_for_toggle);
829 st.settled.retain(|id| id != &step_id_for_toggle);
830 st.settle_deadlines.remove(&step_id_for_toggle);
831 st.uncheck_times.insert(step_id_for_toggle.clone(), t_now);
832 if st.expanded.is_none() {
833 st.expanded = Some(step_id_for_toggle.clone());
834 }
835 } else {
836 let mut next_done = st.done.clone();
837 next_done.push(step_id_for_toggle.clone());
838 st.done = onboarding_ordered_ids(&next_done, &all_ids_for_toggle);
839 st.pending.remove(&step_id_for_toggle);
840 st.errors.remove(&step_id_for_toggle);
841 let settle_delay = if red_now {
842 0.18
843 } else {
844 ONBOARDING_SETTLE_SECS
845 };
846 st.settle_deadlines
847 .insert(step_id_for_toggle.clone(), (t_now, t_now + settle_delay));
848 }
849 st.done.clone()
850 };
851 if let Some(ref cb) = on_completed_cb {
852 cb(&updated_done, window, cx);
853 }
854 window.refresh();
855 })
856 .child(
857 div().w(px(22.0)).h(px(22.0)).child(
858 canvas(
859 move |_, _, _| {},
860 move |bounds, _, window, _| {
861 paint_checkbox_disc(
862 bounds,
863 check_fill,
864 check_tick,
865 CheckboxDiscColors {
866 border: disc_border,
867 surface: disc_surface,
868 accent: disc_accent,
869 tick: disc_tick_color,
870 },
871 window,
872 );
873 },
874 )
875 .size_full(),
876 ),
877 );
878
879 let state_for_expand = state_cell.clone();
880 let step_id_for_expand = step_id.clone();
881 let title_color = if is_done {
882 colors.text_muted
883 } else {
884 colors.foreground
885 };
886 let title_text = step.title.clone();
887 let strike_color = title_color;
888
889 let title_btn = div()
890 .id((self.id.clone(), format!("step-title-{step_id}")))
891 .w_full()
892 .min_h(px(40.0))
893 .flex()
894 .items_center()
895 .cursor_pointer()
896 .on_mouse_down(MouseButton::Left, move |_, window, _| {
897 {
898 let mut st = state_for_expand.borrow_mut();
899 if st.expanded.as_ref() == Some(&step_id_for_expand) {
900 st.expanded = None;
901 } else {
902 st.expanded = Some(step_id_for_expand.clone());
903 }
904 }
905 window.refresh();
906 })
907 .child(
908 div()
909 .relative()
910 .flex()
911 .items_center()
912 .text_size(Typography::TEXT_SM)
913 .font_medium()
914 .text_color(title_color)
915 .child(title_text)
916 .when(strike_prog > 0.01, |el| {
917 el.child(
918 div()
919 .absolute()
920 .left_0()
921 .top(px(10.0))
922 .h(px(1.5))
923 .w(gpui_kit::relative(strike_prog))
924 .bg(strike_color),
925 )
926 }),
927 );
928
929 let panel_el: Option<AnyElement> = if panel_prog > 0.01 {
931 let state_for_run = state_cell.clone();
932 let step_id_for_run = step_id.clone();
933 let all_ids_for_run = step_ids.clone();
934 let action_handler = step.on_action.clone();
935 let on_completed_for_run = self.on_completed_change.clone();
936
937 let action_btn: Option<AnyElement> = step.action_label.as_ref().map(|label| {
938 let label_text = label.clone();
939 let icon_name = if is_done {
940 IconName::Check
941 } else {
942 IconName::ArrowRight
943 };
944 div()
945 .id((self.id.clone(), format!("step-action-{step_id}")))
946 .mt(Spacing::S3)
947 .h(px(32.0))
948 .pl(Spacing::S4)
949 .pr(Spacing::S3)
950 .rounded(Radii::CONTROL)
951 .bg(colors.foreground)
952 .text_color(colors.background)
953 .text_size(Typography::TEXT_SM)
954 .font_medium()
955 .flex()
956 .items_center()
957 .gap(Spacing::S2)
958 .cursor_pointer()
959 .hover(|s| s.opacity(0.92))
960 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
961 let is_already_done = {
962 let st = state_for_run.borrow();
963 st.done.contains(&step_id_for_run)
964 || st.pending.contains(&step_id_for_run)
965 };
966 if is_already_done {
967 return;
968 }
969 let result = if let Some(ref handler) = action_handler {
970 handler(&step_id_for_run, window, cx)
971 } else {
972 Ok(())
973 };
974 match result {
975 Ok(()) => {
976 let t_now = motion_now(cx);
977 let red_now = is_reduced_motion(cx);
978 let updated_done = {
979 let mut st = state_for_run.borrow_mut();
980 let mut next_done = st.done.clone();
981 next_done.push(step_id_for_run.clone());
982 st.done =
983 onboarding_ordered_ids(&next_done, &all_ids_for_run);
984 st.pending.remove(&step_id_for_run);
985 st.errors.remove(&step_id_for_run);
986 let settle_delay = if red_now {
987 0.18
988 } else {
989 ONBOARDING_SETTLE_SECS
990 };
991 st.settle_deadlines.insert(
992 step_id_for_run.clone(),
993 (t_now, t_now + settle_delay),
994 );
995 st.done.clone()
996 };
997 if let Some(ref cb) = on_completed_for_run {
998 cb(&updated_done, window, cx);
999 }
1000 }
1001 Err(err_msg) => {
1002 let mut st = state_for_run.borrow_mut();
1003 st.pending.remove(&step_id_for_run);
1004 st.errors.insert(step_id_for_run.clone(), err_msg);
1005 }
1006 }
1007 window.refresh();
1008 })
1009 .child(label_text)
1010 .child(
1011 svg()
1012 .path(icon_name.path())
1013 .size(px(14.0))
1014 .text_color(colors.background),
1015 )
1016 .into_any_element()
1017 });
1018
1019 let panel_heights_ref = snapshot.measured_panel_heights.clone();
1020 let default_panel_h =
1021 28.0 + if step.description.is_some() {
1022 38.0
1023 } else {
1024 0.0
1025 } + if step_err.is_some() { 26.0 } else { 0.0 }
1026 + if step.action_label.is_some() {
1027 44.0
1028 } else {
1029 0.0
1030 };
1031 let natural_panel_h = panel_heights_ref
1032 .borrow()
1033 .get(&step_id)
1034 .copied()
1035 .unwrap_or(default_panel_h)
1036 .max(24.0);
1037 let animated_panel_h = (natural_panel_h * panel_prog.clamp(0.0, 1.0)).max(0.0);
1038 let panel_alpha = (panel_prog * 1.25).clamp(0.0, 1.0);
1039 let panel_dy = if reduced {
1040 0.0
1041 } else {
1042 -5.0 * (1.0 - panel_prog.clamp(0.0, 1.0))
1043 };
1044
1045 let measure_panel_ref = panel_heights_ref.clone();
1046 let measure_panel_id = step_id.clone();
1047 let inner_panel = div()
1048 .w_full()
1049 .pb(Spacing::S3)
1050 .mt(px(panel_dy))
1051 .opacity(panel_alpha)
1052 .flex()
1053 .flex_col()
1054 .items_start()
1055 .when_some(step.description.clone(), |el, desc| {
1056 el.child(
1057 div()
1058 .text_size(Typography::TEXT_SM)
1059 .text_color(colors.text_secondary)
1060 .child(desc),
1061 )
1062 })
1063 .when_some(step_err, |el, err| {
1064 el.child(
1065 div()
1066 .pt(Spacing::S2)
1067 .text_size(Typography::TEXT_SM)
1068 .text_color(colors.danger)
1069 .child(err),
1070 )
1071 })
1072 .when_some(action_btn, |el, btn| el.child(btn))
1073 .on_children_prepainted(move |children_bounds, _, _| {
1074 let mut min_y = f32::INFINITY;
1075 let mut max_y = f32::NEG_INFINITY;
1076 for b in children_bounds {
1077 min_y = min_y.min(f32::from(b.origin.y));
1078 max_y = max_y.max(f32::from(b.origin.y + b.size.height));
1079 }
1080 if max_y > min_y {
1081 let h = (max_y - min_y) + 14.0;
1082 measure_panel_ref
1083 .borrow_mut()
1084 .insert(measure_panel_id.clone(), h);
1085 }
1086 });
1087
1088 let mut panel_clip = div().w_full().overflow_hidden().child(inner_panel);
1089 if panel_prog < 0.995 {
1090 panel_clip = panel_clip.h(px(animated_panel_h));
1091 }
1092 Some(panel_clip.into_any_element())
1093 } else {
1094 None
1095 };
1096
1097 let tint_t = panel_prog.clamp(0.0, 1.0);
1098 let row_bg = if tint_t > 0.01 {
1099 colors.surface_muted.opacity(tint_t)
1100 } else {
1101 gpui_kit::transparent_black()
1102 };
1103 let hover_bg = if is_expanded {
1104 colors.surface_muted
1105 } else {
1106 oklab_mix(colors.surface_muted, colors.surface, 0.4)
1107 };
1108
1109 let row_heights_ref = snapshot.measured_row_heights.clone();
1110 let natural_row_h = row_heights_ref
1111 .borrow()
1112 .get(&step_id)
1113 .copied()
1114 .unwrap_or(52.0)
1115 .max(44.0);
1116 let measure_row_ref = row_heights_ref.clone();
1117 let measure_row_id = step_id.clone();
1118
1119 let row_inner = div()
1120 .w_full()
1121 .px(Spacing::S3)
1122 .py(Spacing::S1)
1123 .rounded(Radii::PANEL)
1124 .bg(row_bg)
1125 .hover(move |s| s.bg(hover_bg))
1126 .flex()
1127 .items_start()
1128 .gap(Spacing::S3)
1129 .child(check_btn)
1130 .child(
1131 div()
1132 .flex_1()
1133 .min_w(px(0.0))
1134 .flex()
1135 .flex_col()
1136 .child(title_btn)
1137 .when_some(panel_el, |el, p| el.child(p)),
1138 )
1139 .on_children_prepainted(move |children_bounds, _, _| {
1140 let mut max_h = 0.0_f32;
1141 for b in children_bounds {
1142 max_h = max_h.max(f32::from(b.size.height));
1143 }
1144 if max_h > 20.0 {
1145 measure_row_ref
1146 .borrow_mut()
1147 .insert(measure_row_id.clone(), max_h + 10.0);
1148 }
1149 });
1150
1151 let row_clamped = row_presence.clamp(0.0, 1.0);
1152 let mut row_wrapper = div()
1153 .w_full()
1154 .overflow_hidden()
1155 .opacity(row_clamped)
1156 .child(row_inner);
1157 if row_clamped < 0.995 {
1158 row_wrapper = row_wrapper.h(px((natural_row_h * row_clamped).max(0.0)));
1159 }
1160
1161 open_rows.push(row_wrapper.into_any_element());
1162 }
1163
1164 let completed_group_el: Option<AnyElement> = if !completed_steps.is_empty() {
1166 let state_for_group = state_cell.clone();
1167 let group_icon = if snapshot.group_open {
1168 IconName::ChevronUp
1169 } else {
1170 IconName::ChevronDown
1171 };
1172 let hover_group_bg = oklab_mix(colors.surface_muted, colors.surface, 0.4);
1173 let completed_len = completed_steps.len();
1174 let suffix = self.completed_suffix.clone();
1175
1176 let group_toggle = div()
1177 .id((self.id.clone(), "completed-group-toggle"))
1178 .w_full()
1179 .min_h(px(44.0))
1180 .mt(px(2.0))
1181 .pl(px(64.0))
1182 .pr(Spacing::S3)
1183 .rounded(Radii::PANEL)
1184 .flex()
1185 .items_center()
1186 .justify_between()
1187 .cursor_pointer()
1188 .text_size(Typography::TEXT_SM)
1189 .font_medium()
1190 .text_color(colors.text_secondary)
1191 .hover(move |s| s.bg(hover_group_bg).text_color(colors.foreground))
1192 .on_mouse_down(MouseButton::Left, move |_, window, _| {
1193 {
1194 let mut st = state_for_group.borrow_mut();
1195 st.group_open = !st.group_open;
1196 }
1197 window.refresh();
1198 })
1199 .child(
1200 div()
1201 .flex()
1202 .items_center()
1203 .gap(px(4.0))
1204 .child(
1205 AnimatedCounter::new(
1206 (self.id.clone(), "completed-group-counter"),
1207 completed_len as f64,
1208 )
1209 .decimals(0)
1210 .text_size(Typography::TEXT_SM),
1211 )
1212 .child(suffix),
1213 )
1214 .child(
1215 svg()
1216 .path(group_icon.path())
1217 .size(px(14.0))
1218 .text_color(colors.text_secondary),
1219 );
1220
1221 let group_open_target = if snapshot.group_open {
1222 1.0_f32
1223 } else {
1224 0.0_f32
1225 };
1226 let group_prog = if reduced {
1227 group_open_target
1228 } else {
1229 arc_spring(
1230 (self.id.clone(), "completed-group-open"),
1231 group_open_target,
1232 motion.spring.smooth,
1233 window,
1234 cx,
1235 )
1236 };
1237
1238 let completed_list_el: Option<AnyElement> = if group_prog > 0.01 {
1239 let mut completed_rows: Vec<AnyElement> = Vec::with_capacity(completed_steps.len());
1240 for step in &completed_steps {
1241 let step_id = step.id.clone();
1242 let state_for_uncheck = state_cell.clone();
1243 let step_id_for_uncheck = step_id.clone();
1244 let on_completed_cb = self.on_completed_change.clone();
1245 let disc_border = colors.accent;
1246 let disc_surface = colors.surface;
1247 let disc_accent = colors.accent;
1248 let disc_tick = colors.accent_foreground;
1249 let strike_color = colors.text_muted;
1250
1251 completed_rows.push(
1252 div()
1253 .w_full()
1254 .px(Spacing::S3)
1255 .py(Spacing::S1)
1256 .rounded(Radii::PANEL)
1257 .hover(move |s| s.bg(hover_group_bg))
1258 .flex()
1259 .items_center()
1260 .gap(Spacing::S3)
1261 .child(
1262 div()
1263 .id((self.id.clone(), format!("completed-uncheck-{step_id}")))
1264 .w(px(40.0))
1265 .h(px(40.0))
1266 .flex_shrink_0()
1267 .flex()
1268 .items_center()
1269 .justify_center()
1270 .rounded_full()
1271 .cursor_pointer()
1272 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1273 let t_now = motion_now(cx);
1274 let updated_done = {
1275 let mut st = state_for_uncheck.borrow_mut();
1276 st.done.retain(|id| id != &step_id_for_uncheck);
1277 st.settled.retain(|id| id != &step_id_for_uncheck);
1278 st.settle_deadlines.remove(&step_id_for_uncheck);
1279 st.uncheck_times
1280 .insert(step_id_for_uncheck.clone(), t_now);
1281 if st.expanded.is_none() {
1282 st.expanded = Some(step_id_for_uncheck.clone());
1283 }
1284 st.done.clone()
1285 };
1286 if let Some(ref cb) = on_completed_cb {
1287 cb(&updated_done, window, cx);
1288 }
1289 window.refresh();
1290 })
1291 .child(
1292 div().w(px(22.0)).h(px(22.0)).child(
1293 canvas(
1294 move |_, _, _| {},
1295 move |bounds, _, window, _| {
1296 paint_checkbox_disc(
1297 bounds,
1298 1.0,
1299 1.0,
1300 CheckboxDiscColors {
1301 border: disc_border,
1302 surface: disc_surface,
1303 accent: disc_accent,
1304 tick: disc_tick,
1305 },
1306 window,
1307 );
1308 },
1309 )
1310 .size_full(),
1311 ),
1312 ),
1313 )
1314 .child(
1315 div().min_h(px(40.0)).flex().items_center().child(
1316 div()
1317 .relative()
1318 .flex()
1319 .items_center()
1320 .text_size(Typography::TEXT_SM)
1321 .font_medium()
1322 .text_color(colors.text_muted)
1323 .child(step.title.clone())
1324 .child(
1325 div()
1326 .absolute()
1327 .left_0()
1328 .top(px(10.0))
1329 .h(px(1.5))
1330 .w_full()
1331 .bg(strike_color),
1332 ),
1333 ),
1334 )
1335 .into_any_element(),
1336 );
1337 }
1338
1339 let completed_h_ref = snapshot.measured_completed_height.clone();
1340 let default_completed_h = (completed_steps.len() as f32) * 48.0;
1341 let natural_completed_h = {
1342 let h = *completed_h_ref.borrow();
1343 if h > 10.0 { h } else { default_completed_h }
1344 };
1345 let measure_completed_ref = completed_h_ref.clone();
1346 let group_clamped = group_prog.clamp(0.0, 1.0);
1347 let inner_completed = div()
1348 .w_full()
1349 .flex()
1350 .flex_col()
1351 .opacity((group_clamped * 1.2).clamp(0.0, 1.0))
1352 .children(completed_rows)
1353 .on_children_prepainted(move |children_bounds, _, _| {
1354 let mut min_y = f32::INFINITY;
1355 let mut max_y = f32::NEG_INFINITY;
1356 for b in children_bounds {
1357 min_y = min_y.min(f32::from(b.origin.y));
1358 max_y = max_y.max(f32::from(b.origin.y + b.size.height));
1359 }
1360 if max_y > min_y {
1361 *measure_completed_ref.borrow_mut() = max_y - min_y;
1362 }
1363 });
1364 let mut clip = div().w_full().overflow_hidden().child(inner_completed);
1365 if group_clamped < 0.995 {
1366 clip = clip.h(px((natural_completed_h * group_clamped).max(0.0)));
1367 }
1368 Some(clip.into_any_element())
1369 } else {
1370 None
1371 };
1372
1373 Some(
1374 div()
1375 .w_full()
1376 .flex()
1377 .flex_col()
1378 .child(group_toggle)
1379 .when_some(completed_list_el, |el, list| el.child(list))
1380 .into_any_element(),
1381 )
1382 } else {
1383 None
1384 };
1385
1386 let body_open_target = if snapshot.hidden { 0.0_f32 } else { 1.0_f32 };
1388 let body_prog = if reduced {
1389 body_open_target
1390 } else {
1391 arc_spring(
1392 (self.id.clone(), "body-open"),
1393 body_open_target,
1394 motion.spring.smooth,
1395 window,
1396 cx,
1397 )
1398 };
1399 let body_clamped = body_prog.clamp(0.0, 1.0);
1400 let body_el: Option<AnyElement> = if body_clamped > 0.01 {
1401 let body_h_ref = snapshot.measured_body_height.clone();
1402 let natural_body_h = {
1403 let h = *body_h_ref.borrow();
1404 if h > 20.0 { h } else { 320.0 }
1405 };
1406 let measure_body_ref = body_h_ref.clone();
1407 let inner_body = div()
1408 .w_full()
1409 .p(Spacing::S2)
1410 .border_t_1()
1411 .border_color(colors.border.opacity(body_clamped))
1412 .opacity((body_clamped * 1.15).clamp(0.0, 1.0))
1413 .flex()
1414 .flex_col()
1415 .children(open_rows)
1416 .when_some(completed_group_el, |b, grp| b.child(grp))
1417 .on_children_prepainted(move |children_bounds, _, _| {
1418 let mut min_y = f32::INFINITY;
1419 let mut max_y = f32::NEG_INFINITY;
1420 for b in children_bounds {
1421 min_y = min_y.min(f32::from(b.origin.y));
1422 max_y = max_y.max(f32::from(b.origin.y + b.size.height));
1423 }
1424 if max_y > min_y {
1425 *measure_body_ref.borrow_mut() = (max_y - min_y) + 18.0;
1426 }
1427 });
1428 let mut body_clip = div().w_full().overflow_hidden().child(inner_body);
1429 if body_clamped < 0.995 {
1430 body_clip = body_clip.h(px((natural_body_h * body_clamped).max(0.0)));
1431 }
1432 Some(body_clip.into_any_element())
1433 } else {
1434 None
1435 };
1436
1437 let focus_handle = snapshot.focus_handle.clone();
1439 let state_for_key = state_cell.clone();
1440 let open_ids: Vec<SharedString> = open_steps.iter().map(|s| s.id.clone()).collect();
1441
1442 div()
1443 .id(self.id)
1444 .role(Role::Region)
1445 .track_focus(&focus_handle)
1446 .w_full()
1447 .rounded(Radii::SURFACE)
1448 .border_1()
1449 .border_color(colors.border)
1450 .bg(colors.surface)
1451 .text_color(colors.foreground)
1452 .flex()
1453 .flex_col()
1454 .on_key_down(move |event: &KeyDownEvent, window, _| {
1455 if open_ids.is_empty() {
1456 return;
1457 }
1458 let key = event.keystroke.key.as_str();
1459 let mut st = state_for_key.borrow_mut();
1460 let len = open_ids.len();
1461 match key {
1462 "down" | "arrowdown" => {
1463 st.focused_index = (st.focused_index + 1) % len;
1464 st.expanded = Some(open_ids[st.focused_index].clone());
1465 window.refresh();
1466 }
1467 "up" | "arrowup" => {
1468 st.focused_index = (st.focused_index + len - 1) % len;
1469 st.expanded = Some(open_ids[st.focused_index].clone());
1470 window.refresh();
1471 }
1472 "home" => {
1473 st.focused_index = 0;
1474 st.expanded = Some(open_ids[0].clone());
1475 window.refresh();
1476 }
1477 "end" => {
1478 st.focused_index = len - 1;
1479 st.expanded = Some(open_ids[len - 1].clone());
1480 window.refresh();
1481 }
1482 _ => {}
1483 }
1484 })
1485 .child(header)
1486 .when_some(body_el, |el, body| el.child(body))
1487 }
1488}
1489
1490#[cfg(test)]
1491mod tests {
1492 use super::*;
1493
1494 #[test]
1495 fn onboarding_ordered_and_next_expanded_wrap_correctly() {
1496 let step_ids: Vec<SharedString> =
1497 vec!["domain".into(), "dns".into(), "ssl".into(), "invite".into()];
1498 let done: Vec<SharedString> = vec!["ssl".into(), "domain".into()];
1499 let ordered = onboarding_ordered_ids(&done, &step_ids);
1500 assert_eq!(
1501 ordered,
1502 vec![SharedString::from("domain"), SharedString::from("ssl")]
1503 );
1504
1505 let next = onboarding_next_expanded_id(&step_ids, &ordered, Some("ssl"), "ssl");
1506 assert_eq!(next, Some("invite".into()));
1507
1508 let done_three: Vec<SharedString> = vec!["domain".into(), "ssl".into(), "invite".into()];
1509 let wrapped = onboarding_next_expanded_id(&step_ids, &done_three, Some("invite"), "invite");
1510 assert_eq!(wrapped, Some("dns".into()));
1511 }
1512
1513 #[test]
1514 fn onboarding_strike_progress_respects_delay_and_reduced_motion() {
1515 assert_eq!(onboarding_strike_progress(true, 0.0, true), 1.0);
1516 assert_eq!(onboarding_strike_progress(false, 0.0, true), 0.0);
1517
1518 assert_eq!(onboarding_strike_progress(true, 0.04, false), 0.0);
1519 let mid = onboarding_strike_progress(true, 0.22, false);
1520 assert!(mid > 0.2 && mid < 0.99);
1521 assert!(onboarding_strike_progress(true, 0.45, false) >= 0.99);
1522 }
1523}