Skip to content

Commit

Permalink
desktop: Make file pickers asynchronous
Browse files Browse the repository at this point in the history
This patch fixes the problem of Ruffle not responding
when picking a file.
  • Loading branch information
kjarosh authored and torokati44 committed Aug 20, 2024
1 parent 4918596 commit 63b373d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
18 changes: 11 additions & 7 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) => {
Expand Down
12 changes: 8 additions & 4 deletions desktop/src/gui/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions desktop/src/util.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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"))]
Expand Down

0 comments on commit 63b373d

Please sign in to comment.