rustydialogs 0.4.2

Provides a simple and cross-platform way to display native dialog boxes.
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*!
Rusty Dialogs is a cross-platform library for showing native dialog boxes and notifications.

Supported platforms and backends:

### Windows

Win32-based legacy dialogs compatible with any COM apartment model.

By default, notifications use a tray icon with balloon tips.

Optional WinRT-Toast notifications are available on Windows 10 and later. (feature: `winrt-toast`)

### Linux & BSDs

By default, executable-based backends (`kdialog` and `zenity`) are used.

Optional GTK3 and GTK4 backends are available with libnotify-based notifications. (feature: `gtk3`, `gtk4`)

XDG desktop portal support is also available, but limited to file and folder dialogs. (feature: `xdg-portal`)

### macOS

By default, AppleScript-based dialogs are used.

Optional AppKit-based dialogs and notifications are also available. (feature: `appkit`)
*/

use std::path::{Path, PathBuf};
use raw_window_handle::HasWindowHandle;

mod utils;

/// Icon types for message dialogs.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MessageIcon {
	/// Information icon.
	Info,
	/// Warning icon.
	Warning,
	/// Error icon.
	Error,
	/// Question icon.
	Question,
}

/// Button configurations for message dialogs.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MessageButtons {
	/// OK button only.
	Ok,
	/// OK and Cancel buttons.
	OkCancel,
	/// Yes and No buttons.
	YesNo,
	/// Yes, No, and Cancel buttons.
	YesNoCancel,
}

/// Result of a message dialog.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MessageResult {
	/// OK result.
	Ok,
	/// Cancel result.
	Cancel,
	/// Yes result.
	Yes,
	/// No result.
	No,
}

/// Message box dialog.
///
/// ```no_run
/// let result = rustydialogs::MessageBox {
/// 	title: "Confirm Action",
/// 	message: "Are you sure you want to proceed?",
/// 	icon: rustydialogs::MessageIcon::Question,
/// 	buttons: rustydialogs::MessageButtons::YesNo,
/// 	owner: None,
/// }.show();
/// if result == Some(rustydialogs::MessageResult::Yes) {
/// 	println!("User chose Yes");
/// }
/// ```
#[derive(Copy, Clone)]
pub struct MessageBox<'a> {
	/// The title of the dialog.
	pub title: &'a str,
	/// The message to display to the user.
	pub message: &'a str,
	/// The icon to show in the dialog.
	pub icon: MessageIcon,
	/// The buttons to show in the dialog.
	pub buttons: MessageButtons,
	/// The owner window of the dialog.
	pub owner: Option<&'a dyn HasWindowHandle>,
}

impl<'a> MessageBox<'a> {
	/// Show the dialog.
	///
	/// Prefer to check if the dialog result matches `Some(Ok)` or `Some(Yes)` rather than checking for `Some(No)` or `Some(Cancel)`.
	///
	/// When the dialog is dismissed (closing the dialog or pressing ESC), the result maybe `None` or may be any of the message results even if that button is not present (e.g. `Some(Cancel)`).
	#[inline]
	pub fn show(&self) -> Option<MessageResult> {
		message_box(self)
	}
}


/// File filter for file dialogs.
#[derive(Copy, Clone, Debug)]
pub struct FileFilter<'a> {
	/// The description of the file filter, e.g. `"Text Files"`.
	pub desc: &'a str,
	/// The file patterns of the file filter, e.g. `&["*.txt"]` or `&["*.jpg", "*.jpeg"]`.
	pub patterns: &'a [&'a str],
}

/// File dialog.
///
/// The file dialog allows the user to select a file or multiple files, or to specify a file name for saving.
///
/// ```no_run
/// use std::env;
///
/// let file = rustydialogs::FileDialog {
/// 	title: "Open File",
/// 	path: env::current_dir().ok().as_deref(),
/// 	filter: Some(&[
/// 		rustydialogs::FileFilter {
/// 			desc: "Text Files",
/// 			patterns: &["*.txt", "*.md"],
/// 		},
/// 	]),
/// 	owner: None,
/// }.pick_file();
///
/// if let Some(path) = file {
/// 	println!("Picked file: {}", path.display());
/// }
/// ```
#[derive(Copy, Clone)]
pub struct FileDialog<'a> {
	/// The title of the dialog.
	pub title: &'a str,
	/// The initial path to show in the file dialog.
	///
	/// If the path is relative, it is joined with the current working directory.
	///
	/// If the resulting path exists and is a directory, no default file name is provided.
	///
	/// Otherwise, [`Path::file_name`] is used as the default file name.
	pub path: Option<&'a Path>,
	/// An optional list of file filters to show in the file dialog.
	pub filter: Option<&'a [FileFilter<'a>]>,
	/// The owner window of the dialog.
	pub owner: Option<&'a dyn HasWindowHandle>,
}

impl<'a> FileDialog<'a> {
	/// Show open file dialog, allowing the user to select a single file.
	#[inline]
	pub fn pick_file(&self) -> Option<PathBuf> {
		pick_file(self)
	}

	/// Show open file dialog, allowing the user to select multiple files.
	#[inline]
	pub fn pick_files(&self) -> Option<Vec<PathBuf>> {
		pick_files(self)
	}

	/// Show save file dialog.
	#[inline]
	pub fn save_file(&self) -> Option<PathBuf> {
		save_file(self)
	}
}

/// Folder dialog.
///
/// The folder dialog allows the user to select a folder or directory.
///
/// ```no_run
/// use std::env;
///
/// let folder = rustydialogs::FolderDialog {
/// 	title: "Select Folder",
/// 	directory: env::current_dir().ok().as_deref(),
/// 	owner: None,
/// }.show();
///
/// if let Some(path) = folder {
/// 	println!("Picked folder: {}", path.display());
/// }
/// ```
#[derive(Copy, Clone)]
pub struct FolderDialog<'a> {
	/// The title of the dialog.
	pub title: &'a str,
	/// The initial directory to show in the folder dialog.
	pub directory: Option<&'a Path>,
	/// The owner window of the dialog.
	pub owner: Option<&'a dyn HasWindowHandle>,
}

impl<'a> FolderDialog<'a> {
	/// Show the dialog.
	#[inline]
	pub fn show(&self) -> Option<std::path::PathBuf> {
		folder_dialog(self)
	}
}

/// Modes for text input dialogs.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TextInputMode {
	/// Single line text input dialog.
	SingleLine,
	/// Multi-line text input dialog.
	MultiLine,
	/// Password input dialog, which hides the input text.
	Password,
}

/// Text input dialog.
///
/// The text input dialog allows the user to enter text, which is returned as a string.
///
/// ```no_run
/// let name = rustydialogs::TextInput {
/// 	title: "User Name",
/// 	message: "Enter your name:",
/// 	value: "",
/// 	mode: rustydialogs::TextInputMode::SingleLine,
/// 	owner: None,
/// }.show();
///
/// if let Some(name) = name {
/// 	println!("Hello, {name}!");
/// }
/// ```
#[derive(Copy, Clone)]
pub struct TextInput<'a> {
	/// The title of the dialog.
	pub title: &'a str,
	/// The message to display to the user.
	pub message: &'a str,
	/// The initial value to display in the text input.
	pub value: &'a str,
	/// The mode of the text input, which determines the type of dialog shown and how the input is handled.
	pub mode: TextInputMode,
	/// The owner window of the dialog.
	pub owner: Option<&'a dyn HasWindowHandle>,
}

impl<'a> TextInput<'a> {
	/// Show the dialog.
	///
	/// Returns `Some(String)` if the user provided input and confirmed the dialog, or `None` if the user cancelled the dialog.
	#[inline]
	pub fn show(&self) -> Option<String> {
		text_input(self)
	}
}

/// Color value.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ColorValue {
	/// The red component of the color, in the range [0, 255].
	pub red: u8,
	/// The green component of the color, in the range [0, 255].
	pub green: u8,
	/// The blue component of the color, in the range [0, 255].
	pub blue: u8,
}

/// Color picker dialog.
///
/// The color picker dialog allows the user to select a color, which is returned as an RGB value.
/// The dialog may also show a palette of predefined colors for the user to choose from.
///
/// ```no_run
/// let color = rustydialogs::ColorPicker {
/// 	title: "Pick a Color",
/// 	value: rustydialogs::ColorValue {
/// 		red: 64,
/// 		green: 128,
/// 		blue: 255,
/// 	},
/// 	owner: None,
/// }.show();
///
/// if let Some(color) = color {
/// 	println!("RGB({}, {}, {})", color.red, color.green, color.blue);
/// }
/// ```
#[derive(Copy, Clone)]
pub struct ColorPicker<'a> {
	/// The title of the dialog.
	pub title: &'a str,
	/// The initial color value to show in the color picker dialog.
	pub value: ColorValue,
	/// The owner window of the dialog.
	pub owner: Option<&'a dyn HasWindowHandle>,
}

impl<'a> ColorPicker<'a> {
	/// Show the dialog.
	///
	/// Returns `Some(ColorValue)` if the user selected a color and confirmed the dialog, or `None` if the user cancelled the dialog.
	#[inline]
	pub fn show(&self) -> Option<ColorValue> {
		color_picker(self)
	}
}

/// Notification.
///
/// Shows a brief message to the user without blocking their interaction with the application.
///
/// ```no_run
/// // Define a unique application identifier for the notification system.
/// const APP_ID: &str = "com.example.myapp";
///
/// // Invoke setup at application initialization to ensure the application
/// // is registered and ready to show notifications later.
/// rustydialogs::Notification::setup(APP_ID);
///
/// rustydialogs::Notification {
/// 	app_id: APP_ID,
/// 	title: "Task Complete",
/// 	message: "All files were processed successfully.",
/// 	icon: rustydialogs::MessageIcon::Info,
/// 	timeout: rustydialogs::Notification::SHORT_TIMEOUT,
/// }.show();
/// ```
#[derive(Copy, Clone, Debug)]
pub struct Notification<'a> {
	/// Application identifier used by notification backends.
	///
	/// This is a best-effort hint: some backends may ignore it, and some only honor the first value seen by the process/session.
	pub app_id: &'a str,
	/// The title of the notification.
	pub title: &'a str,
	/// The message to display in the notification.
	pub message: &'a str,
	/// The icon to show in the notification.
	// Future: Change to optional Option<MessageIcon>
	pub icon: MessageIcon,
	/// Timeout in milliseconds after which the notification should automatically close.
	///
	/// A value less than or equal to `0` means that the notification will not automatically close.
	///
	/// This is a best-effort hint: some backends may ignore it and use their own default timeout, or may not support timeouts at all.
	// Future: Change to dedicated duration type
	pub timeout: i32,
}

impl<'a> Notification<'a> {
	/// Short timeout duration in milliseconds for notification popups.
	pub const SHORT_TIMEOUT: i32 = 5000;
	/// Long timeout duration in milliseconds for notification popups.
	pub const LONG_TIMEOUT: i32 = 10000;

	/// Perform any necessary setup for notifications, such as registering the application with the notification system.
	///
	/// This step is optional, when skipped the library will attempt to perform any necessary setup automatically when showing the first notification,
	/// but this method can be used to ensure that the setup is done at a specific time in the application lifecycle.
	///
	/// ### Windows
	///
	/// By default, this initializes a process-wide tray icon used for balloon notifications.
	///
	/// When using the `winrt-toast` backend, this creates a Start Menu shortcut for the application with the provided application identifier, which is required for showing toast notifications on Windows.
	/// It is recommended to call this method during application initialization before showing any notifications or the first notification may be skipped due to delays in the shortcut creation process.
	///
	/// ### Linux
	///
	/// When using the `libnotify` backend, this registers the application with the notification system using the provided application identifier.
	#[inline]
	pub fn setup(app_id: &str) {
		// Future: Return whether setup was successful (API breaking change)
		notify_setup(app_id);
	}

	/// Show the notification.
	#[inline]
	pub fn show(&self) {
		notify(self)
	}
}

#[cfg(windows)]
mod win32;
#[cfg(windows)]
use win32::*;

#[cfg(any(
	target_os = "linux",
	target_os = "freebsd",
	target_os = "dragonfly",
	target_os = "netbsd",
	target_os = "openbsd",
))]
mod linux;
#[cfg(any(
	target_os = "linux",
	target_os = "freebsd",
	target_os = "dragonfly",
	target_os = "netbsd",
	target_os = "openbsd",
))]
use linux::*;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
use macos::*;

#[cfg(not(any(
	windows,
	target_os = "linux",
	target_os = "freebsd",
	target_os = "dragonfly",
	target_os = "netbsd",
	target_os = "openbsd",
	target_os = "macos",
)))]
mod unsupported;
#[cfg(not(any(
	windows,
	target_os = "linux",
	target_os = "freebsd",
	target_os = "dragonfly",
	target_os = "netbsd",
	target_os = "openbsd",
	target_os = "macos",
)))]
use unsupported::*;