use super::{api::CarouselPage, motion};
use crate::time::Instant;
use crate::{Id, Rect};
#[derive(Clone, Copy, Debug)]
pub struct Drag {
pub raw: f32,
pub start: i64,
}
#[doc(hidden)]
#[derive(Clone, Debug, Default)]
pub struct CarouselState {
pub last_frame: u64,
pub seen: bool,
pub count: usize,
pub target: i64,
pub from: i64,
pub page: usize,
pub key: Option<Id>,
pub position: f32,
pub drag: Option<Drag>,
pub settled: bool,
pub velocity: motion::Velocity,
pub wheel: f32,
pub wheel_until: Option<Instant>,
pub autoplay_at: Option<Instant>,
pub layers: Vec<(i64, Rect)>,
}
pub(super) struct Model<'a> {
pub(super) count: usize,
pub(super) keys: Option<&'a [Id]>,
}
impl Model<'_> {
pub(super) fn key(&self, index: usize) -> Id {
self.keys
.and_then(|keys| keys.get(index).copied())
.unwrap_or_else(|| Id::new(index))
}
fn index_of(&self, key: Id) -> Option<usize> {
(0..self.count).find(|&i| self.key(i) == key)
}
}
pub(super) struct Synced {
pub(super) external: bool,
pub(super) shift: f32,
}
impl CarouselState {
pub(super) fn page_of(&self, target: i64, wrap: bool) -> usize {
let n = self.count.max(1) as i64;
if wrap {
target.rem_euclid(n) as usize
} else {
target.clamp(0, n - 1) as usize
}
}
pub(super) fn sync(
&mut self,
model: &Model<'_>,
page: &CarouselPage<'_>,
wrap: bool,
initial: usize,
) -> Synced {
let n = model.count;
let last = n - 1;
let requested = match page {
CarouselPage::Index(i) => Some((**i).min(last)),
CarouselPage::Key(key) => model.index_of(**key),
CarouselPage::Internal => None,
};
if !self.seen {
let first = requested.unwrap_or(initial.min(last));
*self = Self {
seen: true,
count: n,
target: first as i64,
from: first as i64,
page: first,
key: Some(model.key(first)),
position: first as f32,
last_frame: self.last_frame,
..Self::default()
};
return Synced {
external: false,
shift: 0.0,
};
}
let follows_key = !matches!(page, CarouselPage::Index(_)) && model.keys.is_some();
let here = if follows_key {
self.key
.and_then(|key| model.index_of(key))
.unwrap_or(self.page.min(last))
} else {
self.page.min(last)
};
let mut shift = 0.0;
if n != self.count || here != self.page {
let rebased = here as i64;
shift = (rebased - self.target) as f32;
self.target = rebased;
self.from = rebased;
self.count = n;
}
let mut external = false;
let moved_key = matches!(page, CarouselPage::Key(key) if Some(**key) != self.key);
let moved_index = matches!(page, CarouselPage::Index(_));
if let Some(wanted) = requested.filter(|&w| w != here && (moved_key || moved_index)) {
self.target += motion::distance(here, wanted, n, wrap);
external = true;
self.drag = None;
}
self.page = self.page_of(self.target, wrap);
self.key = Some(model.key(self.page));
Synced { external, shift }
}
pub(super) fn go(&mut self, target: i64, model: &Model<'_>, wrap: bool) -> bool {
let target = if wrap {
target
} else {
target.clamp(0, model.count as i64 - 1)
};
if target == self.target {
return false;
}
self.from = self.position.round() as i64;
self.target = target;
let page = self.page_of(target, wrap);
let changed = page != self.page;
self.page = page;
self.key = Some(model.key(page));
changed
}
}