Skip to content

Commit

Permalink
remember folder used by file picker for the duration of the execution
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Dec 20, 2024
1 parent 1cd9c2d commit ddfe818
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/ui/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct RuxApplication {
sound_font_file: Option<PathBuf>, // sound font file
beat_sender: Arc<Sender<usize>>, // beat notifier
beat_receiver: Arc<Mutex<Receiver<usize>>>, // beat receiver
file_picker_folder: Option<PathBuf>, // last folder used in file picker,
}

#[derive(Debug)]
Expand Down Expand Up @@ -114,10 +115,10 @@ impl Display for TrackSelection {

#[derive(Debug, Clone)]
pub enum Message {
OpenFileDialog, // open file dialog
OpenFile(PathBuf), // open file path
FileOpened(Result<(Vec<u8>, String), FilePickerError>), // file content & file name
TrackSelected(TrackSelection), // track selection
OpenFileDialog, // open file dialog
OpenFile(PathBuf), // open file path
FileOpened(Result<(Vec<u8>, Option<PathBuf>, String), FilePickerError>), // file content, parent folder & file name
TrackSelected(TrackSelection), // track selection
FocusMeasure(usize), // used when clicking on measure in tablature
FocusTick(usize), // focus on a specific tick in the tablature
PlayPause, // toggle play/pause
Expand Down Expand Up @@ -145,6 +146,7 @@ impl RuxApplication {
sound_font_file,
beat_receiver: Arc::new(Mutex::new(beat_receiver)),
beat_sender: Arc::new(beat_sender),
file_picker_folder: None, // TODO store last folder used in $user/home/.ruxguitar
}
}

Expand Down Expand Up @@ -192,7 +194,10 @@ impl RuxApplication {
Task::none()
} else {
self.tab_file_is_loading = true;
Task::perform(open_file_dialog(), Message::FileOpened)
Task::perform(
open_file_dialog(self.file_picker_folder.clone()),
Message::FileOpened,
)
}
}
Message::OpenFile(path) => {
Expand All @@ -206,7 +211,8 @@ impl RuxApplication {
audio_player.stop();
}
match result {
Ok((contents, file_name)) => {
Ok((contents, parent_folder, file_name)) => {
self.file_picker_folder = parent_folder;
if let Ok(song) = parse_gp_data(&contents) {
// build all tracks selection
let track_selections: Vec<_> = song
Expand Down
22 changes: 16 additions & 6 deletions src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ pub enum FilePickerError {
}

/// Opens a file dialog and returns the content of the picked file.
pub async fn open_file_dialog() -> Result<(Vec<u8>, String), FilePickerError> {
let picked_file = rfd::AsyncFileDialog::new()
pub async fn open_file_dialog(
picker_folder: Option<PathBuf>,
) -> Result<(Vec<u8>, Option<PathBuf>, String), FilePickerError> {
let mut picker = rfd::AsyncFileDialog::new()
.add_filter("Guitar Pro files", &["gp5", "gp4"])
.set_title("Pick a GP file")
//.set_directory() TODO remember last directory and set it here
.set_title("Select a Guitar Pro file");

if let Some(folder) = picker_folder {
picker = picker.set_directory(folder);
}

let picked_file = picker
.pick_file()
.await
.ok_or(FilePickerError::DialogClosed)?;
Expand All @@ -23,7 +30,9 @@ pub async fn open_file_dialog() -> Result<(Vec<u8>, String), FilePickerError> {
/// Loads the content of a file at the given path.
///
/// Return the content of the file and its name.
pub async fn load_file(path: impl Into<PathBuf>) -> Result<(Vec<u8>, String), FilePickerError> {
pub async fn load_file(
path: impl Into<PathBuf>,
) -> Result<(Vec<u8>, Option<PathBuf>, String), FilePickerError> {
let path = path.into();
let file_extension = path
.extension()
Expand All @@ -41,9 +50,10 @@ pub async fn load_file(path: impl Into<PathBuf>) -> Result<(Vec<u8>, String), Fi
.and_then(|f| f.to_str())
.map(|f| f.to_string())
.unwrap_or_default();
let parent_folder = path.parent().map(|p| p.into());
log::info!("Loading file: {:?}", file_name);
tokio::fs::read(&path)
.await
.map_err(|error| FilePickerError::IoError(error.to_string()))
.map(|content| (content, file_name))
.map(|content| (content, parent_folder, file_name))
}

0 comments on commit ddfe818

Please sign in to comment.