-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create JS and Rust dialog guide #1623
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1b56162
init dialog guide
vasfvitor 9f31c1f
Stubs
vasfvitor 8476c73
create basic js guides
vasfvitor ec866e8
fix: typo
vasfvitor c537e4d
improvements based on v1 docs
vasfvitor 4c4c007
fix: typo
vasfvitor 35a02bf
reorder based on summary (a-z)
vasfvitor 67beeb6
Update dialog.mdx
vasfvitor 16508db
update headings
vasfvitor eff6781
Save to file/directory
vasfvitor 39d9eaa
simplify and match TOC
vasfvitor 39c1cb7
fix: variable names
vasfvitor d81e389
reduce duplicated code
vasfvitor b8d2898
improve "Save to"
vasfvitor 4a0a669
specify return info
vasfvitor 8305084
draft rust guide
vasfvitor 73f892f
Update `dialog.mdx`
vasfvitor 25d93bd
refactor `dialog.mdx`
vasfvitor 1ec5a04
Update dialog.mdx
vasfvitor d0b5512
add notes and api links to body
vasfvitor b2a5bef
fix headings
vasfvitor 014a5da
i18nReady
vasfvitor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,297 @@ | ||
--- | ||
title: Dialog | ||
description: Native system dialogs for opening and saving files along with message dialogs. | ||
i18nReady: true | ||
tableOfContents: | ||
maxHeadingLevel: 4 | ||
--- | ||
|
||
import Stub from '@components/Stub.astro'; | ||
import PluginLinks from '@components/PluginLinks.astro'; | ||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
import CommandTabs from '@components/CommandTabs.astro'; | ||
|
||
<PluginLinks plugin="dialog" /> | ||
|
||
<Stub> | ||
Based on | ||
https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/dialog | ||
</Stub> | ||
Native system dialogs for opening and saving files along with message dialogs. | ||
|
||
## Setup | ||
|
||
Install the dialog plugin to get started. | ||
|
||
<Tabs> | ||
<TabItem label="Automatic"> | ||
|
||
1. Use your project's package manager to add the dependency: | ||
|
||
<CommandTabs npm="npm run tauri add dialog" | ||
yarn="yarn run tauri add dialog" | ||
pnpm="pnpm tauri add dialog" | ||
cargo="cargo tauri add dialog" /> | ||
|
||
2. Modify `lib.rs` to initialize the plugin: | ||
|
||
```rust | ||
#[cfg_attr(mobile, tauri::mobile_entry_point)] | ||
pub fn run() { | ||
tauri::Builder::default() | ||
// Initialize the plugin | ||
.plugin(tauri_plugin_dialog::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
``` | ||
</TabItem> | ||
<TabItem label="Manual"> | ||
|
||
1. Run `cargo add tauri-plugin-dialog` to add the plugin to the project's dependencies in `Cargo.toml`. | ||
|
||
2. Modify `lib.rs` to initialize the plugin: | ||
|
||
```rust | ||
// lib.rs | ||
#[cfg_attr(mobile, tauri::mobile_entry_point)] | ||
pub fn run() { | ||
tauri::Builder::default() | ||
// Initialize the plugin | ||
.plugin(tauri_plugin_dialog::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
``` | ||
|
||
3. If you'd like create dialogs in JavaScript, install the npm package as well: | ||
|
||
<CommandTabs | ||
npm="npm install @tauri-apps/plugin-dialog" | ||
yarn="yarn add @tauri-apps/plugin-dialog" | ||
pnpm="pnpm add @tauri-apps/plugin-dialog" | ||
/> | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Usage | ||
|
||
The dialog plugin is available in both JavaScript and Rust. Here's how you can use it: | ||
|
||
in JavaScript: | ||
- [Create Yes/No Dialog](#create-yesno-dialog) | ||
- [Create Ok/Cancel Dialog](#create-okcancel-dialog) | ||
- [Create Message Dialog](#create-message-dialog) | ||
- [Open a File Selector Dialog](#open-a-file-selector-dialog) | ||
- [Save to File Dialog](#save-to-file-dialog) | ||
|
||
in Rust: | ||
- [Build an Ask Dialog](#build-an-ask-dialog) | ||
- [Build a Message Dialog](#build-a-message-dialog) | ||
- [Build a File Selector Dialog](#build-a-file-selector-dialog) | ||
|
||
*** | ||
### JavaScript | ||
|
||
See all [Dialog Options](/2/reference/js/dialog/) at the JavaScript API reference. | ||
|
||
{/* ASK */} | ||
#### Create Yes/No Dialog | ||
|
||
Shows a question dialog with `Yes` and `No` buttons. | ||
|
||
```js | ||
import { ask } from '@tauri-apps/plugin-dialog'; | ||
|
||
// Create a Yes/No dialog | ||
const answer = await ask('This action cannot be reverted. Are you sure?', { | ||
title: 'Tauri', | ||
type: 'warning', | ||
}); | ||
|
||
console.log(answer); | ||
// Prints boolean to the console | ||
``` | ||
|
||
{/* CONFIRM */} | ||
|
||
#### Create Ok/Cancel Dialog | ||
|
||
Shows a question dialog with `Ok` and `Cancel` buttons. | ||
|
||
```js | ||
import { confirm } from '@tauri-apps/plugin-dialog'; | ||
|
||
// Creates a confirmation Ok/Cancel dialog | ||
const confirmation = await confirm( | ||
'This action cannot be reverted. Are you sure?', | ||
{ title: 'Tauri', type: 'warning' } | ||
); | ||
|
||
console.log(confirmation); | ||
// Prints boolean to the console | ||
``` | ||
{/* MESSAGE */} | ||
|
||
#### Create Message Dialog | ||
|
||
Shows a message dialog with an `Ok` button. Keep in mind that if the user closes the dialog it will return `false`. | ||
|
||
```js | ||
import { message } from '@tauri-apps/plugin-dialog'; | ||
|
||
// Shows message | ||
await message('File not found', { title: 'Tauri', type: 'error' }); | ||
``` | ||
|
||
{/* OPEN */} | ||
|
||
#### Open a File Selector Dialog | ||
|
||
Open a file/directory selection dialog. | ||
|
||
The `multiple` option controls whether the dialog allows multiple selection or not, while the `directory`, whether is a directory selection or not. | ||
|
||
```js | ||
import { open } from '@tauri-apps/plugin-dialog'; | ||
|
||
// Open a dialog | ||
const file = await open({ | ||
multiple: false, | ||
directory: false, | ||
}); | ||
console.log(file); | ||
// Prints file path and name to the console | ||
``` | ||
|
||
{/* SAVE */} | ||
|
||
#### Save to File Dialog | ||
|
||
Open a file/directory save dialog. | ||
|
||
```js | ||
import { save } from '@tauri-apps/plugin-dialog'; | ||
// Prompt to save a 'My Filter' with extension .png or .jpeg | ||
const path = await save({ | ||
filters: [ | ||
{ | ||
name: 'My Filter', | ||
extensions: ['png', 'jpeg'], | ||
}, | ||
], | ||
}); | ||
console.log(path); | ||
// Prints the chosen path | ||
``` | ||
|
||
*** | ||
### Rust | ||
|
||
Refer to the [Rust API reference](https://docs.rs/tauri-plugin-dialog/) to see all available options. | ||
|
||
#### Build an Ask Dialog | ||
|
||
Shows a question dialog with `Absolutely` and `Totally` buttons. | ||
|
||
```rust | ||
use tauri_plugin_dialog::DialogExt; | ||
|
||
let answer = app.dialog() | ||
.message("Tauri is Awesome") | ||
.title("Tauri is Awesome") | ||
.ok_button_label("Absolutely") | ||
.cancel_button_label("Totally") | ||
.blocking_show(); | ||
``` | ||
|
||
If you need a non blocking operation you can use `show()` instead: | ||
|
||
```rust | ||
use tauri_plugin_dialog::DialogExt; | ||
|
||
app.dialog() | ||
.message("Tauri is Awesome") | ||
.title("Tauri is Awesome") | ||
.ok_button_label("Absolutely") | ||
.cancel_button_label("Totally") | ||
.show(|result| match result { | ||
true => // do something, | ||
false =>// do something, | ||
}); | ||
``` | ||
|
||
#### Build a Message Dialog | ||
|
||
Shows a message dialog with an `Ok` button. Keep in mind that if the user closes the dialog it will return `false`. | ||
|
||
```rust | ||
use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; | ||
|
||
let ans = app.dialog() | ||
.message("File not found") | ||
.kind(MessageDialogKind::Error) | ||
.title("Warning") | ||
.blocking_show(); | ||
``` | ||
|
||
If you need a non blocking operation you can use `show()` instead: | ||
|
||
```rust | ||
use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; | ||
|
||
app.dialog() | ||
.message("Tauri is Awesome") | ||
.kind(MessageDialogKind::Info) | ||
.title("Information") | ||
.ok_button_label("Absolutely") | ||
.show(|result| match result { | ||
true => // do something, | ||
false => // do something, | ||
}); | ||
``` | ||
|
||
|
||
#### Build a File Selector Dialog | ||
|
||
|
||
#### Pick Files | ||
```rust | ||
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 | ||
``` | ||
|
||
If you need a non blocking operation you can use `show()` instead: | ||
|
||
```rust | ||
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 | ||
}) | ||
``` | ||
|
||
#### Save Files | ||
|
||
```rust | ||
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 | ||
``` | ||
|
||
or, alternatively: | ||
|
||
```rust | ||
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 | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of listing out each permutation of
multiple
anddirectory
I think just adding a paragraph before the code block explaining whatmultiple
anddirectory
mean would be just as effective and reduce a lot of duplication.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I reduced the duplication. Now, since there are multiple options, should those be explained as well? Not only the Open dialog, but Save, Message, and Confirm function, each have multiple options.
Current Open dialog (The link to js api is inside the JavaScript tab)
![](https://private-user-images.githubusercontent.com/61759797/280888511-dadaf42c-b2d6-4473-9c30-fbeb9235ddc6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4OTAzMDEsIm5iZiI6MTczODg5MDAwMSwicGF0aCI6Ii82MTc1OTc5Ny8yODA4ODg1MTEtZGFkYWY0MmMtYjJkNi00NDczLTljMzAtZmJlYjkyMzVkZGM2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDAxMDAwMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTYzM2M0ZmVkOGE4YmQzNDRkNmIyYjdhYjQzMjkxZmVhOGE4ZjVjZDg2YTY4YmEwNTlkMzU0ZGVjYmZiODc3YmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.a0KE1SIpiwrsjci7dv-gPt7xN-90Sl8znsm-QjheESM)
To compare with Save dialog
![](https://private-user-images.githubusercontent.com/61759797/280888588-db863f7f-2631-4bc6-8751-f8d1d5c0fefa.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4OTAzMDEsIm5iZiI6MTczODg5MDAwMSwicGF0aCI6Ii82MTc1OTc5Ny8yODA4ODg1ODgtZGI4NjNmN2YtMjYzMS00YmM2LTg3NTEtZjhkMWQ1YzBmZWZhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDAxMDAwMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNhODEzOTFjYTNiYzE2ZWQ4NGFhNWRkZDJmZTczZTZlOTRmYzM0NzY4NzVmYTM1YWE0NDM2ZTMwNzU5NDA2ZTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.ry4l2a9k_0-VLEU1bddnpI3cx1sqWcbN3e1n2Hi9o-A)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think how you've done in the "Open" one is perfect by linking to the API references. This lets the reader understand the very basic functionality (which is what this page is for) and let's them jump off to customise and configure it from there (which is what the reference page is for).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I separated Rust and JavaScript guides into 2 blocks and moved stuff around. I hope that is ok. My reasoning is that this plugin have different ways to be used that does not translate into 1:1 blocks.
Also moved the link of Rust docs and JavaScript docs to the start of the block. Since it should be basic functionality I guess, content-wise, ready.