use super::Widget;
use core::marker::PhantomData;
use embedded_graphics::{pixelcolor::PixelColor, prelude::*, primitives::Rectangle};
use zest_core::{Constraints, Length, RenderError, Renderer, TouchPhase};
use zest_theme::Theme;
pub struct Divider<C: PixelColor, M: Clone> {
rect: Rectangle,
width: Length,
height: Length,
color: Option<C>,
_phantom: PhantomData<M>,
}
impl<C: PixelColor, M: Clone> Divider<C, M> {
pub fn new(width: impl Into<Length>, height: impl Into<Length>) -> Self {
Self {
rect: Rectangle::zero(),
width: width.into(),
height: height.into(),
color: None,
_phantom: PhantomData,
}
}
#[must_use]
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
#[must_use]
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
#[must_use]
pub fn color(mut self, color: C) -> Self {
self.color = Some(color);
self
}
#[must_use]
pub fn thickness(self, thickness: u32) -> Self {
let thin_is_height = matches!(
(self.width, self.height),
(Length::Fill | Length::FillPortion(_), _),
);
if thin_is_height {
self.height(Length::Fixed(thickness))
} else {
self.width(Length::Fixed(thickness))
}
}
}
pub fn horizontal_divider<C: PixelColor, M: Clone>() -> Divider<C, M> {
Divider::new(Length::Fill, Length::Fixed(1))
}
pub fn vertical_divider<C: PixelColor, M: Clone>() -> Divider<C, M> {
Divider::new(Length::Fixed(1), Length::Fill)
}
impl<C: PixelColor, M: Clone> Widget<C, M> for Divider<C, M> {
fn measure(&mut self, constraints: Constraints) -> Size {
let w = self.width.resolve(1, constraints.max.width);
let h = self.height.resolve(1, constraints.max.height);
constraints.clamp(Size::new(w, h))
}
fn preferred_size(&self) -> (Length, Length) {
(self.width, self.height)
}
fn arrange(&mut self, rect: Rectangle) {
self.rect = rect;
}
fn rect(&self) -> Rectangle {
self.rect
}
fn handle_touch(&mut self, _point: Point, _phase: TouchPhase) -> Option<M> {
None
}
fn draw<'t>(
&self,
renderer: &mut dyn Renderer<C>,
theme: &Theme<'t, C>,
) -> Result<(), RenderError> {
let color = self.color.unwrap_or(theme.background.divider);
renderer.fill_rect(self.rect, color)?;
Ok(())
}
}