diff --git a/xilem/src/app.rs b/xilem/src/app.rs index 9726dedd6..18d594c3a 100644 --- a/xilem/src/app.rs +++ b/xilem/src/app.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use masonry::core::DefaultProperties; use masonry::peniko::{Blob, Color}; use masonry::theme::{BACKGROUND_COLOR, default_property_set}; -use masonry_winit::app::{EventLoopBuilder, MasonryUserEvent, NewWindow, WindowId}; +use masonry_winit::app::{EventLoopBuilder, MasonryState, MasonryUserEvent, NewWindow, WindowId}; use tokio::runtime::Runtime as TokioRuntime; use winit::error::EventLoopError; @@ -27,6 +27,8 @@ pub struct Xilem { default_base_color: Color, // Font data to include in loading. fonts: Vec>, + // Callback invoked once on startup, after windows creation. + on_start: Option)>>, } /// State type used by [`Xilem::new_simple`]. @@ -147,6 +149,7 @@ where default_properties: None, default_base_color: BACKGROUND_COLOR, fonts: Vec::new(), + on_start: None, } } @@ -172,6 +175,12 @@ where self } + /// Registers a callback to be called once the application has started + pub fn with_on_start(mut self, callback: impl FnOnce(&mut MasonryState<'_>) + 'static) -> Self { + self.on_start = Some(Box::new(callback)); + self + } + /// Run app with custom window attributes. pub fn run_in(mut self, mut event_loop: EventLoopBuilder) -> Result<(), EventLoopError> { let event_loop = event_loop.build()?; @@ -200,6 +209,7 @@ where self.runtime, self.default_base_color, self.fonts, + self.on_start, ) } } diff --git a/xilem/src/driver.rs b/xilem/src/driver.rs index 9c27dde7e..c52f7f690 100644 --- a/xilem/src/driver.rs +++ b/xilem/src/driver.rs @@ -31,6 +31,8 @@ pub struct MasonryDriver { default_base_color: Color, // Fonts which will be registered on startup. fonts: Vec>, + // Optional callback invoked once on startup, after windows creation. + start_callback: Option)>>, } struct Window { @@ -54,6 +56,7 @@ where runtime: Arc, default_base_color: Color, fonts: Vec>, + start_callback: Option)>>, ) -> (Self, Vec) { let mut driver = Self { state, @@ -63,6 +66,7 @@ where runtime, default_base_color, fonts, + start_callback, }; let windows: Vec<_> = (driver.logic)(&mut driver.state) .map(|view| driver.build_window(view)) @@ -367,6 +371,11 @@ where drop(root.register_fonts(font.clone())); } } + + // Calls callback functions after windows creation. + if let Some(cb) = self.start_callback.take() { + cb(state); + } } fn on_close_requested(&mut self, window_id: WindowId, ctx: &mut DriverCtx<'_, '_>) {