Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ordering issues when with_menu(None) is called before setting the window builder #2903

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
34 changes: 23 additions & 11 deletions packages/desktop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ pub enum WindowCloseBehaviour {
CloseWindow,
}

/// The state of the menu builder. We need to keep track of if the state is default
/// so we only swap out the default menu bar when decorations are disabled
pub(crate) enum MenuBuilderState {
Unset,
Set(Option<DioxusMenu>),
}

impl From<MenuBuilderState> for Option<DioxusMenu> {
fn from(val: MenuBuilderState) -> Self {
match val {
MenuBuilderState::Unset => Some(default_menu_bar()),
MenuBuilderState::Set(menu) => menu,
}
}
}

/// The configuration for the desktop application.
pub struct Config {
pub(crate) window: WindowBuilder,
pub(crate) menu: Option<DioxusMenu>,
pub(crate) menu: MenuBuilderState,
pub(crate) protocols: Vec<WryProtocol>,
pub(crate) asynchronous_protocols: Vec<AsyncWryProtocol>,
pub(crate) pre_rendered: Option<String>,
Expand Down Expand Up @@ -67,7 +83,7 @@ impl Config {

Self {
window,
menu: Some(default_menu_bar()),
menu: MenuBuilderState::Unset,
protocols: Vec::new(),
asynchronous_protocols: Vec::new(),
pre_rendered: None,
Expand Down Expand Up @@ -110,15 +126,11 @@ impl Config {

/// Set the configuration for the window.
pub fn with_window(mut self, window: WindowBuilder) -> Self {
// gots to do a swap because the window builder only takes itself as muy self
// I wish more people knew about returning &mut Self
// We need to do a swap because the window builder only takes itself as muy self
self.window = window;
if self.window.window.decorations {
if self.menu.is_none() {
self.menu = Some(default_menu_bar());
}
} else {
self.menu = None;
// If the decorations are off for the window, remove the menu as well
if !self.window.window.decorations && matches!(self.menu, MenuBuilderState::Unset) {
self.menu = MenuBuilderState::Set(None);
}
self
}
Expand Down Expand Up @@ -224,7 +236,7 @@ impl Config {
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
if self.window.window.decorations {
self.menu = menu.into();
self.menu = MenuBuilderState::Set(menu.into())
}
}
self
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ impl WebviewInstance {
let webview = webview.build().unwrap();

let menu = if cfg!(not(any(target_os = "android", target_os = "ios"))) {
if let Some(menu) = &cfg.menu {
let menu_option = cfg.menu.into();
if let Some(menu) = &menu_option {
crate::menubar::init_menu_bar(menu, &window);
}
cfg.menu
menu_option
} else {
None
};
Expand Down
Loading