关于 tauri-desktop
tauri-desktop 是一项使用 Tauri 2.0 构建桌面和移动应用的技能,结合 Rust 后端与 Web 前端。内容涵盖项目结构、IPC、权限、插件、更新和分发。
主要功能
- 01Rust 后端与 Web 前端结构
- 02前端调用 Rust 命令的 IPC
- 03基于能力的权限控制
- 04Tauri 2.0 插件配置
- 05更新器配置与应用构建
适用场景
- 01搭建 Tauri 桌面或移动应用
- 02限制前端对获准 API 的访问
- 03连接前端代码与 Rust 命令
- 04配置应用更新和分发
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ travisjneuman/ .claude --skill tauri-desktop安装后在 Claude Code 中输入/tauri-desktop确认已加载。
详细介绍
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 |
项目结构
project/
├── src/ # Frontend (any web framework)
│ ├── main.ts
│ ├── App.tsx # React/Vue/Svelte/vanilla
│ └── styles.css
├── src-tauri/ # Rust backend
│ ├── src/
│ │ ├── main.rs # Entry point
│ │ ├── lib.rs # Commands and setup
│ │ └── commands/ # IPC command modules
│ │ ├── mod.rs
│ │ └── files.rs
│ ├── capabilities/ # Security permissions
│ │ └── default.json
│ ├── icons/ # App icons
│ ├── Cargo.toml # Rust dependencies
│ └── tauri.conf.json # Tauri configuration
├── package.json
├── vite.config.ts # Frontend bundler
└── tsconfig.json
IPC 命令
Rust 端(后端)
// src-tauri/src/lib.rs
use tauri::Manager;
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! From 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("error while running tauri application");
}
前端(TypeScript)
import { invoke } from '@tauri-apps/api/core';
// Call Rust commands from frontend
const greeting = await invoke<string>('greet', { name: 'World' });
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": "Default app permissions",
"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"
// Register plugins
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("error while running tauri application");
自动更新
// src-tauri/tauri.conf.json (excerpt)
{
"plugins": {
"updater": {
"pubkey": "YOUR_PUBLIC_KEY",
"endpoints": [
"https://releases.example.com/{{target}}/{{arch}}/{{current_version}}"
]
}
}
}
// Frontend update check
import { check } from '@tauri-apps/plugin-updater';
const update = await check();
if (update) {
await update.downloadAndInstall();
await relaunch();
}
构建与分发
# Development
npm run tauri dev
# Build for current platform
npm run tauri build
# Build for specific target
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
# Mobile (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 端状态,使用前端 store 管理 UI 状态 - 文件对话框:使用
@tauri-apps/plugin-dialog,而不是浏览器文件输入 - 系统托盘:在
tauri.conf.json中配置菜单项 - 多窗口:在 Rust 中使用
WebviewWindow::new()创建,或在 JS 中使用WebviewWindow.create()
相关资源
agent-rust-expert技能——Rust 语言模式agent-desktop-developer技能——桌面开发专家~/.claude/docs/reference/stacks/rust.md——Rust 技术栈指南
小巧的二进制文件。强大的安全性。原生性能。Web 灵活性。
评论
支持 Markdown