diff --git a/src/gui/draw.rs b/src/gui/draw.rs index 00fafe3..d0adeb1 100644 --- a/src/gui/draw.rs +++ b/src/gui/draw.rs @@ -1,4 +1,4 @@ -use crate::gui::{SUPPORTED_EXTENSIONS, SoundpadGui}; +use crate::gui::SoundpadGui; use egui::{ Align, AtomExt, Button, CollapsingHeader, Color32, ComboBox, CursorIcon, FontFamily, Label, Layout, RichText, ScrollArea, Sense, Slider, TextEdit, Ui, Vec2, @@ -376,37 +376,15 @@ impl SoundpadGui { ui.set_min_height(area_size.y); ui.vertical(|ui| { - let mut files: Vec = self.app_state.files.iter().cloned().collect(); - files.sort(); + let files = self.get_filtered_files(); for entry_path in files { - if entry_path.is_dir() { - continue; - } - - if !SUPPORTED_EXTENSIONS - .contains(&entry_path.extension().unwrap_or_default().to_str().unwrap()) - { - continue; - } - let file_name = entry_path .file_name() .unwrap() .to_string_lossy() .to_string(); - let search_query = self - .app_state - .search_query - .to_lowercase() - .trim() - .to_string(); - - if !file_name.to_lowercase().contains(search_query.as_str()) { - continue; - } - let mut file_button_text = RichText::new(file_name); if let Some(current_file) = &self.app_state.selected_file { if current_file.eq(&entry_path) { diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 5f53f48..2775572 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -145,6 +145,47 @@ impl SoundpadGui { pub fn stop(&mut self, id: Option) { make_request_async(Request::stop(id)); } + + pub fn get_filtered_files(&self) -> Vec { + let mut files: Vec = self.app_state.files.iter().cloned().collect(); + files.sort(); + + let search_query = self.app_state.search_query.to_lowercase(); + let search_query = search_query.trim(); + + files + .into_iter() + .filter(|entry_path| { + if entry_path.is_dir() { + return false; + } + + if !SUPPORTED_EXTENSIONS.contains( + &entry_path + .extension() + .unwrap_or_default() + .to_str() + .unwrap_or_default(), + ) { + return false; + } + + if !search_query.is_empty() { + let file_name = entry_path + .file_name() + .unwrap_or_default() + .to_string_lossy() + .to_string(); + + if !file_name.to_lowercase().contains(search_query) { + return false; + } + } + + true + }) + .collect() + } } pub async fn run() -> Result<(), Box> {