Skip to main content

fui_controls/controls/
shadow.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use fui_core::*;
5use typed_builder::TypedBuilder;
6
7use crate::style::*;
8
9#[derive(TypedBuilder)]
10pub struct Shadow {}
11
12impl Shadow {
13    pub fn to_view(
14        self,
15        style: Option<Box<dyn Style<Self>>>,
16        context: ViewContext,
17    ) -> Rc<RefCell<dyn ControlObject>> {
18        StyledControl::new(
19            self,
20            style.unwrap_or_else(|| {
21                Box::new(DefaultShadowStyle::new(
22                    DefaultShadowStyleParams::builder().build(),
23                ))
24            }),
25            context,
26        )
27    }
28}
29
30//
31// Default Shadow Style
32//
33
34#[derive(TypedBuilder)]
35pub struct DefaultShadowStyleParams {
36    #[builder(default = 6.0f32)]
37    size: f32,
38}
39
40pub struct DefaultShadowStyle {
41    params: DefaultShadowStyleParams,
42}
43
44impl DefaultShadowStyle {
45    pub fn new(params: DefaultShadowStyleParams) -> Self {
46        DefaultShadowStyle { params }
47    }
48}
49
50impl Style<Shadow> for DefaultShadowStyle {
51    fn setup(&mut self, _data: &mut Shadow, _control_context: &mut ControlContext) {}
52
53    fn handle_event(
54        &mut self,
55        _data: &mut Shadow,
56        _control_context: &mut ControlContext,
57        _drawing_context: &mut FuiDrawingContext,
58        _event_context: &mut dyn EventContext,
59        _event: ControlEvent,
60    ) {
61    }
62
63    fn measure(
64        &mut self,
65        _data: &mut Shadow,
66        control_context: &mut ControlContext,
67        drawing_context: &mut FuiDrawingContext,
68        size: Size,
69    ) -> Size {
70        let children = control_context.get_children();
71
72        match children.into_iter().next() {
73            Some(ref content) => {
74                content.borrow_mut().measure(drawing_context, size);
75                let rect = content.borrow().get_rect();
76                Size::new(rect.width, rect.height)
77            }
78            _ => Size::new(0f32, 0f32),
79        }
80    }
81
82    fn set_rect(
83        &mut self,
84        _data: &mut Shadow,
85        control_context: &mut ControlContext,
86        drawing_context: &mut FuiDrawingContext,
87        rect: Rect,
88    ) {
89        let children = control_context.get_children();
90        if let Some(ref content) = children.into_iter().next() {
91            content.borrow_mut().set_rect(drawing_context, rect);
92        }
93    }
94
95    fn hit_test(
96        &self,
97        _data: &Shadow,
98        control_context: &ControlContext,
99        point: Point,
100    ) -> Option<Rc<RefCell<dyn ControlObject>>> {
101        if point.is_inside(&control_context.get_rect()) {
102            let children = control_context.get_children();
103            if let Some(ref content) = children.into_iter().next() {
104                let c = content.borrow();
105                let rect = c.get_rect();
106                if point.is_inside(&rect) {
107                    let child_hit_test = c.hit_test(point);
108                    if child_hit_test.is_some() {
109                        return child_hit_test;
110                    }
111                }
112            }
113            Some(control_context.get_self_rc())
114        } else {
115            None
116        }
117    }
118
119    fn draw(
120        &mut self,
121        _data: &Shadow,
122        control_context: &ControlContext,
123        drawing_context: &mut FuiDrawingContext,
124    ) {
125        let rect = control_context.get_rect();
126
127        let x = rect.x;
128        let y = rect.y;
129        let width = rect.width;
130        let height = rect.height;
131
132        default_theme::shadow_under_rect(
133            &mut drawing_context.display,
134            x,
135            y,
136            width,
137            height,
138            self.params.size,
139        );
140
141        let children = control_context.get_children();
142        if let Some(ref content) = children.into_iter().next() {
143            content.borrow_mut().draw(drawing_context);
144        }
145    }
146}