适用于不同 Windows 和平台的功能
本指南将帮助你自定义 Tauri 应用的功能。
¥This guide will help you customize the capabilities of your Tauri app.
本指南的内容
¥Content of this guide
-
在 Tauri 应用中创建多个窗口
¥Create multiple windows in a Tauri app
-
对不同的窗口使用不同的功能
¥Use different capabilities for different windows
-
使用平台特定功能
¥Use platform-specific capabilities
先决条件
¥Prerequisites
本练习应在完成 Using Plugin Permissions
后阅读。
¥This exercise is meant to be read after completing Using Plugin Permissions
.
指南
¥Guide
-
Here we create an app with two windows labelled
first
andsecond
. There are multiple ways to create windows in your Tauri application.In the Tauri configuration file, usually named
tauri.conf.json
:"productName": "multiwindow",..."app": {"windows": [{"label": "first","title": "First","width": 800,"height": 600},{"label": "second","title": "Second","width": 800,"height": 600}],},...}Create Windows Programmatically
In the Rust code to create a Tauri app:
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"); -
The windows of a Tauri app can use different features or plugins of the Tauri backend. For better security it is recommended to only give the necessary capabilities to each window. We simulate a scenario where the
first
windows uses filesystem and dialog functionalities andsecond
only needs dialog functionalities.It is recommended to separate the capability files per category of actions they enable.
JSON files in the
src-tauri/capabilities
will be taken into account for the capability system. Here we separate capabilities related to the filesystem and dialog window intofilesystem.json
anddialog.json
.filetree of the Tauri project:
/src/src-tauri/capabilitiesfilesystem.jsondialog.jsontauri.conf.jsonpackage.jsonREADME.mdGive filesystem capabilities to the
first
windowWe give the
first
window the capability to have read access to the content of the$HOME
directory.Use the
windows
field in a capability file with one or multiple window labels.filesystem.json {"identifier": "fs-read-home","description": "Allow access file access to home directory","local": true,"windows": ["first"],"permissions": ["fs:allow-home-read",]}Give dialog capabilities to the
first
andsecond
windowWe give to
first
andsecond
windows the capability to create a “Yes/No” dialogUse the
windows
field in a capability file with one or multiple window labels.dialog.json {"identifier": "dialog","description": "Allow to open a dialog","local": true,"windows": ["first", "second"],"permissions": ["dialog:allow-ask"]} -
We now want to customize the capabilities to be active only on certain platforms. We make our filesystem capabilities only active on
linux
andwindows
.Use the
platforms
field in a capability file to make it platform-specific.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"]}The currently available platforms are
linux
,windows
,macos
,android
, andios
.
结论和资源
¥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
示例 中找到使用窗口功能的示例应用。功能文件中使用的字段在 功能 参考中列出。
¥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号