#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ScrollId(u64);
#[must_use = "a scroll stops only when its stop runs"]
pub struct ScrollStop(Box<dyn FnOnce()>);
impl ScrollStop {
pub fn run(self) {
(self.0)();
}
}
#[derive(Default)]
pub struct RunningScroll {
running: Option<(ScrollId, ScrollStop)>,
next: u64,
}
impl RunningScroll {
pub fn start(&mut self, stop: impl FnOnce() + 'static) -> (ScrollId, Option<ScrollStop>) {
let id = ScrollId(self.next);
self.next += 1;
let previous = self.running.replace((id, ScrollStop(Box::new(stop))));
(id, previous.map(|(_, stop)| stop))
}
pub fn end(&mut self, id: ScrollId) {
if self
.running
.as_ref()
.is_some_and(|(running, _)| *running == id)
{
self.running = None;
}
}
pub fn take(&mut self) -> Option<ScrollStop> {
self.running.take().map(|(_, stop)| stop)
}
pub fn is_running(&self) -> bool {
self.running.is_some()
}
}