#How to get main window only menu bar

8 messages · Page 1 of 1 (latest)

jovial birch
#

Hello all, I'm trying to follow the instructions here: https://tauri.app/v1/guides/features/menu/

I see variations of getting all windows with the same menu bar, or creating a one-off window with a menu bar, but I'm still too early in my rust/tauri adventure to understand how to apply a one-off window menu only to the main window.

So far as I can tell the default main window is being automagically generated behind the scenes by the default builder when this is called: .run(tauri::generate_context()), so the only builder I can attach a .menu call to appears to be the tauri::Builder::default(). However the link above does this and states this will force ALL windows to have this menu bar.

Is there some way for me to still rely on the default generate_context to build my window and rig a window menu in it somehow?

Native application menus can be attached to a window.

jovial birch
#

How to get main window only menu bar

toxic violet
jovial birch
#

Oh thank you. I figured I was missing something. So I should remove the window definition from my conf, and then just create the window manually in code, using the builder in place of the conf.

I'll give that a shot after work today. Thanks @toxic violet !

jovial birch
#

Hey @toxic violet maybe I'm messing something up.

I tried this:

    tauri::Builder::default()
        .setup(|app| {
            let window = WindowBuilder::new(
                app,
                "main-window".to_string(),
                tauri::WindowUrl::App("index.html".into()),
            )
            .menu(
                Menu::new().add_submenu(Submenu::new(
                    "File",
                    Menu::new()
                        .add_item(CustomMenuItem::new("open".to_string(), "Open Workspace"))
                        .add_item(CustomMenuItem::new("close".to_string(), "Close Workspace"))
                        .add_native_item(MenuItem::Separator)
                        .add_item(CustomMenuItem::new(
                            "cache-clear".to_string(),
                            "Clear Cache",
                        ))
                        .add_native_item(MenuItem::Separator)
                        .add_item(CustomMenuItem::new("exit".to_string(), "Exit")),
                )),
            )
            .build()?
            .on_menu_event(|ev| {
                ev.window()
                    .emit(
                        "windowMenuTrigger",
                        WindowMenuEvent {
                            menuItemId: ev.menu_item_id().to_string(),
                        },
                    )
                    .unwrap();
            });
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
#

When .on_menu_event was outside the setup function it worked fine, but attached to the WindowBuilder it complains that there is no window method on ev. I guess the event passed in is a different type?

#

I tried assigning the window builder to a variable (let window = ...) however using it in the on_menu_event call made me add the move keyword to the argument, but then complained that window was borrowed but needed to survive past it's stack or something along those lines.

I can get you exact error messages, and I apologize, it feels like I'm asking for help with rust more than tauri, but any assistance would be greatly appreciated.

#

Nevermind I just found the document example, let me try that.