use std::{cell::Cell, rc::Rc, time::Instant};
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, Bounds, ElementId, FontWeight, Hsla, ImageSource, InteractiveElement,
IntoElement, KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
ObjectFit, ParentElement, Pixels, RenderOnce, ScrollWheelEvent, SharedString, Styled,
StyledImage, Window, canvas, div, img, px,
};
use super::{ChangeHandler, RoundedExt, all};
use crate::interaction::{focus_ring, focusable, use_focus};
use crate::motion::{MotionSettings, Spring, animate_f32, key, request_frame};
use crate::theme::{Theme, alpha, font_family};
const ITEM_RADIUS: f32 = 28.;
const GAP: f32 = 8.;
const SMALL: f32 = 56.;
const DEFAULT_ITEM_WIDTH: f32 = 186.;
const CLICK_SLOP: f32 = 5.;
const WHEEL_IDLE: f32 = 0.12;
const OVERSCROLL: f32 = 4.;
const RUBBER: f32 = 0.35;
const FLING: Spring = Spring {
stiffness: 140.,
damping_ratio: 1.,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CarouselLayout {
#[default]
MultiBrowse,
Uncontained,
Hero,
CenterHero,
FullScreen,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct CarouselSlot {
pub index: usize,
pub start: f32,
pub extent: f32,
}
fn keylines(layout: CarouselLayout, container: f32) -> Option<(Vec<f32>, usize)> {
let small = SMALL.min(container / 8.);
match layout {
CarouselLayout::MultiBrowse => {
let large = (container - 2. * GAP - small).max(0.) / 1.6;
Some((vec![large, large * 0.6, small], 0))
}
CarouselLayout::Hero => Some((vec![(container - GAP - small).max(0.), small], 0)),
CarouselLayout::CenterHero => Some((
vec![small, (container - 2. * GAP - 2. * small).max(0.), small],
1,
)),
CarouselLayout::Uncontained | CarouselLayout::FullScreen => None,
}
}
fn extent_at(widths: &[f32], lead: usize, distance: f32) -> f32 {
let q = distance + lead as f32;
let last = widths.len() - 1;
if q <= -1. {
0.
} else if q < 0. {
widths[0] * (q + 1.)
} else if q >= last as f32 {
widths[last]
} else {
let i = q.floor() as usize;
let f = q - i as f32;
widths[i] + (widths[i + 1] - widths[i]) * f
}
}
fn natural_extent(layout: CarouselLayout, container: f32, item: f32) -> f32 {
match keylines(layout, container) {
Some((widths, lead)) => widths[lead],
None => item,
}
}
pub(crate) fn carousel_slots(
layout: CarouselLayout,
container: f32,
item: f32,
count: usize,
offset: f32,
) -> Vec<CarouselSlot> {
let mut slots = Vec::new();
if count == 0 || container <= 0. {
return slots;
}
let Some((widths, lead)) = keylines(layout, container) else {
for index in 0..count {
let start = (index as f32 - offset) * (item + GAP);
if start + item > 0. && start < container {
slots.push(CarouselSlot {
index,
start,
extent: item,
});
}
}
return slots;
};
let mut i = offset.floor() as isize - lead as isize - 1;
let mut x = 0.;
while i < count as isize && x < container {
let extent = extent_at(&widths, lead, i as f32 - offset);
if i >= 0 && extent > 0.5 {
slots.push(CarouselSlot {
index: i as usize,
start: x,
extent,
});
}
x += extent + GAP * (extent / GAP).min(1.);
i += 1;
}
slots
}
fn step_px(layout: CarouselLayout, container: f32, item: f32) -> f32 {
(natural_extent(layout, container, item) + GAP).max(1.)
}
fn max_offset(layout: CarouselLayout, container: f32, item: f32, count: usize) -> f32 {
let last = count.saturating_sub(1) as f32;
match layout {
CarouselLayout::MultiBrowse => count.saturating_sub(3) as f32,
CarouselLayout::Hero | CarouselLayout::CenterHero | CarouselLayout::FullScreen => last,
CarouselLayout::Uncontained => {
let step = item + GAP;
((count as f32 * step - GAP - container) / step).max(0.)
}
}
}
fn snap_target(offset: f32, velocity: f32, max: f32) -> f32 {
let thrown = (velocity * 0.12).clamp(-1.5, 1.5);
(offset + thrown).round().clamp(0., max.max(0.))
}
fn wheel_target(from: f32, offset: f32, max: f32) -> f32 {
let net = offset - from;
let target = if net > 1e-3 {
(offset - 1e-3).ceil()
} else if net < -1e-3 {
(offset + 1e-3).floor()
} else {
offset.round()
};
target.clamp(0., max.max(0.))
}
fn step_target(from: f32, direction: f32, max: f32) -> f32 {
let next = if direction > 0. {
(from + 1e-3).floor() + 1.
} else {
(from - 1e-3).ceil() - 1.
};
next.clamp(0., max.max(0.))
}
fn rubber_band(raw: f32, max: f32) -> f32 {
let max = max.max(0.);
if raw < 0. {
raw * RUBBER
} else if raw > max {
max + (raw - max) * RUBBER
} else {
raw
}
}
#[derive(Clone, Copy)]
struct Settle {
spring: Spring,
target: f32,
from: f32,
velocity: f32,
start: Instant,
}
struct Drag {
origin: (f32, f32),
moved: f32,
samples: Vec<(Instant, f32)>,
}
#[derive(Default)]
struct Scroll {
pos: f32,
settling: Option<Settle>,
drag: Option<Drag>,
last_wheel: Option<Instant>,
wheel_from: f32,
slots: Vec<CarouselSlot>,
step: f32,
max: f32,
snap: bool,
used: f32,
requested: u64,
bounds: Rc<Cell<Bounds<Pixels>>>,
}
impl Scroll {
fn settle_to(&mut self, target: f32, velocity: f32, spring: Spring, now: Instant) {
if (target - self.pos).abs() < 1e-3 && velocity.abs() < 0.01 {
self.pos = target;
self.settling = None;
return;
}
self.settling = Some(Settle {
spring,
target,
from: self.pos - target,
velocity,
start: now,
});
}
fn heading(&self) -> f32 {
self.settling.map_or(self.pos, |s| s.target)
}
fn busy(&self) -> bool {
self.settling.is_some() || self.last_wheel.is_some()
}
fn advance(&mut self, now: Instant, motion: MotionSettings) {
let scale = motion.time_scale.max(0.01);
if self.drag.is_some() {
return;
}
if let Some(wheel) = self.last_wheel {
if now.duration_since(wheel).as_secs_f32() / scale >= WHEEL_IDLE {
self.last_wheel = None;
if self.snap {
let target = wheel_target(self.wheel_from, self.pos, self.max);
self.settle_to(target, 0., Spring::SPATIAL, now);
}
}
return;
}
if let Some(settle) = self.settling {
let t = now.duration_since(settle.start).as_secs_f32() / scale;
let (x, v) = settle.spring.solve(settle.from, settle.velocity, t);
let resting = t >= settle.spring.settle_time().as_secs_f32()
|| (x.abs() < 5e-4 && v.abs() < 0.01);
if resting || motion.reduced {
self.pos = settle.target;
self.settling = None;
} else {
self.pos = settle.target + x;
}
return;
}
let clamped = self.pos.clamp(0., self.max.max(0.));
if (clamped - self.pos).abs() > 1e-3 {
self.settle_to(clamped, 0., Spring::SPATIAL, now);
}
}
fn begin_drag(&mut self, pointer: f32, now: Instant) {
self.settling = None;
self.last_wheel = None;
self.drag = Some(Drag {
origin: (pointer, self.pos),
moved: 0.,
samples: vec![(now, self.pos)],
});
}
fn drag_to(&mut self, pointer: f32, now: Instant) {
let Some(drag) = self.drag.as_mut() else {
return;
};
let travelled = drag.origin.0 - pointer;
drag.moved = drag.moved.max(travelled.abs());
let raw = (drag.origin.1 + travelled / self.step.max(1.))
.clamp(-OVERSCROLL, self.max.max(0.) + OVERSCROLL);
self.pos = rubber_band(raw, self.max);
drag.samples.push((now, self.pos));
if drag.samples.len() > 8 {
drag.samples.remove(0);
}
}
fn end_drag(&mut self, now: Instant) -> bool {
let Some(drag) = self.drag.take() else {
return false;
};
let click = drag.moved < CLICK_SLOP;
let velocity = drag
.samples
.iter()
.find(|(t, _)| now.duration_since(*t).as_secs_f32() < 0.1)
.filter(|(t, _)| *t < now)
.map_or(0., |(t, p)| {
(self.pos - p) / now.duration_since(*t).as_secs_f32()
});
let max = self.max.max(0.);
if self.snap {
let target = snap_target(self.pos, velocity, max);
self.settle_to(target, velocity, Spring::SPATIAL, now);
} else {
let target = (self.pos + velocity * 0.3).clamp(0., max);
self.settle_to(target, velocity, FLING, now);
}
click
}
fn item_at(&self, along: f32) -> Option<usize> {
self.slots
.iter()
.find(|s| along >= s.start && along < s.start + s.extent)
.map(|s| s.index)
}
}
enum Content {
Image(ImageSource),
Element(AnyElement),
}
pub struct CarouselItem {
content: Content,
caption: Option<SharedString>,
background: Option<Hsla>,
}
impl CarouselItem {
pub fn image(source: impl Into<ImageSource>) -> Self {
Self {
content: Content::Image(source.into()),
caption: None,
background: None,
}
}
pub fn element(content: impl IntoElement) -> Self {
Self {
content: Content::Element(content.into_any_element()),
caption: None,
background: None,
}
}
pub fn caption(mut self, caption: impl Into<SharedString>) -> Self {
self.caption = Some(caption.into());
self
}
pub fn background(mut self, color: Hsla) -> Self {
self.background = Some(color);
self
}
}
#[derive(IntoElement)]
pub struct Carousel {
id: ElementId,
layout: CarouselLayout,
vertical: bool,
width: Option<Pixels>,
height: Option<Pixels>,
item_width: Option<f32>,
snap: Option<bool>,
backdrop: Option<Hsla>,
selected: Option<usize>,
scroll_to: Option<(usize, u64)>,
on_select: Option<ChangeHandler<usize>>,
items: Vec<CarouselItem>,
}
impl Carousel {
pub fn new(id: impl Into<ElementId>, layout: CarouselLayout) -> Self {
Self {
id: id.into(),
layout,
vertical: false,
width: None,
height: None,
item_width: None,
snap: None,
backdrop: None,
selected: None,
scroll_to: None,
on_select: None,
items: Vec::new(),
}
}
pub fn multi_browse(id: impl Into<ElementId>) -> Self {
Self::new(id, CarouselLayout::MultiBrowse)
}
pub fn uncontained(id: impl Into<ElementId>) -> Self {
Self::new(id, CarouselLayout::Uncontained)
}
pub fn hero(id: impl Into<ElementId>) -> Self {
Self::new(id, CarouselLayout::Hero)
}
pub fn center_hero(id: impl Into<ElementId>) -> Self {
Self::new(id, CarouselLayout::CenterHero)
}
pub fn full_screen(id: impl Into<ElementId>) -> Self {
Self::new(id, CarouselLayout::FullScreen)
}
pub fn vertical(mut self) -> Self {
self.vertical = true;
self
}
pub fn width(mut self, width: Pixels) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: Pixels) -> Self {
self.height = Some(height);
self
}
pub fn item_width(mut self, width: Pixels) -> Self {
self.item_width = Some(f32::from(width));
self
}
pub fn snap(mut self, snap: bool) -> Self {
self.snap = Some(snap);
self
}
pub fn backdrop(mut self, color: Hsla) -> Self {
self.backdrop = Some(color);
self
}
pub fn selected(mut self, index: Option<usize>) -> Self {
self.selected = index;
self
}
pub fn scroll_to(mut self, index: usize, generation: u64) -> Self {
self.scroll_to = Some((index, generation));
self
}
pub fn on_select(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
pub fn item(mut self, item: CarouselItem) -> Self {
self.items.push(item);
self
}
pub fn items(mut self, items: impl IntoIterator<Item = CarouselItem>) -> Self {
self.items.extend(items);
self
}
}
fn item_key(id: &ElementId, index: usize) -> ElementId {
ElementId::NamedChild(Box::new(id.clone()), format!("item-{index}").into())
}
impl RenderOnce for Carousel {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let layout = self.layout;
let vertical = self.vertical;
let snap = self.snap.unwrap_or(layout != CarouselLayout::Uncontained);
let scroll = window.use_keyed_state(key(&self.id, "scroll"), cx, |_, _| Scroll::default());
let bounds_cell = scroll.read(cx).bounds.clone();
let tall_default = vertical || layout == CarouselLayout::FullScreen;
let height = self
.height
.map_or(if tall_default { 400. } else { 220. }, f32::from);
let cross_width = self.width.map_or(220., f32::from);
let fixed = if vertical {
Some(height)
} else {
self.width.map(f32::from)
};
let measured = {
let b = bounds_cell.get();
f32::from(if vertical {
b.size.height
} else {
b.size.width
})
};
let container = fixed.unwrap_or(measured);
let container = if container > 0. {
container
} else {
request_frame(window);
480.
};
let cross = if vertical { cross_width } else { height };
let item = match layout {
CarouselLayout::Uncontained => {
self.item_width.unwrap_or(DEFAULT_ITEM_WIDTH).min(container)
}
CarouselLayout::FullScreen => container,
_ => 0.,
};
let count = self.items.len();
let natural = natural_extent(layout, container, item);
let now = Instant::now();
let motion = MotionSettings::get(cx);
let (pos, busy) = scroll.update(cx, |sc, _| {
sc.snap = snap;
sc.step = step_px(layout, container, item);
sc.max = max_offset(layout, container, item, count);
sc.used = container;
if let Some((index, generation)) = self.scroll_to
&& generation != sc.requested
{
sc.requested = generation;
sc.last_wheel = None;
let target = (index as f32).clamp(0., sc.max.max(0.));
sc.settle_to(target, 0., Spring::SPATIAL, now);
}
sc.advance(now, motion);
(sc.pos, sc.busy())
});
if busy {
request_frame(window);
}
let slots = carousel_slots(layout, container, item, count, pos);
scroll.update(cx, |sc, _| sc.slots = slots.clone());
let on_select = self.on_select.clone();
let mut items: Vec<Option<CarouselItem>> = self.items.into_iter().map(Some).collect();
let mut strip = div().relative().size_full().overflow_hidden();
for slot in slots {
let Some(entry) = items.get_mut(slot.index).and_then(Option::take) else {
continue;
};
let radius = ITEM_RADIUS.min(slot.extent / 2.).min(cross / 2.);
let ring = animate_f32(
key(&item_key(&self.id, slot.index), "ring"),
Spring::EFFECTS,
if self.selected == Some(slot.index) {
1.
} else {
0.
},
window,
cx,
);
let mut el = div()
.absolute()
.overflow_hidden()
.corners(all(px(radius)))
.bg(entry.background.unwrap_or(s.surface_container_highest));
el = if vertical {
el.top(px(slot.start))
.left_0()
.h(px(slot.extent))
.w(px(cross))
} else {
el.left(px(slot.start))
.top_0()
.w(px(slot.extent))
.h(px(cross))
};
el = match entry.content {
Content::Image(source) => {
let backdrop = self.backdrop.unwrap_or(s.surface);
let cover = px(radius + 1.);
el.child(img(source).size_full().object_fit(ObjectFit::Cover))
.child(
div()
.absolute()
.top(-cover)
.left(-cover)
.right(-cover)
.bottom(-cover)
.border(cover)
.border_color(backdrop)
.rounded(cover + px(radius)),
)
}
Content::Element(content) => {
let inner = div()
.absolute()
.top_0()
.left_0()
.overflow_hidden()
.child(content);
el.child(if vertical {
inner.w(px(cross)).h(px(natural))
} else {
inner.h(px(cross)).w(px(natural))
})
}
};
if let Some(caption) = entry.caption {
let fade = ((slot.extent / natural.max(1.) - 0.6) / 0.4).clamp(0., 1.);
if fade > 0. {
el = el.child(
div()
.absolute()
.left(px(12.))
.bottom(px(12.))
.max_w(px((natural - 24.).max(0.)))
.px(px(12.))
.h(px(32.))
.flex()
.items_center()
.rounded(px(16.))
.bg(alpha(s.inverse_surface, 0.85 * fade))
.text_color(alpha(s.inverse_on_surface, fade))
.text_size(px(14.))
.font_weight(FontWeight(500.))
.whitespace_nowrap()
.child(caption),
);
}
}
if ring > 0.01 {
el = el.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.corners(all(px(radius)))
.border(px(3.))
.border_color(alpha(s.primary, ring.clamp(0., 1.))),
);
}
strip = strip.child(el);
}
let (focus_state, handle, ring_visible) = use_focus(&self.id, window, cx);
let listeners = {
let scroll = scroll.clone();
let handle = handle.clone();
canvas(
{
let scroll = scroll.clone();
move |bounds: Bounds<Pixels>, window, cx| {
bounds_cell.set(bounds);
let length = f32::from(if vertical {
bounds.size.height
} else {
bounds.size.width
});
if (length - scroll.read(cx).used).abs() > 0.5 {
window.request_animation_frame();
}
}
},
move |bounds, _, window, _| {
let visible = bounds.intersect(&window.content_mask().bounds);
let along =
move |p: gpui::Point<Pixels>| f32::from(if vertical { p.y } else { p.x });
let st = scroll.clone();
window.on_mouse_event(move |e: &MouseDownEvent, phase, window, cx| {
if !phase.bubble()
|| e.button != MouseButton::Left
|| !visible.contains(&e.position)
{
return;
}
window.focus(&handle);
st.update(cx, |sc, cx| {
sc.begin_drag(along(e.position), Instant::now());
cx.notify();
});
});
let st = scroll.clone();
window.on_mouse_event(move |e: &MouseMoveEvent, phase, _, cx| {
if !phase.bubble() || st.read(cx).drag.is_none() {
return;
}
st.update(cx, |sc, cx| {
if e.pressed_button == Some(MouseButton::Left) {
sc.drag_to(along(e.position), Instant::now());
} else {
sc.end_drag(Instant::now());
}
cx.notify();
});
});
let st = scroll.clone();
let on_select = on_select.clone();
window.on_mouse_event(move |e: &MouseUpEvent, phase, window, cx| {
if !phase.bubble() || st.read(cx).drag.is_none() {
return;
}
let origin = along(bounds.origin);
let pressed = st.update(cx, |sc, cx| {
cx.notify();
sc.end_drag(Instant::now())
.then(|| sc.item_at(along(e.position) - origin))
.flatten()
});
if let (Some(index), Some(f)) = (pressed, &on_select) {
f(index, window, cx);
}
});
},
)
.absolute()
.size_full()
};
let wheel = scroll.clone();
let keys = scroll.clone();
let ring = focus_ring(&self.id, ring_visible, all(px(ITEM_RADIUS)), window, cx);
let el = div()
.id(self.id.clone())
.relative()
.flex_none()
.font_family(font_family())
.h(px(if vertical { height } else { cross }))
.when_some(self.width, |d, w| d.w(w))
.when(self.width.is_none() && !vertical, |d| d.w_full())
.when(vertical && self.width.is_none(), |d| d.w(px(cross)))
.when(self.on_select.is_some(), |d| d.cursor_pointer())
.on_scroll_wheel(move |e: &ScrollWheelEvent, _, cx| {
let delta = e.delta.pixel_delta(px(40.));
let delta = f32::from(if vertical {
delta.y
} else if delta.x.abs() >= delta.y.abs() {
delta.x
} else {
delta.y
});
if delta == 0. {
return;
}
let moved = wheel.update(cx, |sc, cx| {
let next = (sc.pos - delta / sc.step.max(1.)).clamp(0., sc.max.max(0.));
let moved = (next - sc.pos).abs() > 1e-4;
if moved {
if sc.last_wheel.is_none() {
sc.wheel_from = sc.pos;
}
sc.settling = None;
sc.pos = next;
sc.last_wheel = Some(Instant::now());
cx.notify();
}
moved
});
if moved {
cx.stop_propagation();
}
})
.on_key_down(move |e: &KeyDownEvent, _, cx| {
let (back, forward) = if vertical {
("up", "down")
} else {
("left", "right")
};
let key = e.keystroke.key.as_str();
if ![back, forward, "home", "end"].contains(&key) {
return;
}
cx.stop_propagation();
keys.update(cx, |sc, cx| {
let max = sc.max.max(0.);
let target = match key {
"home" => 0.,
"end" => max,
_ if key == forward => step_target(sc.heading(), 1., max),
_ => step_target(sc.heading(), -1., max),
};
let now = Instant::now();
sc.last_wheel = None;
sc.settle_to(target, 0., Spring::SPATIAL, now);
cx.notify();
});
})
.child(strip.child(listeners))
.children(ring);
focusable(el, &focus_state, &handle)
}
}
#[cfg(test)]
mod tests {
use super::*;
const W: f32 = 600.;
fn slots(layout: CarouselLayout, offset: f32) -> Vec<CarouselSlot> {
carousel_slots(layout, W, DEFAULT_ITEM_WIDTH, 10, offset)
}
fn extents(layout: CarouselLayout, offset: f32) -> Vec<f32> {
slots(layout, offset).iter().map(|s| s.extent).collect()
}
#[test]
fn multi_browse_shows_a_large_a_medium_and_a_small_item() {
let at_rest = slots(CarouselLayout::MultiBrowse, 0.);
assert_eq!(at_rest.len(), 3);
let [large, medium, small] = [at_rest[0], at_rest[1], at_rest[2]];
assert!(large.extent > medium.extent && medium.extent > small.extent);
assert_eq!(small.extent, SMALL);
assert_eq!(large.start, 0.);
assert!((medium.start - (large.extent + GAP)).abs() < 1e-3);
assert!((small.start + small.extent - W).abs() < 1e-3);
}
#[test]
fn multi_browse_items_shrink_toward_the_start_as_they_scroll() {
let halfway = slots(CarouselLayout::MultiBrowse, 0.5);
let at_rest = slots(CarouselLayout::MultiBrowse, 0.);
assert!((halfway[0].extent - at_rest[0].extent / 2.).abs() < 1e-3);
assert_eq!(halfway[0].index, 0);
let grown = (at_rest[0].extent + at_rest[1].extent) / 2.;
assert!((halfway[1].extent - grown).abs() < 1e-3);
let moved = slots(CarouselLayout::MultiBrowse, 1.);
assert_eq!(moved[0].index, 1);
assert!((moved[0].extent - at_rest[0].extent).abs() < 1e-3);
}
#[test]
fn sizes_follow_the_offset_without_jumps() {
for layout in [
CarouselLayout::MultiBrowse,
CarouselLayout::Hero,
CarouselLayout::CenterHero,
] {
let mut previous = slots(layout, 0.);
let mut offset = 0.;
while offset < 4. {
offset += 0.01;
let next = slots(layout, offset);
for slot in &next {
if let Some(before) = previous.iter().find(|p| p.index == slot.index) {
assert!(
(slot.extent - before.extent).abs() < 6.,
"{layout:?} at {offset}"
);
assert!(
(slot.start - before.start).abs() < 12.,
"{layout:?} at {offset}"
);
}
}
previous = next;
}
}
}
#[test]
fn hero_has_one_large_item_and_a_peeking_one() {
let at_rest = slots(CarouselLayout::Hero, 0.);
assert_eq!(at_rest.len(), 2);
assert_eq!(at_rest[1].extent, SMALL);
assert!((at_rest[0].extent - (W - GAP - SMALL)).abs() < 1e-3);
assert!((at_rest[1].start + at_rest[1].extent - W).abs() < 1e-3);
}
#[test]
fn center_hero_centers_the_large_item_with_a_small_one_each_side() {
let middle = slots(CarouselLayout::CenterHero, 3.);
assert_eq!(
middle.iter().map(|s| s.index).collect::<Vec<_>>(),
[2, 3, 4]
);
let large = middle[1];
assert!((large.start + large.extent / 2. - W / 2.).abs() < 1e-3);
assert_eq!(middle[0].extent, SMALL);
assert_eq!(middle[2].extent, SMALL);
let first = slots(CarouselLayout::CenterHero, 0.);
assert_eq!(first[0].index, 0);
assert!((first[0].start + first[0].extent / 2. - W / 2.).abs() < 1e-3);
}
#[test]
fn uncontained_items_are_equal_and_scroll_freely() {
let at_rest = slots(CarouselLayout::Uncontained, 0.);
assert!(at_rest.iter().all(|s| s.extent == DEFAULT_ITEM_WIDTH));
assert_eq!(at_rest[1].start, DEFAULT_ITEM_WIDTH + GAP);
let half = slots(CarouselLayout::Uncontained, 0.5);
assert!((half[0].start + (DEFAULT_ITEM_WIDTH + GAP) / 2.).abs() < 1e-3);
let almost = slots(CarouselLayout::Uncontained, 0.95);
assert!(almost.last().unwrap().index > at_rest.last().unwrap().index);
let far = slots(CarouselLayout::Uncontained, 3.);
assert_eq!(far[0].index, 3);
}
#[test]
fn expressive_sizes_stay_inside_the_strip_and_never_negative() {
for layout in [
CarouselLayout::MultiBrowse,
CarouselLayout::Hero,
CarouselLayout::CenterHero,
CarouselLayout::Uncontained,
] {
for step in 0..60 {
let offset = step as f32 * 0.15 - 1.;
for slot in slots(layout, offset) {
assert!(
slot.extent > 0. && slot.extent <= W,
"{layout:?} at {offset}"
);
assert!(slot.start < W && slot.index < 10);
}
}
}
}
#[test]
fn there_is_nothing_to_lay_out_without_items_or_room() {
assert!(carousel_slots(CarouselLayout::MultiBrowse, W, 0., 0, 0.).is_empty());
assert!(carousel_slots(CarouselLayout::MultiBrowse, 0., 0., 5, 0.).is_empty());
assert_eq!(extents(CarouselLayout::MultiBrowse, 0.).len(), 3);
let two = carousel_slots(CarouselLayout::MultiBrowse, W, 0., 2, 0.);
assert_eq!(two.len(), 2);
}
#[test]
fn ranges_end_on_the_last_items() {
assert_eq!(max_offset(CarouselLayout::MultiBrowse, W, 0., 10), 7.);
assert_eq!(max_offset(CarouselLayout::MultiBrowse, W, 0., 2), 0.);
assert_eq!(max_offset(CarouselLayout::Hero, W, 0., 10), 9.);
assert_eq!(max_offset(CarouselLayout::CenterHero, W, 0., 1), 0.);
let max = max_offset(CarouselLayout::Uncontained, W, 186., 10);
let slots = carousel_slots(CarouselLayout::Uncontained, W, 186., 10, max);
let last = slots.last().unwrap();
assert_eq!(last.index, 9);
assert!((last.start + last.extent - W).abs() < 1e-2);
assert_eq!(max_offset(CarouselLayout::Uncontained, W, 186., 2), 0.);
}
#[test]
fn a_release_settles_on_the_nearest_item_thrown_by_the_speed() {
assert_eq!(snap_target(1.4, 0., 7.), 1.);
assert_eq!(snap_target(1.6, 0., 7.), 2.);
assert_eq!(snap_target(1.2, 6., 7.), 2.);
assert_eq!(snap_target(1.0, 100., 7.), 3.);
assert_eq!(snap_target(1.0, -100., 7.), 0.);
assert_eq!(snap_target(6.9, 100., 7.), 7.);
assert_eq!(snap_target(-0.4, 0., 7.), 0.);
assert_eq!(snap_target(0.5, 0., 0.), 0.);
}
#[test]
fn arrow_keys_move_one_item_and_stop_at_the_ends() {
assert_eq!(step_target(2., 1., 7.), 3.);
assert_eq!(step_target(2., -1., 7.), 1.);
assert_eq!(step_target(2.4, 1., 7.), 3.);
assert_eq!(step_target(2.4, -1., 7.), 2.);
assert_eq!(step_target(7., 1., 7.), 7.);
assert_eq!(step_target(0., -1., 7.), 0.);
assert_eq!(step_target(3., 1., 3.4), 3.4);
assert_eq!(step_target(3.4, -1., 3.4), 3.);
}
#[test]
fn pulling_past_the_end_follows_at_a_fraction_of_the_distance() {
assert_eq!(rubber_band(2., 7.), 2.);
assert!((rubber_band(-2., 7.) + 2. * RUBBER).abs() < 1e-6);
assert!((rubber_band(9., 7.) - (7. + 2. * RUBBER)).abs() < 1e-6);
}
#[test]
fn a_drag_moves_the_offset_and_settles_on_an_item() {
let t0 = Instant::now();
let mut scroll = Scroll {
step: 100.,
max: 7.,
snap: true,
..Scroll::default()
};
scroll.begin_drag(300., t0);
scroll.drag_to(170., t0);
assert!((scroll.pos - 1.3).abs() < 1e-5);
assert!(!scroll.end_drag(t0));
assert_eq!(scroll.settling.map(|s| s.target), Some(1.));
let mut still = Scroll {
step: 100.,
max: 7.,
snap: true,
..Scroll::default()
};
still.begin_drag(300., t0);
still.drag_to(298., t0);
assert!(still.end_drag(t0));
assert_eq!(still.settling.map(|s| s.target), Some(0.));
}
#[test]
fn a_settle_ends_exactly_on_its_target() {
let t0 = Instant::now();
let mut scroll = Scroll {
max: 7.,
snap: true,
..Scroll::default()
};
scroll.pos = 1.3;
scroll.settle_to(1., 0., Spring::SPATIAL, t0);
scroll.advance(
t0 + std::time::Duration::from_secs(5),
MotionSettings::default(),
);
assert_eq!(scroll.pos, 1.);
assert!(!scroll.busy());
}
#[test]
fn the_wheel_snaps_once_it_rests() {
let t0 = Instant::now();
let mut scroll = Scroll {
max: 7.,
snap: true,
pos: 2.4,
wheel_from: 2.,
last_wheel: Some(t0),
..Scroll::default()
};
let motion = MotionSettings::default();
scroll.advance(t0 + std::time::Duration::from_millis(50), motion);
assert!(scroll.settling.is_none() && scroll.last_wheel.is_some());
scroll.advance(t0 + std::time::Duration::from_millis(200), motion);
assert_eq!(scroll.settling.map(|s| s.target), Some(3.));
}
#[test]
fn one_wheel_notch_moves_one_item_in_its_direction() {
assert_eq!(wheel_target(0., 0.24, 7.), 1.);
assert_eq!(wheel_target(1., 0.76, 7.), 0.);
assert_eq!(wheel_target(2., 2.0004, 7.), 2.);
assert_eq!(wheel_target(1., 2.3, 7.), 3.);
assert_eq!(wheel_target(6.8, 7., 7.), 7.);
assert_eq!(wheel_target(0.2, 0., 7.), 0.);
}
}