1use crate::{Property, StandardButtonKind};
2use std::{
3 cell::Cell,
4 collections::VecDeque,
5 fmt,
6 rc::Rc,
7 sync::atomic::{AtomicU64, Ordering},
8};
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub enum FileDialogMode {
12 #[default]
13 OpenFile,
14 OpenFiles,
15 SaveFile,
16 SelectFolder,
17}
18
19#[derive(Debug, Default)]
20pub struct FileDialogOptions {
21 pub mode: FileDialogMode,
22 pub title: String,
23 pub directory: String,
24 pub file_name: String,
25 pub name_filters: Vec<String>,
26}
27
28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
29pub enum MessageLevel {
30 #[default]
31 Info,
32 Warning,
33 Error,
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37pub enum MessageButtons {
38 #[default]
39 Ok,
40 OkCancel,
41 YesNo,
42 YesNoCancel,
43}
44
45#[derive(Debug, Default)]
46pub struct MessageDialogOptions {
47 pub level: MessageLevel,
48 pub buttons: MessageButtons,
49 pub title: String,
50 pub text: String,
51 pub informative_text: String,
52}
53
54#[doc(hidden)]
55#[derive(Debug)]
56pub enum NativeDialogOptions {
57 File(FileDialogOptions),
58 Message(MessageDialogOptions),
59}
60
61#[doc(hidden)]
62#[derive(Debug, PartialEq, Eq)]
63pub enum NativeDialogResult {
64 Files(Vec<String>),
65 Button(StandardButtonKind),
66 Canceled,
67 Failed(String),
68 Abandoned,
69}
70
71#[derive(Clone, Debug)]
72struct Request {
73 id: u64,
74 options: Rc<NativeDialogOptions>,
75}
76
77#[doc(hidden)]
79#[derive(Default)]
80pub struct NativeDialogSlot(Property<Option<Request>>);
81
82impl NativeDialogSlot {
83 pub fn open(&self, options: impl FnOnce() -> NativeDialogOptions) {
84 if self.0.get(|| None).is_some() {
85 return;
86 }
87 static NEXT: AtomicU64 = AtomicU64::new(1);
88 let id = NEXT
89 .try_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
90 .expect("native dialog request identities exhausted");
91 self.0.set(Some(Request {
92 id,
93 options: Rc::new(options()),
94 }));
95 }
96
97 pub fn register(
98 &self,
99 deliver: impl Fn(u64, NativeDialogResult) -> bool + 'static,
100 ) -> Option<NativeDialogRegistration> {
101 Some(NativeDialogRegistration {
102 request: self.0.get(|| None)?,
103 deliver: Rc::new(deliver),
104 })
105 }
106
107 pub fn finish(&self, id: u64) -> bool {
108 if self.0.get(|| None).is_none_or(|request| request.id != id) {
109 return false;
110 }
111 self.0.set(None);
112 true
113 }
114}
115
116#[doc(hidden)]
117#[derive(Clone)]
118pub struct NativeDialogRegistration {
119 request: Request,
120 deliver: Rc<dyn Fn(u64, NativeDialogResult) -> bool>,
121}
122
123impl NativeDialogRegistration {
124 pub fn id(&self) -> u64 {
125 self.request.id
126 }
127
128 pub fn options(&self) -> Rc<NativeDialogOptions> {
129 self.request.options.clone()
130 }
131
132 pub fn deliver(self, result: NativeDialogResult) -> bool {
133 (self.deliver)(self.request.id, result)
134 }
135}
136
137impl fmt::Debug for NativeDialogRegistration {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 f.debug_struct("NativeDialogRegistration")
140 .field("id", &self.id())
141 .finish()
142 }
143}
144
145impl PartialEq for NativeDialogRegistration {
146 fn eq(&self, other: &Self) -> bool {
147 self.id() == other.id()
148 }
149}
150
151struct ActiveDialog {
152 registration: NativeDialogRegistration,
153 abandoned: Rc<Cell<bool>>,
154}
155
156#[doc(hidden)]
157pub struct NativeDialogPresentation {
158 pub id: u64,
159 pub options: Rc<NativeDialogOptions>,
160 abandoned: Rc<Cell<bool>>,
161}
162
163impl NativeDialogPresentation {
164 pub fn is_abandoned(&self) -> bool {
165 self.abandoned.get()
166 }
167}
168
169#[doc(hidden)]
171#[derive(Default)]
172pub struct NativeDialogQueue {
173 waiting: VecDeque<NativeDialogRegistration>,
174 active: Option<ActiveDialog>,
175}
176
177impl NativeDialogQueue {
178 pub fn close(&mut self) {
179 self.sync(&[]);
180 if let Some(active) = self.active.take() {
181 active.registration.deliver(NativeDialogResult::Abandoned);
182 }
183 }
184
185 pub fn sync(&mut self, live: &[&NativeDialogRegistration]) {
186 let is_live = |id| live.iter().any(|entry| entry.id() == id);
187 if let Some(active) = &self.active
188 && !is_live(active.registration.id())
189 {
190 active.abandoned.set(true);
191 }
192 self.waiting.retain(|entry| {
193 if !is_live(entry.id()) {
194 entry.clone().deliver(NativeDialogResult::Abandoned);
195 return false;
196 }
197 true
198 });
199 let queued = self.waiting.len();
200 for entry in live {
201 if self
202 .active
203 .as_ref()
204 .is_some_and(|active| active.registration.id() == entry.id())
205 || self.waiting.iter().any(|queued| queued.id() == entry.id())
206 {
207 continue;
208 }
209 self.waiting.push_back((*entry).clone());
210 }
211 if self.waiting.len() > queued {
212 self.waiting
214 .make_contiguous()
215 .sort_by_key(NativeDialogRegistration::id);
216 }
217 }
218
219 pub fn begin_next(&mut self) -> Option<NativeDialogPresentation> {
220 if self.active.is_some() {
221 return None;
222 }
223 let registration = self.waiting.pop_front()?;
224 let abandoned = Rc::new(Cell::new(false));
225 let request = NativeDialogPresentation {
226 id: registration.id(),
227 options: registration.options(),
228 abandoned: abandoned.clone(),
229 };
230 self.active = Some(ActiveDialog {
231 registration,
232 abandoned,
233 });
234 Some(request)
235 }
236
237 pub fn complete(&mut self, id: u64, result: NativeDialogResult) -> bool {
238 if self
239 .active
240 .as_ref()
241 .is_none_or(|active| active.registration.id() != id)
242 {
243 return false;
244 }
245 let active = self.active.take().expect("matching active native dialog");
246 active.registration.deliver(if active.abandoned.get() {
247 NativeDialogResult::Abandoned
248 } else {
249 result
250 });
251 true
252 }
253}