makepad_widgets/
tooltip.rs

1use crate::{
2    makepad_derive_widget::*,
3    makepad_draw::*,
4    view::*,
5    label::*,
6    widget::*
7};
8
9live_design!{
10    link widgets;
11    use link::widgets::*;
12    use link::theme::*;
13    use makepad_draw::shader::std::*;
14    
15    pub TooltipBase = {{Tooltip}} {}
16    pub Tooltip = <TooltipBase> {
17        width: Fill,
18        height: Fill,
19        
20        flow: Overlay
21        align: {x: 0.0, y: 0.0}
22        
23        draw_bg: {
24            fn pixel(self) -> vec4 {
25                return vec4(0., 0., 0., 0.0)
26            }
27        }
28        
29        content: <View> {
30            flow: Overlay
31            width: Fit
32            height: Fit
33            
34            <RoundedView> {
35                width: Fit,
36                height: Fit,
37                
38                padding: 16,
39                
40                draw_bg: {
41                    color: #fff,
42                    border_size: 1.0,
43                    border_color: #D0D5DD,
44                    radius: 2.
45                }
46                
47                tooltip_label = <Label> {
48                    width: 270,
49                    draw_text: {
50                        text_style: <THEME_FONT_REGULAR>{font_size: 9},
51                        text_wrap: Word,
52                        color: #000
53                    }
54                }
55            }
56        }
57    }
58}
59
60#[derive(Live, LiveHook, Widget)]
61pub struct Tooltip {
62    #[rust]
63    opened: bool,
64
65    #[live]
66    #[find]
67    content: View,
68
69    #[rust(DrawList2d::new(cx))]
70    draw_list: DrawList2d,
71
72    #[redraw]
73    #[area]
74    #[live]
75    draw_bg: DrawQuad,
76    #[layout]
77    layout: Layout,
78    #[walk]
79    walk: Walk,
80}
81
82impl Widget for Tooltip {
83    fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
84        if !self.opened {
85            return;
86        }
87
88        self.content.handle_event(cx, event, scope);
89
90        match event.hits_with_capture_overload(cx, self.content.area(), true) {
91            Hit::FingerUp(fue) if fue.is_over => {
92                self.hide(cx);
93            }
94            _ => { }
95        }
96    }
97
98    fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, _walk: Walk) -> DrawStep {
99        self.draw_list.begin_overlay_reuse(cx);
100        
101        let size = cx.current_pass_size();
102        cx.begin_sized_turtle(size, self.layout);
103        self.draw_bg.begin(cx, self.walk, self.layout);
104
105        if self.opened {
106            let _ = self.content.draw_all(cx, scope);
107        }
108
109        self.draw_bg.end(cx);
110
111        cx.end_pass_sized_turtle();
112        self.draw_list.end(cx);
113
114        DrawStep::done()
115    }
116
117    fn set_text(&mut self, cx:&mut Cx, text: &str) {
118        self.label(id!(tooltip_label)).set_text(cx, text);
119    }
120}
121
122impl Tooltip {
123    pub fn set_pos(&mut self, cx: &mut Cx, pos: DVec2) {
124        self.apply_over(
125            cx,
126            live! {
127                content: { margin: { left: (pos.x), top: (pos.y) } }
128            },
129        );
130    }
131
132    pub fn show(&mut self, cx: &mut Cx) {
133        self.opened = true;
134        self.redraw(cx);
135    }
136
137    pub fn show_with_options(&mut self, cx: &mut Cx, pos: DVec2, text: &str) {
138        self.set_text(cx, text);
139        self.set_pos(cx, pos);
140        self.show(cx);
141    }
142
143    pub fn hide(&mut self, cx: &mut Cx) {
144        self.opened = false;
145        self.redraw(cx);
146    }
147}
148
149impl TooltipRef {
150    pub fn set_text(&mut self, cx:&mut Cx, text: &str) {
151        if let Some(mut inner) = self.borrow_mut() {
152            inner.set_text(cx, text);
153        }
154    }
155
156    pub fn set_pos(&self, cx: &mut Cx, pos: DVec2) {
157        if let Some(mut inner) = self.borrow_mut() {
158            inner.set_pos(cx, pos);
159        }
160    }
161
162    pub fn show(&self, cx: &mut Cx) {
163        if let Some(mut inner) = self.borrow_mut() {
164            inner.show(cx);
165        }
166    }
167
168    pub fn show_with_options(&self, cx: &mut Cx, pos: DVec2, text: &str) {
169        if let Some(mut inner) = self.borrow_mut() {
170            inner.show_with_options(cx, pos, text);
171        }
172    }
173
174    pub fn hide(&self, cx: &mut Cx) {
175        if let Some(mut inner) = self.borrow_mut() {
176            inner.hide(cx);
177        }
178    }
179}