1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::path::Path;

pub use nfd::Response;
use nfd::{open_dialog, DialogType};
pub use tauri_dialog::DialogSelection;
use tauri_dialog::{DialogBuilder, DialogButtons, DialogStyle};

fn open_dialog_internal(
  dialog_type: DialogType,
  filter: Option<impl AsRef<str>>,
  default_path: Option<impl AsRef<Path>>,
) -> crate::Result<Response> {
  let response = open_dialog(
    filter.map(|s| s.as_ref().to_string()).as_deref(),
    default_path
      .map(|s| s.as_ref().to_string_lossy().to_string())
      .as_deref(),
    dialog_type,
  )?;
  match response {
    Response::Cancel => Err(crate::Error::Dialog("user cancelled".into()).into()),
    _ => Ok(response),
  }
}

/// Displays a dialog with a message and an optional title with a "yes" and a "no" button
pub fn ask(message: impl AsRef<str>, title: impl AsRef<str>) -> DialogSelection {
  DialogBuilder::new()
    .message(message.as_ref())
    .title(title.as_ref())
    .style(DialogStyle::Question)
    .buttons(DialogButtons::YesNo)
    .build()
    .show()
}

/// Displays a message dialog
pub fn message(message: impl AsRef<str>, title: impl AsRef<str>) {
  DialogBuilder::new()
    .message(message.as_ref())
    .title(title.as_ref())
    .style(DialogStyle::Info)
    .build()
    .show();
}

/// Open single select file dialog
pub fn select(
  filter_list: Option<impl AsRef<str>>,
  default_path: Option<impl AsRef<Path>>,
) -> crate::Result<Response> {
  open_dialog_internal(DialogType::SingleFile, filter_list, default_path)
}

/// Open multiple select file dialog
pub fn select_multiple(
  filter_list: Option<impl AsRef<str>>,
  default_path: Option<impl AsRef<Path>>,
) -> crate::Result<Response> {
  open_dialog_internal(DialogType::MultipleFiles, filter_list, default_path)
}

/// Open save dialog
pub fn save_file(
  filter_list: Option<impl AsRef<str>>,
  default_path: Option<impl AsRef<Path>>,
) -> crate::Result<Response> {
  open_dialog_internal(DialogType::SaveFile, filter_list, default_path)
}

/// Open pick folder dialog
pub fn pick_folder(default_path: Option<impl AsRef<Path>>) -> crate::Result<Response> {
  let filter: Option<String> = None;
  open_dialog_internal(DialogType::PickFolder, filter, default_path)
}