From 713224c4652e97c1f109266af5606be814219996 Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Mon, 26 Aug 2024 12:57:39 +0200 Subject: [PATCH] desktop: Allow only one file picker open at a time --- desktop/src/gui/picker.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/desktop/src/gui/picker.rs b/desktop/src/gui/picker.rs index 0f23795ba..fd73eb806 100644 --- a/desktop/src/gui/picker.rs +++ b/desktop/src/gui/picker.rs @@ -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, + picking: AtomicBool, } impl FilePicker { pub fn new(parent: Weak) -> Self { Self { - data: Arc::new(FilePickerData { parent }), + data: Arc::new(FilePickerData { + parent, + picking: AtomicBool::new(false), + }), } } pub async fn pick_file(&self, dir: Option) -> Option { + 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 } }