不同 Windows 和平台的功能
本指南将帮助你自定义 Tauri 应用的功能。
本指南内容
- 在 Tauri 应用中创建多个窗口
- 为不同窗口使用不同功能
- 使用平台特定功能
先决条件
本练习旨在在你完成 Using Plugin Permissions
后阅读。
指南
-
在这里,我们创建一个带有两个窗口的应用,分别标记为
first
和second
。在你的 Tauri 应用中,有多种创建窗口的方式。在 Tauri 配置文件中,通常命名为
tauri.conf.json
"productName": "multiwindow",..."app": {"windows": [{"label": "first","title": "First","width": 800,"height": 600},{"label": "second","title": "Second","width": 800,"height": 600}],},...}以编程方式创建窗口
在 Rust 代码中创建 Tauri 应用
tauri::Builder::default().invoke_handler(tauri::generate_handler![greet]).setup(|app| {let webview_url = tauri::WebviewUrl::App("index.html".into());// First windowtauri::WebviewWindowBuilder::new(app, "first", webview_url.clone()).title("First").build()?;// Second windowtauri::WebviewWindowBuilder::new(app, "second", webview_url).title("Second").build()?;Ok(())}).run(context).expect("error while running tauri application"); -
Tauri 应用的窗口可以使用 Tauri 后端不同的功能或插件。为了更好的安全性,建议仅为每个窗口提供必要的功能。我们模拟一个场景,其中
first
窗口使用文件系统和对话框功能,而second
仅需要对话框功能。建议按其启用的操作类别分离功能文件。
在
src-tauri/capabilities
中的 JSON 文件将被功能系统考虑在内。在这里,我们将与文件系统和对话框窗口相关的功能分离到filesystem.json
和dialog.json
中。Tauri 项目的文件树
/src/src-tauri/capabilitiesfilesystem.jsondialog.jsontauri.conf.jsonpackage.jsonREADME.md为
first
窗口赋予文件系统功能我们赋予
first
窗口读取$HOME
目录内容的权限。在功能文件中使用带有单个或多个窗口标签的
windows
字段。filesystem.json {"identifier": "fs-read-home","description": "Allow access file access to home directory","local": true,"windows": ["first"],"permissions": ["fs:allow-home-read",]}为
first
和second
窗口赋予对话框功能我们赋予
first
和second
窗口创建“是/否”对话框的功能在功能文件中使用带有单个或多个窗口标签的
windows
字段。dialog.json {"identifier": "dialog","description": "Allow to open a dialog","local": true,"windows": ["first", "second"],"permissions": ["dialog:allow-ask"]} -
我们现在想自定义功能,使其仅在某些平台上处于活动状态。我们将文件系统功能仅在
linux
和windows
上激活。在功能文件中使用
platforms
字段使其特定于平台。filesystem.json {"identifier": "fs-read-home","description": "Allow access file access to home directory","local": true,"windows": ["first"],"permissions": ["fs:allow-home-read",],"platforms": ["linux", "windows"]}当前可用的平台有
linux
、windows
、macos
、android
和ios
。
结论和资源
我们已经学习了如何在 Tauri 应用中创建多个窗口并赋予它们特定的功能。此外,这些功能也可以针对特定平台。
可以在 api
示例 中找到使用窗口功能的示例应用程序,该示例位于 Tauri Github 仓库 中。功能文件中可以使用的字段在 Capability 参考中列出。
© 2025 Tauri 贡献者。CC-BY / MIT