Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desktop: Use async IO for file browsing in AVM #19134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ chrono = { workspace = true }
fluent-templates = "0.12.0"
toml_edit = { version = "0.22.22", features = ["parse"] }
gilrs = "0.11"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "fs"] }

tracing-tracy = { version = "0.11.3", optional = true, features = ["demangle"] }
rand = "0.8.5"
Expand Down
47 changes: 37 additions & 10 deletions desktop/src/backends/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use ruffle_core::backend::ui::{
DialogLoaderError, DialogResultFuture, FileDialogResult, FileFilter, FontDefinition,
FullscreenError, LanguageIdentifier, MouseCursor, UiBackend,
};
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use tokio::io::AsyncReadExt;
use tracing::error;
use url::Url;
use winit::event_loop::EventLoopProxy;
Expand All @@ -30,15 +32,40 @@ pub struct DesktopFileDialogResult {

impl DesktopFileDialogResult {
/// Create a new [`DesktopFileDialogResult`] from a given file handle
pub fn new(handle: Option<FileHandle>) -> Self {
let md = handle
.as_ref()
.and_then(|x| std::fs::metadata(x.path()).ok());
pub async fn new(handle: Option<FileHandle>) -> Self {
async fn read_file(path: &Path) -> (Option<std::fs::Metadata>, Vec<u8>) {
let file = match tokio::fs::File::open(path).await {
Ok(file) => file,
Err(err) => {
let path = path.to_string_lossy();
tracing::error!("Error opening file {path}: {err}");
return (None, Vec::new());
}
};
let metadata = match file.metadata().await {
Ok(metadata) => Some(metadata),
Err(err) => {
let path = path.to_string_lossy();
tracing::error!("Error reading metadata of file {path}: {err}");
None
}
};

let mut contents = Vec::new();
let mut file = file;
if let Err(err) = file.read_to_end(&mut contents).await {
contents.clear();
let path = path.to_string_lossy();
tracing::error!("Error reading file {path}: {err}");
}
(metadata, contents)
}

let contents = handle
.as_ref()
.and_then(|handle| std::fs::read(handle.path()).ok())
.unwrap_or_default();
let (md, contents) = if let Some(ref handle) = handle {
read_file(handle.path()).await
} else {
(None, Vec::new())
};

Self {
handle,
Expand Down Expand Up @@ -348,7 +375,7 @@ impl UiBackend for DesktopUiBackend {

Some(Box::pin(async move {
let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> =
Ok(Box::new(DesktopFileDialogResult::new(result.await)));
Ok(Box::new(DesktopFileDialogResult::new(result.await).await));
result
}))
}
Expand All @@ -367,7 +394,7 @@ impl UiBackend for DesktopUiBackend {

Some(Box::pin(async move {
let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> =
Ok(Box::new(DesktopFileDialogResult::new(result.await)));
Ok(Box::new(DesktopFileDialogResult::new(result.await).await));
result
}))
}
Expand Down