refactor: refactor input handling for Enter key and directory navigation

This commit is contained in:
2026-01-28 00:34:44 +03:00
parent 4e7606fdc6
commit 2c6f0d932e
+20 -24
View File
@@ -20,20 +20,6 @@ impl SoundpadGui {
self.app_state.show_settings = !self.app_state.show_settings;
}
if self.key_pressed(ctx, Key::Enter) && self.app_state.selected_file.is_some() {
let path = &self.app_state.selected_file.clone().unwrap();
if modifiers.ctrl {
self.play_file(path, true);
} else if modifiers.shift
&& let Some(last_track) = self.audio_player_state.tracks.last()
{
self.stop(Some(last_track.id));
self.play_file(path, true);
} else {
self.play_file(path, false);
}
}
if !self.app_state.show_settings {
// Pause / resume audio on space
if self.key_pressed(ctx, Key::Space) {
@@ -50,12 +36,25 @@ impl SoundpadGui {
self.app_state.force_focus_search = true;
}
// Iterate through dirs if there are some
// Play selected file on Enter
if self.key_pressed(ctx, Key::Enter) && self.app_state.selected_file.is_some() {
let path = &self.app_state.selected_file.clone().unwrap();
if modifiers.ctrl {
self.play_file(path, true);
} else if modifiers.shift
&& let Some(last_track) = self.audio_player_state.tracks.last()
{
self.stop(Some(last_track.id));
self.play_file(path, true);
} else {
self.play_file(path, false);
}
}
// Iterate through dirs and files with Ctrl + Up/Down
let arrow_up_pressed = self.key_pressed(ctx, Key::ArrowUp);
let arrow_down_pressed = self.key_pressed(ctx, Key::ArrowDown);
if arrow_up_pressed || arrow_down_pressed {
if modifiers.ctrl && (arrow_up_pressed || arrow_down_pressed) {
if modifiers.shift && !self.app_state.dirs.is_empty() {
let mut dirs: Vec<PathBuf> = self.app_state.dirs.iter().cloned().collect();
dirs.sort();
@@ -84,8 +83,7 @@ impl SoundpadGui {
self.open_dir(&dirs[new_dir_index as usize]);
} else if self.app_state.current_dir.is_some() {
let mut files: Vec<PathBuf> =
self.app_state.files.iter().cloned().collect();
let mut files: Vec<PathBuf> = self.app_state.files.iter().cloned().collect();
files.sort();
let current_files_index: i64;
@@ -101,8 +99,8 @@ impl SoundpadGui {
let mut new_files_index: i64;
new_files_index = current_files_index - arrow_up_pressed as i64
+ arrow_down_pressed as i64;
new_files_index =
current_files_index - arrow_up_pressed as i64 + arrow_down_pressed as i64;
if new_files_index < 0 {
new_files_index = (files.len() - 1) as i64;
@@ -110,9 +108,7 @@ impl SoundpadGui {
new_files_index = 0;
}
self.app_state.selected_file =
Some(files[new_files_index as usize].clone());
}
self.app_state.selected_file = Some(files[new_files_index as usize].clone());
}
}
}