vizia_winit/
lib.rs

1pub mod application;
2mod convert;
3pub mod window;
4pub mod window_modifiers;
5
6pub trait ModifyWindow {
7    /// Takes a closure which mutates the parent window of the current view.
8    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T>;
9    /// Returns a read-only pointer to the parent window of the current view.
10    fn window(&self) -> Option<Arc<winit::window::Window>>;
11}
12
13use std::sync::Arc;
14
15use vizia_core::{context::TreeProps, prelude::EventContext};
16use window::Window;
17
18impl ModifyWindow for EventContext<'_> {
19    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T> {
20        self.with_current(self.parent_window(), move |cx| {
21            cx.get_view::<Window>()
22                .and_then(|window| window.window.clone())
23                .map(|window| (f)(window.as_ref()))
24        })
25    }
26
27    fn window(&self) -> Option<Arc<winit::window::Window>> {
28        self.get_view_with::<Window>(self.parent_window()).and_then(|window| window.window.clone())
29    }
30}