Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 39 ยท Windows and Menus

Native Menus and Context Menus in Tauri

Build native application menus in Tauri 2: MenuBuilder, SubmenuBuilder, items, checkboxes, accelerators, predefined items, menu events, context menus and per-window menus.

Desktop users expect menus: File, Edit, View, Help. On macOS the menu bar is part of the app's identity, and on Windows and Linux it sits inside the window. Tauri 2 builds native menus from Rust or JavaScript, so they look and behave like menus from any other program.

Building a menu in Rust

The pieces are Menu (the whole bar), Submenu (File, Edit...), and items. Builders keep the code readable:

Rust
use tauri::menu::{MenuBuilder, MenuItemBuilder, SubmenuBuilder};

pub fn run() {
    tauri::Builder::default()
        .setup(|app| {
            let new_note = MenuItemBuilder::new("New Note")
                .id("new-note")
                .accelerator("CmdOrCtrl+N")
                .build(app)?;
            let quit = MenuItemBuilder::new("Quit").id("quit").accelerator("CmdOrCtrl+Q").build(app)?;

            let file = SubmenuBuilder::new(app, "File")
                .item(&new_note)
                .separator()
                .item(&quit)
                .build()?;

            let edit = SubmenuBuilder::new(app, "Edit")
                .undo()
                .redo()
                .separator()
                .cut()
                .copy()
                .paste()
                .select_all()
                .build()?;

            let menu = MenuBuilder::new(app).items(&[&file, &edit]).build()?;
            app.set_menu(menu)?;
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error");
}

Key ideas:

  • Every clickable item has an id you choose. Events refer to ids.
  • .accelerator("CmdOrCtrl+N") sets the keyboard shortcut. CmdOrCtrl becomes Command on macOS and Control elsewhere.
  • Methods like .undo(), .copy() and .paste() add predefined items that work with no extra code, using the operating system's behavior and localization. Others include .quit(), .minimize(), .fullscreen(), .about(...), .services() (macOS) and .hide().
  • app.set_menu(menu) installs it as the app-wide menu. On Windows and Linux this menu shows in every window.

Item types

Type Builder Use
Normal item MenuItemBuilder An action
Checkbox CheckMenuItemBuilder On/off option such as "Word wrap"
Icon item IconMenuItemBuilder Item with an icon
Submenu SubmenuBuilder Nested menu
Separator .separator() A divider line
Rust
use tauri::menu::CheckMenuItemBuilder;

let wrap = CheckMenuItemBuilder::new("Word Wrap").id("wrap").checked(true).build(app)?;

Handling clicks

Menu actions arrive as events. Register one handler on the builder or the app:

Rust
tauri::Builder::default()
    .on_menu_event(|app, event| {
        match event.id().as_ref() {
            "new-note" => {
                use tauri::Emitter;
                let _ = app.emit("menu:new-note", ());
            }
            "quit" => app.exit(0),
            _ => {}
        }
    })

A common pattern is to forward the action to the frontend as an event, so the UI code decides what "New Note" means.

You can change items later: keep a handle (or find one by id with menu.get("wrap")) and call set_enabled(false), set_text("...") or set_checked(true) as the app state changes, for example to disable "Save" when nothing changed.

Per-window menus

On macOS there is one global menu bar. On Windows and Linux each window can have its own menu: build a menu and call window.set_menu(menu). This is how a settings window can omit the editing menu. Use window.hide_menu() and show_menu() to toggle.

Context menus

Right-click menus use the same pieces. Show one at the pointer with popup:

Rust
use tauri::menu::{ContextMenu, MenuBuilder};

#[tauri::command]
fn show_note_menu(window: tauri::Window) -> Result<(), String> {
    let menu = MenuBuilder::new(&window)
        .text("rename", "Rename")
        .text("delete", "Delete")
        .build()
        .map_err(|e| e.to_string())?;
    menu.popup(window).map_err(|e| e.to_string())
}

Clicks reach the same on_menu_event handler, so give items distinct ids.

The JavaScript API can create menus too (it needs the matching core:menu:* permissions):

TypeScript
import { Menu, MenuItem, Submenu } from "@tauri-apps/api/menu";

const open = await MenuItem.new({
  id: "open", text: "Open...", accelerator: "CmdOrCtrl+O",
  action: () => console.log("open clicked"),
});
const file = await Submenu.new({ text: "File", items: [open] });
const menu = await Menu.new({ items: [file] });
await menu.setAsAppMenu();

Here each item carries its own action callback, so you do not need a global handler.

Platform notes

  • macOS expects an app menu (named after your app) first, with About, Services, Hide and Quit. Predefined items give you these.
  • Windows and Linux conventionally put Quit or Exit under File and About under Help.
  • Mnemonics (the underlined letter) are written with an underscore in the text on some platforms, for example "_File"; behavior varies, so test.
  • Accelerators with letters are case-insensitive: CmdOrCtrl+Shift+S.

Try it: a menu model (stand-in)

This JavaScript stand-in models menus as data: ids, accelerators, a click dispatcher, and enabling or checking items later.

Node.js Stand-in: menu tree and click dispatch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Try it: accelerators per platform (stand-in)

CmdOrCtrl resolves differently by platform. This stand-in shows how a label is produced.

Node.js Stand-in: CmdOrCtrl resolution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the accelerator 'CmdOrCtrl+N' mean?

    CmdOrCtrl adapts to the platform convention.
  2. 2. How do you react to a menu item click in Rust?

    Menu clicks are delivered as events carrying the item id.
  3. 3. What are predefined items like .copy() and .paste() good for?

    They wire standard editing commands automatically.
  4. 4. How do you show a context menu at the pointer?

    popup displays a menu at the cursor position.

Summary

  • Build menus with MenuBuilder, SubmenuBuilder and item builders; install with set_menu.
  • Give items ids and handle clicks with on_menu_event, often by emitting an event to the UI.
  • Predefined items provide standard editing and window actions.
  • macOS has one global menu bar; Windows and Linux allow per-window menus.
  • Context menus use the same items with popup.

Next: Custom title bars and window effects