fn main() {
// here `"quit".to_string()` defines the menu item id, and the second parameter is the menu item label.
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let close = CustomMenuItem::new("close".to_string(), "Close");
let submenu = Submenu::new("Custom", Menu::new().add_item(quit).add_item(close));
let menu = Menu::new()
.add_native_item(MenuItem::Copy)
.add_item(CustomMenuItem::new("hide", "Hide"))
.add_submenu(submenu);
...
其中quit
和close
變數,是創立我們MenuItemsubmenu
變數,將MenuItem加入道我們的submenu裡menu
變數,是定義總菜單項目
新增自定義Menu,我們在tauri::Builder::default()
加入on_menu_event
.on_menu_event(|event| match event.menu_item_id() {
"quit" => {
// Custom
std::process::exit(0);
}
"close" => {
// Custom
event.window().close().unwrap();
}
_ => {}
})
我們先試試CopyCtrl+C,反白並點選。Ctrl+V
結果
測試quit和Close,點選後皆會自動關閉
main.rs
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
fn main() {
// here `"quit".to_string()` defines the menu item id, and the second parameter is the menu item label.
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let close = CustomMenuItem::new("close".to_string(), "Close");
let submenu = Submenu::new("Custom", Menu::new().add_item(quit).add_item(close));
let menu = Menu::new()
.add_native_item(MenuItem::Copy)
.add_item(CustomMenuItem::new("hide", "Hide"))
.add_submenu(submenu);
tauri::Builder::default()
.setup(|app| {
let splashscreen_window = app.get_window("splashscreen").unwrap();
let main_window = app.get_window("main").unwrap();
// we perform the initialization code on a new task so the app doesn't freeze
tauri::async_runtime::spawn(async move {
// initialize your app here instead of sleeping :)
println!("Initializing...");
std::thread::sleep(std::time::Duration::from_secs(5));
println!("Done initializing.");
// After it's done, close the splashscreen and display the main window
splashscreen_window.close().unwrap();
main_window.show().unwrap();
});
Ok(())
})
.menu(menu)
.on_menu_event(|event| match event.menu_item_id() {
"quit" => {
// Custom
std::process::exit(0);
}
"close" => {
// Custom
event.window().close().unwrap();
}
_ => {}
})
.run(tauri::generate_context!())
.expect("failed to run app");
}
今天我們大致介紹了Menu的功能,接下來明天我們要在Tauri Backend創立我們Todo的資料處理