1use std::{cell::RefCell, rc::Rc, time::Instant};
16
17use arc_motion::{arc_spring_with_options, is_reduced_motion, spring::SpringOptions};
18use arc_tokens::{ActiveArcTheme as _, Radii, Shadows, Spacing, SpringSpec, Typography, oklab_mix};
19use gpui_kit::{
20 AnyElement, App, Bounds, ElementId, FocusHandle, Hsla, IntoElement, KeyDownEvent, MouseButton,
21 MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, PathBuilder, Pixels, RenderOnce,
22 Role, SharedString, Styled, Window, canvas, div, point, prelude::*, px,
23};
24
25pub const CARD_STACK_VISIBLE: usize = 4;
27pub const CARD_STACK_PEEK_PX: f32 = 12.0;
29pub const CARD_STACK_SHRINK_STEP: f32 = 0.05;
31pub const CARD_STACK_LIFT: f32 = 0.40;
33pub const CARD_STACK_LIFT_DISTANCE: f32 = 160.0;
35pub const CARD_STACK_FREE_Y: f32 = 48.0;
37pub const CARD_STACK_STRETCH_Y: f32 = 140.0;
39pub const CARD_STACK_FLICK_PX_S: f32 = 500.0;
41
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
44pub enum CardDecision {
45 Left,
46 Right,
47}
48
49pub fn card_stack_resist_y(raw: f32) -> f32 {
51 let dist = raw.abs();
52 if dist <= CARD_STACK_FREE_Y {
53 raw
54 } else {
55 raw.signum()
56 * (CARD_STACK_FREE_Y
57 + (1.0 - 1.0 / ((dist - CARD_STACK_FREE_Y) * 0.55 / CARD_STACK_STRETCH_Y + 1.0))
58 * CARD_STACK_STRETCH_Y)
59 }
60}
61
62pub fn card_stack_slot_metrics(depth: usize, lift: f32) -> (f32, f32, f32) {
64 let shown = if depth == 0 {
65 0.0
66 } else {
67 (depth as f32 - lift.clamp(0.0, 1.0) * CARD_STACK_LIFT).max(0.0)
68 };
69 let scale = (1.0 - shown * CARD_STACK_SHRINK_STEP).max(0.75);
70 let offset_y = shown * CARD_STACK_PEEK_PX;
71 let presence = if shown >= (CARD_STACK_VISIBLE - 1) as f32 {
72 0.0
73 } else if shown <= (CARD_STACK_VISIBLE - 2) as f32 {
74 1.0
75 } else {
76 1.0 - (shown - (CARD_STACK_VISIBLE - 2) as f32)
77 };
78 (scale, offset_y, presence)
79}
80
81pub fn card_stack_stamp_opacities(dx: f32) -> (f32, f32) {
83 let keep = ((dx - 16.0) / 80.0).clamp(0.0, 1.0);
84 let pass = ((-dx - 16.0) / 80.0).clamp(0.0, 1.0);
85 (keep, pass)
86}
87
88pub fn card_stack_should_commit(dx: f32, vx: f32, card_width: f32) -> Option<CardDecision> {
90 if dx == 0.0 {
91 return None;
92 }
93 let dir = dx.signum();
94 let flick = vx.abs() > CARD_STACK_FLICK_PX_S && vx.signum() == dir && dx.abs() > 12.0;
95 let far =
96 dx.abs() > (card_width * 0.35).min(140.0) && !(vx.signum() == -dir && vx.abs() > 300.0);
97 if flick || far {
98 Some(if dir > 0.0 {
99 CardDecision::Right
100 } else {
101 CardDecision::Left
102 })
103 } else {
104 None
105 }
106}
107
108fn paint_x_icon(cx: f32, cy: f32, r: f32, color: Hsla, window: &mut Window) {
109 let mut a = PathBuilder::stroke(px(1.75));
110 a.move_to(point(px(cx - r), px(cy - r)));
111 a.line_to(point(px(cx + r), px(cy + r)));
112 if let Ok(p) = a.build() {
113 window.paint_path(p, color);
114 }
115 let mut b = PathBuilder::stroke(px(1.75));
116 b.move_to(point(px(cx + r), px(cy - r)));
117 b.line_to(point(px(cx - r), px(cy + r)));
118 if let Ok(p) = b.build() {
119 window.paint_path(p, color);
120 }
121}
122
123fn paint_check_icon(cx: f32, cy: f32, color: Hsla, window: &mut Window) {
124 let mut pb = PathBuilder::stroke(px(1.85));
125 pb.move_to(point(px(cx - 4.5), px(cy + 0.2)));
126 pb.line_to(point(px(cx - 1.2), px(cy + 3.5)));
127 pb.line_to(point(px(cx + 5.0), px(cy - 3.5)));
128 if let Ok(p) = pb.build() {
129 window.paint_path(p, color);
130 }
131}
132
133fn paint_undo_icon(cx: f32, cy: f32, color: Hsla, window: &mut Window) {
134 let mut pb = PathBuilder::stroke(px(1.65));
135 pb.move_to(point(px(cx - 4.5), px(cy - 1.5)));
136 pb.line_to(point(px(cx - 2.0), px(cy - 4.2)));
137 pb.move_to(point(px(cx - 4.5), px(cy - 1.5)));
138 pb.line_to(point(px(cx - 2.0), px(cy + 1.2)));
139 pb.move_to(point(px(cx - 4.0), px(cy - 1.5)));
140 pb.line_to(point(px(cx + 2.5), px(cy - 1.5)));
141 pb.line_to(point(px(cx + 4.5), px(cy + 0.5)));
142 pb.line_to(point(px(cx + 4.5), px(cy + 3.0)));
143 pb.line_to(point(px(cx + 2.5), px(cy + 4.5)));
144 pb.line_to(point(px(cx - 2.5), px(cy + 4.5)));
145 if let Ok(p) = pb.build() {
146 window.paint_path(p, color);
147 }
148}
149
150type CardRenderer = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;
151
152#[derive(Clone)]
154pub struct CardStackItem {
155 pub id: SharedString,
156 pub label: SharedString,
157 pub render: CardRenderer,
158}
159
160impl CardStackItem {
161 pub fn new(
162 id: impl Into<SharedString>,
163 label: impl Into<SharedString>,
164 render: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
165 ) -> Self {
166 Self {
167 id: id.into(),
168 label: label.into(),
169 render: Rc::new(render),
170 }
171 }
172}
173
174type DecideCallback = Rc<dyn Fn(SharedString, CardDecision, &mut Window, &mut App)>;
175type ResetCallback = Rc<dyn Fn(&mut Window, &mut App)>;
176
177#[derive(Clone)]
178struct CardDrag {
179 start_x: f32,
180 start_y: f32,
181 samples: Vec<(Instant, f32, f32)>,
182}
183
184#[derive(Clone)]
185struct CardStackInternal {
186 position: usize,
187 history: Vec<(SharedString, CardDecision)>,
188 dragging: bool,
189 drag_x: f32,
190 drag_y: f32,
191 drag: Option<CardDrag>,
192 stage_bounds: Option<Bounds<Pixels>>,
193 focus_handle: FocusHandle,
194}
195
196impl CardStackInternal {
197 fn velocity(&self, now: Instant) -> (f32, f32) {
198 let Some(drag) = &self.drag else {
199 return (0.0, 0.0);
200 };
201 let recent: Vec<&(Instant, f32, f32)> = drag
202 .samples
203 .iter()
204 .filter(|(t, _, _)| now.saturating_duration_since(*t).as_millis() <= 80)
205 .collect();
206 if recent.len() < 2 {
207 return (0.0, 0.0);
208 }
209 let (t0, x0, y0) = *recent[0];
210 let (t1, x1, y1) = *recent[recent.len() - 1];
211 if now.saturating_duration_since(t1).as_millis() > 60 {
212 return (0.0, 0.0);
213 }
214 let secs = t1.saturating_duration_since(t0).as_secs_f32();
215 if secs > 0.001 {
216 ((x1 - x0) / secs, (y1 - y0) / secs)
217 } else {
218 (0.0, 0.0)
219 }
220 }
221}
222
223#[derive(IntoElement)]
225pub struct CardStack {
226 id: ElementId,
227 label: SharedString,
228 items: Vec<CardStackItem>,
229 left_label: SharedString,
230 right_label: SharedString,
231 undo_label: SharedString,
232 empty_title: SharedString,
233 reset_label: SharedString,
234 card_width: f32,
235 card_height: f32,
236 on_decide: Option<DecideCallback>,
237 on_undo: Option<DecideCallback>,
238 on_reset: Option<ResetCallback>,
239}
240
241impl CardStack {
242 pub fn new(id: impl Into<ElementId>, items: Vec<CardStackItem>) -> Self {
243 Self {
244 id: id.into(),
245 label: "Cards".into(),
246 items,
247 left_label: "Pass".into(),
248 right_label: "Keep".into(),
249 undo_label: "Undo".into(),
250 empty_title: "All cards reviewed".into(),
251 reset_label: "Start over".into(),
252 card_width: 320.0,
253 card_height: 304.0,
254 on_decide: None,
255 on_undo: None,
256 on_reset: None,
257 }
258 }
259
260 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
261 self.label = label.into();
262 self
263 }
264
265 pub fn labels(mut self, left: impl Into<SharedString>, right: impl Into<SharedString>) -> Self {
266 self.left_label = left.into();
267 self.right_label = right.into();
268 self
269 }
270
271 pub fn empty_title(mut self, title: impl Into<SharedString>) -> Self {
272 self.empty_title = title.into();
273 self
274 }
275
276 pub fn card_size(mut self, width: f32, height: f32) -> Self {
277 self.card_width = width.max(220.0);
278 self.card_height = height.max(180.0);
279 self
280 }
281
282 pub fn on_decide(
283 mut self,
284 handler: impl Fn(SharedString, CardDecision, &mut Window, &mut App) + 'static,
285 ) -> Self {
286 self.on_decide = Some(Rc::new(handler));
287 self
288 }
289
290 pub fn on_undo(
291 mut self,
292 handler: impl Fn(SharedString, CardDecision, &mut Window, &mut App) + 'static,
293 ) -> Self {
294 self.on_undo = Some(Rc::new(handler));
295 self
296 }
297
298 pub fn on_reset(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
299 self.on_reset = Some(Rc::new(handler));
300 self
301 }
302}
303
304impl RenderOnce for CardStack {
305 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
306 let theme = cx.arc_theme().clone();
307 let reduced = is_reduced_motion(cx);
308 let total = self.items.len();
309
310 let state = window.use_keyed_state(
311 ElementId::Name(format!("arc-card-stack-state-{:?}", self.id).into()),
312 cx,
313 |_, cx| {
314 Rc::new(RefCell::new(CardStackInternal {
315 position: 0,
316 history: Vec::new(),
317 dragging: false,
318 drag_x: 0.0,
319 drag_y: 0.0,
320 drag: None,
321 stage_bounds: None,
322 focus_handle: cx.focus_handle(),
323 }))
324 },
325 );
326 let shared = state.read(cx).clone();
327 let focus_handle = shared.borrow().focus_handle.clone();
328
329 let position = shared.borrow().position.min(total);
330 let has_top = position < total;
331 let can_undo = !shared.borrow().history.is_empty();
332 let is_dragging = shared.borrow().dragging;
333 let raw_dx = shared.borrow().drag_x;
334 let raw_dy = shared.borrow().drag_y;
335
336 let spring_x = if is_dragging || reduced {
338 raw_dx
339 } else {
340 arc_spring_with_options(
341 format!("arc-card-stack-x-{:?}-{}", self.id, position),
342 0.0,
343 SpringOptions::new(SpringSpec::visual(0.28, 0.08)),
344 window,
345 cx,
346 )
347 .value
348 };
349 let spring_y = if is_dragging || reduced {
350 raw_dy
351 } else {
352 arc_spring_with_options(
353 format!("arc-card-stack-y-{:?}-{}", self.id, position),
354 0.0,
355 SpringOptions::new(SpringSpec::visual(0.28, 0.08)),
356 window,
357 cx,
358 )
359 .value
360 };
361 let shown_x = if is_dragging { raw_dx } else { spring_x };
362 let shown_y = if is_dragging { raw_dy } else { spring_y };
363
364 let lift = ((shown_x.hypot(shown_y * 0.5)) / CARD_STACK_LIFT_DISTANCE).clamp(0.0, 1.0);
365 let (keep_alpha, pass_alpha) = card_stack_stamp_opacities(shown_x);
366
367 let item_ids: Rc<Vec<SharedString>> =
368 Rc::new(self.items.iter().map(|i| i.id.clone()).collect());
369
370 let decide_fn = {
371 let shared = shared.clone();
372 let item_ids = item_ids.clone();
373 let on_decide = self.on_decide.clone();
374 Rc::new(
375 move |decision: CardDecision, window: &mut Window, cx: &mut App| {
376 let decided_id = {
377 let mut st = shared.borrow_mut();
378 if st.position >= item_ids.len() {
379 return;
380 }
381 let id = item_ids[st.position].clone();
382 st.position += 1;
383 st.history.push((id.clone(), decision));
384 st.dragging = false;
385 st.drag = None;
386 st.drag_x = 0.0;
387 st.drag_y = 0.0;
388 id
389 };
390 if let Some(cb) = &on_decide {
391 cb(decided_id, decision, window, cx);
392 }
393 window.refresh();
394 },
395 )
396 };
397
398 let undo_fn = {
399 let shared = shared.clone();
400 let on_undo = self.on_undo.clone();
401 Rc::new(move |window: &mut Window, cx: &mut App| {
402 let restored = {
403 let mut st = shared.borrow_mut();
404 let Some((id, decision)) = st.history.pop() else {
405 return;
406 };
407 st.position = st.position.saturating_sub(1);
408 st.dragging = false;
409 st.drag = None;
410 st.drag_x = 0.0;
411 st.drag_y = 0.0;
412 (id, decision)
413 };
414 if let Some(cb) = &on_undo {
415 cb(restored.0, restored.1, window, cx);
416 }
417 window.refresh();
418 })
419 };
420
421 let reset_fn = {
422 let shared = shared.clone();
423 let on_reset = self.on_reset.clone();
424 Rc::new(move |window: &mut Window, cx: &mut App| {
425 {
426 let mut st = shared.borrow_mut();
427 if st.history.is_empty() && st.position == 0 {
428 return;
429 }
430 st.position = 0;
431 st.history.clear();
432 st.dragging = false;
433 st.drag = None;
434 st.drag_x = 0.0;
435 st.drag_y = 0.0;
436 }
437 if let Some(cb) = &on_reset {
438 cb(window, cx);
439 }
440 window.refresh();
441 })
442 };
443
444 let shared_bounds = shared.clone();
446 let shared_move = shared.clone();
447 let shared_up = shared.clone();
448 let decide_up = decide_fn.clone();
449 let card_w = self.card_width;
450
451 let drag_canvas = canvas(
452 move |bounds, _, _| {
453 shared_bounds.borrow_mut().stage_bounds = Some(bounds);
454 bounds
455 },
456 move |_bounds, _, window, _| {
457 let sm = shared_move.clone();
458 window.on_mouse_event(move |event: &MouseMoveEvent, _, window, _| {
459 let mut st = sm.borrow_mut();
460 let Some(drag) = st.drag.as_mut() else {
461 return;
462 };
463 let px_x = f32::from(event.position.x);
464 let px_y = f32::from(event.position.y);
465 let dx = px_x - drag.start_x;
466 let dy = card_stack_resist_y(px_y - drag.start_y);
467 drag.samples.push((Instant::now(), px_x, px_y));
468 if drag.samples.len() > 12 {
469 drag.samples.remove(0);
470 }
471 st.drag_x = dx;
472 st.drag_y = dy;
473 window.refresh();
474 });
475
476 let su = shared_up.clone();
477 let du = decide_up.clone();
478 window.on_mouse_event(move |_event: &MouseUpEvent, _, window, cx| {
479 let release = {
480 let mut st = su.borrow_mut();
481 if !st.dragging {
482 return;
483 }
484 let (vx, _) = st.velocity(Instant::now());
485 let dx = st.drag_x;
486 st.dragging = false;
487 st.drag = None;
488 let decision = card_stack_should_commit(dx, vx, card_w);
489 if decision.is_none() {
490 st.drag_x = 0.0;
491 st.drag_y = 0.0;
492 }
493 decision
494 };
495 if let Some(decision) = release {
496 du(decision, window, cx);
497 } else {
498 window.refresh();
499 }
500 });
501 },
502 )
503 .size_full()
504 .absolute()
505 .inset_0();
506
507 let stage_h = self.card_height + 24.0;
509 let visible_end = (position + CARD_STACK_VISIBLE).min(total);
510 let mut card_layers = Vec::new();
511
512 for idx in (position..visible_end).rev() {
513 let depth = idx - position;
514 let is_top = depth == 0;
515 let (scale, offset_y, presence) = card_stack_slot_metrics(depth, lift);
516 let w = self.card_width * scale;
517 let h = self.card_height * scale;
518 let left = (self.card_width - w) * 0.5 + if is_top { shown_x } else { 0.0 };
519 let top = (self.card_height - h) + offset_y + if is_top { shown_y } else { 0.0 };
520 let item = &self.items[idx];
521 let face_content = (item.render)(window, cx);
522
523 let shade_alpha = ((depth as f32) * 0.035).min(0.10);
524 let shade_bg = if matches!(theme.mode, arc_tokens::ThemeMode::Dark) {
525 gpui_kit::hsla(0.0, 0.0, 0.0, shade_alpha * 2.4)
526 } else {
527 gpui_kit::hsla(0.0, 0.0, 0.0, shade_alpha)
528 };
529
530 let mut card_box = div()
531 .id(ElementId::Name(
532 format!("arc-card-stack-card-{:?}-{}", self.id, item.id).into(),
533 ))
534 .absolute()
535 .left(px(left))
536 .top(px(top))
537 .w(px(w))
538 .h(px(h))
539 .opacity(presence)
540 .rounded(Radii::SURFACE)
541 .border_1()
542 .border_color(theme.colors.border)
543 .bg(theme.colors.surface)
544 .overflow_hidden()
545 .when(is_top && is_dragging, |el| {
546 el.shadow(Shadows::floating(theme.mode))
547 })
548 .when(is_top, |el| {
549 let sd = shared.clone();
550 let fh = focus_handle.clone();
551 el.role(Role::Group)
552 .aria_label(format!("{}, {} of {}", item.label, idx + 1, total))
553 .cursor_grab()
554 .on_mouse_down(
555 MouseButton::Left,
556 move |event: &MouseDownEvent, window, cx| {
557 window.focus(&fh, cx);
558 let px_x = f32::from(event.position.x);
559 let px_y = f32::from(event.position.y);
560 let mut st = sd.borrow_mut();
561 st.dragging = true;
562 st.drag_x = 0.0;
563 st.drag_y = 0.0;
564 st.drag = Some(CardDrag {
565 start_x: px_x,
566 start_y: px_y,
567 samples: vec![(Instant::now(), px_x, px_y)],
568 });
569 window.refresh();
570 },
571 )
572 })
573 .child(div().size_full().child(face_content));
574
575 if depth > 0 {
576 card_box = card_box.child(div().absolute().inset_0().bg(shade_bg));
577 }
578
579 if is_top && keep_alpha > 0.01 {
581 let keep_border = oklab_mix(theme.colors.border, theme.colors.success, 0.35);
582 let keep_bg = oklab_mix(theme.colors.surface_raised, theme.colors.success, 0.10);
583 let success_color = theme.colors.success;
584 card_box = card_box.child(
585 div()
586 .absolute()
587 .top(Spacing::S4)
588 .left(Spacing::S4)
589 .h(px(32.0))
590 .pl(px(10.0))
591 .pr(Spacing::S3)
592 .rounded(Radii::PILL)
593 .border_1()
594 .border_color(keep_border)
595 .bg(keep_bg)
596 .opacity(keep_alpha)
597 .flex()
598 .items_center()
599 .gap(px(6.0))
600 .child(
601 canvas(
602 |b, _, _| b,
603 move |b, _, window, _| {
604 let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
605 let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
606 paint_check_icon(cx, cy, success_color, window);
607 },
608 )
609 .w(px(14.0))
610 .h(px(14.0)),
611 )
612 .child(
613 div()
614 .text_size(Typography::TEXT_SM)
615 .text_color(theme.colors.foreground)
616 .child(self.right_label.clone()),
617 ),
618 );
619 }
620
621 if is_top && pass_alpha > 0.01 {
622 let muted_color = theme.colors.text_secondary;
623 card_box = card_box.child(
624 div()
625 .absolute()
626 .top(Spacing::S4)
627 .right(Spacing::S4)
628 .h(px(32.0))
629 .pl(px(10.0))
630 .pr(Spacing::S3)
631 .rounded(Radii::PILL)
632 .border_1()
633 .border_color(theme.colors.border)
634 .bg(theme.colors.surface_raised)
635 .opacity(pass_alpha)
636 .flex()
637 .items_center()
638 .gap(px(6.0))
639 .child(
640 canvas(
641 |b, _, _| b,
642 move |b, _, window, _| {
643 let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
644 let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
645 paint_x_icon(cx, cy, 4.0, muted_color, window);
646 },
647 )
648 .w(px(14.0))
649 .h(px(14.0)),
650 )
651 .child(
652 div()
653 .text_size(Typography::TEXT_SM)
654 .text_color(theme.colors.foreground)
655 .child(self.left_label.clone()),
656 ),
657 );
658 }
659
660 card_layers.push(card_box);
661 }
662
663 if !has_top {
665 let reset_click = reset_fn.clone();
666 let fg_icon = theme.colors.foreground;
667 card_layers.push(
668 div()
669 .id(ElementId::Name(
670 format!("arc-card-stack-empty-{:?}", self.id).into(),
671 ))
672 .absolute()
673 .top_0()
674 .left_0()
675 .w(px(self.card_width))
676 .h(px(self.card_height))
677 .rounded(Radii::SURFACE)
678 .border_1()
679 .border_color(theme.colors.border)
680 .bg(theme.colors.surface)
681 .flex()
682 .flex_col()
683 .items_center()
684 .justify_center()
685 .gap(Spacing::S3)
686 .p(Spacing::S6)
687 .child(
688 div()
689 .text_size(Typography::TEXT_BASE)
690 .text_color(theme.colors.foreground)
691 .child(self.empty_title),
692 )
693 .child(
694 div()
695 .id(ElementId::Name(
696 format!("arc-card-stack-restart-{:?}", self.id).into(),
697 ))
698 .role(Role::Button)
699 .h(px(36.0))
700 .px(Spacing::S4)
701 .rounded(Radii::CONTROL)
702 .border_1()
703 .border_color(theme.colors.border)
704 .bg(theme.colors.surface)
705 .hover(|s| s.bg(theme.colors.surface_muted))
706 .cursor_pointer()
707 .flex()
708 .items_center()
709 .gap(Spacing::S2)
710 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
711 reset_click(window, cx);
712 })
713 .child(
714 canvas(
715 |b, _, _| b,
716 move |b, _, window, _| {
717 let cx =
718 f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
719 let cy =
720 f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
721 paint_undo_icon(cx, cy, fg_icon, window);
722 },
723 )
724 .w(px(14.0))
725 .h(px(14.0)),
726 )
727 .child(
728 div()
729 .text_size(Typography::TEXT_SM)
730 .text_color(theme.colors.foreground)
731 .child(self.reset_label),
732 ),
733 ),
734 );
735 }
736
737 let decide_key = decide_fn.clone();
739 let undo_key = undo_fn.clone();
740 let stage = div()
741 .id(ElementId::Name(
742 format!("arc-card-stack-stage-{:?}", self.id).into(),
743 ))
744 .role(Role::Group)
745 .aria_label(self.label)
746 .track_focus(&focus_handle)
747 .relative()
748 .w(px(self.card_width))
749 .h(px(stage_h))
750 .on_key_down(move |event: &KeyDownEvent, window, cx| {
751 let key = event.keystroke.key.as_str();
752 if key == "left" {
753 decide_key(CardDecision::Left, window, cx);
754 } else if key == "right" {
755 decide_key(CardDecision::Right, window, cx);
756 } else if key == "backspace"
757 || (key.eq_ignore_ascii_case("z")
758 && (event.keystroke.modifiers.control
759 || event.keystroke.modifiers.platform))
760 {
761 undo_key(window, cx);
762 }
763 })
764 .child(drag_canvas)
765 .children(card_layers);
766
767 let decide_left = decide_fn.clone();
769 let decide_right = decide_fn.clone();
770 let undo_click = undo_fn.clone();
771 let fg_color = theme.colors.foreground;
772 let muted_color = theme.colors.text_secondary;
773 let accent_fg = theme.colors.accent_foreground;
774
775 let pass_btn = div()
776 .id(ElementId::Name(
777 format!("arc-card-stack-pass-{:?}", self.id).into(),
778 ))
779 .role(Role::Button)
780 .h(px(36.0))
781 .px(Spacing::S4)
782 .rounded(Radii::CONTROL)
783 .border_1()
784 .border_color(theme.colors.border)
785 .bg(theme.colors.surface)
786 .opacity(if has_top { 1.0 } else { 0.42 })
787 .when(has_top, |el| {
788 el.cursor_pointer()
789 .hover(|s| s.bg(theme.colors.surface_muted))
790 })
791 .flex()
792 .items_center()
793 .gap(Spacing::S2)
794 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
795 if has_top {
796 decide_left(CardDecision::Left, window, cx);
797 }
798 })
799 .child(
800 canvas(
801 |b, _, _| b,
802 move |b, _, window, _| {
803 let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
804 let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
805 paint_x_icon(cx, cy, 4.2, fg_color, window);
806 },
807 )
808 .w(px(14.0))
809 .h(px(14.0)),
810 )
811 .child(
812 div()
813 .text_size(Typography::TEXT_SM)
814 .text_color(theme.colors.foreground)
815 .child(self.left_label),
816 );
817
818 let undo_btn = div()
819 .id(ElementId::Name(
820 format!("arc-card-stack-undo-{:?}", self.id).into(),
821 ))
822 .role(Role::Button)
823 .h(px(36.0))
824 .px(Spacing::S3)
825 .rounded(Radii::CONTROL)
826 .opacity(if can_undo { 1.0 } else { 0.42 })
827 .when(can_undo, |el| {
828 el.cursor_pointer()
829 .hover(|s| s.bg(theme.colors.surface_muted))
830 })
831 .flex()
832 .items_center()
833 .gap(Spacing::S2)
834 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
835 if can_undo {
836 undo_click(window, cx);
837 }
838 })
839 .child(
840 canvas(
841 |b, _, _| b,
842 move |b, _, window, _| {
843 let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
844 let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
845 paint_undo_icon(cx, cy, muted_color, window);
846 },
847 )
848 .w(px(14.0))
849 .h(px(14.0)),
850 )
851 .child(
852 div()
853 .text_size(Typography::TEXT_SM)
854 .text_color(theme.colors.text_secondary)
855 .child(self.undo_label),
856 );
857
858 let keep_btn = div()
859 .id(ElementId::Name(
860 format!("arc-card-stack-keep-{:?}", self.id).into(),
861 ))
862 .role(Role::Button)
863 .h(px(36.0))
864 .px(Spacing::S4)
865 .rounded(Radii::CONTROL)
866 .border_1()
867 .border_color(theme.colors.accent)
868 .bg(theme.colors.accent)
869 .opacity(if has_top { 1.0 } else { 0.42 })
870 .when(has_top, |el| {
871 el.cursor_pointer()
872 .hover(|s| s.bg(theme.colors.accent_strong))
873 })
874 .flex()
875 .items_center()
876 .gap(Spacing::S2)
877 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
878 if has_top {
879 decide_right(CardDecision::Right, window, cx);
880 }
881 })
882 .child(
883 canvas(
884 |b, _, _| b,
885 move |b, _, window, _| {
886 let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
887 let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
888 paint_check_icon(cx, cy, accent_fg, window);
889 },
890 )
891 .w(px(14.0))
892 .h(px(14.0)),
893 )
894 .child(
895 div()
896 .text_size(Typography::TEXT_SM)
897 .text_color(theme.colors.accent_foreground)
898 .child(self.right_label),
899 );
900
901 let controls = div()
902 .flex()
903 .items_center()
904 .justify_center()
905 .gap(Spacing::S2)
906 .child(pass_btn)
907 .child(undo_btn)
908 .child(keep_btn);
909
910 div()
911 .id(self.id)
912 .w_full()
913 .flex()
914 .flex_col()
915 .items_center()
916 .gap(Spacing::S5)
917 .child(stage)
918 .child(controls)
919 }
920}
921
922#[cfg(test)]
923mod tests {
924 use super::*;
925
926 #[test]
927 fn card_stack_math_rubber_slots_stamps_and_commit() {
928 assert!((card_stack_resist_y(30.0) - 30.0).abs() < 1e-4);
930 let damped = card_stack_resist_y(200.0);
931 assert!(damped > CARD_STACK_FREE_Y && damped < 200.0);
932
933 let (s0, y0, p0) = card_stack_slot_metrics(0, 0.0);
935 assert!((s0 - 1.0).abs() < 1e-4 && y0 == 0.0 && (p0 - 1.0).abs() < 1e-4);
936 let (s1, y1, _) = card_stack_slot_metrics(1, 0.0);
937 assert!((s1 - 0.95).abs() < 1e-4 && (y1 - CARD_STACK_PEEK_PX).abs() < 1e-4);
938 let (s1_lifted, y1_lifted, _) = card_stack_slot_metrics(1, 1.0);
940 assert!(s1_lifted > s1 && y1_lifted < y1);
941
942 let (keep, pass) = card_stack_stamp_opacities(96.0);
944 assert!((keep - 1.0).abs() < 1e-4 && pass == 0.0);
945 let (keep2, pass2) = card_stack_stamp_opacities(-56.0);
946 assert!(keep2 == 0.0 && (pass2 - 0.5).abs() < 1e-4);
947
948 assert_eq!(
950 card_stack_should_commit(130.0, 20.0, 320.0),
951 Some(CardDecision::Right)
952 );
953 assert_eq!(
954 card_stack_should_commit(-24.0, -650.0, 320.0),
955 Some(CardDecision::Left)
956 );
957 assert_eq!(card_stack_should_commit(40.0, 10.0, 320.0), None);
958 }
959}