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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
pub mod application;
mod convert;
mod window;

pub mod rwh {
    pub use raw_window_handle::*;
}

pub trait GetRawWindowHandle {
    fn raw_window_handle(&mut self) -> rwh::RawWindowHandle;
    fn mutate_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T>;
}

use raw_window_handle::HasRawWindowHandle;
use vizia_core::prelude::{Context, Entity, EventContext, GenerationalId};
use window::Window;

impl<'a> GetRawWindowHandle for EventContext<'a> {
    fn raw_window_handle(&mut self) -> rwh::RawWindowHandle {
        self.with_current(Entity::root(), |cx| {
            cx.get_view::<Window>().map(|window| window.window().raw_window_handle())
        })
        .unwrap()
    }

    fn mutate_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T> {
        self.with_current(Entity::root(), move |cx| {
            cx.get_view::<Window>().map(move |window| (f)(window.window()))
        })
    }
}

impl GetRawWindowHandle for Context {
    fn raw_window_handle(&mut self) -> rwh::RawWindowHandle {
        let mut cx = EventContext::new(self);
        cx.with_current(Entity::root(), |cx| {
            cx.get_view::<Window>().map(|window| window.window().raw_window_handle())
        })
        .unwrap()
    }

    fn mutate_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T> {
        let mut cx = EventContext::new(self);

        cx.with_current(Entity::root(), move |cx| {
            cx.get_view::<Window>().map(move |window| (f)(window.window()))
        })
    }
}