freya_winit/
extensions.rs

1use freya_core::{
2    prelude::{
3        Platform,
4        UserEvent,
5    },
6    user_event::SingleThreadErasedEvent,
7};
8use winit::window::WindowId;
9
10use crate::{
11    config::WindowConfig,
12    renderer::NativeWindowErasedEventAction,
13};
14
15pub trait WinitPlatformExt {
16    fn launch_window(&self, window_config: WindowConfig) -> impl Future<Output = WindowId>;
17
18    fn close_window(&self, window_id: WindowId);
19}
20
21impl WinitPlatformExt for Platform {
22    async fn launch_window(&self, window_config: WindowConfig) -> WindowId {
23        let (tx, rx) = futures_channel::oneshot::channel();
24        self.send(UserEvent::Erased(SingleThreadErasedEvent(Box::new(
25            NativeWindowErasedEventAction::LaunchWindow {
26                window_config,
27                ack: tx,
28            },
29        ))));
30        rx.await.expect("Failed to create Window")
31    }
32
33    fn close_window(&self, window_id: WindowId) {
34        self.send(UserEvent::Erased(SingleThreadErasedEvent(Box::new(
35            NativeWindowErasedEventAction::CloseWindow(window_id),
36        ))));
37    }
38}