iT邦幫忙

2022 iThome 鐵人賽

DAY 9
0
Modern Web

30天學習Tauri系列 第 9

9.Tauri Window Menu (一)

  • 分享至 

  • xImage
  •  

接著,今天我們來為App,添加Menu菜單的功能

打開todo\src-tauri\src\main.rs

1.引入 Menu、Submenu、MenuItem、CustomMenuItem

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

2.創建Menu實例

// 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("File", 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);

3.將菜單添加到窗口

將菜單添加到所有窗口

#![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("File", 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)
    .run(tauri::generate_context!())
    .expect("failed to run app");
}

https://ithelp.ithome.com.tw/upload/images/20220924/20108931Krx0MJaOqZ.png

今天先簡單介紹如何將Menu加道我們的App中,明天會講解如何將猜單中的功能給自定義。


上一篇
8.Tauri Splashscreen啟動頁面
下一篇
10.Tauri Window Menu (二)
系列文
30天學習Tauri30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言