#![cfg_attr(not(test), no_std)]
use embedded_graphics::{geometry::Point, prelude::*, primitives::Rectangle, DrawTarget};
pub mod horizontal;
pub mod vertical;
pub mod prelude {
pub use crate::{horizontal, vertical, Align, DisplayArea};
}
pub trait DisplayArea<C>: DrawTarget<C>
where
C: PixelColor,
{
fn display_area(&self) -> Rectangle;
}
impl<C, T> DisplayArea<C> for T
where
C: PixelColor,
T: DrawTarget<C>,
{
fn display_area(&self) -> Rectangle {
Rectangle::new(
Point::new(0, 0),
Point::new(
(self.size().width - 1) as i32,
(self.size().height - 1) as i32,
),
)
}
}
pub trait HorizontalAlignment {
fn align(&self, what: &impl Dimensions, reference: &impl Dimensions) -> i32;
}
pub trait VerticalAlignment {
fn align(&self, what: &impl Dimensions, reference: &impl Dimensions) -> i32;
}
pub trait Align: Transform {
fn align_to<D, H, V>(self, reference: D, horizontal: H, vertical: V) -> Self
where
D: Dimensions,
H: HorizontalAlignment,
V: VerticalAlignment;
}
impl<T> Align for T
where
T: Dimensions + Transform,
{
fn align_to<D, H, V>(self, reference: D, horizontal: H, vertical: V) -> Self
where
D: Dimensions,
H: HorizontalAlignment,
V: VerticalAlignment,
{
let h = horizontal.align(&self, &reference);
let v = vertical.align(&self, &reference);
self.translate(Point::new(h, v))
}
}