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
use freya_core::{
elements::rect::Rect,
prelude::{
Event,
EventHandlersExt,
EventsCombos,
GlobalUserEvent,
MouseButton,
Platform,
PlatformWindow,
PointerEventData,
},
user_event::SingleThreadErasedEvent,
};
use freya_engine::prelude::Surface as SkiaSurface;
use winit::window::{
Window,
WindowId,
};
use crate::{
config::WindowConfig,
renderer::{
NativePlatformErasedEventAction,
RendererContext,
},
};
/// Extension trait that adds winit-specific window management capabilities to [`Platform`].
pub trait WinitPlatformExt {
/// Get the [`WindowId`] of the current window.
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// fn close_current_window() {
/// Platform::get().close_window(Platform::window_id());
/// }
/// ```
fn window_id() -> WindowId;
/// Dynamically launch a new window at runtime with the given configuration.
///
/// This is meant to create windows on the fly after the application has started,
/// as opposed to the initial windows registered via [`crate::config::LaunchConfig`].
///
/// Returns the [`WindowId`] of the newly created window once it has been created.
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// async fn open_new_window() {
/// let window_id = Platform::get()
/// .launch_window(WindowConfig::new(my_app).with_title("New Window"))
/// .await;
/// }
/// # fn my_app() -> impl IntoElement { rect() }
/// ```
fn launch_window(&self, window_config: WindowConfig) -> impl Future<Output = WindowId>;
/// Close an existing window by its [`WindowId`].
///
/// # Example
///
/// ```rust,no_run
/// use freya::{
/// prelude::*,
/// winit::window::WindowId,
/// };
///
/// fn close_window(window_id: WindowId) {
/// Platform::get().close_window(window_id);
/// }
/// ```
fn close_window(&self, window_id: WindowId);
/// Focus a window by its [`WindowId`].
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// fn focus_current_window() {
/// Platform::get().focus_window(Platform::window_id());
/// }
/// ```
fn focus_window(&self, window_id: WindowId);
/// Set the title of a window, also updating its accessibility label.
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// fn rename_current_window() {
/// Platform::get().set_window_title(Platform::window_id(), "New Title");
/// }
/// ```
fn set_window_title(&self, window_id: WindowId, title: impl Into<String>);
/// Execute a callback with mutable access to a [`Window`].
///
/// This allows direct manipulation of the underlying winit [`Window`] for advanced use cases.
///
/// To create new windows dynamically, see [`WinitPlatformExt::launch_window()`].
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// fn minimize_current_window() {
/// Platform::get().with_window(Platform::window_id(), |window| {
/// window.set_minimized(true);
/// });
/// }
/// ```
fn with_window(&self, window_id: WindowId, callback: impl FnOnce(&mut Window) + 'static);
/// Queue a renderer callback without an originating window.
/// The returned receiver can be awaited for its result or dropped.
fn post_callback<F, T: 'static>(&self, callback: F) -> futures_channel::oneshot::Receiver<T>
where
F: FnOnce(&mut RendererContext) -> T + 'static;
}
/// Winit APIs for the selected window, not necessarily the focused window.
pub trait WinitPlatformWindowExt {
/// Queue a callback on this window's next render pass, after rendering and before presenting.
///
/// # Example
///
/// ```rust,no_run
/// use freya::prelude::*;
///
/// async fn take_screenshot() {
/// let image = Platform::get()
/// .current_window()
/// .post_render_callback(|surface| surface.image_snapshot())
/// .await;
/// }
/// ```
fn post_render_callback<F, T: 'static>(
&self,
callback: F,
) -> futures_channel::oneshot::Receiver<T>
where
F: FnOnce(&mut SkiaSurface) -> T + 'static;
}
#[derive(Clone, Copy, PartialEq)]
struct WindowDragGesture;
pub trait WindowDragExt {
/// Drag on left press and move, or toggle maximize on a double press.
fn window_drag(self) -> Self;
}
impl WindowDragExt for Rect {
fn window_drag(self) -> Self {
self.on_pointer_down(|event: Event<PointerEventData>| {
if event.button() != Some(MouseButton::Left) {
return;
}
if EventsCombos::<WindowDragGesture>::pressed(event.global_location()).is_double() {
Platform::get().with_window(Platform::window_id(), |window| {
window.set_maximized(!window.is_maximized())
});
}
})
.on_global_pointer_move(|event: Event<PointerEventData>| {
if EventsCombos::<WindowDragGesture>::moved(event.global_location()) {
Platform::get().with_window(Platform::window_id(), |window| {
let _ = window.drag_window();
});
}
})
.on_global_pointer_up(|_: Event<PointerEventData>| {
EventsCombos::<WindowDragGesture>::released();
})
}
}
impl WinitPlatformExt for Platform {
fn window_id() -> WindowId {
Platform::get().current_window().id.into()
}
async fn launch_window(&self, window_config: WindowConfig) -> WindowId {
let (sender, receiver) = futures_channel::oneshot::channel();
self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::LaunchWindow {
window_config: Box::new(window_config),
ack: sender,
},
))));
receiver.await.expect("Failed to create window")
}
fn close_window(&self, window_id: WindowId) {
self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::CloseWindow(window_id),
))));
}
fn focus_window(&self, window_id: WindowId) {
self.with_window(window_id, |window| window.focus_window());
}
fn set_window_title(&self, window_id: WindowId, title: impl Into<String>) {
let title = title.into();
self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
if let Some(app) = context.windows.get_mut(&window_id) {
app.set_title(&title);
}
})),
))));
}
fn with_window(&self, window_id: WindowId, callback: impl FnOnce(&mut Window) + 'static) {
self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
if let Some(app) = context.windows.get_mut(&window_id) {
callback(&mut app.window);
}
})),
))));
}
fn post_callback<F, T: 'static>(&self, callback: F) -> futures_channel::oneshot::Receiver<T>
where
F: FnOnce(&mut RendererContext) -> T + 'static,
{
let (sender, receiver) = futures_channel::oneshot::channel();
self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
let _ = sender.send(callback(context));
})),
))));
receiver
}
}
impl WinitPlatformWindowExt for PlatformWindow {
fn post_render_callback<F, T: 'static>(
&self,
callback: F,
) -> futures_channel::oneshot::Receiver<T>
where
F: FnOnce(&mut SkiaSurface) -> T + 'static,
{
let window_id = self.id.into();
let (sender, receiver) = futures_channel::oneshot::channel();
Platform::get().send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
if let Some(app) = context.windows.get_mut(&window_id) {
app.render_callbacks.push(Box::new(move |surface| {
let _ = sender.send(callback(surface));
}));
app.window.request_redraw();
}
})),
))));
receiver
}
}