agg_gui/widget/app/
pointer.rs1use super::tree_paths::widget_at_path;
6use crate::event::{Event, Modifiers, MouseButton};
7use crate::geometry::Point;
8use crate::widget::tree::{active_modal_path, dispatch_event, hit_test_subtree};
9use crate::widget::tree_inspector::set_current_mouse_world;
10use crate::widget::{App, Widget};
11
12impl App {
13 pub(crate) fn extend_modal_path(&self, modal_path: &[usize], pos_in_root: Point) -> Vec<usize> {
18 let mut widget: &dyn Widget = self.root.as_ref();
19 let mut pos = pos_in_root;
20 for &index in modal_path {
21 let Some(child) = widget.children().get(index) else {
22 return modal_path.to_vec();
23 };
24 let bounds = child.bounds();
25 if let Some(t) = widget.child_transform() {
26 t.inverse_transform(&mut pos.x, &mut pos.y);
27 }
28 pos = Point::new(pos.x - bounds.x, pos.y - bounds.y);
29 widget = child.as_ref();
30 }
31 let mut full = modal_path.to_vec();
32 if let Some(sub_path) = hit_test_subtree(widget, pos) {
33 full.extend(sub_path);
34 }
35 full
36 }
37
38 pub fn on_mouse_move(&mut self, screen_x: f64, screen_y: f64) {
40 crate::cursor::reset_cursor_icon();
42 let screen = self.flip_y(screen_x, screen_y);
43 if crate::widgets::on_screen_keyboard::handle_software_keyboard_mouse_move(screen) {
44 self.drain_keyboard_synthetic_keys();
45 return;
46 }
47 let pos = super::keyboard_scroll::lift_to_world(screen);
48 set_current_mouse_world(pos);
49 if let Some(path) = active_modal_path(self.root.as_ref()) {
50 let path = self.extend_modal_path(&path, pos);
51 let event = Event::MouseMove { pos };
52 dispatch_event(&mut self.root, &path, &event, pos);
53 self.hovered = Some(path);
54 return;
55 }
56 self.dispatch_mouse_move(pos);
57 }
58
59 pub fn on_mouse_down(
61 &mut self,
62 screen_x: f64,
63 screen_y: f64,
64 button: MouseButton,
65 mods: Modifiers,
66 ) {
67 let screen = self.flip_y(screen_x, screen_y);
68 if crate::widgets::on_screen_keyboard::handle_software_keyboard_mouse_down(
73 screen, button, mods,
74 ) {
75 return;
76 }
77 let pos = super::keyboard_scroll::lift_to_world(screen);
78 set_current_mouse_world(pos);
79 crate::animation::bump_pointer_press_epoch();
83 let modal_path = active_modal_path(self.root.as_ref());
84 let event = Event::MouseDown {
85 pos,
86 button,
87 modifiers: mods,
88 };
89 if let Some(path) = modal_path {
90 let path = self.extend_modal_path(&path, pos);
94 if widget_at_path(&mut self.root, &path).is_focusable() {
95 self.set_focus(Some(path.clone()));
96 } else {
97 self.set_focus(None);
98 }
99 if dispatch_event(&mut self.root, &path, &event, pos).is_consumed() {
100 self.captured = Some(path);
101 }
102 return;
103 }
104 let hit = self.compute_hit(pos);
105
106 if let Some(ref path) = hit {
108 let w = widget_at_path(&mut self.root, path);
109 if w.is_focusable() {
110 self.set_focus(Some(path.clone()));
111 } else {
112 self.set_focus(None);
113 }
114 } else {
115 self.set_focus(None);
116 }
117
118 if let Some(mut path) = hit {
119 let result = dispatch_event(&mut self.root, &path, &event, pos);
120 if result.is_consumed() {
121 self.maybe_bring_to_front(&mut path);
122 let capture_path = self.compute_hit(pos).unwrap_or(path);
123 self.captured = Some(capture_path);
124 }
125 }
126 }
132
133 pub fn on_mouse_up(
135 &mut self,
136 screen_x: f64,
137 screen_y: f64,
138 button: MouseButton,
139 mods: Modifiers,
140 ) {
141 let screen = self.flip_y(screen_x, screen_y);
142 if crate::widgets::on_screen_keyboard::handle_software_keyboard_mouse_up(
147 screen, button, mods,
148 ) {
149 self.captured = None;
150 self.drain_keyboard_synthetic_keys();
151 return;
152 }
153 let pos = super::keyboard_scroll::lift_to_world(screen);
154 set_current_mouse_world(pos);
155 let event = Event::MouseUp {
156 pos,
157 button,
158 modifiers: mods,
159 };
160 if let Some(path) = active_modal_path(self.root.as_ref()) {
161 let path = self
164 .captured
165 .take()
166 .unwrap_or_else(|| self.extend_modal_path(&path, pos));
167 dispatch_event(&mut self.root, &path, &event, pos);
168 return;
169 }
170 if let Some(path) = self.captured.take() {
172 dispatch_event(&mut self.root, &path, &event, pos);
173 } else {
174 let hit = self.compute_hit(pos);
175 if let Some(path) = hit {
176 dispatch_event(&mut self.root, &path, &event, pos);
177 }
178 }
179 }
180}