makepad_widgets/
popup_notification.rs

1use crate::{
2    makepad_derive_widget::*,
3    makepad_draw::*,
4    view::*,
5    widget::*
6};
7
8live_design!{
9    link widgets;
10    use link::widgets::*;
11    use link::theme::*;
12    use makepad_draw::shader::std::*;
13    
14    pub PopupNotificationBase = {{PopupNotification}} {}
15        
16    pub PopupNotification = <PopupNotificationBase> {
17        width: Fill
18        height: Fill
19        flow: Overlay
20        align: {x: 1.0, y: 0.0}
21        
22        draw_bg: {
23            fn pixel(self) -> vec4 {
24                return vec4(0., 0., 0., 0.0)
25            }
26        }
27        
28        content: <View> {
29            flow: Overlay
30            width: Fit
31            height: Fit
32            
33            cursor: Default
34            capture_overload: true
35        }
36    }
37}
38
39
40#[derive(Live, Widget)]
41pub struct PopupNotification {
42    #[live]
43    #[find]
44    content: View,
45
46    #[rust(DrawList2d::new(cx))]
47    draw_list: DrawList2d,
48
49    #[redraw]
50    #[live]
51    draw_bg: DrawQuad,
52    #[layout]
53    layout: Layout,
54    #[walk]
55    walk: Walk,
56
57    #[rust]
58    opened: bool,
59}
60
61impl LiveHook for PopupNotification {
62    fn after_apply(&mut self, cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode]) {
63        self.draw_list.redraw(cx);
64    }
65}
66
67impl Widget for PopupNotification {
68    fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
69        if !self.opened {
70            return;
71        }
72
73        self.content.handle_event(cx, event, scope);
74    }
75
76    fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, _walk: Walk) -> DrawStep {
77        self.draw_list.begin_overlay_reuse(cx);
78        
79        let size = cx.current_pass_size();
80        cx.begin_sized_turtle(size, self.layout);
81        self.draw_bg.begin(cx, self.walk, self.layout);
82
83        if self.opened {
84            let _ = self.content.draw_all(cx, scope);
85        }
86
87        self.draw_bg.end(cx);
88
89        cx.end_pass_sized_turtle();
90        self.draw_list.end(cx);
91
92        DrawStep::done()
93    }
94}
95
96impl PopupNotification {
97    pub fn open(&mut self, cx: &mut Cx) {
98        self.opened = true;
99        self.redraw(cx);
100    }
101
102    pub fn close(&mut self, cx: &mut Cx) {
103        self.opened = false;
104        self.draw_bg.redraw(cx);
105    }
106}
107
108impl PopupNotificationRef {
109    pub fn open(&self, cx: &mut Cx) {
110        if let Some(mut inner) = self.borrow_mut() {
111            inner.open(cx);
112        }
113    }
114
115    pub fn close(&self, cx: &mut Cx) {
116        if let Some(mut inner) = self.borrow_mut() {
117            inner.close(cx);
118        }
119    }
120}