yakui_core/event.rs
1//! Defines the events that can be sent to yakui and handled by widgets.
2
3use glam::Vec2;
4
5use crate::geometry::Rect;
6use crate::input::{KeyCode, Modifiers, MouseButton};
7
8/// An event that can be handled by yakui.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Event {
12 /// The viewport has changed. This can mean resizing as well as positioning.
13 ViewportChanged(Rect),
14
15 /// The mouse cursor moved. If `None`, indicates that the mouse moved
16 /// outside the window.
17 CursorMoved(Option<Vec2>),
18
19 /// A mouse button changed, telling whether it is now pressed.
20 MouseButtonChanged {
21 /// Which mouse button was changed.
22 button: MouseButton,
23
24 /// Whether the button is now down.
25 down: bool,
26 },
27
28 /// The user scrolled with the mouse.
29 MouseScroll {
30 /// How far the mouse scrolled in physical pixels.
31 delta: Vec2,
32 },
33
34 /// A key changed, telling whether it is now pressed.
35 KeyChanged {
36 /// Which key's state was changed.
37 key: KeyCode,
38
39 /// Whether the key is now down.
40 down: bool,
41 },
42
43 /// The state of the keyboard modifiers keys changed.
44 ModifiersChanged(Modifiers),
45
46 /// A Unicode codepoint was typed in the window.
47 TextInput(char),
48}
49
50/// An event that can be handled by an individual widget.
51#[derive(Debug)]
52#[non_exhaustive]
53pub enum WidgetEvent {
54 /// The mouse entered the widget's layout rectangle.
55 MouseEnter,
56
57 /// The mouse left the widget's layout rectangle.
58 MouseLeave,
59
60 /// The mouse moved.
61 MouseMoved(Option<Vec2>),
62
63 /// The user scrolled with the mouse.
64 MouseScroll {
65 /// How much the wheel scrolled in logical pixels.
66 delta: Vec2,
67 },
68
69 /// A mouse button changed state while the cursor was inside the widget's
70 /// layout rectangle.
71 #[non_exhaustive]
72 MouseButtonChanged {
73 /// Which button was changed.
74 button: MouseButton,
75
76 /// Whether the button is down or up.
77 down: bool,
78
79 /// Whether the button is inside the widget's layout rectangle.
80 inside: bool,
81
82 /// The position of the mouse cursor at the time of the event.
83 position: Vec2,
84
85 /// The current state of the keyboard modifier keys.
86 modifiers: Modifiers,
87 },
88
89 /// A keyboard key changed.
90 #[non_exhaustive]
91 KeyChanged {
92 /// Which key was changed.
93 key: KeyCode,
94
95 /// Whether the key is down or up.
96 down: bool,
97
98 /// The current state of the keyboard modifier keys.
99 modifiers: Modifiers,
100 },
101
102 /// Text was sent to the widget.
103 TextInput(char),
104
105 /// The widget was focused or unfocused.
106 FocusChanged(bool),
107}
108
109/// Responses that can be given to an event.
110#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub enum EventResponse {
112 /// Bubble the event. This gives other widgets or the application the chance
113 /// to process the event.
114 Bubble,
115
116 /// Sink the event. This stops the event from propagating and tells the host
117 /// application that it should not consider the event.
118 Sink,
119}
120
121bitflags::bitflags! {
122 /// A bitfield of events that a widget can register to be notified about.
123 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy, Default)]
124 pub struct EventInterest: u8 {
125 /// Notify this widget of mouse events occuring within its layout
126 /// rectangle.
127 const MOUSE_INSIDE = 1;
128
129 /// Notify this widget of mouse events occuring outside its layout
130 /// rectangle.
131 const MOUSE_OUTSIDE = 2;
132
133 /// Notify this widget whenever the mouse cursor moves.
134 const MOUSE_MOVE = 4;
135
136 /// This widget can be focused.
137 const FOCUS = 8;
138
139 /// If this widget is focused, it should receive keyboard events.
140 const FOCUSED_KEYBOARD = 16;
141
142 /// Notify this widget of all mouse events.
143 const MOUSE_ALL = Self::MOUSE_INSIDE.bits() | Self::MOUSE_OUTSIDE.bits() | Self::MOUSE_MOVE.bits();
144 }
145}