以下速查表涵盖了 Tauri 应用中最常用的前端 API(来自 @tauri-apps/api),按功能模块分组。所有 API 均可在浏览器环境的 WebView 中直接调用,与 Rust 后端通过 IPC 安全通信。
使用前安装:
npm install @tauri-apps/api(或按你的包管理器导入)
1. 窗口操作 window
| API | 说明 | 示例 |
|-----|------|------|
| appWindow | 获取当前窗口实例 | import { appWindow } from '@tauri-apps/api/window'; |
| appWindow.close() | 关闭当前窗口 | await appWindow.close(); |
| appWindow.minimize() | 最小化 | await appWindow.minimize(); |
| appWindow.maximize() | 最大化 | await appWindow.maximize(); |
| appWindow.toggleMaximize() | 切换最大化/还原 | await appWindow.toggleMaximize(); |
| appWindow.setTitle(title) | 设置窗口标题 | await appWindow.setTitle('新标题'); |
| appWindow.setFullscreen(bool) | 进入/退出全屏 | await appWindow.setFullscreen(true); |
| appWindow.center() | 窗口居中 | await appWindow.center(); |
| appWindow.setSize(w, h) | 设置窗口大小 | await appWindow.setSize(new LogicalSize(800, 600)); |
| appWindow.onResized(cb) | 监听窗口尺寸变化 | appWindow.onResized(({ payload: size }) => { ... }); |
2. 文件系统 fs
需要权限:在 tauri.conf.json 的 allowlist 中开启 fs 中对应接口。
| API | 说明 | 示例 |
|-----|------|------|
| readTextFile(path) | 读取文本文件 | import { readTextFile } from '@tauri-apps/api/fs'; <br> const text = await readTextFile('notes.txt'); |
| writeTextFile(path, text) | 写入文本文件(覆盖) | await writeTextFile('notes.txt', 'hello'); |
| readBinaryFile(path) | 读取二进制文件(返回 Uint8Array) | const buffer = await readBinaryFile('image.png'); |
| writeBinaryFile(path, data) | 写入二进制数据 | await writeBinaryFile('data.bin', new Uint8Array([1,2,3])); |
| exists(path) | 检查文件/目录是否存在 | const fileExists = await exists('config.json'); |
| mkdir(dir) | 创建目录(recursive 默认 true) | await mkdir('new_folder'); |
| readDir(dir) | 读取目录内容列表 | const entries = await readDir('./'); |
| removeDir(dir) | 删除空目录 | await removeDir('temp'); |
| removeFile(path) | 删除文件 | await removeFile('logs.txt'); |
| rename(oldPath, newPath) | 重命名/移动文件或目录 | await rename('old.txt', 'new.txt'); |
| copyFile(src, dst) | 复制文件 | await copyFile('src.txt', 'dst.txt'); |
路径可以是相对(相对于应用根目录)或绝对路径,前提是已在允许的范围内。
3. 对话框 dialog
需要权限:dialog 相关接口。
| API | 说明 | 示例 |
|-----|------|------|
| open(options?) | 打开文件选择框 | import { open } from '@tauri-apps/api/dialog'; <br> const selected = await open({ filters: [{ name: 'Markdown', extensions: ['md'] }] }); |
| save(options?) | 保存文件选择框 | const filePath = await save({ defaultPath: 'untitled.txt' }); |
| message(message, options?) | 消息提示框 | import { message } from '@tauri-apps/api/dialog'; <br> await message('操作成功!', { title: '提示', type: 'info' }); |
| ask(message, options?) | 是/否确认框 | const yes = await ask('确定删除?', { title: '警告', type: 'warning' }); |
返回的是所选文件的路径字符串或 null(取消时)。
4. 通知 notification
需要权限:notification。
| API | 说明 | 示例 |
|-----|------|------|
| sendNotification(options) | 发送系统通知 | import { sendNotification } from '@tauri-apps/api/notification'; <br> sendNotification({ title: 'Tauri', body: '文件已导出' }); |
| requestPermission() | 请求通知权限(通常自动) | import { requestPermission, isPermissionGranted } from '@tauri-apps/api/notification'; <br> if (!(await isPermissionGranted())) await requestPermission(); |
| isPermissionGranted() | 检查是否已授权 | 同上 |
5. 剪贴板 clipboard
| API | 说明 | 示例 |
|-----|------|------|
| writeText(text) | 写入文本到剪贴板 | import { writeText, readText } from '@tauri-apps/api/clipboard'; <br> await writeText('要复制的内容'); |
| readText() | 读取剪贴板文本 | const text = await readText(); |
6. 全局快捷键 globalShortcut
需要权限:globalShortcut,且需要 tauri.conf.json 中开启。
| API | 说明 | 示例 |
|-----|------|------|
| register(shortcut, handler) | 注册全局快捷键 | import { register } from '@tauri-apps/api/globalShortcut'; <br> await register('CommandOrControl+Shift+K', () => { ... }); |
| unregister(shortcut) | 注销指定快捷键 | await unregister('CommandOrControl+Shift+K'); |
| unregisterAll() | 注销所有已注册的快捷键 | await unregisterAll(); |
| isRegistered(shortcut) | 检查是否已注册 | const reg = await isRegistered('Ctrl+K'); |
快捷键字符串格式见文档,支持 Ctrl、Alt、Shift、CommandOrControl 等修饰键。
7. 进程与环境 process
| API | 说明 | 示例 |
|-----|------|------|
| exit(code = 0) | 退出应用 | import { exit } from '@tauri-apps/api/process'; <br> await exit(); |
| relaunch() | 重启应用 | await relaunch(); |
| getCurrentProcess() | 获取当前进程信息 | const proc = await getCurrentProcess(); |
8. 路径处理 path
这些 API 帮助你获得平台规范的应用路径,无需硬编码。
| API | 说明 | 示例 |
|-----|------|------|
| appDir() | 应用二进制所在目录 | import { appDir } from '@tauri-apps/api/path'; <br> const dir = await appDir(); |
| appConfigDir() | 应用配置目录(用户级) | const cfg = await appConfigDir(); |
| appDataDir() | 应用数据目录(用于存储用户数据) | const data = await appDataDir(); |
| appLocalDataDir() | 本地数据目录(不会漫游) | const local = await appLocalDataDir(); |
| appCacheDir() | 缓存目录 | const cache = await appCacheDir(); |
| appLogDir() | 日志目录 | const log = await appLogDir(); |
| desktopDir() | 用户桌面路径 | const desktop = await desktopDir(); |
| documentDir() | 用户文档路径 | const docs = await documentDir(); |
| homeDir() | 用户主目录 | const home = await homeDir(); |
| join(...segments) | 拼接路径(跨平台) | import { join } from '@tauri-apps/api/path'; <br> const file = await join(appDir, 'assets', 'logo.png'); |
9. 自定义 Rust 命令调用 invoke
这是前后端通信的核心,任何你在 Rust 中用 #[tauri::command] 修饰的函数都可以通过它调用。
| API | 说明 | 示例 |
|-----|------|------|
| invoke(cmd, args?) | 调用 Rust 命令 | import { invoke } from '@tauri-apps/api/tauri'; <br> const result = await invoke('greet', { name: 'Tauri' }); |
Rust 端示例:
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
在 main() 中注册: .invoke_handler(tauri::generate_handler![greet])
使用提示:所有 API 均返回 Promise,记得 await。如果需要更复杂的交互(如双向流、事件监听),可使用 event 模块中的 listen 和 emit。以上 API 覆盖了 90% 的桌面应用功能,更多细节请查阅 Tauri 官方文档。