use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Axis {
Vertical,
Horizontal,
}
#[derive(Debug, Clone, Default)]
pub struct BodyScroll {
v: HashMap<String, usize>,
h: HashMap<String, usize>,
}
impl BodyScroll {
pub fn get(&self, path: &str, axis: Axis) -> usize {
match axis {
Axis::Vertical => self.v.get(path).copied().unwrap_or(0),
Axis::Horizontal => self.h.get(path).copied().unwrap_or(0),
}
}
pub fn scroll_by(&mut self, path: &str, axis: Axis, delta: i32, max: usize) {
let map = self.map_mut(axis);
let cur = map.get(path).copied().unwrap_or(0) as i64;
let next = (cur + delta as i64).max(0).min(max as i64) as usize;
map.insert(path.to_string(), next);
}
pub fn set(&mut self, path: &str, axis: Axis, value: usize) {
self.map_mut(axis).insert(path.to_string(), value);
}
pub fn remove(&mut self, path: &str, axis: Axis) {
self.map_mut(axis).remove(path);
}
pub fn clear(&mut self) {
self.v.clear();
self.h.clear();
}
fn map_mut(&mut self, axis: Axis) -> &mut HashMap<String, usize> {
match axis {
Axis::Vertical => &mut self.v,
Axis::Horizontal => &mut self.h,
}
}
}