Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion xilem/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +27,8 @@ pub struct Xilem<State, Logic> {
default_base_color: Color,
// Font data to include in loading.
fonts: Vec<Blob<u8>>,
// Callback invoked once on startup, after windows creation.
on_start: Option<Box<dyn FnOnce(&mut MasonryState<'_>)>>,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not need the MasonryState argument for the use cases I introduced, but maybe someone needs it one day? I can remove it if you think it's unnecessary.

}

/// State type used by [`Xilem::new_simple`].
Expand Down Expand Up @@ -147,6 +149,7 @@ where
default_properties: None,
default_base_color: BACKGROUND_COLOR,
fonts: Vec::new(),
on_start: None,
}
}

Expand All @@ -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()?;
Expand Down Expand Up @@ -200,6 +209,7 @@ where
self.runtime,
self.default_base_color,
self.fonts,
self.on_start,
)
}
}
9 changes: 9 additions & 0 deletions xilem/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct MasonryDriver<State: 'static, Logic> {
default_base_color: Color,
// Fonts which will be registered on startup.
fonts: Vec<Blob<u8>>,
// Optional callback invoked once on startup, after windows creation.
start_callback: Option<Box<dyn FnOnce(&mut MasonryState<'_>)>>,
}

struct Window<State: 'static> {
Expand All @@ -54,6 +56,7 @@ where
runtime: Arc<tokio::runtime::Runtime>,
default_base_color: Color,
fonts: Vec<Blob<u8>>,
start_callback: Option<Box<dyn FnOnce(&mut MasonryState<'_>)>>,
) -> (Self, Vec<NewWindow>) {
let mut driver = Self {
state,
Expand All @@ -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))
Expand Down Expand Up @@ -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<'_, '_>) {
Expand Down
Loading