tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.56k stars 2.54k forks source link

Menu accelerator doesn't work [bug] #6981

Open FengZhongShaoNian opened 1 year ago

FengZhongShaoNian commented 1 year ago

Describe the bug

I created a Tauri App using yarn create tauri-app, and then changed the main.rs file to:

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu, WindowMenuEvent};

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

fn app_menu() -> Menu {
    Menu::with_items([MenuEntry::Submenu(Submenu::new(
        "File",
        Menu::with_items([
            CustomMenuItem::new("New", "New")
                .accelerator("CmdOrCtrl+N")
                .into(),
            CustomMenuItem::new("Open", "Open")
                .accelerator("CmdOrCtrl+O")
                .into(),
            MenuItem::Separator.into(),
            CustomMenuItem::new("Save", "Save")
                .accelerator("CmdOrCtrl+S")
                .into(),
            CustomMenuItem::new("SaveAs", "SaveAs")
                .accelerator("CmdOrCtrl+Shift+S")
                .into(),
            MenuItem::Separator.into(),
            CustomMenuItem::new("Close", "Close")
                .accelerator("CmdOrCtrl+W")
                .into(),
        ]),
    ))])
}

fn menu_handler(event: WindowMenuEvent) {
    println!("Menu Item {} clicked", event.menu_item_id())
}

fn main() {
    tauri::Builder::default()
        .menu(app_menu())
        .on_menu_event(menu_handler)
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

When I run this code on Windows 10, the menu accelerator I configured above doesn't work. When I press "Ctrl+N",nothing happens. (menu_handler function was not called.) But when I click New menu item, menu_handler works normally.

My cargo.toml:

[package]
name = "tauri-app"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.3", features = [] }

[dependencies]
tauri = { version = "1.3", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]

Reproduction

No response

Expected behavior

No response

Platform and versions

[✔] Environment
    - OS: Windows 10.0.19045 X64
    ✔ WebView2: 113.0.1774.42
    ✔ MSVC:
        - Visual Studio Build Tools 2017
        - Visual Studio Build Tools 2022
    ✔ rustc: 1.66.0 (69f9c33d7 2022-12-12)
    ✔ Cargo: 1.66.0 (d65d197ad 2022-11-15)
    ✔ rustup: 1.25.1 (bb60b1e89 2022-07-12)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.12.1
    - pnpm: 8.4.0
    - yarn: 1.22.19
    - npm: 8.19.2

[-] Packages
    - tauri [RUST]: 1.3.0
    - tauri-build [RUST]: 1.3.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.1
    - @tauri-apps/api [NPM]: 1.3.0
    - @tauri-apps/cli [NPM]: 1.3.1

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: Vue.js
    - bundler: Vite
Done in 8.92s.

Stack trace

No response

Additional context

No response

FabianLars commented 1 year ago

This is tracked here: https://github.com/tauri-apps/wry/issues/451. Thanks for the report :)