Skip to main content

mitsuami_core/services/
dialogs.rs

1//! Alerts, file dialogs and launching: what an app asks for.
2
3use std::path::{Path, PathBuf};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum AlertStyle {
7    #[default]
8    Info,
9    Warning,
10    /// Destructive or irreversible consequences.
11    Critical,
12}
13
14/// A message with buttons. The reply is the index of the chosen button.
15#[derive(Clone, Debug, PartialEq)]
16pub struct Alert {
17    pub title: String,
18    pub message: Option<String>,
19    /// In order of importance: the first is the default (Return) button.
20    pub buttons: Vec<String>,
21    pub style: AlertStyle,
22    /// The button that destroys something (Delete, Discard), drawn as
23    /// destructive where the platform's alerts draw one.
24    pub destructive: Option<usize>,
25}
26
27impl Alert {
28    pub fn new(title: impl Into<String>) -> Alert {
29        Alert { title: title.into(), message: None, buttons: Vec::new(), style: AlertStyle::Info, destructive: None }
30    }
31
32    pub fn message(mut self, message: impl Into<String>) -> Alert {
33        self.message = Some(message.into());
34        self
35    }
36
37    pub fn button(mut self, title: impl Into<String>) -> Alert {
38        self.buttons.push(title.into());
39        self
40    }
41
42    /// A button, after the others, that destroys something: TextEdit's
43    /// Delete when closing a document never saved.
44    pub fn destructive_button(mut self, title: impl Into<String>) -> Alert {
45        self.destructive = Some(self.buttons.len());
46        self.buttons.push(title.into());
47        self
48    }
49
50    pub fn style(mut self, style: AlertStyle) -> Alert {
51        self.style = style;
52        self
53    }
54
55    /// The buttons to show: "OK", in the app's language, when none were
56    /// given.
57    pub fn effective_buttons(&self) -> Vec<String> {
58        if self.buttons.is_empty() { vec![crate::l10n::tr("mitsuami-alert-ok", &[])] } else { self.buttons.clone() }
59    }
60}
61
62/// What [`launch`](super::launch) opens, in the app the platform picks.
63#[derive(Clone, Debug, PartialEq, Eq)]
64pub enum Launch {
65    /// A file in the app set for its type, a folder in the file manager,
66    /// an app itself.
67    Path(PathBuf),
68    /// A URL in the app set for its scheme: the browser for `https:`, the
69    /// mail app for `mailto:`.
70    Url(String),
71}
72
73/// Files to offer in a file dialog, e.g. `FileFilter::new("Images", ["png", "jpg"])`.
74/// Where the platform lets the user choose between filters, they're offered
75/// in order, the first one chosen.
76#[derive(Clone, Debug, PartialEq)]
77pub struct FileFilter {
78    pub name: String,
79    /// Empty: every file ([`FileFilter::all`]).
80    pub extensions: Vec<String>,
81}
82
83impl FileFilter {
84    pub fn new<S: Into<String>>(name: impl Into<String>, extensions: impl IntoIterator<Item = S>) -> FileFilter {
85        FileFilter { name: name.into(), extensions: extensions.into_iter().map(Into::into).collect() }
86    }
87
88    /// Every file, e.g. `FileFilter::all("All files")` after a filter by
89    /// type, so a file the types miss can still be chosen.
90    pub fn all(name: impl Into<String>) -> FileFilter {
91        FileFilter { name: name.into(), extensions: Vec::new() }
92    }
93
94    /// Whether this filter lets every file through.
95    pub fn is_all(&self) -> bool {
96        self.extensions.is_empty()
97    }
98}
99
100#[derive(Clone, Debug, Default, PartialEq)]
101pub struct OpenFile {
102    pub title: Option<String>,
103    pub multiple: bool,
104    /// Choose folders instead of files.
105    pub directories: bool,
106    pub filters: Vec<FileFilter>,
107    /// The folder the dialog opens in. `None`, or one that isn't there,
108    /// leaves it to the platform (often the last folder used).
109    pub start_folder: Option<PathBuf>,
110}
111
112impl OpenFile {
113    pub fn new() -> OpenFile {
114        OpenFile::default()
115    }
116
117    pub fn title(mut self, title: impl Into<String>) -> OpenFile {
118        self.title = Some(title.into());
119        self
120    }
121
122    pub fn multiple(mut self) -> OpenFile {
123        self.multiple = true;
124        self
125    }
126
127    pub fn directories(mut self) -> OpenFile {
128        self.directories = true;
129        self
130    }
131
132    pub fn filter(mut self, filter: FileFilter) -> OpenFile {
133        self.filters.push(filter);
134        self
135    }
136
137    /// Opens the dialog in `folder`, e.g. the folder of the file a field
138    /// names.
139    pub fn start_folder(mut self, folder: impl Into<PathBuf>) -> OpenFile {
140        self.start_folder = Some(folder.into());
141        self
142    }
143}
144
145#[derive(Clone, Debug, Default, PartialEq)]
146pub struct SaveFile {
147    pub title: Option<String>,
148    pub default_name: Option<String>,
149    pub filters: Vec<FileFilter>,
150    /// The folder the dialog opens in, as for [`OpenFile::start_folder`].
151    pub start_folder: Option<PathBuf>,
152}
153
154impl SaveFile {
155    pub fn new() -> SaveFile {
156        SaveFile::default()
157    }
158
159    pub fn title(mut self, title: impl Into<String>) -> SaveFile {
160        self.title = Some(title.into());
161        self
162    }
163
164    pub fn name(mut self, name: impl Into<String>) -> SaveFile {
165        self.default_name = Some(name.into());
166        self
167    }
168
169    pub fn filter(mut self, filter: FileFilter) -> SaveFile {
170        self.filters.push(filter);
171        self
172    }
173
174    /// Opens the dialog in `folder`.
175    pub fn start_folder(mut self, folder: impl Into<PathBuf>) -> SaveFile {
176        self.start_folder = Some(folder.into());
177        self
178    }
179}
180
181/// For backends: a dialog's start folder, if it's there, so a missing one
182/// leaves the choice to the platform everywhere, whatever each dialog
183/// would make of it.
184pub fn existing_folder(folder: &Option<PathBuf>) -> Option<&Path> {
185    folder.as_deref().filter(|f| f.is_dir())
186}