-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
dialog.rs
217 lines (200 loc) · 6.12 KB
/
dialog.rs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use super::InvokeResponse;
#[cfg(any(dialog_open, dialog_save))]
use crate::api::dialog::FileDialogBuilder;
use crate::{
api::dialog::{ask as ask_dialog, confirm as confirm_dialog, message as message_dialog},
runtime::Runtime,
Window,
};
use serde::Deserialize;
use std::{path::PathBuf, sync::mpsc::channel};
#[allow(dead_code)]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DialogFilter {
name: String,
extensions: Vec<String>,
}
/// The options for the open dialog API.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenDialogOptions {
/// The filters of the dialog.
#[serde(default)]
pub filters: Vec<DialogFilter>,
/// Whether the dialog allows multiple selection or not.
#[serde(default)]
pub multiple: bool,
/// Whether the dialog is a directory selection (`true` value) or file selection (`false` value).
#[serde(default)]
pub directory: bool,
/// The initial path of the dialog.
pub default_path: Option<PathBuf>,
}
/// The options for the save dialog API.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SaveDialogOptions {
/// The filters of the dialog.
#[serde(default)]
pub filters: Vec<DialogFilter>,
/// The initial path of the dialog.
pub default_path: Option<PathBuf>,
}
/// The API descriptor.
#[derive(Deserialize)]
#[serde(tag = "cmd", rename_all = "camelCase")]
#[allow(clippy::enum_variant_names)]
pub enum Cmd {
/// The open dialog API.
OpenDialog {
options: OpenDialogOptions,
},
/// The save dialog API.
SaveDialog {
options: SaveDialogOptions,
},
MessageDialog {
message: String,
},
AskDialog {
title: Option<String>,
message: String,
},
ConfirmDialog {
title: Option<String>,
message: String,
},
}
impl Cmd {
#[allow(unused_variables)]
pub fn run<R: Runtime>(self, window: Window<R>) -> crate::Result<InvokeResponse> {
match self {
#[cfg(dialog_open)]
Self::OpenDialog { options } => open(&window, options),
#[cfg(not(dialog_open))]
Self::OpenDialog { .. } => Err(crate::Error::ApiNotAllowlisted("dialog > open".to_string())),
#[cfg(dialog_save)]
Self::SaveDialog { options } => save(window, options),
#[cfg(not(dialog_save))]
Self::SaveDialog { .. } => Err(crate::Error::ApiNotAllowlisted("dialog > save".to_string())),
Self::MessageDialog { message } => {
let exe = std::env::current_exe()?;
message_dialog(
Some(&window),
&window.app_handle.package_info().name,
message,
);
Ok(().into())
}
Self::AskDialog { title, message } => {
let answer = ask(
&window,
title.unwrap_or_else(|| window.app_handle.package_info().name.clone()),
message,
)?;
Ok(answer)
}
Self::ConfirmDialog { title, message } => {
let answer = confirm(
&window,
title.unwrap_or_else(|| window.app_handle.package_info().name.clone()),
message,
)?;
Ok(answer)
}
}
}
}
#[cfg(any(dialog_open, dialog_save))]
fn set_default_path(
mut dialog_builder: FileDialogBuilder,
default_path: PathBuf,
) -> FileDialogBuilder {
if default_path.is_file() || !default_path.exists() {
if let (Some(parent), Some(file_name)) = (default_path.parent(), default_path.file_name()) {
dialog_builder = dialog_builder.set_directory(parent);
dialog_builder = dialog_builder.set_file_name(&file_name.to_string_lossy().to_string());
} else {
dialog_builder = dialog_builder.set_directory(default_path);
}
dialog_builder
} else {
dialog_builder.set_directory(default_path)
}
}
/// Shows an open dialog.
#[cfg(dialog_open)]
#[allow(unused_variables)]
pub fn open<R: Runtime>(
window: &Window<R>,
options: OpenDialogOptions,
) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
#[cfg(any(windows, target_os = "macos"))]
{
dialog_builder = dialog_builder.set_parent(window);
}
if let Some(default_path) = options.default_path {
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
}
let (tx, rx) = channel();
if options.directory {
dialog_builder.pick_folder(move |p| tx.send(p.into()).unwrap());
} else if options.multiple {
dialog_builder.pick_files(move |p| tx.send(p.into()).unwrap());
} else {
dialog_builder.pick_file(move |p| tx.send(p.into()).unwrap());
}
Ok(rx.recv().unwrap())
}
/// Shows a save dialog.
#[cfg(dialog_save)]
#[allow(unused_variables)]
pub fn save<R: Runtime>(
window: Window<R>,
options: SaveDialogOptions,
) -> crate::Result<InvokeResponse> {
let mut dialog_builder = FileDialogBuilder::new();
#[cfg(any(windows, target_os = "macos"))]
{
dialog_builder = dialog_builder.set_parent(&window);
}
if let Some(default_path) = options.default_path {
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
}
let (tx, rx) = channel();
dialog_builder.save_file(move |p| tx.send(p).unwrap());
Ok(rx.recv().unwrap().into())
}
/// Shows a dialog with a yes/no question.
pub fn ask<R: Runtime>(
window: &Window<R>,
title: String,
message: String,
) -> crate::Result<InvokeResponse> {
let (tx, rx) = channel();
ask_dialog(Some(window), title, message, move |m| tx.send(m).unwrap());
Ok(rx.recv().unwrap().into())
}
/// Shows a dialog with a ok/cancel message.
pub fn confirm<R: Runtime>(
window: &Window<R>,
title: String,
message: String,
) -> crate::Result<InvokeResponse> {
let (tx, rx) = channel();
confirm_dialog(Some(window), title, message, move |m| tx.send(m).unwrap());
Ok(rx.recv().unwrap().into())
}