use std::collections::HashMap;
pub trait ContainerState {
fn scroll_offset(&self, container_id: &str) -> f64;
fn is_scrollbar_dragging(&self, container_id: &str) -> bool;
fn is_scrollbar_hovered(&self, container_id: &str) -> bool;
fn set_scroll_offset(&mut self, container_id: &str, offset: f64);
fn set_scrollbar_dragging(&mut self, container_id: &str, dragging: bool);
fn set_scrollbar_hovered(&mut self, container_id: &str, hovered: bool);
}
#[derive(Clone, Debug, Default)]
pub struct SimpleContainerState {
pub scroll_offsets: HashMap<String, f64>,
pub dragging: Option<String>,
pub hovered: Option<String>,
}
impl SimpleContainerState {
pub fn new() -> Self {
Self {
scroll_offsets: HashMap::new(),
dragging: None,
hovered: None,
}
}
}
impl ContainerState for SimpleContainerState {
fn scroll_offset(&self, container_id: &str) -> f64 {
*self.scroll_offsets.get(container_id).unwrap_or(&0.0)
}
fn is_scrollbar_dragging(&self, container_id: &str) -> bool {
self.dragging.as_deref() == Some(container_id)
}
fn is_scrollbar_hovered(&self, container_id: &str) -> bool {
self.hovered.as_deref() == Some(container_id)
}
fn set_scroll_offset(&mut self, container_id: &str, offset: f64) {
self.scroll_offsets.insert(container_id.to_string(), offset);
}
fn set_scrollbar_dragging(&mut self, container_id: &str, dragging: bool) {
if dragging {
self.dragging = Some(container_id.to_string());
} else if self.dragging.as_deref() == Some(container_id) {
self.dragging = None;
}
}
fn set_scrollbar_hovered(&mut self, container_id: &str, hovered: bool) {
if hovered {
self.hovered = Some(container_id.to_string());
} else if self.hovered.as_deref() == Some(container_id) {
self.hovered = None;
}
}
}