use crate::prelude::*;
use embedded_graphics::{prelude::Point, primitives::Rectangle};
pub mod horizontal;
pub mod vertical;
pub trait Align {
fn align_to<H, V>(self, reference: &impl View, horizontal: H, vertical: V) -> Self
where
H: HorizontalAlignment,
V: VerticalAlignment;
fn align_to_mut<H, V>(
&mut self,
reference: &impl View,
horizontal: H,
vertical: V,
) -> &mut Self
where
H: HorizontalAlignment,
V: VerticalAlignment;
}
impl<T> Align for T
where
T: View,
{
#[inline]
fn align_to<H, V>(mut self, reference: &impl View, horizontal: H, vertical: V) -> Self
where
H: HorizontalAlignment,
V: VerticalAlignment,
{
self.align_to_mut(reference, horizontal, vertical);
self
}
#[inline]
fn align_to_mut<H, V>(&mut self, reference: &impl View, horizontal: H, vertical: V) -> &mut Self
where
H: HorizontalAlignment,
V: VerticalAlignment,
{
let self_bounds = self.bounds();
let reference_bounds = reference.bounds();
let h = horizontal.align(self_bounds, reference_bounds);
let v = vertical.align(self_bounds, reference_bounds);
self.translate_mut(Point::new(h, v))
}
}
pub trait Alignment: Copy + Clone + Default {
#[inline]
fn align(&self, what: Rectangle, reference: Rectangle) -> i32 {
self.align_with_offset(what, reference, 0)
}
fn align_with_offset(&self, what: Rectangle, reference: Rectangle, offset: i32) -> i32;
}
pub trait HorizontalAlignment: Alignment {}
pub trait VerticalAlignment: Alignment {}