desktop: Make file pickers asynchronous

This patch fixes the problem of Ruffle not responding
when picking a file.
This commit is contained in:
Kamil Jarosz 2024-08-08 01:41:34 +02:00 committed by TÖRÖK Attila
parent 4918596f71
commit 63b373d739
3 changed files with 23 additions and 15 deletions

View File

@ -136,6 +136,7 @@ impl App {
// Poll UI events.
let event_loop = self.event_loop.take().expect("App already running");
let event_loop_proxy = event_loop.create_proxy();
event_loop.run(move |event, elwt| {
let mut check_redraw = false;
match event {
@ -514,13 +515,16 @@ impl App {
}
winit::event::Event::UserEvent(RuffleEvent::BrowseAndOpen(options)) => {
if let Some(url) = pick_file(None, Some(&self.window))
.and_then(|p| Url::from_file_path(p).ok())
{
self.gui
.borrow_mut()
.create_movie(&mut self.player, *options, url);
}
let event_loop = event_loop_proxy.clone();
let window = self.window.clone();
tokio::spawn(async move {
if let Some(url) = pick_file(None, Some(&window))
.await
.and_then(|p| Url::from_file_path(p).ok())
{
let _ = event_loop.send_event(RuffleEvent::OpenURL(url, options));
}
});
}
winit::event::Event::UserEvent(RuffleEvent::OpenURL(url, options)) => {

View File

@ -64,10 +64,14 @@ impl PathOrUrlField {
path
});
if let Some(path) = pick_file(dir, self.window.upgrade().as_ref()) {
let mut value_lock = Self::lock_value(&self.value);
*value_lock = path.to_string_lossy().to_string();
}
let value = self.value.clone();
let window = self.window.upgrade();
tokio::spawn(async move {
if let Some(path) = pick_file(dir, window.as_ref()).await {
let mut value_lock = Self::lock_value(&value);
*value_lock = path.to_string_lossy().to_string();
}
});
}
let mut value_locked = Self::lock_value(&self.value);

View File

@ -1,7 +1,7 @@
use crate::custom_event::RuffleEvent;
use anyhow::{anyhow, Error};
use gilrs::Button;
use rfd::FileDialog;
use rfd::AsyncFileDialog;
use ruffle_core::events::{GamepadButton, KeyCode, TextControlCode};
use std::path::{Path, PathBuf};
use url::Url;
@ -245,11 +245,11 @@ pub fn parse_url(path: &Path) -> Result<Url, Error> {
}
}
pub fn pick_file<W: HasWindowHandle + HasDisplayHandle>(
pub async fn pick_file<W: HasWindowHandle + HasDisplayHandle>(
dir: Option<PathBuf>,
parent: Option<&W>,
) -> Option<PathBuf> {
let mut dialog = FileDialog::new()
let mut dialog = AsyncFileDialog::new()
.add_filter("Flash Files", &["swf", "spl", "ruf"])
.add_filter("All Files", &["*"])
.set_title("Load a Flash File");
@ -262,7 +262,7 @@ pub fn pick_file<W: HasWindowHandle + HasDisplayHandle>(
dialog = dialog.set_parent(parent);
}
dialog.pick_file()
dialog.pick_file().await.map(|h| h.into())
}
#[cfg(not(feature = "tracy"))]