Skip to content

Commit

Permalink
Replace dialog remembers the last path it opened from
Browse files Browse the repository at this point in the history
honestly this was bothering me a lot, hopefully it helps someone else sleep at night too
  • Loading branch information
junetried committed Apr 4, 2022
1 parent 1d3b4d6 commit ae085a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
53 changes: 50 additions & 3 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use fltk::{
WidgetBase,
WidgetExt
},
browser::Browser
browser::Browser,
dialog::{ FileDialogType, NativeFileChooser }
};
use rodio::Source;
use crate::settings::CACHEDIR;
Expand Down Expand Up @@ -59,7 +60,9 @@ pub struct List {
/// Items in this nus3audio file.
pub items: Vec<ListItem>,
/// The browser widget representing the file.
widget: Browser
widget: Browser,
/// The last browse directory of the replace dialog
browser_path: Option<PathBuf>
}

impl List {
Expand All @@ -72,7 +75,8 @@ impl List {
name: String::new(),
path: None,
items: Vec::new(),
widget
widget,
browser_path: None
}
}

Expand All @@ -88,6 +92,49 @@ impl List {
self.widget.clear()
}

/// Replace a sound at `index` via a file dialog.
pub fn replace(&mut self, index: usize, settings: &crate::Settings) -> Result<(), String> {
let list_item = match self.items.get_mut(index) {
Some(item) => item,
None => return Err("Failed to find internal list item.\nYou shouldn't be seeing this during normal use.".to_owned())
};

let mut open_dialog = NativeFileChooser::new(FileDialogType::BrowseFile);
open_dialog.set_filter("*.{ogg,flac,wav,mp3,idsp,lopus}\n*.ogg\n*.flac\n*.wav\n*.mp3\n*.idsp\n*.lopus");
// Set the default path to the last path used
if let Some(path) = &self.browser_path {
let _ = open_dialog.set_directory(path);
}
open_dialog.show();

if open_dialog.filename().exists() {
// Set the last path used to the path we just used
self.browser_path = open_dialog.filename().parent().map(|path| path.to_owned());

let raw = fs::read(open_dialog.filename());
if let Err(error) = raw {
return Err(format!("Could not read file:\n{}", error))
}
let raw = raw.unwrap();

let result = if let Some(extension) = open_dialog.filename().extension() {
match extension.to_str() {
Some("idsp") => { list_item.from_encoded(&self.name, raw, settings) },
Some("lopus") => { list_item.from_encoded(&self.name, raw, settings) },
_ => list_item.set_audio_raw(raw)
}
} else { list_item.set_audio_raw(raw) };

if let Err(error) = result {
return Err(format!("Could not decode file:\n{}", error))
}

Ok(())
} else {
Ok(())
}
}

/// Save this nus3audio to the file at `self.path`.
pub fn save_nus3audio(&mut self, path: Option<&Path>, settings: &crate::settings::Settings) -> Result<(), String> {
let path = if let Some(path) = path { path } else { self.path.as_ref().expect("No path has been set to save.") };
Expand Down
40 changes: 7 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,40 +460,14 @@ fn main() {
},
Message::Replace => {
if let Some((index, _)) = file_list.selected() {
let list_item = file_list.items.get_mut(index).expect("Failed to find internal list item");

let mut open_dialog = NativeFileChooser::new(FileDialogType::BrowseFile);
open_dialog.set_filter("*.{ogg,flac,wav,mp3,idsp,lopus}\n*.ogg\n*.flac\n*.wav\n*.mp3\n*.idsp\n*.lopus");
open_dialog.show();

if open_dialog.filename().exists() {
window.set_cursor(Cursor::Wait);

let raw = fs::read(open_dialog.filename());
if let Err(error) = raw {
fltk::dialog::message_title("Error");
window.set_cursor(Cursor::Default);
alert(&window, &format!("Could not read file:\n{}", error));
continue
}
let raw = raw.unwrap();

let result = if let Some(extension) = open_dialog.filename().extension() {
match extension.to_str() {
Some("idsp") => { list_item.from_encoded(&file_list.name, raw, &settings) },
Some("lopus") => { list_item.from_encoded(&file_list.name, raw, &settings) },
_ => list_item.set_audio_raw(raw)
}
} else { list_item.set_audio_raw(raw) };

if let Err(error) = result {
fltk::dialog::message_title("Error");
window.set_cursor(Cursor::Default);
alert(&window, &format!("Could not decode file:\n{}", error));
}

window.set_cursor(Cursor::Default)
window.set_cursor(Cursor::Wait);
if let Err(error) = file_list.replace(index, &settings) {
fltk::dialog::message_title("Error");
window.set_cursor(Cursor::Default);
alert(&window, &error.to_string());
continue
}
window.set_cursor(Cursor::Default);
} else {
fltk::dialog::message_title("Alert");
alert(&window, "Nothing is selected.");
Expand Down

0 comments on commit ae085a3

Please sign in to comment.