适用于不同 Windows 和平台的功能
本指南将帮助你自定义 Tauri 应用的功能。
🌐 This guide will help you customize the capabilities of your Tauri app.
🌐 Content of this guide
- 在 Tauri 应用中创建多个窗口
- 对不同的窗口使用不同的功能
- 使用平台特定功能
🌐 Prerequisites
此练习应在完成 Using Plugin Permissions 后阅读。
🌐 This exercise is meant to be read after completing Using Plugin Permissions.
🌐 Guide
-
在这里,我们创建了一个带有两个窗口的应用,分别标记为
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}],},...}在用于创建 Tauri 应用的 Rust 代码中:
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窗口对$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窗口提供创建“是/否”对话框的功能在功能文件中使用
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。
🌐 Conclusion and Resources
我们已经学习了如何在 Tauri 应用中创建多个窗口并赋予它们特定的功能。此外,这些功能也可以针对某些平台进行设置。
🌐 We have learned how to create multiple windows in a Tauri app and give them specific capabilities. Furthermore these capabilities can also be targeted to certain platforms.
一个使用窗口功能的示例应用可以在 Tauri Github 仓库 的 api 示例 中找到。可以在功能文件中使用的字段列在 Capability 参考中。
🌐 An example application that used window capabilities can be found in the api example of the Tauri Github repository.
The fields that can be used in a capability file are listed in the Capability reference.
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站