nightshade_api/web/
util.rs1use leptos::prelude::*;
2
3pub fn visible_range(
7 scroll_top: f64,
8 viewport_height: f64,
9 item_height: f64,
10 overscan: usize,
11 total: usize,
12) -> (usize, usize) {
13 let view = viewport_height.max(item_height);
14 let first = ((scroll_top / item_height).floor() as usize).saturating_sub(overscan);
15 let count = (view / item_height).ceil() as usize + overscan * 2 + 1;
16 let start = first.min(total);
17 let end = (start + count).min(total);
18 (start, end)
19}
20
21#[derive(Clone, Copy)]
27pub struct RetainedClosures(StoredValue<Vec<Box<dyn std::any::Any>>, LocalStorage>);
28
29impl RetainedClosures {
30 pub fn retain<T: 'static>(&self, value: T) {
31 self.0.update_value(|held| held.push(Box::new(value)));
32 }
33}
34
35pub fn use_retained_closures() -> RetainedClosures {
37 RetainedClosures(StoredValue::new_local(Vec::new()))
38}