embedded_ui/
block.rs

1use embedded_graphics::primitives::{CornerRadii, Rectangle};
2
3use crate::render::Renderer;
4use crate::size::Size;
5
6use crate::color::UiColor;
7
8#[derive(Clone, Copy, Debug)]
9pub struct BorderRadius {
10    pub top_left: Size,
11    pub top_right: Size,
12    pub bottom_right: Size,
13    pub bottom_left: Size,
14}
15
16impl BorderRadius {
17    pub fn new(top_left: Size, top_right: Size, bottom_right: Size, bottom_left: Size) -> Self {
18        Self { top_left, top_right, bottom_right, bottom_left }
19    }
20
21    pub fn new_equal(ellipse: Size) -> Self {
22        Self::new(ellipse, ellipse, ellipse, ellipse)
23    }
24
25    /// Avoid using border radius larger than half of block size
26    // TODO: Review this logic, it might be an invalid limit for a user
27    pub fn resolve_for_size(self, size: Size) -> Self {
28        let max_size = size / 2;
29        Self {
30            top_left: self.top_left.min(max_size),
31            top_right: self.top_right.min(max_size),
32            bottom_right: self.bottom_right.min(max_size),
33            bottom_left: self.bottom_left.min(max_size),
34        }
35    }
36}
37
38impl Into<CornerRadii> for BorderRadius {
39    fn into(self) -> CornerRadii {
40        CornerRadii {
41            top_left: self.top_left.into(),
42            top_right: self.top_right.into(),
43            bottom_right: self.bottom_right.into(),
44            bottom_left: self.bottom_left.into(),
45        }
46    }
47}
48
49impl From<u32> for BorderRadius {
50    fn from(value: u32) -> Self {
51        Self::new_equal(Size::new_equal(value))
52    }
53}
54
55impl From<[u32; 4]> for BorderRadius {
56    fn from(value: [u32; 4]) -> Self {
57        Self::new(
58            Size::new_equal(value[0]),
59            Size::new_equal(value[1]),
60            Size::new_equal(value[2]),
61            Size::new_equal(value[3]),
62        )
63    }
64}
65
66impl Default for BorderRadius {
67    fn default() -> Self {
68        Self::new_equal(Size::zero())
69    }
70}
71
72#[derive(Debug)]
73pub struct Border<C: UiColor>
74where
75    C: Copy,
76{
77    pub color: C,
78    pub width: u32,
79    pub radius: BorderRadius,
80}
81
82impl<C: UiColor> Clone for Border<C> {
83    fn clone(&self) -> Self {
84        Self { color: self.color, width: self.width, radius: self.radius }
85    }
86}
87
88impl<C: UiColor> Copy for Border<C> {}
89
90impl<C: UiColor> Border<C> {
91    pub fn new() -> Self {
92        Self { color: C::default_foreground(), width: 1, radius: BorderRadius::default() }
93    }
94}
95
96#[derive(Clone, Copy)]
97pub struct Block<C: UiColor + Copy> {
98    pub border: Border<C>,
99    pub rect: Rectangle,
100    pub background: C,
101}