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