makepad_widgets/
slide_panel.rs1use crate::{
2    makepad_derive_widget::*,
3    makepad_draw::*,
4    view::*,
5    widget::*,
6};
7
8live_design!{
9    SlidePanelBase = {{SlidePanel}} {}
10}
11
12
13#[derive(Live)]
14pub struct SlidePanel {
15    #[deref] frame: View,
16    #[animator] animator: Animator,
17    #[live] closed: f64,
18    #[live] side: SlideSide,
19    #[rust] next_frame: NextFrame
20}
21
22impl LiveHook for SlidePanel {
23    fn before_live_design(cx: &mut Cx) {
24        register_widget!(cx, SlidePanel)
25    }
26}
27
28#[derive(Clone, WidgetAction)]
29pub enum SlidePanelAction {
30    None,
31}
32
33impl Widget for SlidePanel {
34    fn handle_widget_event_with(
35        &mut self,
36        cx: &mut Cx,
37        event: &Event,
38        dispatch_action: &mut dyn FnMut(&mut Cx, WidgetActionItem)
39    ) {
40        let uid = self.widget_uid();
41        self.frame.handle_widget_event_with(cx, event, dispatch_action);
42        self.handle_event_with(cx, event, &mut | cx, action | {
43            dispatch_action(cx, WidgetActionItem::new(action.into(), uid));
44        });
45    }
46    
47    fn walk(&mut self, cx:&mut Cx) -> Walk {
48        self.frame.walk(cx)
49    }
50    
51    fn redraw(&mut self, cx: &mut Cx) {
52        self.frame.redraw(cx)
53    }
54    
55    fn find_widgets(&mut self, path: &[LiveId], cached: WidgetCache, results: &mut WidgetSet) {
56        self.frame.find_widgets(path, cached, results);
57    }
58    
59    fn draw_walk_widget(&mut self, cx: &mut Cx2d, mut walk: Walk) -> WidgetDraw {
60        let rect = cx.peek_walk_turtle(walk);
62        match self.side{
63            SlideSide::Top=>{
64                walk.abs_pos = Some(dvec2(0.0, -rect.size.y * self.closed));
65            }
66            SlideSide::Left=>{
67                walk.abs_pos = Some(dvec2(-rect.size.x * self.closed, 0.0));
68            }
69        }
70        self.frame.draw_walk_widget(cx, walk)
71    }
72}
73
74#[derive(Live, LiveHook)]
75#[live_ignore]
76pub enum SlideSide{
77    #[pick] Left,
78    Top
79}
80
81impl SlidePanel {
82    pub fn handle_event_with(&mut self, cx: &mut Cx, event: &Event, _dispatch_action: &mut dyn FnMut(&mut Cx, SlidePanelAction)) {
83        if self.animator_handle_event(cx, event).must_redraw() {
85            self.frame.redraw(cx);
86        }
87        match event {
88            Event::NextFrame(ne) if ne.set.contains(&self.next_frame) => {
89                
90                self.frame.redraw(cx);
91            }
92            _ => ()
93        }
94    }
95    pub fn open(&mut self, cx: &mut Cx) {
96        self.frame.redraw(cx);
97    }
98    pub fn close(&mut self, cx: &mut Cx) {
99        self.frame.redraw(cx);
100    }
101    
102    pub fn redraw(&mut self, cx: &mut Cx) {
103        self.frame.redraw(cx);
104    }
105}
106
107#[derive(Clone, PartialEq, WidgetRef)]
109pub struct SlidePanelRef(WidgetRef);
110
111impl SlidePanelRef {
112    pub fn close(&self, cx: &mut Cx) {
113        if let Some(mut inner) = self.borrow_mut() {
114            inner.animator_play(cx, id!(closed.on))
115        }
116    }
117    pub fn open(&self, cx: &mut Cx) {
118        if let Some(mut inner) = self.borrow_mut() {
119            inner.animator_play(cx, id!(closed.off))
120        }
121    }
122    pub fn toggle(&self, cx: &mut Cx) {
123        if let Some(mut inner) = self.borrow_mut() {
124            if inner.animator_in_state(cx, id!(closed.on)){
125                inner.animator_play(cx, id!(closed.off))
126            }
127            else{
128                inner.animator_play(cx, id!(closed.on))
129            }
130        }
131    }
132}
133
134#[derive(Clone, WidgetSet)]
135pub struct SlidePanelSet(WidgetSet);
136
137impl SlidePanelSet {
138    pub fn close(&self, cx: &mut Cx) {
139        for item in self.iter() {
140            item.close(cx);
141        }
142    }
143    pub fn open(&self, cx: &mut Cx) {
144        for item in self.iter() {
145            item.open(cx);
146        }
147    }
148}
149