Skip to main content

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
9/// Re-export of the [`tray-icon`](https://docs.rs/tray-icon) crate, used with
10/// [`Application::with_tray_icon`](crate::application::Application::with_tray_icon).
11#[cfg(feature = "tray")]
12pub use tray_icon;
13
14pub trait ModifyWindow {
15    /// Takes a closure which mutates the parent window of the current view.
16    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T>;
17    /// Returns a read-only pointer to the parent window of the current view.
18    fn window(&self) -> Option<Arc<winit::window::Window>>;
19}
20
21use std::sync::Arc;
22
23use vizia_core::{context::TreeProps, prelude::EventContext};
24use window::Window;
25
26impl ModifyWindow for EventContext<'_> {
27    fn modify_window<T>(&mut self, f: impl FnOnce(&winit::window::Window) -> T) -> Option<T> {
28        self.with_current(self.parent_window(), move |cx| {
29            cx.get_view::<Window>()
30                .and_then(|window| window.window.clone())
31                .map(|window| (f)(window.as_ref()))
32        })
33    }
34
35    fn window(&self) -> Option<Arc<winit::window::Window>> {
36        self.get_view_with::<Window>(self.parent_window()).and_then(|window| window.window.clone())
37    }
38}