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}
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 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#[derive(Clone, Debug, PartialEq, Eq)]
53pub enum Launch {
54 Path(PathBuf),
57 Url(String),
60}
61
62#[derive(Clone, Debug, PartialEq)]
66pub struct FileFilter {
67 pub name: String,
68 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 pub fn all(name: impl Into<String>) -> FileFilter {
80 FileFilter { name: name.into(), extensions: Vec::new() }
81 }
82
83 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 pub directories: bool,
95 pub filters: Vec<FileFilter>,
96 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 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 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 pub fn start_folder(mut self, folder: impl Into<PathBuf>) -> SaveFile {
165 self.start_folder = Some(folder.into());
166 self
167 }
168}
169
170pub fn existing_folder(folder: &Option<PathBuf>) -> Option<&Path> {
174 folder.as_deref().filter(|f| f.is_dir())
175}