tauri··约 6 分钟读完·2138 次观看

tauri版本2的系统托盘

通过上述两种方式,你可以在 Tauri 2.0.0-rc 应用程序中实现系统托盘功能。第一种方式适用于简单的托盘图标显示,而第二种方式提供了更多的自定义和交互可能性。根据你的应用需求选择合适的实现方参考资料。
rust

Tauri 2.0.0-rc 系统托盘实现指南

在 Tauri 2.0.0-rc 版本中,你有两种主要方式来实现系统托盘功能。以下是这两种方式的详细说明和代码示例。

方式一:通过 tauri.conf.json 配置

在 tauri.conf.json 文件中,你可以直接配置系统托盘的属性,如图标路径、标题和提示信息。

  • 第一中是
json
      
        
          1
          
           {
        
          2
          
            "tauri": {
        
          3
          
              "app": {
        
          4
          
                "windows": [
        
          5
          
                  {
        
          6
          
                    "title": "time",
        
          7
          
                    "width": 800,
        
          8
          
                    "height": 600
        
          9
          
                  }
        
          10
          
                ],
        
          11
          
                "security": {
        
          12
          
                  "csp": null
        
          13
          
                },
        
          14
          
                "trayIcon": {
        
          15
          
                  "iconPath": "icons/icon.ico",
        
          16
          
                  "iconAsTemplate": true,
        
          17
          
                  "title": "时间管理器",
        
          18
          
                  "tooltip": "时间管理器"
        
      
    

这种方式简单直接,适用于不需要复杂交互的托盘图标。

方式二:通过 Rust 代码创建

如果你需要更复杂的交互,比如点击托盘图标显示菜单或执行特定操作,你可以通过 Rust 代码来创建系统托盘。

1. 创建托盘菜单

首先,你需要定义托盘菜单项和子菜单。

tray.rs

rs
      
        
          1
          
          use tauri::{
        
          2
          
              menu::{Menu, MenuItem, Submenu},
        
          3
          
              tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
        
          4
          
              Manager,
        
          5
          
              Runtime,
        
          6
          
          };
        
          7
          
          
        
          8
          
          pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
        
          9
          
              let quit_i = MenuItem::with_id(app, "quit", "突出", true, None::<&str>)?;
        
          10
          
              let show_i = MenuItem::with_id(app, "show", "显示", true, None::<&str>)?;
        
          11
          
              let hide_i = MenuItem::with_id(app, "hide", "隐藏", true, None::<&str>)?;
        
          12
          
              let edit_i = MenuItem::with_id(app, "edit_file", "编辑", true, None::<&str>)?;
        
          13
          
              let new_i = MenuItem::with_id(app, "new_file", "添加", true, None::<&str>)?;
        
          14
          
              let a =Submenu::with_id_and_items(app, "File", "文章", true, &[&new_i, &edit_i])?;
        
          15
          
              // 分割线
        
          16
          
              let menu = Menu::with_items(app, &[&quit_i, &show_i, &hide_i, &a])?;
        
          17
          
          
        
          18
          
              let _ = TrayIconBuilder::with_id("tray")
        
      
    

2. 注册托盘创建函数

在你的主函数中,你需要注册 create_tray 函数,以便在应用启动时创建系统托盘

lib.rs

rs
      
        
          1
          
          #[cfg(desktop)]
        
          2
          
          mod tray;
        
          3
          
          pub fn run() {
        
          4
          
              tauri::Builder::default()
        
          5
          
              // ...
        
          6
          
              .setup(|app| {
        
          7
          
              #[cfg(all(desktop))]
        
          8
          
              {
        
          9
          
              let handle = app.handle();
        
          10
          
              tray::create_tray(handle)?;
        
          11
          
              }
        
          12
          
              Ok(())
        
          13
          
              })
        
          14
          
              // ...
        
          15
          
              .run(tauri::generate_context!())
        
          16
          
              .expect("error while running tauri application");
        
          17
          
          }
        
      
    

总结

通过上述两种方式,你可以在 Tauri 2.0.0-rc 应用程序中实现系统托盘功能。第一种方式适用于简单的托盘图标显示,而第二种方式提供了更多的自定义和交互可能性。根据你的应用需求选择合适的实现方

  • 参考资料https://v2.tauri.app/zh-cn/plugin/system-tray/#creating-the-system-tray-in-rust

这篇文章对你有帮助吗?