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.
This commit is contained in:
parent
c34a4adb71
commit
4e395b26ad
|
@ -33,11 +33,6 @@ impl FilePicker {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn pick_ruffle_file(&self, dir: Option<PathBuf>) -> Option<PathBuf> {
|
pub async fn pick_ruffle_file(&self, dir: Option<PathBuf>) -> Option<PathBuf> {
|
||||||
if self.data.picking.swap(true, Ordering::SeqCst) {
|
|
||||||
// Already picking
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let locale = &self.data.preferences.language();
|
let locale = &self.data.preferences.language();
|
||||||
let mut dialog = AsyncFileDialog::new()
|
let mut dialog = AsyncFileDialog::new()
|
||||||
.add_filter(
|
.add_filter(
|
||||||
|
@ -54,12 +49,27 @@ impl FilePicker {
|
||||||
dialog = dialog.set_directory(dir);
|
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<F, O>(&self, mut dialog: AsyncFileDialog, f: F) -> Option<O>
|
||||||
|
where
|
||||||
|
F: FnOnce(AsyncFileDialog) -> O,
|
||||||
|
{
|
||||||
if let Some(parent) = self.data.parent.upgrade() {
|
if let Some(parent) = self.data.parent.upgrade() {
|
||||||
dialog = dialog.set_parent(&parent);
|
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);
|
self.data.picking.store(false, Ordering::SeqCst);
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue