iT邦幫忙

2022 iThome 鐵人賽

DAY 10
0
Modern Web

30天學習Tauri系列 第 10

10.Tauri Window Menu (二)

  • 分享至 

  • xImage
  •  

自定義Menu事件

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);
	...

其中quitclose變數,是創立我們MenuItem
submenu變數,將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();
      }
      _ => {}
    })

https://ithelp.ithome.com.tw/upload/images/20220925/20108931MCGY9euYvZ.png

我們先試試CopyCtrl+C,反白並點選。Ctrl+V
https://ithelp.ithome.com.tw/upload/images/20220925/201089310KQaKxR6RM.png
結果
https://ithelp.ithome.com.tw/upload/images/20220925/20108931L5wDLZBfE7.png

測試quit和Close,點選後皆會自動關閉
https://ithelp.ithome.com.tw/upload/images/20220925/20108931usDMFQ6X3E.png
https://ithelp.ithome.com.tw/upload/images/20220925/20108931KZGVwjVhNb.png

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的資料處理


上一篇
9.Tauri Window Menu (一)
下一篇
11.實作Tauri Todo - Backend (一) 加入Todo功能
系列文
30天學習Tauri30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言