vizia_core/window/window_event.rs
1use std::path::PathBuf;
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(Debug, 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}
16
17impl From<Entity> for DropData {
18 fn from(value: Entity) -> Self {
19 DropData::Id(value)
20 }
21}
22
23impl From<PathBuf> for DropData {
24 fn from(value: PathBuf) -> Self {
25 DropData::File(value)
26 }
27}
28
29/// Events generated by the application in response to OS events as well as events that can be used
30/// to set properties of the window.
31#[derive(Debug, Clone)]
32pub enum WindowEvent {
33 /// Emitted when a window is closed. Can also be emitted by a view or model to close the window.
34 WindowClose,
35 /// Emitted when the window is moved.
36 WindowMoved(WindowPosition),
37 /// Emitted when a file is dragged and then dropped onto the window.
38 Drop(DropData),
39 /// Emitted when a mouse button is double clicked.
40 MouseDoubleClick(MouseButton),
41 /// Emitted when a mouse button is triple clicked
42 MouseTripleClick(MouseButton),
43 /// Emitted when a mouse button is pressed
44 MouseDown(MouseButton),
45 /// Emitted when a mouse button is released.
46 MouseUp(MouseButton),
47 /// Emitted when the primary mouse button or trigger key is pressed and then released on a view
48 Press {
49 /// Whether the press event was triggered by the mouse.
50 mouse: bool,
51 },
52 /// Emitted when the primary mouse button or trigger key is pressed on a view
53 PressDown {
54 /// Whether the press down event was triggered by the mouse.
55 mouse: bool,
56 },
57 /// Emitted when the mouse cursor is moved
58 MouseMove(f32, f32),
59 /// Emitted when the mouse scroll wheel is scrolled.
60 MouseScroll(f32, f32),
61 /// Emitted when the mouse cursor enters the bounding box of an entity.
62 MouseOver,
63 /// Emitted when the mouse cursor leaves the bounding box of an entity.
64 MouseOut,
65 /// Emitted when the mouse cursor enters an entity.
66 MouseEnter,
67 /// Emitted when the mouse cursor leaves an entity.
68 MouseLeave,
69 /// Emitted when an entity gains keyboard focus.
70 FocusIn,
71 /// Emitted when an entity loses keyboard focus.
72 FocusOut,
73 /// Emitted when an entity's focus visibility has changed.
74 FocusVisibility(bool),
75 /// Emitted when the window gains or loses focus
76 WindowFocused(bool),
77 /// Emitted when a character is typed.
78 CharInput(char),
79 /// Emitted when the input method (IME) is activated or deactivated.
80 ImeActivate(bool),
81 /// Emitted when an input method (IME) commits a string.
82 ImeCommit(String),
83 /// Emitted when an input method (IME) changes the preedit string.
84 ImePreedit(String, Option<(usize, usize)>),
85 /// Emitted when the input method (IME) area needs to be updated.
86 SetImeCursorArea((u32, u32), (u32, u32)),
87 /// Emitted when a keyboard key is pressed.
88 KeyDown(Code, Option<Key>),
89 /// Emitted when a keyboard key is released.
90 KeyUp(Code, Option<Key>),
91 /// Emited when the system window theme has changed.
92 ThemeChanged(ThemeMode),
93 /// Sets the mouse cursor icon.
94 SetCursor(CursorIcon),
95 /// Grabs the mouse cursor, preventing it from leaving the window.
96 GrabCursor(bool),
97 /// Sets the (x,y) position of the mouse cursor in window coordinates.
98 SetCursorPosition(u32, u32),
99 /// Sets the title of the window.
100 SetTitle(String),
101 /// Sets the size of the window.
102 SetSize(WindowSize),
103 /// Sets the position of the window.
104 SetPosition(WindowPosition),
105 /// Sets the maximum size of the window.
106 SetMaxSize(Option<WindowSize>),
107 /// Sets the minimum size of the window.
108 SetMinSize(Option<WindowSize>),
109 /// Sets whether the window is resizable.
110 SetResizable(bool),
111 /// Sets whether the window is minimized.
112 SetMinimized(bool),
113 /// Sets whether the window is maximized.
114 SetMaximized(bool),
115 /// Sets whether the window is visible.
116 SetVisible(bool),
117 /// Sets whether the window has decorations.
118 SetDecorations(bool),
119 /// Sets whether the window remains on top of other windows.
120 SetAlwaysOnTop(bool),
121 /// Emitted when mouse events have been captured.
122 MouseCaptureEvent,
123 /// Emitted when mouse events have been released.
124 MouseCaptureOutEvent,
125 // TODO: check if this includes margins + borders.
126 /// Emitted when an entity changes position or size.
127 GeometryChanged(GeoChanged),
128 /// Requests a redraw of the window contents.
129 Redraw,
130 /// Request a restyle.
131 Restyle,
132 /// Requests a relayout.
133 Relayout,
134 /// Move keyboard focus to the next navigable view.
135 FocusNext,
136 /// Move keyboard focus to the previous navigable view.
137 FocusPrev,
138 /// Prints the debug message to the console.
139 Debug(String),
140 /// Represents an action requested by an accessibility technology.
141 ActionRequest(accesskit::ActionRequest),
142 /// Reloads all application stylesheets.
143 ReloadStyles,
144 /// Enables or disabled mouse and keyboard input to the window.
145 SetEnabled(bool),
146 /// Puts the window in a drag state.
147 DragWindow,
148
149 /// Emitted when the window is destroyed.
150 Destroyed,
151}