mitsuami_core/services/
dialogs.rs1use std::path::{Path, PathBuf};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum AlertStyle {
7 #[default]
8 Info,
9 Warning,
10 Critical,
12}
13
14#[derive(Clone, Debug, PartialEq)]
16pub struct Alert {
17 pub title: String,
18 pub message: Option<String>,
19 pub buttons: Vec<String>,
21 pub style: AlertStyle,
22 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 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 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#[derive(Clone, Debug, PartialEq, Eq)]
64pub enum Launch {
65 Path(PathBuf),
68 Url(String),
71}
72
73#[derive(Clone, Debug, PartialEq)]
77pub struct FileFilter {
78 pub name: String,
79 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 pub fn all(name: impl Into<String>) -> FileFilter {
91 FileFilter { name: name.into(), extensions: Vec::new() }
92 }
93
94 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 pub directories: bool,
106 pub filters: Vec<FileFilter>,
107 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 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 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 pub fn start_folder(mut self, folder: impl Into<PathBuf>) -> SaveFile {
176 self.start_folder = Some(folder.into());
177 self
178 }
179}
180
181pub fn existing_folder(folder: &Option<PathBuf>) -> Option<&Path> {
185 folder.as_deref().filter(|f| f.is_dir())
186}