Skip to main content

baseview/
event.rs

1use std::path::PathBuf;
2
3use keyboard_types::{KeyboardEvent, Modifiers};
4
5use crate::{Point, WindowInfo};
6
7#[derive(Debug, Copy, Clone, Eq, PartialEq)]
8pub enum MouseButton {
9    Left,
10    Middle,
11    Right,
12    Back,
13    Forward,
14    Other(u8),
15}
16
17/// A scroll movement.
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub enum ScrollDelta {
20    /// A line-based scroll movement
21    Lines {
22        /// The number of horizontal lines scrolled
23        x: f32,
24
25        /// The number of vertical lines scrolled
26        y: f32,
27    },
28    /// A pixel-based scroll movement
29    Pixels {
30        /// The number of horizontal pixels scrolled
31        x: f32,
32        /// The number of vertical pixels scrolled
33        y: f32,
34    },
35}
36
37#[derive(Debug, Clone, PartialEq)]
38pub enum MouseEvent {
39    /// The mouse cursor was moved
40    CursorMoved {
41        /// The logical coordinates of the mouse position
42        position: Point,
43        /// The modifiers that were held down just before the event.
44        modifiers: Modifiers,
45    },
46
47    /// A mouse button was pressed.
48    ButtonPressed {
49        /// The button that was pressed.
50        button: MouseButton,
51        /// The modifiers that were held down just before the event.
52        modifiers: Modifiers,
53    },
54
55    /// A mouse button was released.
56    ButtonReleased {
57        /// The button that was released.
58        button: MouseButton,
59        /// The modifiers that were held down just before the event.
60        modifiers: Modifiers,
61    },
62
63    /// The mouse wheel was scrolled.
64    WheelScrolled {
65        /// How much was scrolled, in factional lines.
66        delta: ScrollDelta,
67        /// The modifiers that were held down just before the event.
68        modifiers: Modifiers,
69    },
70
71    /// The mouse cursor entered the window.
72    ///
73    /// May not be available on all platforms.
74    CursorEntered,
75
76    /// The mouse cursor left the window.
77    ///
78    /// May not be available on all platforms.
79    CursorLeft,
80
81    DragEntered {
82        /// The logical coordinates of the mouse position
83        position: Point,
84        /// The modifiers that were held down just before the event.
85        modifiers: Modifiers,
86        /// Data being dragged
87        data: DropData,
88    },
89
90    DragMoved {
91        /// The logical coordinates of the mouse position
92        position: Point,
93        /// The modifiers that were held down just before the event.
94        modifiers: Modifiers,
95        /// Data being dragged
96        data: DropData,
97    },
98
99    DragLeft,
100
101    DragDropped {
102        /// The logical coordinates of the mouse position
103        position: Point,
104        /// The modifiers that were held down just before the event.
105        modifiers: Modifiers,
106        /// Data being dragged
107        data: DropData,
108    },
109}
110
111#[derive(Debug, Clone)]
112pub enum WindowEvent {
113    Resized(WindowInfo),
114    Focused,
115    Unfocused,
116    WillClose,
117}
118
119#[derive(Debug, Clone)]
120pub enum Event {
121    Mouse(MouseEvent),
122    Keyboard(KeyboardEvent),
123    Window(WindowEvent),
124}
125
126#[derive(Debug, Clone, Copy, PartialEq)]
127pub enum DropEffect {
128    Copy,
129    Move,
130    Link,
131    Scroll,
132}
133
134#[derive(Debug, Clone, PartialEq)]
135pub enum DropData {
136    None,
137    Files(Vec<PathBuf>),
138}
139
140/// Return value for [WindowHandler::on_event](`crate::WindowHandler::on_event()`),
141/// indicating whether the event was handled by your window or should be passed
142/// back to the platform.
143///
144/// For most event types, this value won't have any effect. This is the case
145/// when there is no clear meaning of passing back the event to the platform,
146/// or it isn't obviously useful. Currently, only [`Event::Keyboard`] variants
147/// are supported.
148#[derive(Debug, Clone, Copy, PartialEq)]
149pub enum EventStatus {
150    /// Event was handled by your window and will not be sent back to the
151    /// platform for further processing.
152    Captured,
153    /// Event was **not** handled by your window, so pass it back to the
154    /// platform. For parented windows, this usually means that the parent
155    /// window will receive the event. This is useful for cases such as using
156    /// DAW functionality for playing piano keys with the keyboard while a
157    /// plugin window is in focus.
158    Ignored,
159    /// We are prepared to handle the data in the drag and dropping will
160    /// result in [DropEffect]
161    AcceptDrop(DropEffect),
162}