From 4e395b26ade10b56e9300f1b8666c391af095590 Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Wed, 11 Sep 2024 21:27:37 +0200 Subject: [PATCH] desktop: Add FilePicker::show_dialog This method makes it possible to easily integrate an existing RFD dialog with Ruffle, i.e. make sure it's a proper child of the main window and ensure there's only one picker present at any moment. --- desktop/src/gui/picker.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/desktop/src/gui/picker.rs b/desktop/src/gui/picker.rs index 3f0cf9592..d4273449c 100644 --- a/desktop/src/gui/picker.rs +++ b/desktop/src/gui/picker.rs @@ -33,11 +33,6 @@ impl FilePicker { } pub async fn pick_ruffle_file(&self, dir: Option) -> Option { - if self.data.picking.swap(true, Ordering::SeqCst) { - // Already picking - return None; - } - let locale = &self.data.preferences.language(); let mut dialog = AsyncFileDialog::new() .add_filter( @@ -54,12 +49,27 @@ impl FilePicker { dialog = dialog.set_directory(dir); } + if let Some(result) = self.show_dialog(dialog, |d| d.pick_file()) { + result.await.map(|h| h.into()) + } else { + None + } + } + + pub fn show_dialog(&self, mut dialog: AsyncFileDialog, f: F) -> Option + where + F: FnOnce(AsyncFileDialog) -> O, + { if let Some(parent) = self.data.parent.upgrade() { dialog = dialog.set_parent(&parent); } - let result = dialog.pick_file().await.map(|h| h.into()); + if self.data.picking.swap(true, Ordering::SeqCst) { + // Already picking + return None; + } + let result = f(dialog); self.data.picking.store(false, Ordering::SeqCst); - result + Some(result) } }