Dialog(ダイアログ)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
メッセージ・ダイアログを伴う「ファイルを開く/保存する」操作のためのネイティブ・システム・ダイアログ。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | | Does not support folder picker |
| ios | | Does not support folder picker |
はじめに、「dialog(ダイアログ)」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
npm run tauri add dialogyarn run tauri add dialogpnpm tauri add dialogdeno task tauri add dialogbun tauri add dialogcargo 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
「ダイアログ dialog」プラグインは JavaScript と Rust の両方で利用可能です。使い方は以下のとおりです:
JavaScript では:
- 「Yes/No」ダイアログの作成
- 「Ok/Cancel」ダイアログの作成
- 「メッセージ / Message」ダイアログの作成
- 「ファイル・セレクター / File Selector」ダイアログの表示
- 「ファイルへ保存 / Save to File」ダイアログ
Rust では:
JavaScript API の『リファレンス(参考資料)』(英語サイト)で、すべての ダイアログ・オプション を参照してください。
「Yes/はい」ボタンと「No/いいえ」ボタンのある質問ダイアログを表示します。
import { ask } from '@tauri-apps/plugin-dialog';// `"withGlobalTauri": true` を使用する場合は、// const { ask } = window.__TAURI__.dialog; を使用できます
// 「Yes/No」ダイアログを作成const answer = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning',});
console.log(answer);// コンソールにブール値を出力「Ok/OK」ボタンと「Cancel/キャンセル」ボタンのある質問ダイアログを表示します。
import { confirm } from '@tauri-apps/plugin-dialog';// `"withGlobalTauri": true` を使用する場合は、// const { confirm } = window.__TAURI__.dialog; を使用できます
// 「Ok/Cancel の確認」ダイアログを作成const confirmation = await confirm( 'This action cannot be reverted. Are you sure?', { title: 'Tauri', kind: 'warning' });
console.log(confirmation);// コンソールにブール値を出力「Ok/OK」ボタン付きのメッセージ・ダイアログを表示します。ユーザーがこのダイアログを閉じると false が返されることに注意してください。
import { message } from '@tauri-apps/plugin-dialog';// `"withGlobalTauri": true` を使用する場合は、// const { message } = window.__TAURI__.dialog; を使用できます
// メッセージを表示await message('File not found', { title: 'Tauri', kind: 'error' });「ファイル/ディレクトリ」選択ダイアログを開きます。
multiple オプションは、このダイアログで「複数の選択を許可するかどうか」を制御し、directory オプションは「ディレクトリ選択を許可するかどうか」を制御します。
import { open } from '@tauri-apps/plugin-dialog';// `"withGlobalTauri": true` を使用する場合は、// const { open } = window.__TAURI__.dialog; を使用できます
// ダイアログを表示const file = await open({ multiple: false, directory: false,});console.log(file);// 「ファイル・パス」または「URI」を出力URI Uniform Resource Identifier: 「統一資源識別子」。Web上のリソースを識別・認識するための文字列(識別子)の総称。URL と URN はそのサブセット。《wikipedia》
「ファイル/ディレクトリを保存」ダイアログを開きます。
import { save } from '@tauri-apps/plugin-dialog';// `"withGlobalTauri": true` を使用する場合は、// const { save } = window.__TAURI__.dialog; を使用できます
// 拡張子 .png または .jpeg を付けて「マイフィルター」を保存するように指示を出しますconst path = await save({ filters: [ { name: 'My Filter', extensions: ['png', 'jpeg'], }, ],});console.log(path);// 選択されたパスを出力利用可能なすべてのオプションを確認するには、Rust API リファレンス(英語サイト)を参照してください。
次の例では Absolutely と Totally ボタンを含む質問ダイアログを表示します。
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 => // 「true」の場合の処理, false =>// 「false」の場合の処理, });Ok ボタンのあるメッセージ・ダイアログを表示します。ユーザーがダイアログを閉じると、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 => // 「true」の場合の処理, false =>// 「false」の場合の処理, });use tauri_plugin_dialog::DialogExt;
let file_path = app.dialog().file().blocking_pick_file();// file_path `Option` を返し、ユーザーがダイアログを閉じた場合は `None` を返しますノン・ブロッキング操作が必要な場合は、代わりに pick_file() を使用できます:
use tauri_plugin_dialog::DialogExt;
app.dialog().file().pick_file(|file_path| { // file_path `Option` を返し、ユーザーがダイアログを閉じた場合は `None` を返します })use tauri_plugin_dialog::DialogExt;
let file_path = app .dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .blocking_save_file(); // ここに、オプションのファイルパスを用いて行なう処理を記述 // ユーザーがダイアログを閉じた場合には、ファイル・パスは「`None`(なし)」になりますあるいは、その代わりに:
use tauri_plugin_dialog::DialogExt;
app.dialog() .file() .add_filter("My Filter", &["png", "jpeg"]) .pick_file(|file_path| { // file_path `Option` を返し、ユーザーがダイアログを閉じた場合は `None` を返します });Default Permission
This permission set configures the types of dialogs available from the dialog plugin.
Granted Permissions
All dialog types are enabled.
This default permission set includes the following:
allow-askallow-confirmallow-messageallow-saveallow-open
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the ask command without any pre-configured scope. |
|
|
Denies the ask command without any pre-configured scope. |
|
|
Enables the confirm command without any pre-configured scope. |
|
|
Denies the confirm command without any pre-configured scope. |
|
|
Enables the message command without any pre-configured scope. |
|
|
Denies the message command without any pre-configured scope. |
|
|
Enables the open command without any pre-configured scope. |
|
|
Denies the open command without any pre-configured scope. |
|
|
Enables the save command without any pre-configured scope. |
|
|
Denies the save command without any pre-configured scope. |
【※ この日本語版は、「Nov 1, 2024 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT