Skip to main content

agg_gui/widget/app/
pointer.rs

1//! Pointer-input methods on [`App`] — mouse move/down/up plus the
2//! modal-subtree hit extension they share. Split out of `app.rs`
3//! (800-line guardrail); wheel and keyboard routing stay in `app.rs`.
4
5use 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    /// Extend the active modal widget's path by hit-testing inside its
14    /// subtree at `pos_in_root`, so the modal's own children (buttons,
15    /// fields, scroll views) receive pointer events. Falls back to the
16    /// modal widget itself when nothing inside is hit (the scrim).
17    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    /// Mouse cursor moved. `screen_y` is Y-down physical pixels.
39    pub fn on_mouse_move(&mut self, screen_x: f64, screen_y: f64) {
40        // Reset cursor so the hovered widget can set it; Default if nothing sets it.
41        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    /// Mouse button pressed. `screen_y` is Y-down physical pixels.
60    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        // On-screen keyboard captures pointer events on its panel area
69        // before anything in the tree gets a look. Returning here also
70        // means the focused widget keeps focus (so the keyboard does
71        // not dismiss itself by stealing focus on every key tap).
72        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        // Count this press so widgets running their own multi-click gesture
80        // (Scene's background double-click) can detect an intervening press
81        // even when a hosted child consumes it before it can bubble.
82        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            // Hit-test inside the modal subtree so its children get the
91            // click, with the same click-to-focus rule as the normal path
92            // (text fields in dialogs need focus to type).
93            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        // Click-to-focus: if the hit widget is focusable, give it focus.
107        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        // NO blanket request_draw.  Mouse-down on an inert area must not
127        // cause a repaint.  Each widget that changes visual state in
128        // response to a MouseDown (button press, window raise, focus
129        // indicator on the focus-gained widget, etc.) is responsible for
130        // calling `crate::animation::request_draw` itself.
131    }
132
133    /// Mouse button released. `screen_y` is Y-down.
134    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        // On-screen keyboard owns release events on its panel; releases
143        // here commit a key tap and synthesize a `KeyDown`. After
144        // consumption we drain the synthetic-key queue so the focused
145        // text widget receives the character in the same frame.
146        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            // Deliver the release where the press was captured (button
162            // click completion), falling back to the hit inside the modal.
163            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        // Deliver release to captured widget first (if any), then clear capture.
171        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}