tauri_plugin_dialog/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Native system dialogs for opening and saving files along with message dialogs.
6
7#![doc(
8    html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
9    html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
10)]
11
12use serde::Serialize;
13use tauri::{
14    plugin::{Builder, TauriPlugin},
15    Manager, Runtime,
16};
17
18use std::{
19    path::{Path, PathBuf},
20    sync::mpsc::sync_channel,
21};
22
23pub use models::*;
24
25pub use tauri_plugin_fs::FilePath;
26#[cfg(desktop)]
27mod desktop;
28#[cfg(mobile)]
29mod mobile;
30
31mod commands;
32mod error;
33mod models;
34
35pub use error::{Error, Result};
36
37#[cfg(desktop)]
38use desktop::*;
39#[cfg(mobile)]
40use mobile::*;
41
42#[cfg(desktop)]
43pub use desktop::Dialog;
44#[cfg(mobile)]
45pub use mobile::Dialog;
46
47pub(crate) const OK: &str = "Ok";
48pub(crate) const CANCEL: &str = "Cancel";
49pub(crate) const YES: &str = "Yes";
50pub(crate) const NO: &str = "No";
51
52macro_rules! blocking_fn {
53    ($self:ident, $fn:ident) => {{
54        let (tx, rx) = sync_channel(0);
55        let cb = move |response| {
56            tx.send(response).unwrap();
57        };
58        $self.$fn(cb);
59        rx.recv().unwrap()
60    }};
61}
62
63/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the dialog APIs.
64pub trait DialogExt<R: Runtime> {
65    fn dialog(&self) -> &Dialog<R>;
66}
67
68impl<R: Runtime, T: Manager<R>> crate::DialogExt<R> for T {
69    fn dialog(&self) -> &Dialog<R> {
70        self.state::<Dialog<R>>().inner()
71    }
72}
73
74impl<R: Runtime> Dialog<R> {
75    /// Create a new messaging dialog builder.
76    /// The dialog can optionally ask the user for confirmation or include an OK button.
77    ///
78    /// # Examples
79    ///
80    /// - Message dialog:
81    ///
82    /// ```
83    /// use tauri_plugin_dialog::DialogExt;
84    ///
85    /// tauri::Builder::default()
86    ///   .setup(|app| {
87    ///     app
88    ///       .dialog()
89    ///       .message("Tauri is Awesome!")
90    ///       .show(|_| {
91    ///         println!("dialog closed");
92    ///       });
93    ///     Ok(())
94    ///   });
95    /// ```
96    ///
97    /// - Ask dialog:
98    ///
99    /// ```
100    /// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
101    ///
102    /// tauri::Builder::default()
103    ///   .setup(|app| {
104    ///     app.dialog()
105    ///       .message("Are you sure?")
106    ///       .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
107    ///       .show(|yes| {
108    ///         println!("user said {}", if yes { "yes" } else { "no" });
109    ///       });
110    ///     Ok(())
111    ///   });
112    /// ```
113    ///
114    /// - Message dialog with OK button:
115    ///
116    /// ```
117    /// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
118    ///
119    /// tauri::Builder::default()
120    ///   .setup(|app| {
121    ///     app.dialog()
122    ///       .message("Job completed successfully")
123    ///       .buttons(MessageDialogButtons::Ok)
124    ///       .show(|_| {
125    ///         println!("dialog closed");
126    ///       });
127    ///     Ok(())
128    ///   });
129    /// ```
130    ///
131    /// # `show` vs `blocking_show`
132    ///
133    /// The dialog builder includes two separate APIs for rendering the dialog: `show` and `blocking_show`.
134    /// The `show` function is asynchronous and takes a closure to be executed when the dialog is closed.
135    /// To block the current thread until the user acted on the dialog, you can use `blocking_show`,
136    /// but note that it cannot be executed on the main thread as it will freeze your application.
137    ///
138    /// ```
139    /// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
140    ///
141    /// tauri::Builder::default()
142    ///   .setup(|app| {
143    ///     let handle = app.handle().clone();
144    ///     std::thread::spawn(move || {
145    ///       let yes = handle.dialog()
146    ///         .message("Are you sure?")
147    ///         .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
148    ///         .blocking_show();
149    ///     });
150    ///
151    ///     Ok(())
152    ///   });
153    /// ```
154    pub fn message(&self, message: impl Into<String>) -> MessageDialogBuilder<R> {
155        MessageDialogBuilder::new(
156            self.clone(),
157            self.app_handle().package_info().name.clone(),
158            message,
159        )
160    }
161
162    /// Creates a new builder for dialogs that lets the user select file(s) or folder(s).
163    pub fn file(&self) -> FileDialogBuilder<R> {
164        FileDialogBuilder::new(self.clone())
165    }
166}
167
168/// Initializes the plugin.
169pub fn init<R: Runtime>() -> TauriPlugin<R> {
170    #[allow(unused_mut)]
171    let mut builder = Builder::new("dialog");
172
173    // Dialogs are implemented natively on Android
174    #[cfg(not(target_os = "android"))]
175    {
176        builder = builder.js_init_script(include_str!("init-iife.js").to_string());
177    }
178
179    builder
180        .invoke_handler(tauri::generate_handler![
181            commands::open,
182            commands::save,
183            commands::message,
184            commands::ask,
185            commands::confirm
186        ])
187        .setup(|app, api| {
188            #[cfg(mobile)]
189            let dialog = mobile::init(app, api)?;
190            #[cfg(desktop)]
191            let dialog = desktop::init(app, api)?;
192            app.manage(dialog);
193            Ok(())
194        })
195        .build()
196}
197
198/// A builder for message dialogs.
199pub struct MessageDialogBuilder<R: Runtime> {
200    #[allow(dead_code)]
201    pub(crate) dialog: Dialog<R>,
202    pub(crate) title: String,
203    pub(crate) message: String,
204    pub(crate) kind: MessageDialogKind,
205    pub(crate) buttons: MessageDialogButtons,
206    #[cfg(desktop)]
207    pub(crate) parent: Option<crate::desktop::WindowHandle>,
208}
209
210/// Payload for the message dialog mobile API.
211#[cfg(mobile)]
212#[derive(Serialize)]
213#[serde(rename_all = "camelCase")]
214pub(crate) struct MessageDialogPayload<'a> {
215    title: &'a String,
216    message: &'a String,
217    kind: &'a MessageDialogKind,
218    ok_button_label: Option<&'a str>,
219    cancel_button_label: Option<&'a str>,
220}
221
222// raw window handle :(
223unsafe impl<R: Runtime> Send for MessageDialogBuilder<R> {}
224
225impl<R: Runtime> MessageDialogBuilder<R> {
226    /// Creates a new message dialog builder.
227    pub fn new(dialog: Dialog<R>, title: impl Into<String>, message: impl Into<String>) -> Self {
228        Self {
229            dialog,
230            title: title.into(),
231            message: message.into(),
232            kind: Default::default(),
233            buttons: Default::default(),
234            #[cfg(desktop)]
235            parent: None,
236        }
237    }
238
239    #[cfg(mobile)]
240    pub(crate) fn payload(&self) -> MessageDialogPayload<'_> {
241        let (ok_button_label, cancel_button_label) = match &self.buttons {
242            MessageDialogButtons::Ok => (Some(OK), None),
243            MessageDialogButtons::OkCancel => (Some(OK), Some(CANCEL)),
244            MessageDialogButtons::YesNo => (Some(YES), Some(NO)),
245            MessageDialogButtons::OkCustom(ok) => (Some(ok.as_str()), Some(CANCEL)),
246            MessageDialogButtons::OkCancelCustom(ok, cancel) => {
247                (Some(ok.as_str()), Some(cancel.as_str()))
248            }
249        };
250        MessageDialogPayload {
251            title: &self.title,
252            message: &self.message,
253            kind: &self.kind,
254            ok_button_label,
255            cancel_button_label,
256        }
257    }
258
259    /// Sets the dialog title.
260    pub fn title(mut self, title: impl Into<String>) -> Self {
261        self.title = title.into();
262        self
263    }
264
265    /// Set parent windows explicitly (optional)
266    #[cfg(desktop)]
267    pub fn parent<W: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle>(
268        mut self,
269        parent: &W,
270    ) -> Self {
271        if let (Ok(window_handle), Ok(display_handle)) =
272            (parent.window_handle(), parent.display_handle())
273        {
274            self.parent.replace(crate::desktop::WindowHandle::new(
275                window_handle.as_raw(),
276                display_handle.as_raw(),
277            ));
278        }
279        self
280    }
281
282    /// Sets the dialog buttons.
283    pub fn buttons(mut self, buttons: MessageDialogButtons) -> Self {
284        self.buttons = buttons;
285        self
286    }
287
288    /// Set type of a dialog.
289    ///
290    /// Depending on the system it can result in type specific icon to show up,
291    /// the will inform user it message is a error, warning or just information.
292    pub fn kind(mut self, kind: MessageDialogKind) -> Self {
293        self.kind = kind;
294        self
295    }
296
297    /// Shows a message dialog
298    pub fn show<F: FnOnce(bool) + Send + 'static>(self, f: F) {
299        show_message_dialog(self, f)
300    }
301
302    /// Shows a message dialog.
303    /// This is a blocking operation,
304    /// and should *NOT* be used when running on the main thread context.
305    pub fn blocking_show(self) -> bool {
306        blocking_fn!(self, show)
307    }
308}
309#[derive(Debug, Serialize)]
310pub(crate) struct Filter {
311    pub name: String,
312    pub extensions: Vec<String>,
313}
314
315/// The file dialog builder.
316///
317/// Constructs file picker dialogs that can select single/multiple files or directories.
318#[derive(Debug)]
319pub struct FileDialogBuilder<R: Runtime> {
320    #[allow(dead_code)]
321    pub(crate) dialog: Dialog<R>,
322    pub(crate) filters: Vec<Filter>,
323    pub(crate) starting_directory: Option<PathBuf>,
324    pub(crate) file_name: Option<String>,
325    pub(crate) title: Option<String>,
326    pub(crate) can_create_directories: Option<bool>,
327    #[cfg(desktop)]
328    pub(crate) parent: Option<crate::desktop::WindowHandle>,
329}
330
331#[cfg(mobile)]
332#[derive(Serialize)]
333#[serde(rename_all = "camelCase")]
334pub(crate) struct FileDialogPayload<'a> {
335    file_name: &'a Option<String>,
336    filters: &'a Vec<Filter>,
337    multiple: bool,
338}
339
340// raw window handle :(
341unsafe impl<R: Runtime> Send for FileDialogBuilder<R> {}
342
343impl<R: Runtime> FileDialogBuilder<R> {
344    /// Gets the default file dialog builder.
345    pub fn new(dialog: Dialog<R>) -> Self {
346        Self {
347            dialog,
348            filters: Vec::new(),
349            starting_directory: None,
350            file_name: None,
351            title: None,
352            can_create_directories: None,
353            #[cfg(desktop)]
354            parent: None,
355        }
356    }
357
358    #[cfg(mobile)]
359    pub(crate) fn payload(&self, multiple: bool) -> FileDialogPayload<'_> {
360        FileDialogPayload {
361            file_name: &self.file_name,
362            filters: &self.filters,
363            multiple,
364        }
365    }
366
367    /// Add file extension filter. Takes in the name of the filter, and list of extensions
368    #[must_use]
369    pub fn add_filter(mut self, name: impl Into<String>, extensions: &[&str]) -> Self {
370        self.filters.push(Filter {
371            name: name.into(),
372            extensions: extensions.iter().map(|e| e.to_string()).collect(),
373        });
374        self
375    }
376
377    /// Set starting directory of the dialog.
378    #[must_use]
379    pub fn set_directory<P: AsRef<Path>>(mut self, directory: P) -> Self {
380        self.starting_directory.replace(directory.as_ref().into());
381        self
382    }
383
384    /// Set starting file name of the dialog.
385    #[must_use]
386    pub fn set_file_name(mut self, file_name: impl Into<String>) -> Self {
387        self.file_name.replace(file_name.into());
388        self
389    }
390
391    /// Sets the parent window of the dialog.
392    #[cfg(desktop)]
393    #[must_use]
394    pub fn set_parent<
395        W: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle,
396    >(
397        mut self,
398        parent: &W,
399    ) -> Self {
400        if let (Ok(window_handle), Ok(display_handle)) =
401            (parent.window_handle(), parent.display_handle())
402        {
403            self.parent.replace(crate::desktop::WindowHandle::new(
404                window_handle.as_raw(),
405                display_handle.as_raw(),
406            ));
407        }
408        self
409    }
410
411    /// Set the title of the dialog.
412    #[must_use]
413    pub fn set_title(mut self, title: impl Into<String>) -> Self {
414        self.title.replace(title.into());
415        self
416    }
417
418    /// Set whether it should be possible to create new directories in the dialog. Enabled by default. **macOS only**.
419    pub fn set_can_create_directories(mut self, can: bool) -> Self {
420        self.can_create_directories.replace(can);
421        self
422    }
423
424    /// Shows the dialog to select a single file.
425    /// This is not a blocking operation,
426    /// and should be used when running on the main thread to avoid deadlocks with the event loop.
427    ///
428    /// For usage in other contexts such as commands, prefer [`Self::pick_file`].
429    ///
430    /// # Examples
431    ///
432    /// ```
433    /// use tauri_plugin_dialog::DialogExt;
434    /// tauri::Builder::default()
435    ///   .setup(|app| {
436    ///     app.dialog().file().pick_file(|file_path| {
437    ///       // do something with the optional file path here
438    ///       // the file path is `None` if the user closed the dialog
439    ///     });
440    ///     Ok(())
441    ///   });
442    /// ```
443    pub fn pick_file<F: FnOnce(Option<FilePath>) + Send + 'static>(self, f: F) {
444        pick_file(self, f)
445    }
446
447    /// Shows the dialog to select multiple files.
448    /// This is not a blocking operation,
449    /// and should be used when running on the main thread to avoid deadlocks with the event loop.
450    ///
451    /// # Reading the files
452    ///
453    /// The file paths cannot be read directly on Android as they are behind a content URI.
454    /// The recommended way to read the files is using the [`fs`](https://v2.tauri.app/plugin/file-system/) plugin:
455    ///
456    /// ```
457    /// use tauri_plugin_dialog::DialogExt;
458    /// use tauri_plugin_fs::FsExt;
459    /// tauri::Builder::default()
460    ///   .setup(|app| {
461    ///     let handle = app.handle().clone();
462    ///     app.dialog().file().pick_file(move |file_path| {
463    ///       let Some(path) = file_path else { return };
464    ///       let Ok(contents) = handle.fs().read_to_string(path) else {
465    ///         eprintln!("failed to read file, <todo add error handling!>");
466    ///         return;
467    ///       };
468    ///     });
469    ///     Ok(())
470    ///   });
471    /// ```
472    ///
473    /// See <https://developer.android.com/guide/topics/providers/content-provider-basics> for more information.
474    ///
475    /// # Examples
476    ///
477    /// ```
478    /// use tauri_plugin_dialog::DialogExt;
479    /// tauri::Builder::default()
480    ///   .setup(|app| {
481    ///     app.dialog().file().pick_files(|file_paths| {
482    ///       // do something with the optional file paths here
483    ///       // the file paths value is `None` if the user closed the dialog
484    ///     });
485    ///     Ok(())
486    ///   });
487    /// ```
488    pub fn pick_files<F: FnOnce(Option<Vec<FilePath>>) + Send + 'static>(self, f: F) {
489        pick_files(self, f)
490    }
491
492    /// Shows the dialog to select a single folder.
493    /// This is not a blocking operation,
494    /// and should be used when running on the main thread to avoid deadlocks with the event loop.
495    ///
496    /// # Examples
497    ///
498    /// ```
499    /// use tauri_plugin_dialog::DialogExt;
500    /// tauri::Builder::default()
501    ///   .setup(|app| {
502    ///     app.dialog().file().pick_folder(|folder_path| {
503    ///       // do something with the optional folder path here
504    ///       // the folder path is `None` if the user closed the dialog
505    ///     });
506    ///     Ok(())
507    ///   });
508    /// ```
509    #[cfg(desktop)]
510    pub fn pick_folder<F: FnOnce(Option<FilePath>) + Send + 'static>(self, f: F) {
511        pick_folder(self, f)
512    }
513
514    /// Shows the dialog to select multiple folders.
515    /// This is not a blocking operation,
516    /// and should be used when running on the main thread to avoid deadlocks with the event loop.
517    ///
518    /// # Examples
519    ///
520    /// ```
521    /// use tauri_plugin_dialog::DialogExt;
522    /// tauri::Builder::default()
523    ///   .setup(|app| {
524    ///     app.dialog().file().pick_folders(|file_paths| {
525    ///       // do something with the optional folder paths here
526    ///       // the folder paths value is `None` if the user closed the dialog
527    ///     });
528    ///     Ok(())
529    ///   });
530    /// ```
531    #[cfg(desktop)]
532    pub fn pick_folders<F: FnOnce(Option<Vec<FilePath>>) + Send + 'static>(self, f: F) {
533        pick_folders(self, f)
534    }
535
536    /// Shows the dialog to save a file.
537    ///
538    /// This is not a blocking operation,
539    /// and should be used when running on the main thread to avoid deadlocks with the event loop.
540    ///
541    /// # Examples
542    ///
543    /// ```
544    /// use tauri_plugin_dialog::DialogExt;
545    /// tauri::Builder::default()
546    ///   .setup(|app| {
547    ///     app.dialog().file().save_file(|file_path| {
548    ///       // do something with the optional file path here
549    ///       // the file path is `None` if the user closed the dialog
550    ///     });
551    ///     Ok(())
552    ///   });
553    /// ```
554    pub fn save_file<F: FnOnce(Option<FilePath>) + Send + 'static>(self, f: F) {
555        save_file(self, f)
556    }
557}
558
559/// Blocking APIs.
560impl<R: Runtime> FileDialogBuilder<R> {
561    /// Shows the dialog to select a single file.
562    /// This is a blocking operation,
563    /// and should *NOT* be used when running on the main thread context.
564    ///
565    /// # Examples
566    ///
567    /// ```
568    /// use tauri_plugin_dialog::DialogExt;
569    /// #[tauri::command]
570    /// async fn my_command(app: tauri::AppHandle) {
571    ///   let file_path = app.dialog().file().blocking_pick_file();
572    ///   // do something with the optional file path here
573    ///   // the file path is `None` if the user closed the dialog
574    /// }
575    /// ```
576    pub fn blocking_pick_file(self) -> Option<FilePath> {
577        blocking_fn!(self, pick_file)
578    }
579
580    /// Shows the dialog to select multiple files.
581    /// This is a blocking operation,
582    /// and should *NOT* be used when running on the main thread context.
583    ///
584    /// # Examples
585    ///
586    /// ```
587    /// use tauri_plugin_dialog::DialogExt;
588    /// #[tauri::command]
589    /// async fn my_command(app: tauri::AppHandle) {
590    ///   let file_path = app.dialog().file().blocking_pick_files();
591    ///   // do something with the optional file paths here
592    ///   // the file paths value is `None` if the user closed the dialog
593    /// }
594    /// ```
595    pub fn blocking_pick_files(self) -> Option<Vec<FilePath>> {
596        blocking_fn!(self, pick_files)
597    }
598
599    /// Shows the dialog to select a single folder.
600    /// This is a blocking operation,
601    /// and should *NOT* be used when running on the main thread context.
602    ///
603    /// # Examples
604    ///
605    /// ```
606    /// use tauri_plugin_dialog::DialogExt;
607    /// #[tauri::command]
608    /// async fn my_command(app: tauri::AppHandle) {
609    ///   let folder_path = app.dialog().file().blocking_pick_folder();
610    ///   // do something with the optional folder path here
611    ///   // the folder path is `None` if the user closed the dialog
612    /// }
613    /// ```
614    #[cfg(desktop)]
615    pub fn blocking_pick_folder(self) -> Option<FilePath> {
616        blocking_fn!(self, pick_folder)
617    }
618
619    /// Shows the dialog to select multiple folders.
620    /// This is a blocking operation,
621    /// and should *NOT* be used when running on the main thread context.
622    ///
623    /// # Examples
624    ///
625    /// ```
626    /// use tauri_plugin_dialog::DialogExt;
627    /// #[tauri::command]
628    /// async fn my_command(app: tauri::AppHandle) {
629    ///   let folder_paths = app.dialog().file().blocking_pick_folders();
630    ///   // do something with the optional folder paths here
631    ///   // the folder paths value is `None` if the user closed the dialog
632    /// }
633    /// ```
634    #[cfg(desktop)]
635    pub fn blocking_pick_folders(self) -> Option<Vec<FilePath>> {
636        blocking_fn!(self, pick_folders)
637    }
638
639    /// Shows the dialog to save a file.
640    /// This is a blocking operation,
641    /// and should *NOT* be used when running on the main thread context.
642    ///
643    /// # Examples
644    ///
645    /// ```
646    /// use tauri_plugin_dialog::DialogExt;
647    /// #[tauri::command]
648    /// async fn my_command(app: tauri::AppHandle) {
649    ///   let file_path = app.dialog().file().blocking_save_file();
650    ///   // do something with the optional file path here
651    ///   // the file path is `None` if the user closed the dialog
652    /// }
653    /// ```
654    pub fn blocking_save_file(self) -> Option<FilePath> {
655        blocking_fn!(self, save_file)
656    }
657}