freya_winit/
extensions.rs1use freya_core::{
2 elements::rect::Rect,
3 prelude::{
4 Event,
5 EventHandlersExt,
6 EventsCombos,
7 GlobalUserEvent,
8 MouseButton,
9 Platform,
10 PlatformWindow,
11 PointerEventData,
12 },
13 user_event::SingleThreadErasedEvent,
14};
15use freya_engine::prelude::Surface as SkiaSurface;
16use winit::window::{
17 Window,
18 WindowId,
19};
20
21use crate::{
22 config::WindowConfig,
23 renderer::{
24 NativePlatformErasedEventAction,
25 RendererContext,
26 },
27};
28
29pub trait WinitPlatformExt {
31 fn window_id() -> WindowId;
43
44 fn launch_window(&self, window_config: WindowConfig) -> impl Future<Output = WindowId>;
64
65 fn close_window(&self, window_id: WindowId);
80
81 fn focus_window(&self, window_id: WindowId);
93
94 fn set_window_title(&self, window_id: WindowId, title: impl Into<String>);
106
107 fn with_window(&self, window_id: WindowId, callback: impl FnOnce(&mut Window) + 'static);
125
126 fn post_callback<F, T: 'static>(&self, callback: F) -> futures_channel::oneshot::Receiver<T>
129 where
130 F: FnOnce(&mut RendererContext) -> T + 'static;
131}
132
133pub trait WinitPlatformWindowExt {
135 fn post_render_callback<F, T: 'static>(
150 &self,
151 callback: F,
152 ) -> futures_channel::oneshot::Receiver<T>
153 where
154 F: FnOnce(&mut SkiaSurface) -> T + 'static;
155}
156
157#[derive(Clone, Copy, PartialEq)]
158struct WindowDragGesture;
159
160pub trait WindowDragExt {
161 fn window_drag(self) -> Self;
163}
164
165impl WindowDragExt for Rect {
166 fn window_drag(self) -> Self {
167 self.on_pointer_down(|event: Event<PointerEventData>| {
168 if event.button() != Some(MouseButton::Left) {
169 return;
170 }
171 if EventsCombos::<WindowDragGesture>::pressed(event.global_location()).is_double() {
172 Platform::get().with_window(Platform::window_id(), |window| {
173 window.set_maximized(!window.is_maximized())
174 });
175 }
176 })
177 .on_global_pointer_move(|event: Event<PointerEventData>| {
178 if EventsCombos::<WindowDragGesture>::moved(event.global_location()) {
179 Platform::get().with_window(Platform::window_id(), |window| {
180 let _ = window.drag_window();
181 });
182 }
183 })
184 .on_global_pointer_up(|_: Event<PointerEventData>| {
185 EventsCombos::<WindowDragGesture>::released();
186 })
187 }
188}
189
190impl WinitPlatformExt for Platform {
191 fn window_id() -> WindowId {
192 Platform::get().current_window().id.into()
193 }
194
195 async fn launch_window(&self, window_config: WindowConfig) -> WindowId {
196 let (sender, receiver) = futures_channel::oneshot::channel();
197 self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
198 NativePlatformErasedEventAction::LaunchWindow {
199 window_config: Box::new(window_config),
200 ack: sender,
201 },
202 ))));
203 receiver.await.expect("Failed to create window")
204 }
205
206 fn close_window(&self, window_id: WindowId) {
207 self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
208 NativePlatformErasedEventAction::CloseWindow(window_id),
209 ))));
210 }
211
212 fn focus_window(&self, window_id: WindowId) {
213 self.with_window(window_id, |window| window.focus_window());
214 }
215
216 fn set_window_title(&self, window_id: WindowId, title: impl Into<String>) {
217 let title = title.into();
218 self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
219 NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
220 if let Some(app) = context.windows.get_mut(&window_id) {
221 app.set_title(&title);
222 }
223 })),
224 ))));
225 }
226
227 fn with_window(&self, window_id: WindowId, callback: impl FnOnce(&mut Window) + 'static) {
228 self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
229 NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
230 if let Some(app) = context.windows.get_mut(&window_id) {
231 callback(&mut app.window);
232 }
233 })),
234 ))));
235 }
236
237 fn post_callback<F, T: 'static>(&self, callback: F) -> futures_channel::oneshot::Receiver<T>
238 where
239 F: FnOnce(&mut RendererContext) -> T + 'static,
240 {
241 let (sender, receiver) = futures_channel::oneshot::channel();
242 self.send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
243 NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
244 let _ = sender.send(callback(context));
245 })),
246 ))));
247 receiver
248 }
249}
250
251impl WinitPlatformWindowExt for PlatformWindow {
252 fn post_render_callback<F, T: 'static>(
253 &self,
254 callback: F,
255 ) -> futures_channel::oneshot::Receiver<T>
256 where
257 F: FnOnce(&mut SkiaSurface) -> T + 'static,
258 {
259 let window_id = self.id.into();
260 let (sender, receiver) = futures_channel::oneshot::channel();
261 Platform::get().send(GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
262 NativePlatformErasedEventAction::RendererCallback(Box::new(move |context| {
263 if let Some(app) = context.windows.get_mut(&window_id) {
264 app.render_callbacks.push(Box::new(move |surface| {
265 let _ = sender.send(callback(surface));
266 }));
267 app.window.request_redraw();
268 }
269 })),
270 ))));
271 receiver
272 }
273}