use std::{cell::RefCell, rc::Rc, time::Instant};
use arc_motion::{arc_spring_with_options, is_reduced_motion, spring::SpringOptions};
use arc_tokens::{ActiveArcTheme as _, Radii, Shadows, Spacing, SpringSpec, Typography, oklab_mix};
use gpui_kit::{
AnyElement, App, Bounds, ElementId, FocusHandle, Hsla, IntoElement, KeyDownEvent, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, PathBuilder, Pixels, RenderOnce,
Role, SharedString, Styled, Window, canvas, div, point, prelude::*, px,
};
pub const CARD_STACK_VISIBLE: usize = 4;
pub const CARD_STACK_PEEK_PX: f32 = 12.0;
pub const CARD_STACK_SHRINK_STEP: f32 = 0.05;
pub const CARD_STACK_LIFT: f32 = 0.40;
pub const CARD_STACK_LIFT_DISTANCE: f32 = 160.0;
pub const CARD_STACK_FREE_Y: f32 = 48.0;
pub const CARD_STACK_STRETCH_Y: f32 = 140.0;
pub const CARD_STACK_FLICK_PX_S: f32 = 500.0;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CardDecision {
Left,
Right,
}
pub fn card_stack_resist_y(raw: f32) -> f32 {
let dist = raw.abs();
if dist <= CARD_STACK_FREE_Y {
raw
} else {
raw.signum()
* (CARD_STACK_FREE_Y
+ (1.0 - 1.0 / ((dist - CARD_STACK_FREE_Y) * 0.55 / CARD_STACK_STRETCH_Y + 1.0))
* CARD_STACK_STRETCH_Y)
}
}
pub fn card_stack_slot_metrics(depth: usize, lift: f32) -> (f32, f32, f32) {
let shown = if depth == 0 {
0.0
} else {
(depth as f32 - lift.clamp(0.0, 1.0) * CARD_STACK_LIFT).max(0.0)
};
let scale = (1.0 - shown * CARD_STACK_SHRINK_STEP).max(0.75);
let offset_y = shown * CARD_STACK_PEEK_PX;
let presence = if shown >= (CARD_STACK_VISIBLE - 1) as f32 {
0.0
} else if shown <= (CARD_STACK_VISIBLE - 2) as f32 {
1.0
} else {
1.0 - (shown - (CARD_STACK_VISIBLE - 2) as f32)
};
(scale, offset_y, presence)
}
pub fn card_stack_stamp_opacities(dx: f32) -> (f32, f32) {
let keep = ((dx - 16.0) / 80.0).clamp(0.0, 1.0);
let pass = ((-dx - 16.0) / 80.0).clamp(0.0, 1.0);
(keep, pass)
}
pub fn card_stack_should_commit(dx: f32, vx: f32, card_width: f32) -> Option<CardDecision> {
if dx == 0.0 {
return None;
}
let dir = dx.signum();
let flick = vx.abs() > CARD_STACK_FLICK_PX_S && vx.signum() == dir && dx.abs() > 12.0;
let far =
dx.abs() > (card_width * 0.35).min(140.0) && !(vx.signum() == -dir && vx.abs() > 300.0);
if flick || far {
Some(if dir > 0.0 {
CardDecision::Right
} else {
CardDecision::Left
})
} else {
None
}
}
fn paint_x_icon(cx: f32, cy: f32, r: f32, color: Hsla, window: &mut Window) {
let mut a = PathBuilder::stroke(px(1.75));
a.move_to(point(px(cx - r), px(cy - r)));
a.line_to(point(px(cx + r), px(cy + r)));
if let Ok(p) = a.build() {
window.paint_path(p, color);
}
let mut b = PathBuilder::stroke(px(1.75));
b.move_to(point(px(cx + r), px(cy - r)));
b.line_to(point(px(cx - r), px(cy + r)));
if let Ok(p) = b.build() {
window.paint_path(p, color);
}
}
fn paint_check_icon(cx: f32, cy: f32, color: Hsla, window: &mut Window) {
let mut pb = PathBuilder::stroke(px(1.85));
pb.move_to(point(px(cx - 4.5), px(cy + 0.2)));
pb.line_to(point(px(cx - 1.2), px(cy + 3.5)));
pb.line_to(point(px(cx + 5.0), px(cy - 3.5)));
if let Ok(p) = pb.build() {
window.paint_path(p, color);
}
}
fn paint_undo_icon(cx: f32, cy: f32, color: Hsla, window: &mut Window) {
let mut pb = PathBuilder::stroke(px(1.65));
pb.move_to(point(px(cx - 4.5), px(cy - 1.5)));
pb.line_to(point(px(cx - 2.0), px(cy - 4.2)));
pb.move_to(point(px(cx - 4.5), px(cy - 1.5)));
pb.line_to(point(px(cx - 2.0), px(cy + 1.2)));
pb.move_to(point(px(cx - 4.0), px(cy - 1.5)));
pb.line_to(point(px(cx + 2.5), px(cy - 1.5)));
pb.line_to(point(px(cx + 4.5), px(cy + 0.5)));
pb.line_to(point(px(cx + 4.5), px(cy + 3.0)));
pb.line_to(point(px(cx + 2.5), px(cy + 4.5)));
pb.line_to(point(px(cx - 2.5), px(cy + 4.5)));
if let Ok(p) = pb.build() {
window.paint_path(p, color);
}
}
type CardRenderer = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;
#[derive(Clone)]
pub struct CardStackItem {
pub id: SharedString,
pub label: SharedString,
pub render: CardRenderer,
}
impl CardStackItem {
pub fn new(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
render: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
) -> Self {
Self {
id: id.into(),
label: label.into(),
render: Rc::new(render),
}
}
}
type DecideCallback = Rc<dyn Fn(SharedString, CardDecision, &mut Window, &mut App)>;
type ResetCallback = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(Clone)]
struct CardDrag {
start_x: f32,
start_y: f32,
samples: Vec<(Instant, f32, f32)>,
}
#[derive(Clone)]
struct CardStackInternal {
position: usize,
history: Vec<(SharedString, CardDecision)>,
dragging: bool,
drag_x: f32,
drag_y: f32,
drag: Option<CardDrag>,
stage_bounds: Option<Bounds<Pixels>>,
focus_handle: FocusHandle,
}
impl CardStackInternal {
fn velocity(&self, now: Instant) -> (f32, f32) {
let Some(drag) = &self.drag else {
return (0.0, 0.0);
};
let recent: Vec<&(Instant, f32, f32)> = drag
.samples
.iter()
.filter(|(t, _, _)| now.saturating_duration_since(*t).as_millis() <= 80)
.collect();
if recent.len() < 2 {
return (0.0, 0.0);
}
let (t0, x0, y0) = *recent[0];
let (t1, x1, y1) = *recent[recent.len() - 1];
if now.saturating_duration_since(t1).as_millis() > 60 {
return (0.0, 0.0);
}
let secs = t1.saturating_duration_since(t0).as_secs_f32();
if secs > 0.001 {
((x1 - x0) / secs, (y1 - y0) / secs)
} else {
(0.0, 0.0)
}
}
}
#[derive(IntoElement)]
pub struct CardStack {
id: ElementId,
label: SharedString,
items: Vec<CardStackItem>,
left_label: SharedString,
right_label: SharedString,
undo_label: SharedString,
empty_title: SharedString,
reset_label: SharedString,
card_width: f32,
card_height: f32,
on_decide: Option<DecideCallback>,
on_undo: Option<DecideCallback>,
on_reset: Option<ResetCallback>,
}
impl CardStack {
pub fn new(id: impl Into<ElementId>, items: Vec<CardStackItem>) -> Self {
Self {
id: id.into(),
label: "Cards".into(),
items,
left_label: "Pass".into(),
right_label: "Keep".into(),
undo_label: "Undo".into(),
empty_title: "All cards reviewed".into(),
reset_label: "Start over".into(),
card_width: 320.0,
card_height: 304.0,
on_decide: None,
on_undo: None,
on_reset: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = label.into();
self
}
pub fn labels(mut self, left: impl Into<SharedString>, right: impl Into<SharedString>) -> Self {
self.left_label = left.into();
self.right_label = right.into();
self
}
pub fn empty_title(mut self, title: impl Into<SharedString>) -> Self {
self.empty_title = title.into();
self
}
pub fn card_size(mut self, width: f32, height: f32) -> Self {
self.card_width = width.max(220.0);
self.card_height = height.max(180.0);
self
}
pub fn on_decide(
mut self,
handler: impl Fn(SharedString, CardDecision, &mut Window, &mut App) + 'static,
) -> Self {
self.on_decide = Some(Rc::new(handler));
self
}
pub fn on_undo(
mut self,
handler: impl Fn(SharedString, CardDecision, &mut Window, &mut App) + 'static,
) -> Self {
self.on_undo = Some(Rc::new(handler));
self
}
pub fn on_reset(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_reset = Some(Rc::new(handler));
self
}
}
impl RenderOnce for CardStack {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let reduced = is_reduced_motion(cx);
let total = self.items.len();
let state = window.use_keyed_state(
ElementId::Name(format!("arc-card-stack-state-{:?}", self.id).into()),
cx,
|_, cx| {
Rc::new(RefCell::new(CardStackInternal {
position: 0,
history: Vec::new(),
dragging: false,
drag_x: 0.0,
drag_y: 0.0,
drag: None,
stage_bounds: None,
focus_handle: cx.focus_handle(),
}))
},
);
let shared = state.read(cx).clone();
let focus_handle = shared.borrow().focus_handle.clone();
let position = shared.borrow().position.min(total);
let has_top = position < total;
let can_undo = !shared.borrow().history.is_empty();
let is_dragging = shared.borrow().dragging;
let raw_dx = shared.borrow().drag_x;
let raw_dy = shared.borrow().drag_y;
let spring_x = if is_dragging || reduced {
raw_dx
} else {
arc_spring_with_options(
format!("arc-card-stack-x-{:?}-{}", self.id, position),
0.0,
SpringOptions::new(SpringSpec::visual(0.28, 0.08)),
window,
cx,
)
.value
};
let spring_y = if is_dragging || reduced {
raw_dy
} else {
arc_spring_with_options(
format!("arc-card-stack-y-{:?}-{}", self.id, position),
0.0,
SpringOptions::new(SpringSpec::visual(0.28, 0.08)),
window,
cx,
)
.value
};
let shown_x = if is_dragging { raw_dx } else { spring_x };
let shown_y = if is_dragging { raw_dy } else { spring_y };
let lift = ((shown_x.hypot(shown_y * 0.5)) / CARD_STACK_LIFT_DISTANCE).clamp(0.0, 1.0);
let (keep_alpha, pass_alpha) = card_stack_stamp_opacities(shown_x);
let item_ids: Rc<Vec<SharedString>> =
Rc::new(self.items.iter().map(|i| i.id.clone()).collect());
let decide_fn = {
let shared = shared.clone();
let item_ids = item_ids.clone();
let on_decide = self.on_decide.clone();
Rc::new(
move |decision: CardDecision, window: &mut Window, cx: &mut App| {
let decided_id = {
let mut st = shared.borrow_mut();
if st.position >= item_ids.len() {
return;
}
let id = item_ids[st.position].clone();
st.position += 1;
st.history.push((id.clone(), decision));
st.dragging = false;
st.drag = None;
st.drag_x = 0.0;
st.drag_y = 0.0;
id
};
if let Some(cb) = &on_decide {
cb(decided_id, decision, window, cx);
}
window.refresh();
},
)
};
let undo_fn = {
let shared = shared.clone();
let on_undo = self.on_undo.clone();
Rc::new(move |window: &mut Window, cx: &mut App| {
let restored = {
let mut st = shared.borrow_mut();
let Some((id, decision)) = st.history.pop() else {
return;
};
st.position = st.position.saturating_sub(1);
st.dragging = false;
st.drag = None;
st.drag_x = 0.0;
st.drag_y = 0.0;
(id, decision)
};
if let Some(cb) = &on_undo {
cb(restored.0, restored.1, window, cx);
}
window.refresh();
})
};
let reset_fn = {
let shared = shared.clone();
let on_reset = self.on_reset.clone();
Rc::new(move |window: &mut Window, cx: &mut App| {
{
let mut st = shared.borrow_mut();
if st.history.is_empty() && st.position == 0 {
return;
}
st.position = 0;
st.history.clear();
st.dragging = false;
st.drag = None;
st.drag_x = 0.0;
st.drag_y = 0.0;
}
if let Some(cb) = &on_reset {
cb(window, cx);
}
window.refresh();
})
};
let shared_bounds = shared.clone();
let shared_move = shared.clone();
let shared_up = shared.clone();
let decide_up = decide_fn.clone();
let card_w = self.card_width;
let drag_canvas = canvas(
move |bounds, _, _| {
shared_bounds.borrow_mut().stage_bounds = Some(bounds);
bounds
},
move |_bounds, _, window, _| {
let sm = shared_move.clone();
window.on_mouse_event(move |event: &MouseMoveEvent, _, window, _| {
let mut st = sm.borrow_mut();
let Some(drag) = st.drag.as_mut() else {
return;
};
let px_x = f32::from(event.position.x);
let px_y = f32::from(event.position.y);
let dx = px_x - drag.start_x;
let dy = card_stack_resist_y(px_y - drag.start_y);
drag.samples.push((Instant::now(), px_x, px_y));
if drag.samples.len() > 12 {
drag.samples.remove(0);
}
st.drag_x = dx;
st.drag_y = dy;
window.refresh();
});
let su = shared_up.clone();
let du = decide_up.clone();
window.on_mouse_event(move |_event: &MouseUpEvent, _, window, cx| {
let release = {
let mut st = su.borrow_mut();
if !st.dragging {
return;
}
let (vx, _) = st.velocity(Instant::now());
let dx = st.drag_x;
st.dragging = false;
st.drag = None;
let decision = card_stack_should_commit(dx, vx, card_w);
if decision.is_none() {
st.drag_x = 0.0;
st.drag_y = 0.0;
}
decision
};
if let Some(decision) = release {
du(decision, window, cx);
} else {
window.refresh();
}
});
},
)
.size_full()
.absolute()
.inset_0();
let stage_h = self.card_height + 24.0;
let visible_end = (position + CARD_STACK_VISIBLE).min(total);
let mut card_layers = Vec::new();
for idx in (position..visible_end).rev() {
let depth = idx - position;
let is_top = depth == 0;
let (scale, offset_y, presence) = card_stack_slot_metrics(depth, lift);
let w = self.card_width * scale;
let h = self.card_height * scale;
let left = (self.card_width - w) * 0.5 + if is_top { shown_x } else { 0.0 };
let top = (self.card_height - h) + offset_y + if is_top { shown_y } else { 0.0 };
let item = &self.items[idx];
let face_content = (item.render)(window, cx);
let shade_alpha = ((depth as f32) * 0.035).min(0.10);
let shade_bg = if matches!(theme.mode, arc_tokens::ThemeMode::Dark) {
gpui_kit::hsla(0.0, 0.0, 0.0, shade_alpha * 2.4)
} else {
gpui_kit::hsla(0.0, 0.0, 0.0, shade_alpha)
};
let mut card_box = div()
.id(ElementId::Name(
format!("arc-card-stack-card-{:?}-{}", self.id, item.id).into(),
))
.absolute()
.left(px(left))
.top(px(top))
.w(px(w))
.h(px(h))
.opacity(presence)
.rounded(Radii::SURFACE)
.border_1()
.border_color(theme.colors.border)
.bg(theme.colors.surface)
.overflow_hidden()
.when(is_top && is_dragging, |el| {
el.shadow(Shadows::floating(theme.mode))
})
.when(is_top, |el| {
let sd = shared.clone();
let fh = focus_handle.clone();
el.role(Role::Group)
.aria_label(format!("{}, {} of {}", item.label, idx + 1, total))
.cursor_grab()
.on_mouse_down(
MouseButton::Left,
move |event: &MouseDownEvent, window, cx| {
window.focus(&fh, cx);
let px_x = f32::from(event.position.x);
let px_y = f32::from(event.position.y);
let mut st = sd.borrow_mut();
st.dragging = true;
st.drag_x = 0.0;
st.drag_y = 0.0;
st.drag = Some(CardDrag {
start_x: px_x,
start_y: px_y,
samples: vec![(Instant::now(), px_x, px_y)],
});
window.refresh();
},
)
})
.child(div().size_full().child(face_content));
if depth > 0 {
card_box = card_box.child(div().absolute().inset_0().bg(shade_bg));
}
if is_top && keep_alpha > 0.01 {
let keep_border = oklab_mix(theme.colors.border, theme.colors.success, 0.35);
let keep_bg = oklab_mix(theme.colors.surface_raised, theme.colors.success, 0.10);
let success_color = theme.colors.success;
card_box = card_box.child(
div()
.absolute()
.top(Spacing::S4)
.left(Spacing::S4)
.h(px(32.0))
.pl(px(10.0))
.pr(Spacing::S3)
.rounded(Radii::PILL)
.border_1()
.border_color(keep_border)
.bg(keep_bg)
.opacity(keep_alpha)
.flex()
.items_center()
.gap(px(6.0))
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_check_icon(cx, cy, success_color, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.foreground)
.child(self.right_label.clone()),
),
);
}
if is_top && pass_alpha > 0.01 {
let muted_color = theme.colors.text_secondary;
card_box = card_box.child(
div()
.absolute()
.top(Spacing::S4)
.right(Spacing::S4)
.h(px(32.0))
.pl(px(10.0))
.pr(Spacing::S3)
.rounded(Radii::PILL)
.border_1()
.border_color(theme.colors.border)
.bg(theme.colors.surface_raised)
.opacity(pass_alpha)
.flex()
.items_center()
.gap(px(6.0))
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_x_icon(cx, cy, 4.0, muted_color, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.foreground)
.child(self.left_label.clone()),
),
);
}
card_layers.push(card_box);
}
if !has_top {
let reset_click = reset_fn.clone();
let fg_icon = theme.colors.foreground;
card_layers.push(
div()
.id(ElementId::Name(
format!("arc-card-stack-empty-{:?}", self.id).into(),
))
.absolute()
.top_0()
.left_0()
.w(px(self.card_width))
.h(px(self.card_height))
.rounded(Radii::SURFACE)
.border_1()
.border_color(theme.colors.border)
.bg(theme.colors.surface)
.flex()
.flex_col()
.items_center()
.justify_center()
.gap(Spacing::S3)
.p(Spacing::S6)
.child(
div()
.text_size(Typography::TEXT_BASE)
.text_color(theme.colors.foreground)
.child(self.empty_title),
)
.child(
div()
.id(ElementId::Name(
format!("arc-card-stack-restart-{:?}", self.id).into(),
))
.role(Role::Button)
.h(px(36.0))
.px(Spacing::S4)
.rounded(Radii::CONTROL)
.border_1()
.border_color(theme.colors.border)
.bg(theme.colors.surface)
.hover(|s| s.bg(theme.colors.surface_muted))
.cursor_pointer()
.flex()
.items_center()
.gap(Spacing::S2)
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
reset_click(window, cx);
})
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx =
f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy =
f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_undo_icon(cx, cy, fg_icon, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.foreground)
.child(self.reset_label),
),
),
);
}
let decide_key = decide_fn.clone();
let undo_key = undo_fn.clone();
let stage = div()
.id(ElementId::Name(
format!("arc-card-stack-stage-{:?}", self.id).into(),
))
.role(Role::Group)
.aria_label(self.label)
.track_focus(&focus_handle)
.relative()
.w(px(self.card_width))
.h(px(stage_h))
.on_key_down(move |event: &KeyDownEvent, window, cx| {
let key = event.keystroke.key.as_str();
if key == "left" {
decide_key(CardDecision::Left, window, cx);
} else if key == "right" {
decide_key(CardDecision::Right, window, cx);
} else if key == "backspace"
|| (key.eq_ignore_ascii_case("z")
&& (event.keystroke.modifiers.control
|| event.keystroke.modifiers.platform))
{
undo_key(window, cx);
}
})
.child(drag_canvas)
.children(card_layers);
let decide_left = decide_fn.clone();
let decide_right = decide_fn.clone();
let undo_click = undo_fn.clone();
let fg_color = theme.colors.foreground;
let muted_color = theme.colors.text_secondary;
let accent_fg = theme.colors.accent_foreground;
let pass_btn = div()
.id(ElementId::Name(
format!("arc-card-stack-pass-{:?}", self.id).into(),
))
.role(Role::Button)
.h(px(36.0))
.px(Spacing::S4)
.rounded(Radii::CONTROL)
.border_1()
.border_color(theme.colors.border)
.bg(theme.colors.surface)
.opacity(if has_top { 1.0 } else { 0.42 })
.when(has_top, |el| {
el.cursor_pointer()
.hover(|s| s.bg(theme.colors.surface_muted))
})
.flex()
.items_center()
.gap(Spacing::S2)
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if has_top {
decide_left(CardDecision::Left, window, cx);
}
})
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_x_icon(cx, cy, 4.2, fg_color, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.foreground)
.child(self.left_label),
);
let undo_btn = div()
.id(ElementId::Name(
format!("arc-card-stack-undo-{:?}", self.id).into(),
))
.role(Role::Button)
.h(px(36.0))
.px(Spacing::S3)
.rounded(Radii::CONTROL)
.opacity(if can_undo { 1.0 } else { 0.42 })
.when(can_undo, |el| {
el.cursor_pointer()
.hover(|s| s.bg(theme.colors.surface_muted))
})
.flex()
.items_center()
.gap(Spacing::S2)
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if can_undo {
undo_click(window, cx);
}
})
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_undo_icon(cx, cy, muted_color, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.text_secondary)
.child(self.undo_label),
);
let keep_btn = div()
.id(ElementId::Name(
format!("arc-card-stack-keep-{:?}", self.id).into(),
))
.role(Role::Button)
.h(px(36.0))
.px(Spacing::S4)
.rounded(Radii::CONTROL)
.border_1()
.border_color(theme.colors.accent)
.bg(theme.colors.accent)
.opacity(if has_top { 1.0 } else { 0.42 })
.when(has_top, |el| {
el.cursor_pointer()
.hover(|s| s.bg(theme.colors.accent_strong))
})
.flex()
.items_center()
.gap(Spacing::S2)
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if has_top {
decide_right(CardDecision::Right, window, cx);
}
})
.child(
canvas(
|b, _, _| b,
move |b, _, window, _| {
let cx = f32::from(b.origin.x) + f32::from(b.size.width) * 0.5;
let cy = f32::from(b.origin.y) + f32::from(b.size.height) * 0.5;
paint_check_icon(cx, cy, accent_fg, window);
},
)
.w(px(14.0))
.h(px(14.0)),
)
.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(theme.colors.accent_foreground)
.child(self.right_label),
);
let controls = div()
.flex()
.items_center()
.justify_center()
.gap(Spacing::S2)
.child(pass_btn)
.child(undo_btn)
.child(keep_btn);
div()
.id(self.id)
.w_full()
.flex()
.flex_col()
.items_center()
.gap(Spacing::S5)
.child(stage)
.child(controls)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn card_stack_math_rubber_slots_stamps_and_commit() {
assert!((card_stack_resist_y(30.0) - 30.0).abs() < 1e-4);
let damped = card_stack_resist_y(200.0);
assert!(damped > CARD_STACK_FREE_Y && damped < 200.0);
let (s0, y0, p0) = card_stack_slot_metrics(0, 0.0);
assert!((s0 - 1.0).abs() < 1e-4 && y0 == 0.0 && (p0 - 1.0).abs() < 1e-4);
let (s1, y1, _) = card_stack_slot_metrics(1, 0.0);
assert!((s1 - 0.95).abs() < 1e-4 && (y1 - CARD_STACK_PEEK_PX).abs() < 1e-4);
let (s1_lifted, y1_lifted, _) = card_stack_slot_metrics(1, 1.0);
assert!(s1_lifted > s1 && y1_lifted < y1);
let (keep, pass) = card_stack_stamp_opacities(96.0);
assert!((keep - 1.0).abs() < 1e-4 && pass == 0.0);
let (keep2, pass2) = card_stack_stamp_opacities(-56.0);
assert!(keep2 == 0.0 && (pass2 - 0.5).abs() < 1e-4);
assert_eq!(
card_stack_should_commit(130.0, 20.0, 320.0),
Some(CardDecision::Right)
);
assert_eq!(
card_stack_should_commit(-24.0, -650.0, 320.0),
Some(CardDecision::Left)
);
assert_eq!(card_stack_should_commit(40.0, 10.0, 320.0), None);
}
}