Skip to content

Commit be8be12

Browse files
committed
desktop: Move pick_file to picker.rs
1 parent 7931b8f commit be8be12

File tree

9 files changed

+86
-57
lines changed

9 files changed

+86
-57
lines changed

desktop/src/app.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::gui::{GuiController, MENU_HEIGHT};
33
use crate::player::{LaunchOptions, PlayerController};
44
use crate::preferences::GlobalPreferences;
55
use crate::util::{
6-
get_screen_size, gilrs_button_to_gamepad_button, parse_url, pick_file, plot_stats_in_tracy,
6+
get_screen_size, gilrs_button_to_gamepad_button, parse_url, plot_stats_in_tracy,
77
winit_to_ruffle_key_code, winit_to_ruffle_text_control,
88
};
99
use anyhow::{Context, Error};
@@ -482,9 +482,10 @@ impl App {
482482

483483
winit::event::Event::UserEvent(RuffleEvent::BrowseAndOpen(options)) => {
484484
let event_loop = event_loop_proxy.clone();
485-
let window = self.window.clone();
485+
let picker = self.gui.borrow().file_picker();
486486
tokio::spawn(async move {
487-
if let Some(url) = pick_file(None, Some(&window))
487+
if let Some(url) = picker
488+
.pick_file(None)
488489
.await
489490
.and_then(|p| Url::from_file_path(p).ok())
490491
{

desktop/src/gui.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ mod controller;
33
mod dialogs;
44
mod menu_bar;
55
mod movie;
6+
mod picker;
67
mod theme;
78
mod widgets;
89

910
pub use controller::GuiController;
1011
pub use movie::MovieView;
12+
pub use picker::FilePicker;
1113
use std::borrow::Cow;
1214
pub use theme::ThemePreference;
1315
use url::Url;

desktop/src/gui/controller.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use winit::event_loop::EventLoop;
2323
use winit::keyboard::{Key, NamedKey};
2424
use winit::window::{Theme, Window};
2525

26+
use super::FilePicker;
27+
2628
/// Integration layer connecting wgpu+winit to egui.
2729
pub struct GuiController {
2830
descriptors: Arc<Descriptors>,
@@ -149,6 +151,10 @@ impl GuiController {
149151
&self.descriptors
150152
}
151153

154+
pub fn file_picker(&self) -> FilePicker {
155+
self.gui.dialogs.file_picker()
156+
}
157+
152158
pub fn resize(&mut self, size: PhysicalSize<u32>) {
153159
if size.width > 0 && size.height > 0 {
154160
self.size = size;

desktop/src/gui/dialogs.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ use url::Url;
1717
use volume_controls::VolumeControls;
1818
use winit::event_loop::EventLoopProxy;
1919

20+
use super::FilePicker;
21+
2022
pub struct Dialogs {
21-
window: Weak<winit::window::Window>,
2223
event_loop: EventLoopProxy<RuffleEvent>,
2324

25+
picker: FilePicker,
2426
preferences_dialog: Option<PreferencesDialog>,
2527
bookmarks_dialog: Option<BookmarksDialog>,
2628
bookmark_add_dialog: Option<BookmarkAddDialog>,
@@ -44,6 +46,7 @@ impl Dialogs {
4446
window: Weak<winit::window::Window>,
4547
event_loop: EventLoopProxy<RuffleEvent>,
4648
) -> Self {
49+
let picker = FilePicker::new(window);
4750
Self {
4851
preferences_dialog: None,
4952
bookmarks_dialog: None,
@@ -52,7 +55,7 @@ impl Dialogs {
5255
open_dialog: OpenDialog::new(
5356
player_options,
5457
default_path,
55-
window.clone(),
58+
picker.clone(),
5659
event_loop.clone(),
5760
),
5861
is_open_dialog_visible: false,
@@ -62,20 +65,24 @@ impl Dialogs {
6265

6366
is_about_visible: false,
6467

65-
window,
6668
event_loop,
69+
picker,
6770
preferences,
6871
}
6972
}
7073

74+
pub fn file_picker(&self) -> FilePicker {
75+
self.picker.clone()
76+
}
77+
7178
pub fn recreate_open_dialog(
7279
&mut self,
7380
opt: LaunchOptions,
7481
url: Option<Url>,
7582
event_loop: EventLoopProxy<RuffleEvent>,
7683
) {
7784
self.is_open_dialog_visible = false;
78-
self.open_dialog = OpenDialog::new(opt, url, self.window.clone(), event_loop);
85+
self.open_dialog = OpenDialog::new(opt, url, self.picker.clone(), event_loop);
7986
}
8087

8188
pub fn open_file_advanced(&mut self) {
@@ -89,7 +96,7 @@ impl Dialogs {
8996
pub fn open_bookmarks(&mut self) {
9097
self.bookmarks_dialog = Some(BookmarksDialog::new(
9198
self.preferences.clone(),
92-
self.window.clone(),
99+
self.picker.clone(),
93100
self.event_loop.clone(),
94101
));
95102
}
@@ -98,7 +105,7 @@ impl Dialogs {
98105
self.bookmark_add_dialog = Some(BookmarkAddDialog::new(
99106
self.preferences.clone(),
100107
initial_url,
101-
self.window.clone(),
108+
self.picker.clone(),
102109
))
103110
}
104111

desktop/src/gui/dialogs/bookmarks_dialog.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::gui::text;
21
use crate::gui::widgets::PathOrUrlField;
2+
use crate::gui::{text, FilePicker};
33
use crate::preferences::GlobalPreferences;
44
use crate::{custom_event::RuffleEvent, player::LaunchOptions};
55
use egui::{Align2, Button, Grid, Label, Layout, Sense, Ui, Widget, Window};
66
use egui_extras::{Column, TableBuilder};
77
use ruffle_frontend_utils::bookmarks::Bookmark;
8-
use std::sync::Weak;
98
use unic_langid::LanguageIdentifier;
109
use url::Url;
1110
use winit::event_loop::EventLoopProxy;
@@ -20,7 +19,7 @@ impl BookmarkAddDialog {
2019
pub fn new(
2120
preferences: GlobalPreferences,
2221
initial_url: Option<Url>,
23-
window: Weak<winit::window::Window>,
22+
picker: FilePicker,
2423
) -> Self {
2524
Self {
2625
preferences,
@@ -29,7 +28,7 @@ impl BookmarkAddDialog {
2928
.map(|x| ruffle_frontend_utils::url_to_readable_name(x).into_owned())
3029
.unwrap_or_default(),
3130
// TODO: hint.
32-
url: PathOrUrlField::new(initial_url, "", window),
31+
url: PathOrUrlField::new(initial_url, "", picker),
3332
}
3433
}
3534

@@ -100,20 +99,20 @@ struct SelectedBookmark {
10099
}
101100

102101
pub struct BookmarksDialog {
103-
window: Weak<winit::window::Window>,
104102
event_loop: EventLoopProxy<RuffleEvent>,
103+
picker: FilePicker,
105104
preferences: GlobalPreferences,
106105
selected_bookmark: Option<SelectedBookmark>,
107106
}
108107

109108
impl BookmarksDialog {
110109
pub fn new(
111110
preferences: GlobalPreferences,
112-
window: Weak<winit::window::Window>,
111+
picker: FilePicker,
113112
event_loop: EventLoopProxy<RuffleEvent>,
114113
) -> Self {
115114
Self {
116-
window,
115+
picker,
117116
event_loop,
118117
preferences,
119118
selected_bookmark: None,
@@ -224,7 +223,7 @@ impl BookmarksDialog {
224223
url: PathOrUrlField::new(
225224
Some(bookmark.url.clone()),
226225
"",
227-
self.window.clone(),
226+
self.picker.clone(),
228227
),
229228
});
230229
}

desktop/src/gui/dialogs/open_dialog.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::custom_event::RuffleEvent;
2-
use crate::gui::text;
32
use crate::gui::widgets::PathOrUrlField;
3+
use crate::gui::{text, FilePicker};
44
use crate::player::LaunchOptions;
55
use egui::{
66
emath, Align2, Button, Checkbox, ComboBox, Grid, Layout, Slider, TextEdit, Ui, Widget, Window,
@@ -11,7 +11,6 @@ use ruffle_core::{LoadBehavior, PlayerRuntime, StageAlign, StageScaleMode};
1111
use ruffle_render::quality::StageQuality;
1212
use std::borrow::Cow;
1313
use std::ops::RangeInclusive;
14-
use std::sync::Weak;
1514
use std::time::Duration;
1615
use unic_langid::LanguageIdentifier;
1716
use url::Url;
@@ -50,7 +49,7 @@ impl OpenDialog {
5049
pub fn new(
5150
defaults: LaunchOptions,
5251
default_url: Option<Url>,
53-
window: Weak<winit::window::Window>,
52+
picker: FilePicker,
5453
event_loop: EventLoopProxy<RuffleEvent>,
5554
) -> Self {
5655
let spoof_url = OptionalField::new(
@@ -73,7 +72,7 @@ impl OpenDialog {
7372
defaults.proxy.as_ref().map(Url::to_string),
7473
UrlField::new("socks5://localhost:8080"),
7574
);
76-
let path = PathOrUrlField::new(default_url, "path/to/movie.swf", window);
75+
let path = PathOrUrlField::new(default_url, "path/to/movie.swf", picker);
7776
let script_timeout = OptionalField::new(
7877
defaults
7978
.player

desktop/src/gui/picker.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rfd::AsyncFileDialog;
2+
use std::{
3+
path::PathBuf,
4+
sync::{Arc, Weak},
5+
};
6+
use winit::window::Window;
7+
8+
#[derive(Clone)]
9+
pub struct FilePicker {
10+
data: Arc<FilePickerData>,
11+
}
12+
13+
struct FilePickerData {
14+
parent: Weak<Window>,
15+
}
16+
17+
impl FilePicker {
18+
pub fn new(parent: Weak<Window>) -> Self {
19+
Self {
20+
data: Arc::new(FilePickerData { parent }),
21+
}
22+
}
23+
24+
pub async fn pick_file(&self, dir: Option<PathBuf>) -> Option<PathBuf> {
25+
let mut dialog = AsyncFileDialog::new()
26+
.add_filter("Flash Files", &["swf", "spl", "ruf"])
27+
.add_filter("All Files", &["*"])
28+
.set_title("Load a Flash File");
29+
30+
if let Some(dir) = dir {
31+
dialog = dialog.set_directory(dir);
32+
}
33+
34+
if let Some(parent) = self.data.parent.upgrade() {
35+
dialog = dialog.set_parent(&parent);
36+
}
37+
38+
dialog.pick_file().await.map(|h| h.into())
39+
}
40+
}

desktop/src/gui/widgets.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
use crate::gui::text;
2-
use crate::util::pick_file;
32
use egui::{TextEdit, Ui};
43
use std::path::Path;
5-
use std::sync::{Arc, Mutex, MutexGuard, Weak};
4+
use std::sync::{Arc, Mutex, MutexGuard};
65
use unic_langid::LanguageIdentifier;
76
use url::Url;
87

8+
use super::FilePicker;
9+
910
pub struct PathOrUrlField {
10-
window: Weak<winit::window::Window>,
11+
picker: FilePicker,
1112
value: Arc<Mutex<String>>,
1213
result: Option<Url>,
1314
hint: &'static str,
1415
}
1516

1617
impl PathOrUrlField {
17-
pub fn new(
18-
default: Option<Url>,
19-
hint: &'static str,
20-
window: Weak<winit::window::Window>,
21-
) -> Self {
18+
pub fn new(default: Option<Url>, hint: &'static str, picker: FilePicker) -> Self {
2219
if let Some(default) = default {
2320
if default.scheme() == "file" {
2421
if let Ok(path) = default.to_file_path() {
2522
return Self {
26-
window,
23+
picker,
2724
value: Arc::new(Mutex::new(path.to_string_lossy().to_string())),
2825
result: Some(default),
2926
hint,
@@ -32,15 +29,15 @@ impl PathOrUrlField {
3229
}
3330

3431
return Self {
35-
window,
32+
picker,
3633
value: Arc::new(Mutex::new(default.to_string())),
3734
result: Some(default),
3835
hint,
3936
};
4037
}
4138

4239
Self {
43-
window,
40+
picker,
4441
value: Arc::new(Mutex::new("".to_string())),
4542
result: None,
4643
hint,
@@ -65,9 +62,9 @@ impl PathOrUrlField {
6562
});
6663

6764
let value = self.value.clone();
68-
let window = self.window.upgrade();
65+
let picker = self.picker.clone();
6966
tokio::spawn(async move {
70-
if let Some(path) = pick_file(dir, window.as_ref()).await {
67+
if let Some(path) = picker.pick_file(dir).await {
7168
let mut value_lock = Self::lock_value(&value);
7269
*value_lock = path.to_string_lossy().to_string();
7370
}

desktop/src/util.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use crate::custom_event::RuffleEvent;
22
use anyhow::{anyhow, Error};
33
use gilrs::Button;
4-
use rfd::AsyncFileDialog;
54
use ruffle_core::events::{GamepadButton, KeyCode, TextControlCode};
6-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
76
use url::Url;
8-
use wgpu::rwh::{HasDisplayHandle, HasWindowHandle};
97
use winit::dpi::PhysicalSize;
108
use winit::event::{KeyEvent, Modifiers};
119
use winit::event_loop::EventLoop;
@@ -245,26 +243,6 @@ pub fn parse_url(path: &Path) -> Result<Url, Error> {
245243
}
246244
}
247245

248-
pub async fn pick_file<W: HasWindowHandle + HasDisplayHandle>(
249-
dir: Option<PathBuf>,
250-
parent: Option<&W>,
251-
) -> Option<PathBuf> {
252-
let mut dialog = AsyncFileDialog::new()
253-
.add_filter("Flash Files", &["swf", "spl", "ruf"])
254-
.add_filter("All Files", &["*"])
255-
.set_title("Load a Flash File");
256-
257-
if let Some(dir) = dir {
258-
dialog = dialog.set_directory(dir);
259-
}
260-
261-
if let Some(parent) = parent {
262-
dialog = dialog.set_parent(parent);
263-
}
264-
265-
dialog.pick_file().await.map(|h| h.into())
266-
}
267-
268246
#[cfg(not(feature = "tracy"))]
269247
pub fn plot_stats_in_tracy(_instance: &wgpu::Instance) {}
270248

0 commit comments

Comments
 (0)