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

This commit is contained in:
Evan Almloff 2024-08-29 15:51:31 -05:00 committed by GitHub
parent 4676171861
commit aa0ea444cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 13 deletions

View file

@ -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>,
@ -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,
@ -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
}
@ -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

View file

@ -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
};