name: tauri-desktop description: >- Tauri 2.0项目设置,Rust后端 + Web前端,插件系统,IPC命令,安全模型,自动更新,以及移动支持。使用Tauri构建轻量级跨平台桌面或移动应用时使用。
Tauri桌面应用技能
使用Tauri 2.0构建轻量级、安全的桌面(和移动)应用 — Rust后端,Web前端。
为什么选择Tauri
| 特性 | Tauri | Electron |
|---|---|---|
| 捆绑大小 | 2-10 MB | 80-150 MB |
| 内存使用 | 30-80 MB | 100-300 MB |
| 后端语言 | Rust | Node.js |
| 安全模型 | 基于能力的权限 | 完整的Node.js访问 |
| 移动支持 | 是(Tauri 2.0) | 否 |
| 自动更新器 | 内置 | electron-updater |
项目结构
项目/
├── src/ # 前端(任何Web框架)
│ ├── main.ts
│ ├── App.tsx # React/Vue/Svelte/原生
│ └── styles.css
├── src-tauri/ # Rust后端
│ ├── src/
│ │ ├── main.rs # 入口点
│ │ ├── lib.rs # 命令和设置
│ │ └── commands/ # IPC命令模块
│ │ ├── mod.rs
│ │ └── files.rs
│ ├── capabilities/ # 安全权限
│ ├── icons/ # 应用图标
│ ├── Cargo.toml # Rust依赖
│ └── tauri.conf.json # Tauri配置
├── package.json
├── vite.config.ts # 前端打包器
└── tsconfig.json
IPC命令
Rust端(后端)
// src-tauri/src/lib.rs
use tauri::Manager;
#[tauri::command]
fn greet(name: &str) -> String {
format!("你好, {}! 来自Rust。", name)
}
#[tauri::command]
async fn read_file(path: String) -> Result<String, String> {
std::fs::read_to_string(&path).map_err(|e| e.to_string())
}
#[tauri::command]
async fn save_settings(
app: tauri::AppHandle,
settings: serde_json::Value,
) -> Result<(), String> {
let path = app.path().app_config_dir().map_err(|e| e.to_string())?;
std::fs::create_dir_all(&path).map_err(|e| e.to_string())?;
let file = path.join("settings.json");
std::fs::write(file, settings.to_string()).map_err(|e| e.to_string())
}
pub fn run() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet, read_file, save_settings])
.run(tauri::generate_context!())
.expect("运行Tauri应用时出错");
}
前端端(TypeScript)
import { invoke } from '@tauri-apps/api/core';
// 从前端调用Rust命令
const greeting = await invoke<string>('greet', { name: '世界' });
const content = await invoke<string>('read_file', { path: '/tmp/test.txt' });
await invoke('save_settings', {
settings: { theme: 'dark', fontSize: 14 },
});
安全模型(能力)
// src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "默认应用权限",
"windows": ["main"],
"permissions": [
"core:default",
"dialog:default",
"fs:default",
{
"identifier": "fs:allow-read-text-file",
"allow": [{ "path": "$APPDATA/**" }]
},
{
"identifier": "fs:allow-write-text-file",
"allow": [{ "path": "$APPDATA/**" }]
}
]
}
关键安全原则:
- 默认允许列表:前端只能访问明确允许的API
- 范围文件系统访问:限制到特定目录
- 无任意shell访问:命令必须在Rust中预定义
- 强制执行CSP:自动设置内容安全策略头
插件(Tauri 2.0)
# src-tauri/Cargo.toml
[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-dialog = "2"
tauri-plugin-fs = "2"
tauri-plugin-shell = "2"
tauri-plugin-store = "2"
tauri-plugin-updater = "2"
tauri-plugin-notification = "2"
// 注册插件
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_updater::Builder::new().build())
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("运行Tauri应用时出错");
}
自动更新器
// src-tauri/tauri.conf.json (摘录)
{
"plugins": {
"updater": {
"pubkey": "您的公钥",
"endpoints": [
"https://releases.example.com/{{target}}/{{arch}}/{{current_version}}"
]
}
}
}
// 前端更新检查
import { check } from '@tauri-apps/plugin-updater';
const update = await check();
if (update) {
await update.downloadAndInstall();
await relaunch();
}
构建和分发
# 开发
npm run tauri dev
# 构建当前平台
npm run tauri build
# 构建特定目标
npm run tauri build --target x86_64-pc-windows-msvc
npm run tauri build --target aarch64-apple-darwin
npm run tauri build --target x86_64-unknown-linux-gnu
# 移动(Tauri 2.0)
npm run tauri android dev
npm run tauri ios dev
分发格式
| 平台 | 格式 |
|---|---|
| macOS | .dmg, .app |
| Windows | .msi, .exe (NSIS) |
| Linux | .deb, .rpm, .AppImage |
| Android | .apk, .aab |
| iOS | .ipa |
开发技巧
- 使用
window.__TAURI__在前端检测Tauri环境 - 状态管理:使用Tauri的
State用于Rust端状态,前端存储用于UI状态 - 文件对话框:使用
@tauri-apps/plugin-dialog代替浏览器文件输入 - 系统托盘:在
tauri.conf.json中配置菜单项 - 多窗口:从Rust使用
WebviewWindow::new()或从JS使用WebviewWindow.create()创建
相关资源
~/.claude/skills/rust/SKILL.md- Rust语言模式~/.claude/agents/desktop-developer.md- 桌面开发代理~/.claude/rules/stacks/rust.md- Rust栈指南
小体积。强安全。原生性能。Web灵活性。