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 committed Aug 18, 2024
1 parent f618b3c commit 99566f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
17 changes: 10 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,15 @@ impl App {
}

winit::event::Event::UserEvent(RuffleEvent::BrowseAndOpen(options)) => {
if let Some(url) =
pick_file(false, None).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();
tokio::spawn(async move {
if let Some(url) = pick_file(None)
.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
11 changes: 7 additions & 4 deletions desktop/src/gui/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ impl PathOrUrlField {
path
});

if let Some(path) = pick_file(true, dir) {
let mut value_lock = Self::lock_value(&self.value);
*value_lock = path.to_string_lossy().to_string();
}
let value = self.value.clone();
tokio::spawn(async move {
if let Some(path) = pick_file(dir).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
28 changes: 4 additions & 24 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 @@ -244,8 +244,8 @@ pub fn parse_url(path: &Path) -> Result<Url, Error> {
}
}

fn actually_pick_file(dir: Option<PathBuf>) -> Option<PathBuf> {
let mut dialog = FileDialog::new()
pub async fn pick_file(dir: Option<PathBuf>) -> Option<PathBuf> {
let mut dialog = AsyncFileDialog::new()
.add_filter("Flash Files", &["swf", "spl", "ruf"])
.add_filter("All Files", &["*"])
.set_title("Load a Flash File");
Expand All @@ -254,27 +254,7 @@ fn actually_pick_file(dir: Option<PathBuf>) -> Option<PathBuf> {
dialog = dialog.set_directory(dir);
}

dialog.pick_file()
}

// [NA] Horrible hacky workaround for https://github.com/rust-windowing/winit/issues/2291
// We only need the workaround from within UI code, not when executing custom events
// The workaround causes Ruffle to show as "not responding" on windows, so we don't use it if we don't need to
#[cfg(windows)]
pub fn pick_file(in_ui: bool, path: Option<PathBuf>) -> Option<PathBuf> {
if in_ui {
std::thread::spawn(move || actually_pick_file(path))
.join()
.ok()
.flatten()
} else {
actually_pick_file(path)
}
}

#[cfg(not(windows))]
pub fn pick_file(_in_ui: bool, path: Option<PathBuf>) -> Option<PathBuf> {
actually_pick_file(path)
dialog.pick_file().await.map(|h| h.into())
}

#[cfg(not(feature = "tracy"))]
Expand Down

0 comments on commit 99566f8

Please sign in to comment.