Skip to content

Commit

Permalink
desktop: Use async IO for file browsing in AVM
Browse files Browse the repository at this point in the history
Using async IO means we're not blocking the main thread and the content
doesn't freeze while reading.
  • Loading branch information
kjarosh authored and torokati44 committed Jan 9, 2025
1 parent 8d84d8b commit 0a75523
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
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

0 comments on commit 0a75523

Please sign in to comment.