use std::{
any::{Any, TypeId},
cell::RefMut,
collections::HashMap,
error::Error,
};
use ratatui::prelude::Rect;
use crate::*;
use self::widget::WidgetError;
#[derive(Default, State)]
pub struct Chunks {
chunks: HashMap<TypeId, Rect>,
}
impl Chunks {
pub fn clear(&mut self) {
self.chunks = HashMap::new();
}
pub fn register_chunk<T: Any>(&mut self, rect: Rect) {
self.chunks.insert(TypeId::of::<T>(), rect);
}
pub fn get_chunk<T: Any>(&self) -> Result<Rect, WidgetError> {
match self.chunks.get(&TypeId::of::<T>()).cloned() {
Some(chunk) => Ok(chunk),
None => Err(WidgetError::ChunkError),
}
}
}