Skip to content

Commit

Permalink
fix: fixed roms path to default to current directory, if valid, and c…
Browse files Browse the repository at this point in the history
…anonicalize
  • Loading branch information
lukexor committed Jun 22, 2024
1 parent 4947a8c commit e00273f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tetanes/src/nes/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Default for RendererConfig {
},
zoom: 1.0,
recent_roms: HashSet::default(),
roms_path: None,
roms_path: std::env::current_dir().ok(),
show_perf_stats: false,
show_messages: true,
show_menubar: true,
Expand Down
17 changes: 8 additions & 9 deletions tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
use anyhow::anyhow;
use egui::ViewportId;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use tetanes_core::{
action::Action as DeckAction,
apu::Channel,
Expand Down Expand Up @@ -504,13 +504,12 @@ impl Running {
UiEvent::Message((ty, msg)) => self.renderer.add_message(ty, msg),
UiEvent::Error(err) => self.renderer.on_error(anyhow!(err)),
UiEvent::LoadRomDialog => {
let dir = self
.cfg
.renderer
.roms_path
.as_deref()
.unwrap_or_else(|| Path::new("."));
match open_file_dialog("Load ROM", "NES ROMs", &["nes"], dir) {
match open_file_dialog(
"Load ROM",
"NES ROMs",
&["nes"],
self.cfg.renderer.roms_path.as_ref(),
) {
Ok(maybe_path) => {
if let Some(path) = maybe_path {
self.nes_event(EmulationEvent::LoadRomPath(path));
Expand All @@ -527,7 +526,7 @@ impl Running {
"Load Replay",
"Replay Recording",
&["replay"],
Config::default_data_dir(),
Some(Config::default_data_dir()),
) {
Ok(maybe_path) => {
if let Some(path) = maybe_path {
Expand Down
5 changes: 4 additions & 1 deletion tetanes/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ impl Opts {

cfg.audio.enabled = !self.silent && cfg.audio.enabled;

cfg.renderer.roms_path = self.path.or(cfg.renderer.roms_path);
cfg.renderer.roms_path = self
.path
.or(cfg.renderer.roms_path)
.and_then(|path| path.canonicalize().ok());
cfg.renderer.fullscreen = self.fullscreen || cfg.renderer.fullscreen;

Ok(cfg)
Expand Down
2 changes: 1 addition & 1 deletion tetanes/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn open_file_dialog(
title: impl Into<String>,
name: impl Into<String>,
extensions: &[impl ToString],
dir: impl AsRef<Path>,
dir: Option<impl AsRef<Path>>,
) -> anyhow::Result<Option<PathBuf>> {
platform::open_file_dialog_impl(title, name, extensions, dir)
}
Expand Down
8 changes: 5 additions & 3 deletions tetanes/src/sys/platform/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ pub fn open_file_dialog_impl(
title: impl Into<String>,
name: impl Into<String>,
extensions: &[impl ToString],
dir: impl AsRef<Path>,
dir: Option<impl AsRef<Path>>,
) -> anyhow::Result<Option<PathBuf>> {
let dialog = rfd::FileDialog::new()
let mut dialog = rfd::FileDialog::new()
.set_title(title)
.set_directory(dir.as_ref())
.add_filter(name, extensions);
if let Some(dir) = dir {
dialog = dialog.set_directory(dir.as_ref());
}
Ok(dialog.pick_file())
}

Expand Down
2 changes: 1 addition & 1 deletion tetanes/src/sys/platform/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn open_file_dialog_impl(
_title: impl Into<String>,
_name: impl Into<String>,
extensions: &[impl ToString],
_dir: impl AsRef<Path>,
_dir: Option<impl AsRef<Path>>,
) -> anyhow::Result<Option<PathBuf>> {
let input_id = match extensions[0].to_string().as_str() {
"nes" => html_ids::ROM_INPUT,
Expand Down

0 comments on commit e00273f

Please sign in to comment.