From 5992f4e6fd1a3c91457b4c1f53435e47bcd9da50 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 20 Aug 2023 10:42:47 +0200 Subject: [PATCH] don't reload every time the filter changes --- src/main.rs | 54 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index b2568ff..8148794 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ struct Entry { name: String, path: PathBuf, filetype: FileType, + visible: bool, } #[derive(PartialEq)] @@ -87,13 +88,19 @@ impl FileGui { .unwrap() .filter_map(|dir_entry| dir_entry.ok().map(Entry::new)) .flatten() - .filter(|e| e.name.contains(&self.search_input)) .collect(); self.entries .sort_by(|a, b| b.is_dir().cmp(&a.is_dir()).then(a.name.cmp(&b.name))); + self.refresh_filter(); self.loaded_dir = self.current_dir.clone(); } + fn refresh_filter(&mut self) { + for entry in &mut self.entries { + entry.visible = entry.name.contains(&self.search_input); + } + } + fn to_parent(&mut self) { if let Some(path) = self.current_dir.parent() { self.current_dir = path.to_owned(); @@ -115,10 +122,8 @@ impl FileGui { } } } -} -impl eframe::App for FileGui { - fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) { + fn key_inputs(&mut self, ctx: &egui::Context) { if ctx.input(|i| i.key_pressed(Key::F) && i.modifiers.ctrl) { ctx.memory_mut(|m| m.request_focus(self.search_input_id)); } @@ -128,6 +133,15 @@ impl eframe::App for FileGui { if ctx.input(|i| i.key_pressed(Key::R) && i.modifiers.ctrl) { self.refresh_entries(); } + if ctx.input(|i| i.key_pressed(Key::ArrowUp) && i.modifiers.alt) { + self.to_parent(); + } + } +} + +impl eframe::App for FileGui { + fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) { + self.key_inputs(ctx); egui::TopBottomPanel::top("top_bar").show(ctx, |ui| { ui.horizontal(|ui| { if ui.button("⏶").clicked() { @@ -149,7 +163,7 @@ impl eframe::App for FileGui { let filter_input = ui.text_edit_singleline(&mut self.search_input); self.search_input_id = filter_input.id; if filter_input.changed() { - self.refresh_entries(); + self.refresh_filter(); } if let Some(filename) = self.loaded_dir.file_name() { @@ -163,19 +177,24 @@ impl eframe::App for FileGui { .max_scroll_height(frame.info().window_info.size.y) .column(Column::auto()) .column(Column::remainder()) - .body(|body| { + .body(|mut body| { let mut to_open = None; - body.rows(18.0, self.entries.len(), |row_index, mut row| { - let entry = &self.entries[row_index]; - row.col(|ui| { - ui.label(entry.filetype.icon()); - }); - row.col(|ui| { - if ui.button(&entry.name).clicked() { - to_open = Some(row_index); - } - }); - }); + for (row_index, entry) in self.entries.iter().enumerate() { + if entry.visible { + body.row(18.0, |mut row| { + row.col(|ui| { + ui.label(entry.filetype.icon()); + }); + row.col(|ui| { + let f = ui.button(&entry.name); + if f.clicked() { + to_open = Some(row_index); + } + // if f.has_focus() && ui.input(|i| i.key_pressed(Key::Delete)) {} + }); + }); + } + } if let Some(row_index) = to_open { self.open(row_index); } @@ -207,6 +226,7 @@ impl Entry { }; Some(Self { + visible: true, name, path, filetype,