desktop: Allow only one file picker open at a time

This commit is contained in:
Kamil Jarosz 2024-08-26 12:57:39 +02:00
parent be8be123ba
commit 713224c465
1 changed files with 17 additions and 3 deletions

View File

@ -1,7 +1,10 @@
use rfd::AsyncFileDialog;
use std::{
path::PathBuf,
sync::{Arc, Weak},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Weak,
},
};
use winit::window::Window;
@ -12,16 +15,25 @@ pub struct FilePicker {
struct FilePickerData {
parent: Weak<Window>,
picking: AtomicBool,
}
impl FilePicker {
pub fn new(parent: Weak<Window>) -> Self {
Self {
data: Arc::new(FilePickerData { parent }),
data: Arc::new(FilePickerData {
parent,
picking: AtomicBool::new(false),
}),
}
}
pub async fn pick_file(&self, dir: Option<PathBuf>) -> Option<PathBuf> {
if self.data.picking.swap(true, Ordering::SeqCst) {
// Already picking
return None;
}
let mut dialog = AsyncFileDialog::new()
.add_filter("Flash Files", &["swf", "spl", "ruf"])
.add_filter("All Files", &["*"])
@ -35,6 +47,8 @@ impl FilePicker {
dialog = dialog.set_parent(&parent);
}
dialog.pick_file().await.map(|h| h.into())
let result = dialog.pick_file().await.map(|h| h.into());
self.data.picking.store(false, Ordering::SeqCst);
result
}
}