Changing menu items on runtime #7735
Unanswered
FrankLaterza
asked this question in
Q&A
Replies: 1 comment 3 replies
-
I ran across this while trying to add & remove menu items in Tauri and I got it working using v2 (apparently this wasn't possible in v1 according to another github issue I saw). Forgive the fact these functions don't take any parameters. The ones in my app are specific to a couple menus, but I thought it was useful to see the types in play here. Here's some code I use to remove a menu item: pub fn remove_submenu_by_id<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
) -> Result<(), tauri::Error> {
let Some(root) = app.menu() else {
return Ok(());
};
// This removes a menu item from the macOS "app menu" (the leftmost one)
if let Some(app_menu) = find(&root, "MyAppName") {
if let Some(submenu) = app_menu.get("the_submenu_id") {
app_menu.remove(&submenu)?;
}
}
Ok(())
} And some code for adding a menu: pub fn add_submenu_somewhere<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
) -> Result<(), tauri::Error> {
let Some(root) = app.menu() else {
return Ok(());
};
// This is adding a submenu inside the macOS "app menu", the leftmost one
if let Some(app_menu) = find(&root, "MyApp") {
// Check if it exists before adding it
if app_menu.get("the_submenu_id").is_none() {
let submenu = menu(app, "the_submenu_id", "Your New Submenu")?;
// Can insert at some position, or append, or prepend, etc
app_menu.insert(&submenu, 1)?;
}
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Has anyone found a way to update menu items at runtime? I can't find a way to remove or add items to the menu from MenuHandle. Probably because there isn't a way. Does anyone know of a workaround? Could I rebuild them menu from the app handle?
Beta Was this translation helpful? Give feedback.
All reactions