|
| 1 | +--- |
| 2 | +title: 适用于不同 Windows 和平台的功能 |
| 3 | +sidebar: |
| 4 | + order: 11 |
| 5 | +i18nReady: true |
| 6 | +--- |
| 7 | + |
| 8 | +import { Steps } from '@astrojs/starlight/components'; |
| 9 | +import ShowSolution from '@components/ShowSolution.astro' |
| 10 | +import Cta from '@fragments/cta.mdx'; |
| 11 | + |
| 12 | +本指南将帮助你自定义 Tauri 应用程序的功能。 |
| 13 | + |
| 14 | +## 本指南的内容 |
| 15 | + |
| 16 | +- Tauri 应用中创建多个窗口 |
| 17 | +- 对不同的窗口使用不同的功能 |
| 18 | +- 使用平台特定的功能 |
| 19 | + |
| 20 | +## 先决条件 |
| 21 | + |
| 22 | +完成 [`使用插件权限`](/learn/security/using-plugin-permissions/) 后再阅读此练习。 |
| 23 | + |
| 24 | +## 指导 |
| 25 | + |
| 26 | +<Steps> |
| 27 | +1. ### 在 Tauri 应用程序中创建多个窗口 |
| 28 | + |
| 29 | + 这里我们创建一个有两个窗口的应用程序,并分别标记为 `first` 和 `second` 。 |
| 30 | + 在 Tauri 应用程序中,有很多种方法可以创建窗口。 |
| 31 | + |
| 32 | + #### 使用 Tauri 配置文件创建窗口 |
| 33 | + |
| 34 | + 在 Tauri 配置文件中,通常名为 `tauri.conf.json` : |
| 35 | + |
| 36 | + <ShowSolution> |
| 37 | + ```javascript |
| 38 | + "productName": "multiwindow", |
| 39 | + ... |
| 40 | + "app": { |
| 41 | + "windows": [ |
| 42 | + { |
| 43 | + "label": "first", |
| 44 | + "title": "First", |
| 45 | + "width": 800, |
| 46 | + "height": 600 |
| 47 | + }, |
| 48 | + { |
| 49 | + "label": "second", |
| 50 | + "title": "Second", |
| 51 | + "width": 800, |
| 52 | + "height": 600 |
| 53 | + } |
| 54 | + ], |
| 55 | + }, |
| 56 | + ... |
| 57 | + } |
| 58 | + ``` |
| 59 | + </ShowSolution> |
| 60 | + |
| 61 | + #### 以编程的方式创建窗口 |
| 62 | + |
| 63 | + 在 Rust 代码中创建 Tauri 应用程序: |
| 64 | + |
| 65 | + <ShowSolution> |
| 66 | + ```rust |
| 67 | + tauri::Builder::default() |
| 68 | + .invoke_handler(tauri::generate_handler![greet]) |
| 69 | + .setup(|app| { |
| 70 | + let webview_url = tauri::WebviewUrl::App("index.html".into()); |
| 71 | + // 第一个窗口 |
| 72 | + tauri::WebviewWindowBuilder::new(app, "first", webview_url.clone()) |
| 73 | + .title("First") |
| 74 | + .build()?; |
| 75 | + // 第二个窗口 |
| 76 | + tauri::WebviewWindowBuilder::new(app, "second", webview_url) |
| 77 | + .title("Second") |
| 78 | + .build()?; |
| 79 | + Ok(()) |
| 80 | + }) |
| 81 | + .run(context) |
| 82 | + .expect("error while running tauri application"); |
| 83 | + ``` |
| 84 | + </ShowSolution> |
| 85 | + |
| 86 | +2. ### 将不同的功能应用于不同的窗口 |
| 87 | + |
| 88 | + Tauri 应用的窗口可以使用 Tauri 后端的不同功能或插件。为了提高安全性,建议只为每个窗口提供必要的功能。 |
| 89 | + 我们模拟了一个场景,其中 `first` 窗口使用文件系统和对话框功能,`second` 只使用对话框功能。 |
| 90 | + |
| 91 | + #### 每个类别单独的功能文件 |
| 92 | + |
| 93 | + 建议根据功能文件所支持的操作类别来分离功能文件。 |
| 94 | + |
| 95 | + <ShowSolution> |
| 96 | + `src-tauri/capabilities` 中的 JSON 文件将被纳入功能系统。 |
| 97 | + 在这里,我们将与文件系统和对话框窗口相关的功能分别存储到 `filesystem.json` 和 `dialog.json` 中。 |
| 98 | + |
| 99 | + *Tauri 项目的文件树:* |
| 100 | + ``` |
| 101 | + /src |
| 102 | + /src-tauri |
| 103 | + /capabilities |
| 104 | + filesystem.json |
| 105 | + dialog.json |
| 106 | + tauri.conf.json |
| 107 | + package.json |
| 108 | + README.md |
| 109 | + ``` |
| 110 | + </ShowSolution> |
| 111 | + |
| 112 | + #### 为 `first` 窗口提供文件系统功能 |
| 113 | + |
| 114 | + 我们赋予 `first` 窗口读取 `$HOME` 目录内容的权限。 |
| 115 | + |
| 116 | + <ShowSolution> |
| 117 | + 在功能文件中使用具有一个或多个窗口标签的 `windows` 字段。 |
| 118 | + |
| 119 | + ```json title="filesystem.json" |
| 120 | + { |
| 121 | + "identifier": "fs-read-home", |
| 122 | + "description": "Allow access file access to home directory", |
| 123 | + "local": true, |
| 124 | + "windows": ["first"], |
| 125 | + "permissions": [ |
| 126 | + "fs:allow-home-read", |
| 127 | + ] |
| 128 | + } |
| 129 | + ``` |
| 130 | + </ShowSolution> |
| 131 | + |
| 132 | + #### 为 `first` 和 `second` 窗口提供对话框功能 |
| 133 | + |
| 134 | + 我们为 `first` 和 `second` 窗口提供创建“是/否”的对话框的功能。 |
| 135 | + |
| 136 | + <ShowSolution> |
| 137 | + 在功能文件中使用具有一个或多个窗口标签的 `windows` 字段。 |
| 138 | + |
| 139 | + ```json title="dialog.json" |
| 140 | + { |
| 141 | + "identifier": "dialog", |
| 142 | + "description": "Allow to open a dialog", |
| 143 | + "local": true, |
| 144 | + "windows": ["first", "second"], |
| 145 | + "permissions": ["dialog:allow-ask"] |
| 146 | + } |
| 147 | + ``` |
| 148 | + |
| 149 | + </ShowSolution> |
| 150 | + |
| 151 | + |
| 152 | +3. ### 使功能依赖于平台 |
| 153 | + |
| 154 | + 我们现在想自定义功能,使其仅在某些平台上有效。我们使我们的文件系统功能仅在 `linux` 和 `windows` 上有效。 |
| 155 | + |
| 156 | + <ShowSolution> |
| 157 | + 在功能文件中使用 `platforms` 字段使其特定于平台。 |
| 158 | + |
| 159 | + ```json title="filesystem.json" |
| 160 | + { |
| 161 | + "identifier": "fs-read-home", |
| 162 | + "description": "Allow access file access to home directory", |
| 163 | + "local": true, |
| 164 | + "windows": ["first"], |
| 165 | + "permissions": [ |
| 166 | + "fs:allow-home-read", |
| 167 | + ], |
| 168 | + "platforms": ["linux", "windows"] |
| 169 | + } |
| 170 | + ``` |
| 171 | + |
| 172 | + 目前可用的平台有 `linux` 、 `windows` 、 `macos` 、 `android` 和 `ios` 。 |
| 173 | + </ShowSolution> |
| 174 | + |
| 175 | +</Steps> |
| 176 | + |
| 177 | +## 结论和资源 |
| 178 | + |
| 179 | +我们学习了如何在 Tauri 应用中创建多个窗口并赋予它们特定功能。此外,这些功能还可以针对特定平台进行定制。 |
| 180 | + |
| 181 | +在 [Tauri Github 仓库](https://github.com/tauri-apps/tauri) 的 |
| 182 | +[`api` 示例](https://github.com/tauri-apps/tauri/tree/dev/examples/api) |
| 183 | +中可以找到一个使用窗口功能的示例程序。 |
| 184 | +功能文件中可以使用的字段在 [功能](/reference/acl/capability/) 参考中列出。 |
0 commit comments