use super::rect::Rect;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct RegionId(pub u16);
impl RegionId {
pub const fn new(id: u16) -> Self {
Self(id)
}
}
#[derive(Clone, Debug)]
pub struct Region {
pub id: RegionId,
pub rect: Rect,
pub z_index: u8,
pub dirty_generation: u64,
}
impl Region {
pub const fn new(id: RegionId, rect: Rect) -> Self {
Self {
id,
rect,
z_index: 0,
dirty_generation: 0,
}
}
#[must_use]
pub const fn with_z_index(mut self, z: u8) -> Self {
self.z_index = z;
self
}
pub const fn mark_dirty(&mut self) {
self.dirty_generation += 1;
}
}
#[derive(Clone, Debug)]
pub struct Layout {
pub regions: Vec<Region>,
pub terminal_size: (u16, u16),
generation: u64,
}
impl Layout {
pub const fn new(width: u16, height: u16) -> Self {
Self {
regions: Vec::new(),
terminal_size: (width, height),
generation: 0,
}
}
pub fn add_region(&mut self, region: Region) {
self.regions.push(region);
}
pub fn get(&self, id: RegionId) -> Option<&Region> {
self.regions.iter().find(|r| r.id == id)
}
pub fn get_mut(&mut self, id: RegionId) -> Option<&mut Region> {
self.regions.iter_mut().find(|r| r.id == id)
}
pub fn dirty_regions(&self) -> impl Iterator<Item = &Region> {
self.regions.iter().filter(|r| r.dirty_generation > 0)
}
pub fn clear_dirty(&mut self) {
for region in &mut self.regions {
region.dirty_generation = 0;
}
}
pub const fn resize(&mut self, width: u16, height: u16) {
self.terminal_size = (width, height);
self.generation += 1;
}
}