use embedded_graphics::{prelude::Point, primitives::Rectangle};
use crate::{prelude::RectExt, View};
mod object_chain;
mod views;
pub use views::Views;
pub trait ViewGroup: View {
fn len(&self) -> usize;
fn at(&self, idx: usize) -> &dyn View;
fn at_mut(&mut self, idx: usize) -> &mut dyn View;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct EmptyViewGroup;
pub static mut EMPTY_VIEW_GROUP: EmptyViewGroup = EmptyViewGroup;
impl View for EmptyViewGroup {
fn translate_impl(&mut self, _by: Point) {}
fn bounds(&self) -> Rectangle {
Rectangle::zero()
}
}
impl ViewGroup for EmptyViewGroup {
fn len(&self) -> usize {
0
}
fn at(&self, _idx: usize) -> &dyn View {
self
}
fn at_mut(&mut self, _idx: usize) -> &mut dyn View {
self
}
}
pub struct ViewGroupHelper;
impl ViewGroupHelper {
#[inline]
pub fn translate(vg: &mut impl ViewGroup, by: Point) {
for i in 0..ViewGroup::len(vg) {
vg.at_mut(i).translate_impl(by);
}
}
#[inline]
pub fn bounds(vg: &impl ViewGroup) -> Rectangle {
if ViewGroup::len(vg) == 0 {
return EmptyViewGroup.bounds();
}
let mut rect = vg.at(0).bounds();
for i in 1..vg.len() {
rect = rect.enveloping(&vg.at(i).bounds());
}
rect
}
}