extern crate alloc;
use core::convert::Infallible;
use alloc::vec::Vec;
use embedded_graphics::{
pixelcolor::Rgb565,
prelude::{DrawTarget, PixelColor},
};
use crate::{
Style,
element::{Element, IntoElement, ParentElement, draw_box},
layout::BoxLayout,
style::StyledElement,
};
#[derive(Default, PartialEq, Eq)]
pub struct RowStyle {}
#[derive(Default, PartialEq, Eq)]
pub struct Row<'a, C = Rgb565, CE = Infallible>
where
C: PixelColor,
{
pub(crate) children: Vec<Element<'a, C, CE>>,
pub(crate) style: Style<RowStyle, C>,
}
impl<C> Row<'_, C>
where
C: PixelColor,
{
pub fn new() -> Self {
Self { children: Vec::new(), style: Style::default() }
}
pub const fn with_style(mut self, style: Style<RowStyle, C>) -> Self {
self.style = style;
self
}
pub const fn style(&self) -> &Style<RowStyle, C> {
&self.style
}
pub(crate) fn draw<D>(&self, layout: &BoxLayout, target: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = C>,
{
draw_box(&self.style, layout, target)
}
}
impl<'a, C> IntoElement for Row<'a, C>
where
C: PixelColor,
{
type Element = Element<'a, C>;
fn into_element(self) -> Element<'a, C> {
Element::Row(self)
}
}
impl<'a, C> ParentElement<'a, C> for Row<'a, C>
where
C: PixelColor,
{
fn extend(&mut self, elements: impl IntoIterator<Item = Element<'a, C>>) {
self.children.extend(elements);
}
}
impl<C> StyledElement for Row<'_, C>
where
C: PixelColor,
{
type Color = C;
type Specific = RowStyle;
fn style_mut(&mut self) -> &mut Style<Self::Specific, Self::Color> {
&mut self.style
}
}
pub fn row<'a, C>() -> Row<'a, C>
where
C: PixelColor,
{
Row::new()
}