1use core::hash::{Hash, Hasher};
2
3use blit::{LogicalPoint, LogicalRect, Paint, Ui, Widget, state};
4
5use crate::{color::Color, style::BorderRadius};
6
7blit::builder! {
8 #[derive(Clone, Copy, Debug, PartialEq)]
9 pub struct Shadow {
10 new(color: Color),
11 radius: BorderRadius = BorderRadius::new(),
12 offset: LogicalPoint = LogicalPoint::new(0.0, 0.0),
13 blur: f32 = 0.0,
14 spread: f32 = 0.0,
15 inset: bool = false,
16 }
17}
18
19impl Shadow {
20 pub fn bounds(self, area: LogicalRect) -> LogicalRect {
21 if self.inset {
22 return area;
23 }
24 let outset = self.spread + self.blur.max(0.0);
25 LogicalRect {
26 x: area.x + self.offset.x - outset,
27 y: area.y + self.offset.y - outset,
28 width: area.width + outset * 2.0,
29 height: area.height + outset * 2.0,
30 }
31 }
32}
33
34impl<C> Widget<C, state::Insert> for Shadow
35where
36 C: Paint<Self, Scalar = f32>,
37{
38 type Response = ();
39
40 fn build(self, mut ui: Ui<'_, C, state::Insert>) {
41 ui.paint(move |cx, area| cx.context_mut().paint(area, &self));
42 }
43}
44
45impl Hash for Shadow {
46 fn hash<H: Hasher>(&self, state: &mut H) {
47 [
48 self.radius.top_left,
49 self.radius.top_right,
50 self.radius.bottom_right,
51 self.radius.bottom_left,
52 self.offset.x,
53 self.offset.y,
54 self.blur,
55 self.spread,
56 ]
57 .map(|value| if value == 0.0 { 0 } else { value.to_bits() })
58 .hash(state);
59 self.color.hash(state);
60 self.inset.hash(state);
61 }
62}