Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/layout/linear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub use orientation::{Horizontal, Orientation, Vertical};
pub use secondary_alignment::SecondaryAlignment;
pub use spacing::{ElementSpacing, FixedMargin};

use crate::utils::cached::Cached;
use layout_element::LayoutElement;
use spacing::Tight;

Expand Down Expand Up @@ -221,10 +222,10 @@ where
/// Views will be laid out sequentially, keeping the order in which they were added to the
/// layout.
#[inline]
pub fn add_view<V: View>(self, view: V) -> LinearLayout<LD, Link<V, LE>> {
pub fn add_view<V: View>(self, view: V) -> LinearLayout<LD, Link<Cached<V>, LE>> {
LinearLayout {
direction: self.direction,
views: self.views.add_view(view),
views: self.views.add_view(Cached::new(view)),
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/utils/cached.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Bounding box cache wrapper
use crate::prelude::*;
use embedded_graphics::{primitives::Rectangle, DrawTarget};

/// Cache the bounding box of the wrapped [`View`]
pub struct Cached<V: View> {
bounds: Rectangle,
view: V,
}

impl<V: View> Cached<V> {
/// Calculate a View's bounding box and store it for reuse
#[inline]
pub fn new(v: V) -> Self {
let bounds = v.bounds();

Self { view: v, bounds }
}

/// Unwrap the inner View
#[inline]
pub fn into_inner(self) -> V {
self.view
}
}

impl<V: View> View for Cached<V> {
#[inline]
fn translate(&mut self, by: Point) {
self.bounds.translate(by);
self.view.translate(by);
}

#[inline]
fn bounds(&self) -> Rectangle {
self.bounds
}
}

impl<'a, V, C> Drawable<C> for &'a Cached<V>
where
C: PixelColor,
V: View,
&'a V: Drawable<C>,
{
#[inline]
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
self.view.draw(display)
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Utility collection module

pub mod cached;
pub mod display_area;
pub mod object_chain;
pub mod rect_helper;