Skip to content

feat: app.with_on_start() function#1783

Open
l0uisgrange wants to merge 1 commit intolinebender:mainfrom
l0uisgrange:on-start
Open

feat: app.with_on_start() function#1783
l0uisgrange wants to merge 1 commit intolinebender:mainfrom
l0uisgrange:on-start

Conversation

@l0uisgrange
Copy link
Copy Markdown
Contributor

@l0uisgrange l0uisgrange commented May 1, 2026

A new function .with_on_start() is introduced on Xilem::new to run a FnOnce after the app has started.

let app = Xilem::new_simple(state, app_logic, opts)
  .with_on_start(move |_| {
      #[cfg(target_os = "macos")]
      menu.init_for_nsapp();
  })
app.run_in(EventLoop::with_user_event())?;

This has a few use cases, and amongst them:

  1. Check for update on startup (with some API/GitHub releases/etc.)

  2. More specific for macOS, but this allows compatibility with the muda crate. Without this, it's almost impossible to add items in the macOS menu bar (used by all applications), because NSApplication needs to be set before using muda functions, which is only possible after the initialization of Xilem

    If you're not familiar with macOS, the menu bar looks like this

    Capture d’écran 2026-05-01 à 13 59 51

Comment thread xilem/src/app.rs
// 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.

@RagibHasin
Copy link
Copy Markdown
Contributor

Have you tried https://github.com/baltobor/xilem_extras ? It claims to provide application menus using muda. Although I have not personally tried it, yet.

@l0uisgrange
Copy link
Copy Markdown
Contributor Author

Yes, I've found the library in a comment a few days ago and tried it. It is good, indeed, though I would love to bring some of its components to xilem directly.
I could not try the application menu since xilem_extras only provides a clean way to declare it, but no way to activate it (after NSApplication instantiation). Even xilem_extras would need a .with_on_start() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants