对话框
原生文件系统和消息对话框。
支持的平台
此插件需要至少Rust版本1.77.2
平台 | 级别 | 备注 |
---|---|---|
Windows | ||
Linux | ||
macos | ||
android | | 不支持文件夹选择 |
ios | | 不支持文件夹选择 |
设置
安装对话框插件开始使用。
使用项目包管理器添加依赖项
npm run tauri add dialog
yarn run tauri add dialog
pnpm tauri add dialog
deno task tauri add dialog
bun tauri add dialog
cargo tauri add dialog
-
在
src-tauri
文件夹中运行以下命令,将插件添加到项目中Cargo.toml
的依赖项中cargo add tauri-plugin-dialog -
修改
lib.rs
以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_dialog::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果您想在JavaScript中创建对话框,也请安装npm包
npm install @tauri-apps/plugin-dialogyarn add @tauri-apps/plugin-dialogpnpm add @tauri-apps/plugin-dialogdeno add npm:@tauri-apps/plugin-dialogbun add @tauri-apps/plugin-dialog
用法
对话框插件以JavaScript和Rust提供。以下是如何使用它
在JavaScript中
在Rust中
JavaScript
在JavaScript API参考中查看所有对话框选项。
创建是/否对话框
显示带是
和否
按钮的问题对话框。
import { ask } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { ask } = window.__TAURI__.dialog;
// Create a Yes/No dialogconst answer = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning',});
console.log(answer);// Prints boolean to the console
创建确认/取消对话框
显示带确认
和取消
按钮的问题对话框。
import { confirm } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { confirm } = window.__TAURI__.dialog;
// Creates a confirmation Ok/Cancel dialogconst confirmation = await confirm( 'This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning' });
console.log(confirmation);// Prints boolean to the console
创建消息对话框
显示带确认
按钮的消息对话框。请注意,如果用户关闭对话框,它将返回false
。
import { message } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { message } = window.__TAURI__.dialog;
// Shows messageawait message('File not found', { title: 'Tauri', kind: 'error' });
打开文件选择器对话框
打开文件/目录选择对话框。
multiple
选项控制对话框是否允许多选,而directory
则表示是否为目录选择。
import { open } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { open } = window.__TAURI__.dialog;
// Open a dialogconst file = await open({ multiple: false, directory: false,});console.log(file);// Prints file path or URI
保存至文件对话框
打开文件/目录保存对话框。
import { save } from '@tauri-apps/plugin-dialog';// when using `"withGlobalTauri": true`, you may use// const { save } = window.__TAURI__.dialog;
// Prompt to save a 'My Filter' with extension .png or .jpegconst path = await save({ filters: [ { name: 'My Filter', extensions: ['png', 'jpeg'], }, ],});console.log(path);// Prints the chosen path
Rust
参考Rust API参考查看所有可用选项。
构建询问对话框
显示包含绝对是
和完全是的
按钮的询问对话框。
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
let answer = app.dialog() .message("Tauri is Awesome") .title("Tauri is Awesome") .buttons(MessageDialogButtons::OkCancelCustom("Absolutely", "Totally")) .blocking_show();
如果您需要非阻塞操作,可以使用show()
代替。
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
app.dialog() .message("Tauri is Awesome") .title("Tauri is Awesome") .buttons(MessageDialogButtons::OkCancelCustom("Absolutely", "Totally")) .show(|result| match result { true => // do something, false =>// do something, });
构建消息对话框
显示带确认
按钮的消息对话框。请注意,如果用户关闭对话框,它将返回false
。
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
let ans = app.dialog() .message("File not found") .kind(MessageDialogKind::Error) .title("Warning") .blocking_show();
如果您需要非阻塞操作,可以使用show()
代替。
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};
app.dialog() .message("Tauri is Awesome") .kind(MessageDialogKind::Info) .title("Information") .buttons(MessageDialogButtons::OkCustom("Absolutely")) .show(|result| match result { true => // do something, false => // do something, });
构建文件选择器对话框
选择文件
use tauri_plugin_dialog::DialogExt;
let file_path = app.dialog().file().blocking_pick_file();// return a file_path `Option`, or `None` if the user closes the dialog
如果您需要非阻塞操作,可以使用pick_file()
代替。
use tauri_plugin_dialog::DialogExt;
app.dialog().file().pick_file(|file_path| { // return a file_path `Option`, or `None` if the user closes the dialog })
保存文件
use tauri_plugin_dialog::DialogExt;
let file_path = app .dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .blocking_save_file(); // do something with the optional file path here // the file path is `None` if the user closed the dialog
或者
use tauri_plugin_dialog::DialogExt;
app.dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .pick_file(|file_path| { // return a file_path `Option`, or `None` if the user closes the dialog });
默认权限
此权限集配置了对话框插件可用的对话框类型。
已授权权限
所有对话框类型都已启用。
allow-ask
allow-confirm
allow-message
allow-save
allow-open
权限表
标识符 | 描述 |
---|---|
|
启用不带任何预配置作用域的ask命令。 |
|
拒绝不带任何预配置作用域的ask命令。 |
|
启用不带任何预配置作用域的confirm命令。 |
|
拒绝不带任何预配置作用域的confirm命令。 |
|
启用不带任何预配置作用域的message命令。 |
|
拒绝不带任何预配置作用域的message命令。 |
|
启用不带任何预配置作用域的open命令。 |
|
拒绝不带任何预配置作用域的open命令。 |
|
启用不带任何预配置作用域的save命令。 |
|
拒绝不带任何预配置作用域的save命令。 |
© 2025 Tauri贡献者。CC-BY / MIT