desktop: Allow only one file picker open at a time
This commit is contained in:
parent
be8be123ba
commit
713224c465
|
@ -1,7 +1,10 @@
|
||||||
use rfd::AsyncFileDialog;
|
use rfd::AsyncFileDialog;
|
||||||
use std::{
|
use std::{
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, Weak},
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc, Weak,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
|
@ -12,16 +15,25 @@ pub struct FilePicker {
|
||||||
|
|
||||||
struct FilePickerData {
|
struct FilePickerData {
|
||||||
parent: Weak<Window>,
|
parent: Weak<Window>,
|
||||||
|
picking: AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FilePicker {
|
impl FilePicker {
|
||||||
pub fn new(parent: Weak<Window>) -> Self {
|
pub fn new(parent: Weak<Window>) -> Self {
|
||||||
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> {
|
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()
|
let mut dialog = AsyncFileDialog::new()
|
||||||
.add_filter("Flash Files", &["swf", "spl", "ruf"])
|
.add_filter("Flash Files", &["swf", "spl", "ruf"])
|
||||||
.add_filter("All Files", &["*"])
|
.add_filter("All Files", &["*"])
|
||||||
|
@ -35,6 +47,8 @@ impl FilePicker {
|
||||||
dialog = dialog.set_parent(&parent);
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue