don't reload every time the filter changes

This commit is contained in:
Crispy 2023-08-20 10:42:47 +02:00
parent a553050f91
commit 5992f4e6fd

View file

@ -29,6 +29,7 @@ struct Entry {
name: String, name: String,
path: PathBuf, path: PathBuf,
filetype: FileType, filetype: FileType,
visible: bool,
} }
#[derive(PartialEq)] #[derive(PartialEq)]
@ -87,13 +88,19 @@ impl FileGui {
.unwrap() .unwrap()
.filter_map(|dir_entry| dir_entry.ok().map(Entry::new)) .filter_map(|dir_entry| dir_entry.ok().map(Entry::new))
.flatten() .flatten()
.filter(|e| e.name.contains(&self.search_input))
.collect(); .collect();
self.entries self.entries
.sort_by(|a, b| b.is_dir().cmp(&a.is_dir()).then(a.name.cmp(&b.name))); .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(); 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) { fn to_parent(&mut self) {
if let Some(path) = self.current_dir.parent() { if let Some(path) = self.current_dir.parent() {
self.current_dir = path.to_owned(); self.current_dir = path.to_owned();
@ -115,10 +122,8 @@ impl FileGui {
} }
} }
} }
}
impl eframe::App for FileGui { fn key_inputs(&mut self, ctx: &egui::Context) {
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) {
if ctx.input(|i| i.key_pressed(Key::F) && i.modifiers.ctrl) { if ctx.input(|i| i.key_pressed(Key::F) && i.modifiers.ctrl) {
ctx.memory_mut(|m| m.request_focus(self.search_input_id)); 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) { if ctx.input(|i| i.key_pressed(Key::R) && i.modifiers.ctrl) {
self.refresh_entries(); 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| { egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button("").clicked() { if ui.button("").clicked() {
@ -149,7 +163,7 @@ impl eframe::App for FileGui {
let filter_input = ui.text_edit_singleline(&mut self.search_input); let filter_input = ui.text_edit_singleline(&mut self.search_input);
self.search_input_id = filter_input.id; self.search_input_id = filter_input.id;
if filter_input.changed() { if filter_input.changed() {
self.refresh_entries(); self.refresh_filter();
} }
if let Some(filename) = self.loaded_dir.file_name() { 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) .max_scroll_height(frame.info().window_info.size.y)
.column(Column::auto()) .column(Column::auto())
.column(Column::remainder()) .column(Column::remainder())
.body(|body| { .body(|mut body| {
let mut to_open = None; let mut to_open = None;
body.rows(18.0, self.entries.len(), |row_index, mut row| { for (row_index, entry) in self.entries.iter().enumerate() {
let entry = &self.entries[row_index]; if entry.visible {
row.col(|ui| { body.row(18.0, |mut row| {
ui.label(entry.filetype.icon()); row.col(|ui| {
}); ui.label(entry.filetype.icon());
row.col(|ui| { });
if ui.button(&entry.name).clicked() { row.col(|ui| {
to_open = Some(row_index); 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 { if let Some(row_index) = to_open {
self.open(row_index); self.open(row_index);
} }
@ -207,6 +226,7 @@ impl Entry {
}; };
Some(Self { Some(Self {
visible: true,
name, name,
path, path,
filetype, filetype,