use embedded_graphics::primitives::Rectangle;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ViewRedraw {
None,
Dirty(Rectangle),
Full,
}
impl ViewRedraw {
pub fn merge(self, other: Self) -> Self {
match (self, other) {
(Self::Full, _) | (_, Self::Full) => Self::Full,
(Self::Dirty(left), Self::Dirty(right)) => Self::Dirty(union_rect(left, right)),
(Self::Dirty(area), Self::None) | (Self::None, Self::Dirty(area)) => Self::Dirty(area),
_ => Self::None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ViewEvent<Message> {
pub redraw: ViewRedraw,
pub captured: bool,
pub message: Option<Message>,
}
impl<Message> ViewEvent<Message> {
pub const fn none() -> Self {
Self {
redraw: ViewRedraw::None,
captured: false,
message: None,
}
}
pub const fn redraw(redraw: ViewRedraw) -> Self {
Self {
redraw,
captured: false,
message: None,
}
}
pub const fn captured(mut self, captured: bool) -> Self {
self.captured = captured;
self
}
pub fn message(mut self, message: Message) -> Self {
self.message = Some(message);
self
}
}
fn union_rect(left: Rectangle, right: Rectangle) -> Rectangle {
let left_x = left.top_left.x.min(right.top_left.x);
let top_y = left.top_left.y.min(right.top_left.y);
let left_bottom = left.bottom_right().unwrap_or(left.top_left);
let right_bottom = right.bottom_right().unwrap_or(right.top_left);
let right_x = left_bottom.x.max(right_bottom.x);
let bottom_y = left_bottom.y.max(right_bottom.y);
Rectangle::with_corners(
embedded_graphics::prelude::Point::new(left_x, top_y),
embedded_graphics::prelude::Point::new(right_x, bottom_y),
)
}