1use crate::{
2 div, point, Element, IntoElement, Keystroke, Modifiers, Pixels, Point, Render, ViewContext,
3};
4use smallvec::SmallVec;
5use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf};
6
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub struct KeyDownEvent {
9 pub keystroke: Keystroke,
10 pub is_held: bool,
11}
12
13#[derive(Clone, Debug)]
14pub struct KeyUpEvent {
15 pub keystroke: Keystroke,
16}
17
18#[derive(Clone, Debug, Default)]
19pub struct ModifiersChangedEvent {
20 pub modifiers: Modifiers,
21}
22
23impl Deref for ModifiersChangedEvent {
24 type Target = Modifiers;
25
26 fn deref(&self) -> &Self::Target {
27 &self.modifiers
28 }
29}
30
31/// The phase of a touch motion event.
32/// Based on the winit enum of the same name.
33#[derive(Clone, Copy, Debug)]
34pub enum TouchPhase {
35 Started,
36 Moved,
37 Ended,
38}
39
40#[derive(Clone, Debug, Default)]
41pub struct MouseDownEvent {
42 pub button: MouseButton,
43 pub position: Point<Pixels>,
44 pub modifiers: Modifiers,
45 pub click_count: usize,
46}
47
48#[derive(Clone, Debug, Default)]
49pub struct MouseUpEvent {
50 pub button: MouseButton,
51 pub position: Point<Pixels>,
52 pub modifiers: Modifiers,
53 pub click_count: usize,
54}
55
56#[derive(Clone, Debug, Default)]
57pub struct ClickEvent {
58 pub down: MouseDownEvent,
59 pub up: MouseUpEvent,
60}
61
62pub struct Drag<S, R, V, E>
63where
64 R: Fn(&mut V, &mut ViewContext<V>) -> E,
65 V: 'static,
66 E: IntoElement,
67{
68 pub state: S,
69 pub render_drag_handle: R,
70 view_element_types: PhantomData<(V, E)>,
71}
72
73impl<S, R, V, E> Drag<S, R, V, E>
74where
75 R: Fn(&mut V, &mut ViewContext<V>) -> E,
76 V: 'static,
77 E: Element,
78{
79 pub fn new(state: S, render_drag_handle: R) -> Self {
80 Drag {
81 state,
82 render_drag_handle,
83 view_element_types: Default::default(),
84 }
85 }
86}
87
88#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
89pub enum MouseButton {
90 Left,
91 Right,
92 Middle,
93 Navigate(NavigationDirection),
94}
95
96impl MouseButton {
97 pub fn all() -> Vec<Self> {
98 vec![
99 MouseButton::Left,
100 MouseButton::Right,
101 MouseButton::Middle,
102 MouseButton::Navigate(NavigationDirection::Back),
103 MouseButton::Navigate(NavigationDirection::Forward),
104 ]
105 }
106}
107
108impl Default for MouseButton {
109 fn default() -> Self {
110 Self::Left
111 }
112}
113
114#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
115pub enum NavigationDirection {
116 Back,
117 Forward,
118}
119
120impl Default for NavigationDirection {
121 fn default() -> Self {
122 Self::Back
123 }
124}
125
126#[derive(Clone, Debug, Default)]
127pub struct MouseMoveEvent {
128 pub position: Point<Pixels>,
129 pub pressed_button: Option<MouseButton>,
130 pub modifiers: Modifiers,
131}
132
133impl MouseMoveEvent {
134 pub fn dragging(&self) -> bool {
135 self.pressed_button == Some(MouseButton::Left)
136 }
137}
138
139#[derive(Clone, Debug)]
140pub struct ScrollWheelEvent {
141 pub position: Point<Pixels>,
142 pub delta: ScrollDelta,
143 pub modifiers: Modifiers,
144 pub touch_phase: TouchPhase,
145}
146
147impl Deref for ScrollWheelEvent {
148 type Target = Modifiers;
149
150 fn deref(&self) -> &Self::Target {
151 &self.modifiers
152 }
153}
154
155#[derive(Clone, Copy, Debug)]
156pub enum ScrollDelta {
157 Pixels(Point<Pixels>),
158 Lines(Point<f32>),
159}
160
161impl Default for ScrollDelta {
162 fn default() -> Self {
163 Self::Lines(Default::default())
164 }
165}
166
167impl ScrollDelta {
168 pub fn precise(&self) -> bool {
169 match self {
170 ScrollDelta::Pixels(_) => true,
171 ScrollDelta::Lines(_) => false,
172 }
173 }
174
175 pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
176 match self {
177 ScrollDelta::Pixels(delta) => *delta,
178 ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
179 }
180 }
181}
182
183#[derive(Clone, Debug, Default)]
184pub struct MouseExitEvent {
185 pub position: Point<Pixels>,
186 pub pressed_button: Option<MouseButton>,
187 pub modifiers: Modifiers,
188}
189
190impl Deref for MouseExitEvent {
191 type Target = Modifiers;
192
193 fn deref(&self) -> &Self::Target {
194 &self.modifiers
195 }
196}
197
198#[derive(Debug, Clone, Default)]
199pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
200
201impl ExternalPaths {
202 pub fn paths(&self) -> &[PathBuf] {
203 &self.0
204 }
205}
206
207impl Render for ExternalPaths {
208 fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
209 div() // Intentionally left empty because the platform will render icons for the dragged files
210 }
211}
212
213#[derive(Debug, Clone)]
214pub enum FileDropEvent {
215 Entered {
216 position: Point<Pixels>,
217 files: ExternalPaths,
218 },
219 Pending {
220 position: Point<Pixels>,
221 },
222 Submit {
223 position: Point<Pixels>,
224 },
225 Exited,
226}
227
228#[derive(Clone, Debug)]
229pub enum InputEvent {
230 KeyDown(KeyDownEvent),
231 KeyUp(KeyUpEvent),
232 ModifiersChanged(ModifiersChangedEvent),
233 MouseDown(MouseDownEvent),
234 MouseUp(MouseUpEvent),
235 MouseMove(MouseMoveEvent),
236 MouseExited(MouseExitEvent),
237 ScrollWheel(ScrollWheelEvent),
238 FileDrop(FileDropEvent),
239}
240
241impl InputEvent {
242 pub fn position(&self) -> Option<Point<Pixels>> {
243 match self {
244 InputEvent::KeyDown { .. } => None,
245 InputEvent::KeyUp { .. } => None,
246 InputEvent::ModifiersChanged { .. } => None,
247 InputEvent::MouseDown(event) => Some(event.position),
248 InputEvent::MouseUp(event) => Some(event.position),
249 InputEvent::MouseMove(event) => Some(event.position),
250 InputEvent::MouseExited(event) => Some(event.position),
251 InputEvent::ScrollWheel(event) => Some(event.position),
252 InputEvent::FileDrop(FileDropEvent::Exited) => None,
253 InputEvent::FileDrop(
254 FileDropEvent::Entered { position, .. }
255 | FileDropEvent::Pending { position, .. }
256 | FileDropEvent::Submit { position, .. },
257 ) => Some(*position),
258 }
259 }
260
261 pub fn mouse_event(&self) -> Option<&dyn Any> {
262 match self {
263 InputEvent::KeyDown { .. } => None,
264 InputEvent::KeyUp { .. } => None,
265 InputEvent::ModifiersChanged { .. } => None,
266 InputEvent::MouseDown(event) => Some(event),
267 InputEvent::MouseUp(event) => Some(event),
268 InputEvent::MouseMove(event) => Some(event),
269 InputEvent::MouseExited(event) => Some(event),
270 InputEvent::ScrollWheel(event) => Some(event),
271 InputEvent::FileDrop(event) => Some(event),
272 }
273 }
274
275 pub fn keyboard_event(&self) -> Option<&dyn Any> {
276 match self {
277 InputEvent::KeyDown(event) => Some(event),
278 InputEvent::KeyUp(event) => Some(event),
279 InputEvent::ModifiersChanged(event) => Some(event),
280 InputEvent::MouseDown(_) => None,
281 InputEvent::MouseUp(_) => None,
282 InputEvent::MouseMove(_) => None,
283 InputEvent::MouseExited(_) => None,
284 InputEvent::ScrollWheel(_) => None,
285 InputEvent::FileDrop(_) => None,
286 }
287 }
288}
289
290#[cfg(test)]
291mod test {
292 use crate::{
293 self as gpui, div, Element, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
294 Keystroke, ParentElement, Render, TestAppContext, VisualContext,
295 };
296
297 struct TestView {
298 saw_key_down: bool,
299 saw_action: bool,
300 focus_handle: FocusHandle,
301 }
302
303 actions!(test, [TestAction]);
304
305 impl Render for TestView {
306 fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl Element {
307 div().id("testview").child(
308 div()
309 .key_context("parent")
310 .on_key_down(cx.listener(|this, _, cx| {
311 cx.stop_propagation();
312 this.saw_key_down = true
313 }))
314 .on_action(
315 cx.listener(|this: &mut TestView, _: &TestAction, _| {
316 this.saw_action = true
317 }),
318 )
319 .child(
320 div()
321 .key_context("nested")
322 .track_focus(&self.focus_handle)
323 .into_element(),
324 ),
325 )
326 }
327 }
328
329 #[gpui::test]
330 fn test_on_events(cx: &mut TestAppContext) {
331 let window = cx.update(|cx| {
332 cx.open_window(Default::default(), |cx| {
333 cx.new_view(|cx| TestView {
334 saw_key_down: false,
335 saw_action: false,
336 focus_handle: cx.focus_handle(),
337 })
338 })
339 });
340
341 cx.update(|cx| {
342 cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
343 });
344
345 window
346 .update(cx, |test_view, cx| cx.focus(&test_view.focus_handle))
347 .unwrap();
348
349 cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap(), false);
350 cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap(), false);
351
352 window
353 .update(cx, |test_view, _| {
354 assert!(test_view.saw_key_down || test_view.saw_action);
355 assert!(test_view.saw_key_down);
356 assert!(test_view.saw_action);
357 })
358 .unwrap();
359 }
360}