use std::{
slice::{Iter, IterMut},
vec::IntoIter,
};
use cursive_core::{Rect, XY};
pub struct PlacedElement<T> {
pub element: T,
pub position: Rect,
}
pub struct Layout<T> {
pub elements: Vec<PlacedElement<T>>,
}
impl<T> Layout<T> {
pub fn element_at(&self, position: XY<usize>) -> Option<&PlacedElement<T>> {
self.iter()
.find(|&element| element.position.contains(position))
}
pub fn iter(&self) -> Iter<'_, PlacedElement<T>> {
self.into_iter()
}
pub fn size(&self) -> XY<usize> {
let mut max_size = XY::from((0, 0));
self.iter().for_each(|item| {
if item.position.right() > max_size.x {
max_size.x = item.position.right() + 1;
}
if item.position.bottom() > max_size.y {
max_size.y = item.position.bottom() + 1;
}
});
max_size
}
}
impl<T> IntoIterator for Layout<T> {
type Item = PlacedElement<T>;
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.elements.into_iter()
}
}
impl<'a, T> IntoIterator for &'a Layout<T> {
type Item = &'a PlacedElement<T>;
type IntoIter = Iter<'a, PlacedElement<T>>;
fn into_iter(self) -> Self::IntoIter {
self.elements.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Layout<T> {
type Item = &'a mut PlacedElement<T>;
type IntoIter = IterMut<'a, PlacedElement<T>>;
fn into_iter(self) -> Self::IntoIter {
self.elements.iter_mut()
}
}