1use crate::{
2 div, point, Div, 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 type Element = Div;
209
210 fn render(&mut self, _: &mut ViewContext<Self>) -> Self::Element {
211 div() // Intentionally left empty because the platform will render icons for the dragged files
212 }
213}
214
215#[derive(Debug, Clone)]
216pub enum FileDropEvent {
217 Entered {
218 position: Point<Pixels>,
219 files: ExternalPaths,
220 },
221 Pending {
222 position: Point<Pixels>,
223 },
224 Submit {
225 position: Point<Pixels>,
226 },
227 Exited,
228}
229
230#[derive(Clone, Debug)]
231pub enum InputEvent {
232 KeyDown(KeyDownEvent),
233 KeyUp(KeyUpEvent),
234 ModifiersChanged(ModifiersChangedEvent),
235 MouseDown(MouseDownEvent),
236 MouseUp(MouseUpEvent),
237 MouseMove(MouseMoveEvent),
238 MouseExited(MouseExitEvent),
239 ScrollWheel(ScrollWheelEvent),
240 FileDrop(FileDropEvent),
241}
242
243impl InputEvent {
244 pub fn position(&self) -> Option<Point<Pixels>> {
245 match self {
246 InputEvent::KeyDown { .. } => None,
247 InputEvent::KeyUp { .. } => None,
248 InputEvent::ModifiersChanged { .. } => None,
249 InputEvent::MouseDown(event) => Some(event.position),
250 InputEvent::MouseUp(event) => Some(event.position),
251 InputEvent::MouseMove(event) => Some(event.position),
252 InputEvent::MouseExited(event) => Some(event.position),
253 InputEvent::ScrollWheel(event) => Some(event.position),
254 InputEvent::FileDrop(FileDropEvent::Exited) => None,
255 InputEvent::FileDrop(
256 FileDropEvent::Entered { position, .. }
257 | FileDropEvent::Pending { position, .. }
258 | FileDropEvent::Submit { position, .. },
259 ) => Some(*position),
260 }
261 }
262
263 pub fn mouse_event<'a>(&'a self) -> Option<&'a dyn Any> {
264 match self {
265 InputEvent::KeyDown { .. } => None,
266 InputEvent::KeyUp { .. } => None,
267 InputEvent::ModifiersChanged { .. } => None,
268 InputEvent::MouseDown(event) => Some(event),
269 InputEvent::MouseUp(event) => Some(event),
270 InputEvent::MouseMove(event) => Some(event),
271 InputEvent::MouseExited(event) => Some(event),
272 InputEvent::ScrollWheel(event) => Some(event),
273 InputEvent::FileDrop(event) => Some(event),
274 }
275 }
276
277 pub fn keyboard_event<'a>(&'a self) -> Option<&'a dyn Any> {
278 match self {
279 InputEvent::KeyDown(event) => Some(event),
280 InputEvent::KeyUp(event) => Some(event),
281 InputEvent::ModifiersChanged(event) => Some(event),
282 InputEvent::MouseDown(_) => None,
283 InputEvent::MouseUp(_) => None,
284 InputEvent::MouseMove(_) => None,
285 InputEvent::MouseExited(_) => None,
286 InputEvent::ScrollWheel(_) => None,
287 InputEvent::FileDrop(_) => None,
288 }
289 }
290}
291
292#[cfg(test)]
293mod test {
294 use crate::{
295 self as gpui, div, Div, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
296 Keystroke, ParentElement, Render, Stateful, TestAppContext, VisualContext,
297 };
298
299 struct TestView {
300 saw_key_down: bool,
301 saw_action: bool,
302 focus_handle: FocusHandle,
303 }
304
305 actions!(test, [TestAction]);
306
307 impl Render for TestView {
308 type Element = Stateful<Div>;
309
310 fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
311 div().id("testview").child(
312 div()
313 .key_context("parent")
314 .on_key_down(cx.listener(|this, _, _| this.saw_key_down = true))
315 .on_action(
316 cx.listener(|this: &mut TestView, _: &TestAction, _| {
317 this.saw_action = true
318 }),
319 )
320 .child(
321 div()
322 .key_context("nested")
323 .track_focus(&self.focus_handle)
324 .into_element(),
325 ),
326 )
327 }
328 }
329
330 #[gpui::test]
331 fn test_on_events(cx: &mut TestAppContext) {
332 let window = cx.update(|cx| {
333 cx.open_window(Default::default(), |cx| {
334 cx.build_view(|cx| TestView {
335 saw_key_down: false,
336 saw_action: false,
337 focus_handle: cx.focus_handle(),
338 })
339 })
340 });
341
342 cx.update(|cx| {
343 cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
344 });
345
346 window
347 .update(cx, |test_view, cx| cx.focus(&test_view.focus_handle))
348 .unwrap();
349
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}