qframe/widget/context/event.rs
1//! The event handling context.
2
3use std::time::Duration;
4
5use super::frame::Interaction;
6use crate::env::Env;
7use crate::event::Event;
8use crate::geometry::Rect;
9use crate::keymap::Scope;
10use crate::runtime::CopyKind;
11use crate::widget::memory::Memory;
12use crate::widget::{Node, WidgetId};
13
14/// Requests widgets make of the runtime while handling an event.
15#[derive(Debug, Default)]
16pub(crate) struct Effects {
17 pub(crate) focus: Option<WidgetId>,
18 pub(crate) key_capture: Option<Option<WidgetId>>,
19 pub(crate) pointer_capture: bool,
20 pub(crate) flash: Option<WidgetId>,
21 pub(crate) copy: Vec<String>,
22 pub(crate) run_action: Option<(Scope, String)>,
23 pub(crate) pointer_repeat: Option<Duration>,
24 /// End this widget's pointer repeat, see [`EventCx::stop_pointer_repeat`].
25 pub(crate) stop_pointer_repeat: bool,
26 pub(crate) answer: Option<bool>,
27 pub(crate) focus_step: Option<isize>,
28 /// Read the clipboard to learn whether pasting is possible, see [`EventCx::probe_clipboard`].
29 pub(crate) probe_clipboard: bool,
30 /// Copy the mouse selection, see [`EventCx::copy_selection`].
31 pub(crate) copy_selection: Option<CopyKind>,
32}
33
34/// Event handling context.
35pub struct EventCx<'a, Msg> {
36 pub(crate) id: WidgetId,
37 pub(crate) rect: Rect,
38 pub(crate) focus_rect: Option<Rect>,
39 pub(crate) env: &'a Env,
40 pub(crate) memory: &'a mut Memory,
41 pub(crate) interaction: &'a Interaction,
42 pub(crate) messages: &'a mut Vec<Msg>,
43 pub(crate) effects: &'a mut Effects,
44 pub(crate) now: Duration,
45 pub(crate) persistent: bool,
46 /// Whether this is a press shown to a widget before the widgets inside it, see
47 /// [`PaintCx::preview_presses`](super::PaintCx::preview_presses).
48 pub(crate) preview: bool,
49 /// Whether this widget, or the widget that forwarded the event to it, holds the pointer: the
50 /// press it belongs to began on it. See [`EventCx::holds_pointer`].
51 pub(crate) holds_pointer: bool,
52}
53
54impl<Msg> EventCx<'_, Msg> {
55 /// Whether the press this pointer event belongs to began on this widget, which captured the
56 /// pointer then. A release that reaches a widget without it began somewhere else: on another
57 /// widget, or on a screen that changed since.
58 pub(crate) fn holds_pointer(&self) -> bool {
59 self.holds_pointer
60 }
61
62 /// Whether the event is a press shown to this widget before the widgets inside it, because
63 /// it asked with [`PaintCx::preview_presses`](super::PaintCx::preview_presses). Using it
64 /// keeps it from them; leaving it lets it go on as usual, to this widget too.
65 pub(crate) fn is_preview(&self) -> bool {
66 self.preview
67 }
68
69 /// The id of the widget handling the event.
70 #[must_use]
71 pub fn id(&self) -> WidgetId {
72 self.id
73 }
74
75 /// The area the widget was painted in during the last frame.
76 #[must_use]
77 pub fn area(&self) -> Rect {
78 self.rect
79 }
80
81 /// The area the focused widget was painted in during the last frame, if a widget has focus.
82 /// Lets a container place something next to the focused child, e.g. a context menu opened
83 /// from the keyboard.
84 #[must_use]
85 pub fn focused_area(&self) -> Option<Rect> {
86 self.focus_rect
87 }
88
89 /// The environment.
90 #[must_use]
91 pub fn env(&self) -> &Env {
92 self.env
93 }
94
95 /// Time since the runtime started.
96 #[must_use]
97 pub fn now(&self) -> Duration {
98 self.now
99 }
100
101 /// Sends a message to the application.
102 pub fn emit(&mut self, message: Msg) {
103 self.messages.push(message);
104 }
105
106 /// This widget's state of type `T`.
107 pub fn memory<T: Default + 'static>(&mut self) -> &mut T {
108 self.memory.get::<T>(self.id, self.persistent)
109 }
110
111 /// Whether this widget has keyboard focus.
112 #[must_use]
113 pub fn is_focused(&self) -> bool {
114 self.interaction.focused == Some(self.id)
115 }
116
117 /// Moves keyboard focus to this widget.
118 pub fn request_focus(&mut self) {
119 self.effects.focus = Some(self.id);
120 }
121
122 /// Moves keyboard focus to the next widget in focus order, as Tab does. Forms use it to go
123 /// to the next field on Enter.
124 pub fn focus_next(&mut self) {
125 self.effects.focus_step = Some(1);
126 }
127
128 /// Offers `event` to a child `node` painted in `rect`, as if the child had received it: the
129 /// child keeps its own memory, and its messages and requests go out with this widget's. For
130 /// widgets that take focus as one control and let a child act, e.g. a settings row passing
131 /// Space to its switch. Returns whether the child used the event.
132 pub fn forward(&mut self, node: &Node<Msg>, rect: Rect, event: &Event) -> bool
133 where
134 Msg: 'static,
135 {
136 let mut child = EventCx {
137 id: node.id,
138 rect,
139 focus_rect: self.focus_rect,
140 env: self.env,
141 memory: &mut *self.memory,
142 interaction: self.interaction,
143 messages: &mut *self.messages,
144 effects: &mut *self.effects,
145 now: self.now,
146 persistent: self.persistent,
147 preview: self.preview,
148 holds_pointer: self.holds_pointer,
149 };
150 node.widget.event(&mut child, event)
151 }
152
153 /// While on, every key event goes to this widget first (an open dropdown), and a pointer
154 /// press on any other widget, including one inside it, first sends it
155 /// [`Event::PointerOutside`](crate::event::Event::PointerOutside) and then reaches that widget
156 /// as usual. A press on this widget itself reaches only this widget.
157 pub fn capture_keys(&mut self, on: bool) {
158 self.effects.key_capture = Some(on.then_some(self.id));
159 }
160
161 /// Keeps pointer events flowing to this widget until the button is released.
162 pub fn capture_pointer(&mut self) {
163 self.effects.pointer_capture = true;
164 }
165
166 /// Flashes this widget to confirm an activation.
167 pub fn flash(&mut self) {
168 self.effects.flash = Some(self.id);
169 }
170
171 /// Copies `text` to the system clipboard.
172 pub fn copy(&mut self, text: impl Into<String>) {
173 self.effects.copy.push(text.into());
174 }
175
176 /// Runs keymap action `action` of `scope` as if its key had been pressed, after this event.
177 /// Application actions reach [`App::action`](crate::runtime::App::action) even while a
178 /// modal layer is open, since the user asked for them explicitly (e.g. from a command
179 /// palette).
180 pub fn run_action(&mut self, scope: Scope, action: impl Into<String>) {
181 self.effects.run_action = Some((scope, action.into()));
182 }
183
184 /// While the pointer is captured, delivers a `Drag` event at the last pointer position to
185 /// this widget every `interval` until the button is released, as if the pointer moved in
186 /// place. Terminals send nothing while a button is held still; this lets a widget react
187 /// to how long it is held (hold-to-confirm, auto-repeating steppers). Call it together
188 /// with [`EventCx::capture_pointer`] on the button press.
189 pub fn repeat_pointer(&mut self, interval: Duration) {
190 self.effects.pointer_repeat = Some(interval.max(Duration::from_millis(1)));
191 }
192
193 /// Ends the repeat [`EventCx::repeat_pointer`] started for this widget before the button is
194 /// released, so a widget that needs timed drags only for a while (scrolling while a dragged
195 /// item rests against an edge) does not keep waking the loop afterwards. A repeat asked for in
196 /// the same event wins. Nothing happens when this widget has no repeat running.
197 pub fn stop_pointer_repeat(&mut self) {
198 self.effects.stop_pointer_repeat = true;
199 }
200
201 /// Runs `handle` with a context whose messages are of type `M` instead of `Msg`, and returns
202 /// its result with the messages it sent; everything else (memory, focus, captures, copies)
203 /// is this widget's. Lets a widget drive an inner widget of its own, such as the edit menu of
204 /// a text field, whose choices are the field's business rather than the application's.
205 pub(crate) fn with_messages<M, R>(&mut self, handle: impl FnOnce(&mut EventCx<'_, M>) -> R) -> (R, Vec<M>) {
206 let mut messages = Vec::new();
207 let result = {
208 let mut inner = EventCx {
209 id: self.id,
210 rect: self.rect,
211 focus_rect: self.focus_rect,
212 env: self.env,
213 memory: &mut *self.memory,
214 interaction: self.interaction,
215 messages: &mut messages,
216 effects: &mut *self.effects,
217 now: self.now,
218 persistent: self.persistent,
219 preview: self.preview,
220 holds_pointer: self.holds_pointer,
221 };
222 handle(&mut inner)
223 };
224 (result, messages)
225 }
226
227 /// Reads the clipboard in the background so `can_paste` on this context and on
228 /// [`PaintCx`](crate::widget::PaintCx) soon tells whether it has text, e.g. when an edit menu
229 /// opens.
230 pub(crate) fn probe_clipboard(&mut self) {
231 self.effects.probe_clipboard = true;
232 }
233
234 /// Whether pasting would insert text, as far as the runtime knows.
235 pub(crate) fn can_paste(&self) -> bool {
236 self.interaction.can_paste
237 }
238
239 /// Copies the runtime's mouse selection as `kind`.
240 pub(crate) fn copy_selection(&mut self, kind: CopyKind) {
241 self.effects.copy_selection = Some(kind);
242 }
243
244 /// Resolves the confirmation dialog the runtime shows for
245 /// [`Command::confirm`](crate::runtime::Command::confirm).
246 pub(crate) fn answer(&mut self, confirmed: bool) {
247 self.effects.answer = Some(confirmed);
248 }
249}