dialog.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #[cfg(any(dialog_open, dialog_save))]
  5. use std::path::{Path, PathBuf};
  6. /// The file dialog builder.
  7. /// Constructs file picker dialogs that can select single/multiple files or directories.
  8. #[cfg(any(dialog_open, dialog_save))]
  9. #[derive(Debug, Default)]
  10. pub struct FileDialogBuilder(rfd::FileDialog);
  11. #[cfg(any(dialog_open, dialog_save))]
  12. impl FileDialogBuilder {
  13. /// Gets the default file dialog builder.
  14. pub fn new() -> Self {
  15. Default::default()
  16. }
  17. /// Add file extension filter. Takes in the name of the filter, and list of extensions
  18. pub fn add_filter(mut self, name: impl AsRef<str>, extensions: &[&str]) -> Self {
  19. self.0 = self.0.add_filter(name.as_ref(), extensions);
  20. self
  21. }
  22. /// Set starting directory of the dialog.
  23. pub fn set_directory<P: AsRef<Path>>(mut self, directory: P) -> Self {
  24. self.0 = self.0.set_directory(directory);
  25. self
  26. }
  27. /// Set starting file name of the dialog.
  28. pub fn set_file_name(mut self, file_name: &str) -> Self {
  29. self.0 = self.0.set_file_name(file_name);
  30. self
  31. }
  32. #[cfg(windows)]
  33. /// Sets the parent window of the dialog.
  34. pub fn set_parent<W: raw_window_handle::HasRawWindowHandle>(mut self, parent: &W) -> Self {
  35. self.0 = self.0.set_parent(parent);
  36. self
  37. }
  38. /// Pick one file.
  39. pub fn pick_file(self) -> Option<PathBuf> {
  40. self.0.pick_file()
  41. }
  42. /// Pick multiple files.
  43. pub fn pick_files(self) -> Option<Vec<PathBuf>> {
  44. self.0.pick_files()
  45. }
  46. /// Pick one folder.
  47. pub fn pick_folder(self) -> Option<PathBuf> {
  48. self.0.pick_folder()
  49. }
  50. /// Opens save file dialog.
  51. pub fn save_file(self) -> Option<PathBuf> {
  52. self.0.save_file()
  53. }
  54. }
  55. /// Response for the ask dialog
  56. #[derive(Debug, Clone, PartialEq, Eq)]
  57. pub enum AskResponse {
  58. /// User confirmed.
  59. Yes,
  60. /// User denied.
  61. No,
  62. }
  63. /// Displays a dialog with a message and an optional title with a "yes" and a "no" button
  64. pub fn ask(title: impl AsRef<str>, message: impl AsRef<str>) -> AskResponse {
  65. match rfd::MessageDialog::new()
  66. .set_title(title.as_ref())
  67. .set_description(message.as_ref())
  68. .set_buttons(rfd::MessageButtons::YesNo)
  69. .set_level(rfd::MessageLevel::Info)
  70. .show()
  71. {
  72. true => AskResponse::Yes,
  73. false => AskResponse::No,
  74. }
  75. }
  76. /// Displays a message dialog
  77. pub fn message(title: impl AsRef<str>, message: impl AsRef<str>) {
  78. rfd::MessageDialog::new()
  79. .set_title(title.as_ref())
  80. .set_description(message.as_ref())
  81. .set_buttons(rfd::MessageButtons::Ok)
  82. .set_level(rfd::MessageLevel::Info)
  83. .show();
  84. }