Tauri 監(jiān)聽自定義菜單項上的事件

2024-01-26 10:00 更新

每一個 在點擊時觸發(fā)一個事件。使用 API 來處理它們,無論是在全局的 上還是在特定的窗口上。CustomMenuItemon_menu_eventtauri::Builder

監(jiān)聽全局菜單上的事件

use tauri::{CustomMenuItem, Menu, MenuItem};

fn main() {
let menu = Menu::new(); // configure the menu
tauri::Builder::default()
.menu(menu)
.on_menu_event(|event| {
match event.menu_item_id() {
"quit" => {
std::process::exit(0);
}
"close" => {
event.window().close().unwrap();
}
_ => {}
}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

監(jiān)聽窗口菜單上的事件

use tauri::{CustomMenuItem, Menu, MenuItem};use tauri::{Manager, WindowBuilder};

fn main() {
let menu = Menu::new(); // configure the menu
tauri::Builder::default()
.setup(|app| {
let window = WindowBuilder::new(
app,
"main-window".to_string(),
tauri::WindowUrl::App("index.html".into()),
)
.menu(menu)
.build()?;
let window_ = window.clone();
window.on_menu_event(move |event| {
match event.menu_item_id() {
"quit" => {
std::process::exit(0);
}
"close" => {
window_.close().unwrap();
}
_ => {}
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號