vizia_core/window/window_event.rs
1use std::{any::Any, fmt, path::PathBuf, sync::Arc};
2
3use crate::{entity::Entity, environment::ThemeMode, layout::cache::GeoChanged};
4use vizia_input::{Code, Key, MouseButton};
5use vizia_style::CursorIcon;
6use vizia_window::{WindowPosition, WindowSize};
7
8#[derive(Clone)]
9/// Data associated with a drop event.
10pub enum DropData {
11 /// Path to a dropped file.
12 File(PathBuf),
13 /// Entity ID of a dropped entity.
14 Id(Entity),
15 /// Arbitrary user payload for app-defined drag/drop operations.
16 Any(Arc<dyn Any + Send + Sync>),
17}
18
19impl fmt::Debug for DropData {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 DropData::File(path) => f.debug_tuple("File").field(path).finish(),
23 DropData::Id(entity) => f.debug_tuple("Id").field(entity).finish(),
24 DropData::Any(_) => f.debug_tuple("Any").field(&"<opaque>").finish(),
25 }
26 }
27}
28
29impl DropData {
30 /// Creates a drop payload from arbitrary application-defined data.
31 pub fn any<T: Any + Send + Sync>(value: T) -> Self {
32 Self::Any(Arc::new(value))
33 }
34
35 /// Attempts to downcast arbitrary drop payload data by reference.
36 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
37 match self {
38 DropData::Any(value) => value.downcast_ref::<T>(),
39 _ => None,
40 }
41 }
42}
43
44impl From<Entity> for DropData {
45 fn from(value: Entity) -> Self {
46 DropData::Id(value)
47 }
48}
49
50impl From<PathBuf> for DropData {
51 fn from(value: PathBuf) -> Self {
52 DropData::File(value)
53 }
54}
55
56impl From<Arc<dyn Any + Send + Sync>> for DropData {
57 fn from(value: Arc<dyn Any + Send + Sync>) -> Self {
58 DropData::Any(value)
59 }
60}
61
62/// Events generated by the application in response to OS events as well as events that can be used
63/// to set properties of the window.
64#[derive(Debug, Clone)]
65pub enum WindowEvent {
66 /// Emitted when a window is closed. Can also be emitted by a view or model to close the window.
67 WindowClose,
68 /// Emitted when the window is moved.
69 WindowMoved(WindowPosition),
70 /// Emitted when a file is dragged and then dropped onto the window.
71 Drop(DropData),
72 /// Emitted when a mouse button is double clicked.
73 MouseDoubleClick(MouseButton),
74 /// Emitted when a mouse button is triple clicked
75 MouseTripleClick(MouseButton),
76 /// Emitted when a mouse button is pressed
77 MouseDown(MouseButton),
78 /// Emitted when a mouse button is released.
79 MouseUp(MouseButton),
80 /// Emitted when the primary mouse button or trigger key is pressed and then released on a view
81 Press {
82 /// Whether the press event was triggered by the mouse.
83 mouse: bool,
84 },
85 /// Emitted when the primary mouse button or trigger key is pressed on a view
86 PressDown {
87 /// Whether the press down event was triggered by the mouse.
88 mouse: bool,
89 },
90 /// Emitted when the mouse cursor is moved
91 MouseMove(f32, f32),
92 /// Emitted when the mouse scroll wheel is scrolled.
93 MouseScroll(f32, f32),
94 /// Emitted when the mouse cursor enters the bounding box of an entity.
95 MouseOver,
96 /// Emitted when the mouse cursor leaves the bounding box of an entity.
97 MouseOut,
98 /// Emitted when the mouse cursor enters an entity.
99 MouseEnter,
100 /// Emitted when the mouse cursor leaves an entity.
101 MouseLeave,
102 /// Emitted when drag data enters the hovered entity.
103 DragEnter,
104 /// Emitted when drag data leaves the previously hovered entity.
105 DragLeave,
106 /// Emitted when drag data moves over the currently hovered entity.
107 DragMove(f32, f32),
108 /// Emitted when an entity gains keyboard focus.
109 FocusIn,
110 /// Emitted when an entity loses keyboard focus.
111 FocusOut,
112 /// Emitted when an entity's focus visibility has changed.
113 FocusVisibility(bool),
114 /// Emitted when the window gains or loses focus
115 WindowFocused(bool),
116 /// Emitted when a character is typed.
117 CharInput(char),
118 /// Emitted when the input method (IME) is activated or deactivated.
119 ImeActivate(bool),
120 /// Emitted when an input method (IME) commits a string.
121 ImeCommit(String),
122 /// Emitted when an input method (IME) changes the preedit string.
123 ImePreedit(String, Option<(usize, usize)>),
124 /// Emitted when the input method (IME) area needs to be updated.
125 SetImeCursorArea((u32, u32), (u32, u32)),
126 /// Emitted when a keyboard key is pressed.
127 KeyDown(Code, Option<Key>),
128 /// Emitted when a keyboard key is released.
129 KeyUp(Code, Option<Key>),
130 /// Emited when the system window theme has changed.
131 ThemeChanged(ThemeMode),
132 /// Sets the mouse cursor icon.
133 SetCursor(CursorIcon),
134 /// Grabs the mouse cursor, preventing it from leaving the window.
135 GrabCursor(bool),
136 /// Sets the (x,y) position of the mouse cursor in window coordinates.
137 SetCursorPosition(u32, u32),
138 /// Sets the title of the window.
139 SetTitle(String),
140 /// Sets the size of the window.
141 SetSize(WindowSize),
142 /// Sets the user scale factor applied on top of the system DPI
143 /// scale. The windowing backend resizes the window and updates
144 /// vizia's internal scale + bounds in lockstep so every
145 /// `Pixels(N)` value (layout, font sizes) reflows at the new
146 /// scale on the next layout pass. Currently honoured by the
147 /// `vizia_baseview` backend; the `vizia_winit` backend ignores
148 /// it.
149 SetUserScale(f64),
150 /// Sets the position of the window.
151 SetPosition(WindowPosition),
152 /// Sets the maximum size of the window.
153 SetMaxSize(Option<WindowSize>),
154 /// Sets the minimum size of the window.
155 SetMinSize(Option<WindowSize>),
156 /// Sets whether the window is resizable.
157 SetResizable(bool),
158 /// Sets whether the window is minimized.
159 SetMinimized(bool),
160 /// Sets whether the window is maximized.
161 SetMaximized(bool),
162 /// Sets whether the window is visible.
163 SetVisible(bool),
164 /// Sets whether the window has decorations.
165 SetDecorations(bool),
166 /// Sets whether the window remains on top of other windows.
167 SetAlwaysOnTop(bool),
168 /// Emitted when mouse events have been captured.
169 MouseCaptureEvent,
170 /// Emitted when mouse events have been released.
171 MouseCaptureOutEvent,
172 // TODO: check if this includes margins + borders.
173 /// Emitted when an entity changes position or size.
174 GeometryChanged(GeoChanged),
175 /// Requests a redraw of the window contents.
176 Redraw,
177 /// Request a restyle.
178 Restyle,
179 /// Requests a relayout.
180 Relayout,
181 /// Move keyboard focus to the next navigable view.
182 FocusNext,
183 /// Move keyboard focus to the previous navigable view.
184 FocusPrev,
185 /// Prints the debug message to the console.
186 Debug(String),
187 /// Represents an action requested by an accessibility technology.
188 ActionRequest(accesskit::ActionRequest),
189 /// Reloads all application stylesheets.
190 ReloadStyles,
191 /// Enables or disabled mouse and keyboard input to the window.
192 SetEnabled(bool),
193 /// Puts the window in a drag state.
194 DragWindow,
195
196 /// Emitted when the window is destroyed.
197 Destroyed,
198}