vizia_winit/
lib.rs

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