1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Quickly create ux-dx applications.
#![allow(unused_variables)]
use std::{fmt::Debug, path::PathBuf};
pub use warmy::Store;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{
AxisId, DeviceId, ElementState, KeyboardInput, ModifiersState, MouseButton,
MouseScrollDelta, Touch, TouchPhase, VirtualKeyCode,
},
event_loop::ControlFlow,
window::Theme,
};
pub use crate::{resource::key::Key, time::Time};
pub mod error;
pub mod runner;
/// Class of simple applications.
///
/// Simple application is basically just a single function that takes the current time and display something.
pub trait Application<Runner>: Sized {
/// Context carried around with the application.
type Context;
/// Initialization error that might occur.
type Error: Sized + Debug;
/// Initialize the application with a given store.
///
/// The runner is passed so that specific initialization is possible.
fn init(
runner: &mut Runner,
store: &mut Store<Self::Context, Key>,
context: &mut Self::Context,
) -> Result<Self, Self::Error>;
/// Resize the application when the framebuffer gets resized.
///
/// The runner is passed so that specific resizing is possible.
fn resized(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
width: u32,
height: u32,
);
// TODO: need to implement user-event logic for UserEvent(T)
// Emitted when an event is sent from [`EventLoopProxy::send_event`](crate::event_loop::EventLoopProxy::send_event)
/// Notify the application has been suspended.
fn suspend(&mut self, runner: &mut Runner, context: &mut Self::Context) {}
/// Notify the application has been resumed.
fn resume(&mut self, runner: &mut Runner, context: &mut Self::Context) {}
/// The position of the window has changed. Contains the window's new position.
fn moved(&mut self, runner: &mut Runner, context: &mut Self::Context, x: i32, y: i32) {}
/// The window has been requested to close.
///
/// By default application will exit
fn close(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
control_flow: &mut ControlFlow,
) {
*control_flow = ControlFlow::Exit
}
/// The window has been destroyed.
fn destroyed(&mut self, runner: &mut Runner, context: &mut Self::Context) {}
/// A file has been dropped into the window.
///
/// When the user drops multiple files at once, this event will be emitted for each file
/// separately.
fn dropped_file(&mut self, runner: &mut Runner, context: &mut Self::Context, path: &PathBuf) {}
/// A file is being hovered over the window.
///
/// When the user hovers multiple files at once, this event will be emitted for each file
/// separately.
fn hovered_file(&mut self, runner: &mut Runner, context: &mut Self::Context, path: &PathBuf) {}
/// A file was hovered, but has exited the window.
///
/// There will be a single `HoveredFileCancelled` event triggered even if multiple files were
/// hovered.
fn hovered_file_cancelled(&mut self, runner: &mut Runner, context: &mut Self::Context) {}
/// The window received a unicode character.
fn received_character(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
character: char,
) {
}
/// The window gained or lost focus.
///
/// The parameter is true if the window has gained focus, and false if it has lost focus.
fn focused(&mut self, runner: &mut Runner, context: &mut Self::Context, is_focused: bool) {}
/// An event from the keyboard has been received.
///
/// If `true`, the event was generated synthetically by winit
/// in one of the following circumstances:
///
/// * Synthetic key press events are generated for all keys pressed
/// when a window gains focus. Likewise, synthetic key release events
/// are generated for all keys pressed when a window goes out of focus.
/// ***Currently, this is only functional on X11 and Windows***
///
/// Otherwise, this value is always `false`.
fn keyboard_input(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
control_flow: &mut ControlFlow,
device_id: &DeviceId,
input: &KeyboardInput,
is_synthetic: bool,
) {
match input {
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
} => *control_flow = ControlFlow::Exit,
_ => {}
}
}
/// The keyboard modifiers have changed.
///
/// Platform-specific behavior:
/// - **Web**: This API is currently unimplemented on the web. This isn't by design - it's an
/// issue, and it should get fixed - but it's the current state of the API.
fn modifiers_changed(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
state: &ModifiersState,
) {
}
/// The cursor has moved on the window.
///
/// (x,y) coords in pixels relative to the top-left corner of the window. Because the range of this data is
/// limited by the display area and it may have been transformed by the OS to implement effects such as cursor
/// acceleration, it should not be used to implement non-cursor-like interactions such as 3D camera control.
fn cursor_moved(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
position: &PhysicalPosition<f64>,
) {
}
/// The cursor has entered the window.
fn cursor_entered(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
) {
}
/// The cursor has left the window.
fn cursor_left(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
) {
}
/// A mouse wheel movement or touchpad scroll occurred.
fn mouse_wheel(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
delta: &MouseScrollDelta,
phase: &TouchPhase,
) {
}
/// An mouse button press has been received.
fn mouse_input(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
state: &ElementState,
button: &MouseButton,
) {
}
/// Touchpad pressure event.
///
/// At the moment, only supported on Apple forcetouch-capable macbooks.
/// The parameters are: pressure level (value between 0 and 1 representing how hard the touchpad
/// is being pressed) and stage (integer representing the click level).
fn touchpad_pressure(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
pressure: f32,
stage: i64,
) {
}
/// Motion on some analog axis. May report data redundant to other, more specific events.
fn axis_motion(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
device_id: &DeviceId,
axis: &AxisId,
value: f64,
) {
}
/// Touch event has been received
fn touch(&mut self, runner: &mut Runner, context: &mut Self::Context, touch: &Touch) {}
/// The window's scale factor has changed.
///
/// The following user actions can cause DPI changes:
///
/// * Changing the display's resolution.
/// * Changing the display's scale factor (e.g. in Control Panel on Windows).
/// * Moving the window to a display with a different scale factor.
///
/// After this event callback has been processed, the window will be resized to whatever value
/// is pointed to by the `new_inner_size` reference. By default, this will contain the size suggested
/// by the OS, but it can be changed to any value.
///
/// For more information about DPI in general, see the [`dpi`](crate::dpi) module.
fn scale_factor_changed(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
scale_factor: f64,
new_inner_size: &&mut PhysicalSize<u32>,
) {
}
/// The system window theme has changed.
///
/// Applications might wish to react to this to change the theme of the content of the window
/// when the system changes the window theme.
///
/// At the moment this is only supported on Windows.
fn theme_changed(&mut self, runner: &mut Runner, context: &mut Self::Context, theme: &Theme) {}
/// Render the application at a given time.
///
/// The runner is passed so that specific rendering is possible.
/// return `agressive` state. true for applications which FPS aggressive,
/// like a games or animation/media oriented applications.
/// also you can change the behaviour for your needs.
fn render(
&mut self,
runner: &mut Runner,
context: &mut Self::Context,
t: Time,
// back_buffer: &Backbuffer,
// builder: Builder,
) -> bool;
}