vizia_winit/
lib.rs

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
pub mod application;
mod convert;
pub mod window;
pub mod window_modifiers;

pub trait ModifyWindow {
    /// Takes a closure which mutates the parent window of the current view.
    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T>;
    /// Returns a read-only pointer to the parent window of the current view.
    fn window(&self) -> Option<Arc<winit::window::Window>>;
}

use std::sync::Arc;

use vizia_core::{context::TreeProps, prelude::EventContext};
use window::Window;

impl ModifyWindow for EventContext<'_> {
    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T> {
        self.with_current(self.parent_window(), move |cx| {
            cx.get_view::<Window>()
                .and_then(|window| window.window.clone())
                .map(|window| (f)(window.as_ref()))
        })
    }

    fn window(&self) -> Option<Arc<winit::window::Window>> {
        self.get_view_with::<Window>(self.parent_window()).and_then(|window| window.window.clone())
    }
}