前言

Rust的GUI框架对比

Production ready Compatibility
gtk-rs Yes Cross-platform
fltk-rs Yes Cross-platform
iced No Cross-platform and web
relm No Cross-platform
Azul Yes Cross-platform
egui Older Releases Cross-platform
Tauri Yes Desktop and web
Slint Yes Web
Druid Yes Desktop and web

环境搭建

➜ cargo install create-tauri-app
➜ cargo install tauri-cli

初始化项目

➜  IdeaProjects cargo create-tauri-app

✔ Project name · tauri-app
✔ Choose your package manager · cargo
✔ Choose your UI template · vanilla

Please follow <https://tauri.app/v1/guides/getting-started/prerequisites> to install the needed prerequisites, if you haven't already.
You also need to install tauri-cli (cargo install tauri-cli)

Done, Now run:
  cd tauri-app
  cargo tauri dev

➜  IdeaProjects
notion-rss git:(main) ✗ ls --tree --depth 1 --icon-theme unicode 
🗁 .
├── 🗋 Cargo.lock
├── 🗋 Cargo.toml
├── 🗋 CHANGELOG.md
├── 🗁 dev
├── 🗁 dist
├── 🗋 index.html
├── 🗋 jsconfig.json
├── 🗋 LICENSE
├── 🗁 node_modules
├── 🗋 package.json
├── 🗁 public
├── 🗋 README.md
├── 🗁 src
├── 🗁 src-tauri
├── 🗁 target
├── 🗋 vite.config.js
└── 🗋 yarn.lock

tauri.conf.json

自定义icon图标

➜  notion-rss git:(main) ✗ cargo tauri icon src-tauri/icons/index.ico

Window系统隐藏命令行控制台

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

Rust与前端互相调用

前端调用Rust

#[tauri::command]
pub async fn init_user() -> Option<notion_sdk::user::User> {
    if let Ok(Object::User { user }) = NOTION_FEED.notion.users_me().await {
        return Some(user);
    }
    None
}
let builder = tauri::Builder::default()
            .system_tray(tauri::SystemTray::new().with_menu(MyTray::tray_menu()))
            .setup(|app| {
                resolve_setup(app);
                Ok(())
            })
            .on_system_tray_event(MyTray::on_system_tray_event)
            .invoke_handler(tauri::generate_handler![
                notion_rss::ui::save_config,
                notion_rss::ui::init_config,
                notion_rss::ui::init_user,
                notion_rss::ui::update_once,
                notion_rss::ui::run_api_server
            ]);
// 初始化用户信息
    async init_user() {
      if (
        this.config.notion_token &&
        this.config.archive_id &&
        this.config.source_id
      ) {
        invoke("init_user")
          .then((response) => {
            this.user = response;
          })
          .catch((error) => {
            this.snackbar = { text: error, show: true, color: "error" };
          });
      }
    },

Rust调用前端

// 监听事件
    async event_listen() {
      await appWindow.listen("PROGRESS", ({ event, payload }) => {
        console.log(event, payload);
        this.snackbar = {
          text: payload.toString(),
          show: true,
          color: "success",
        };
        this.update_loading = false;
      });
    },
#[tauri::command]
pub async fn update_once(window: tauri::Window) {
    update(Some(window.clone())).await;
}
window.emit("PROGRESS", err.to_string()).unwrap_or_default();

发布版本

效果

2023-03-07_19-39.png

参考

Powered by Kali-Team