desktop: Use FilePicker for picking files in ActionScript

This improves the experience of picking files from AS, as the pickers
will now be properly integrated with Ruffle.
This commit is contained in:
Kamil Jarosz 2024-09-11 20:05:21 +02:00 committed by Nathan Adams
parent 4e395b26ad
commit 94bdf6b9c6
3 changed files with 40 additions and 47 deletions

View File

@ -84,6 +84,7 @@ impl App {
gui.descriptors().clone(), gui.descriptors().clone(),
font_database, font_database,
preferences.clone(), preferences.clone(),
gui.file_picker(),
); );
if let Some(movie_url) = &movie_url { if let Some(movie_url) = &movie_url {

View File

@ -1,6 +1,6 @@
use crate::custom_event::RuffleEvent; use crate::custom_event::RuffleEvent;
use crate::gui::dialogs::message_dialog::MessageDialogConfiguration; use crate::gui::dialogs::message_dialog::MessageDialogConfiguration;
use crate::gui::{DialogDescriptor, LocalizableText}; use crate::gui::{DialogDescriptor, FilePicker, LocalizableText};
use crate::preferences::GlobalPreferences; use crate::preferences::GlobalPreferences;
use anyhow::Error; use anyhow::Error;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@ -125,8 +125,7 @@ pub struct DesktopUiBackend {
preferred_cursor: MouseCursor, preferred_cursor: MouseCursor,
open_url_mode: OpenURLMode, open_url_mode: OpenURLMode,
font_database: Rc<fontdb::Database>, font_database: Rc<fontdb::Database>,
/// Is a dialog currently open file_picker: FilePicker,
dialog_open: bool,
} }
impl DesktopUiBackend { impl DesktopUiBackend {
@ -136,6 +135,7 @@ impl DesktopUiBackend {
open_url_mode: OpenURLMode, open_url_mode: OpenURLMode,
font_database: Rc<fontdb::Database>, font_database: Rc<fontdb::Database>,
preferences: GlobalPreferences, preferences: GlobalPreferences,
file_picker: FilePicker,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
// The window handle is only relevant to linux/wayland // The window handle is only relevant to linux/wayland
// If it fails it'll fallback to x11 or wlr-data-control // If it fails it'll fallback to x11 or wlr-data-control
@ -154,8 +154,8 @@ impl DesktopUiBackend {
preferences, preferences,
preferred_cursor: MouseCursor::Arrow, preferred_cursor: MouseCursor::Arrow,
open_url_mode, open_url_mode,
dialog_open: false,
font_database, font_database,
file_picker,
}) })
} }
@ -329,34 +329,28 @@ impl UiBackend for DesktopUiBackend {
} }
fn display_file_open_dialog(&mut self, filters: Vec<FileFilter>) -> Option<DialogResultFuture> { fn display_file_open_dialog(&mut self, filters: Vec<FileFilter>) -> Option<DialogResultFuture> {
// Prevent opening multiple dialogs at the same time let mut dialog = AsyncFileDialog::new();
if self.dialog_open {
return None;
}
self.dialog_open = true;
// Create the dialog future for filter in filters {
Some(Box::pin(async move { if cfg!(target_os = "macos") && filter.mac_type.is_some() {
let mut dialog = AsyncFileDialog::new(); let mac_type = filter.mac_type.expect("Checked above");
let extensions: Vec<&str> = mac_type.split(';').collect();
for filter in filters { dialog = dialog.add_filter(&filter.description, &extensions);
if cfg!(target_os = "macos") && filter.mac_type.is_some() { } else {
let mac_type = filter.mac_type.expect("Checked above"); let extensions: Vec<&str> = filter
let extensions: Vec<&str> = mac_type.split(';').collect(); .extensions
dialog = dialog.add_filter(&filter.description, &extensions); .split(';')
} else { .map(|x| x.trim_start_matches("*."))
let extensions: Vec<&str> = filter .collect();
.extensions dialog = dialog.add_filter(&filter.description, &extensions);
.split(';')
.map(|x| x.trim_start_matches("*."))
.collect();
dialog = dialog.add_filter(&filter.description, &extensions);
}
} }
}
let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> = Ok(Box::new( let result = self.file_picker.show_dialog(dialog, |d| d.pick_file())?;
DesktopFileDialogResult::new(dialog.pick_file().await),
)); Some(Box::pin(async move {
let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> =
Ok(Box::new(DesktopFileDialogResult::new(result.await)));
result result
})) }))
} }
@ -366,27 +360,19 @@ impl UiBackend for DesktopUiBackend {
file_name: String, file_name: String,
title: String, title: String,
) -> Option<DialogResultFuture> { ) -> Option<DialogResultFuture> {
// Prevent opening multiple dialogs at the same time // Select the location to save the file to
if self.dialog_open { let dialog = AsyncFileDialog::new()
return None; .set_title(&title)
} .set_file_name(&file_name);
self.dialog_open = true;
let result = self.file_picker.show_dialog(dialog, |d| d.save_file())?;
// Create the dialog future
Some(Box::pin(async move { Some(Box::pin(async move {
// Select the location to save the file to let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> =
let dialog = AsyncFileDialog::new() Ok(Box::new(DesktopFileDialogResult::new(result.await)));
.set_title(&title)
.set_file_name(&file_name);
let result: Result<Box<dyn FileDialogResult>, DialogLoaderError> = Ok(Box::new(
DesktopFileDialogResult::new(dialog.save_file().await),
));
result result
})) }))
} }
fn close_file_dialog(&mut self) { fn close_file_dialog(&mut self) {}
self.dialog_open = false;
}
} }

View File

@ -3,7 +3,7 @@ use crate::backends::{
DesktopNavigatorInterface, DesktopUiBackend, DesktopNavigatorInterface, DesktopUiBackend,
}; };
use crate::custom_event::RuffleEvent; use crate::custom_event::RuffleEvent;
use crate::gui::MovieView; use crate::gui::{FilePicker, MovieView};
use crate::preferences::GlobalPreferences; use crate::preferences::GlobalPreferences;
use crate::{CALLSTACK, RENDER_INFO, SWF_INFO}; use crate::{CALLSTACK, RENDER_INFO, SWF_INFO};
use anyhow::anyhow; use anyhow::anyhow;
@ -130,6 +130,7 @@ impl ActivePlayer {
movie_view: MovieView, movie_view: MovieView,
font_database: Rc<fontdb::Database>, font_database: Rc<fontdb::Database>,
preferences: GlobalPreferences, preferences: GlobalPreferences,
file_picker: FilePicker,
) -> Self { ) -> Self {
let mut builder = PlayerBuilder::new(); let mut builder = PlayerBuilder::new();
@ -277,6 +278,7 @@ impl ActivePlayer {
opt.open_url_mode, opt.open_url_mode,
font_database, font_database,
preferences, preferences,
file_picker,
) )
.expect("Couldn't create ui backend"), .expect("Couldn't create ui backend"),
) )
@ -390,6 +392,7 @@ pub struct PlayerController {
descriptors: Arc<Descriptors>, descriptors: Arc<Descriptors>,
font_database: Rc<fontdb::Database>, font_database: Rc<fontdb::Database>,
preferences: GlobalPreferences, preferences: GlobalPreferences,
file_picker: FilePicker,
} }
impl PlayerController { impl PlayerController {
@ -399,6 +402,7 @@ impl PlayerController {
descriptors: Arc<Descriptors>, descriptors: Arc<Descriptors>,
font_database: fontdb::Database, font_database: fontdb::Database,
preferences: GlobalPreferences, preferences: GlobalPreferences,
file_picker: FilePicker,
) -> Self { ) -> Self {
Self { Self {
player: None, player: None,
@ -407,6 +411,7 @@ impl PlayerController {
descriptors, descriptors,
font_database: Rc::new(font_database), font_database: Rc::new(font_database),
preferences, preferences,
file_picker,
} }
} }
@ -420,6 +425,7 @@ impl PlayerController {
movie_view, movie_view,
self.font_database.clone(), self.font_database.clone(),
self.preferences.clone(), self.preferences.clone(),
self.file_picker.clone(),
)); ));
} }