use crate::{
core::{Pos, Rect, Size},
ops::{ExactSizeGrid, GridBase, GridRead},
};
pub struct Viewed<G> {
pub(super) source: G,
pub(super) bounds: Rect,
}
impl<G> GridBase for Viewed<G>
where
G: GridBase,
{
fn size_hint(&self) -> (Size, Option<Size>) {
let size = Size::new(self.bounds.width(), self.bounds.height());
(size, Some(size))
}
}
impl<G> ExactSizeGrid for Viewed<G>
where
G: ExactSizeGrid,
{
fn width(&self) -> usize {
self.bounds.width()
}
fn height(&self) -> usize {
self.bounds.height()
}
}
impl<G> GridRead for Viewed<G>
where
G: GridRead,
{
type Element<'b>
= G::Element<'b>
where
Self: 'b;
type Layout = G::Layout;
fn get(&self, pos: Pos) -> Option<Self::Element<'_>> {
let pos = pos - self.bounds.top_left();
if !self.bounds.contains_pos(pos) {
return None;
}
self.source.get(pos)
}
fn iter_rect(&self, bounds: Rect) -> impl Iterator<Item = Self::Element<'_>> {
let bounds = bounds - self.bounds.top_left();
self.source.iter_rect(bounds)
}
}