Skip to main content

blit_gui/
rectangle.rs

1use core::hash::{Hash, Hasher};
2
3use blit::{FrameSlice, HasFrameStore, LogicalRect, Paint, Ui, Widget, state};
4
5use crate::{
6    color::Color,
7    display_list::{Compare, DisplayList},
8    style::{Border, BorderRadius, ConicGradient, Fill, GradientStop, LinearGradient},
9};
10
11blit::builder! {
12    #[derive(Clone, Copy, Debug, PartialEq)]
13    pub struct Rectangle<'a> {
14        new(),
15        #[into]
16        background: Fill<'a> = Fill::Solid(Color::TRANSPARENT),
17        border: Border<'a> = Border::default(),
18        radius: BorderRadius = BorderRadius::new(),
19        opacity: f32 = 1.0,
20    }
21}
22
23impl<C> Widget<C, state::Insert> for Rectangle<'_>
24where
25    C: HasFrameStore + Paint<FrameRectangle, Scalar = f32>,
26{
27    type Response = ();
28
29    fn build(self, mut ui: Ui<'_, C, state::Insert>) {
30        if self.background == Fill::Solid(Color::TRANSPARENT) && self.border.width <= 0.0 {
31            return;
32        }
33        let background = FrameFill::new(self.background, ui.context_mut());
34        let border_fill = FrameFill::new(self.border.fill, ui.context_mut());
35        let rectangle = FrameRectangle {
36            background,
37            border_fill,
38            border_width: self.border.width,
39            radius: self.radius,
40            opacity: self.opacity,
41        };
42        ui.paint(move |cx, area| cx.context_mut().paint(area, &rectangle));
43    }
44}
45
46impl Rectangle<'_> {
47    /// stores borrowed fills in the context and paints the result
48    pub fn paint<C: HasFrameStore + Paint<FrameRectangle, Scalar = f32>>(self, area: LogicalRect, context: &mut C) {
49        let rectangle = FrameRectangle {
50            background: FrameFill::new(self.background, context),
51            border_fill: FrameFill::new(self.border.fill, context),
52            border_width: self.border.width,
53            radius: self.radius,
54            opacity: self.opacity,
55        };
56        context.paint(area, &rectangle);
57    }
58}
59
60blit::builder! {
61    /// rectangle appearance retained in the frame
62    #[derive(Clone, Copy, Debug, PartialEq)]
63    pub struct FrameRectangle {
64        new(),
65        #[into]
66        background: FrameFill = FrameFill::Solid(Color::TRANSPARENT),
67        #[into]
68        border_fill: FrameFill = FrameFill::Solid(Color::TRANSPARENT),
69        border_width: f32 = 0.0,
70        radius: BorderRadius = BorderRadius::new(),
71        opacity: f32 = 1.0,
72    }
73}
74
75impl From<Color> for FrameFill {
76    fn from(color: Color) -> Self {
77        Self::Solid(color)
78    }
79}
80
81impl Hash for Rectangle<'_> {
82    fn hash<H: Hasher>(&self, state: &mut H) {
83        [
84            self.radius.top_left,
85            self.radius.top_right,
86            self.radius.bottom_right,
87            self.radius.bottom_left,
88            self.opacity,
89            self.border.width,
90        ]
91        .map(|value| if value == 0.0 { 0 } else { value.to_bits() })
92        .hash(state);
93        self.background.hash(state);
94        self.border.fill.hash(state);
95    }
96}
97
98impl FrameRectangle {
99    #[inline]
100    pub fn view<'a>(&self, list: &'a DisplayList) -> Rectangle<'a> {
101        Rectangle {
102            background: self.background.view(list),
103            border: Border {
104                width: self.border_width,
105                fill: self.border_fill.view(list),
106            },
107            radius: self.radius,
108            opacity: self.opacity,
109        }
110    }
111}
112
113impl Compare for FrameRectangle {
114    #[inline]
115    fn equal(a: &Self, left: &DisplayList, b: &Self, right: &DisplayList) -> bool {
116        a.view(left) == b.view(right)
117    }
118
119    fn hash(value: &Self, list: &DisplayList, state: &mut impl Hasher) {
120        value.view(list).hash(state);
121    }
122}
123
124#[derive(Clone, Copy, Debug, PartialEq)]
125pub enum FrameFill {
126    Solid(Color),
127    LinearGradient {
128        stops: FrameSlice<GradientStop>,
129        angle: f32,
130    },
131    ConicGradient {
132        stops: FrameSlice<GradientStop>,
133        center: [f32; 2],
134        angle: f32,
135    },
136}
137
138impl FrameFill {
139    #[inline]
140    pub(crate) fn new(fill: Fill<'_>, store: &mut impl HasFrameStore) -> Self {
141        match fill {
142            Fill::Solid(color) => Self::Solid(color),
143            Fill::LinearGradient(gradient) => Self::LinearGradient {
144                stops: store.store_mut().slice_from(gradient.stops),
145                angle: gradient.angle,
146            },
147            Fill::ConicGradient(gradient) => Self::ConicGradient {
148                stops: store.store_mut().slice_from(gradient.stops),
149                center: gradient.center,
150                angle: gradient.angle,
151            },
152        }
153    }
154
155    #[inline]
156    pub(crate) fn view<'a>(&self, list: &'a DisplayList) -> Fill<'a> {
157        match *self {
158            Self::Solid(color) => Fill::Solid(color),
159            Self::LinearGradient { stops, angle } => Fill::LinearGradient(LinearGradient {
160                stops: list.store().view(stops),
161                angle,
162            }),
163            Self::ConicGradient { stops, center, angle } => Fill::ConicGradient(ConicGradient {
164                stops: list.store().view(stops),
165                center,
166                angle,
167            }),
168        }
169    }
170}