From 76722d4c2d855951637ffb89da54e8bd0f65bdaf Mon Sep 17 00:00:00 2001 From: Romain Giot Date: Sun, 13 Aug 2023 09:53:46 +0200 Subject: [PATCH] [visualbnd] Add an open recent file menu item --- cpclib-visual-bndbuild/Cargo.toml | 1 - cpclib-visual-bndbuild/src/lib.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cpclib-visual-bndbuild/Cargo.toml b/cpclib-visual-bndbuild/Cargo.toml index f5eeebb3..913720c5 100644 --- a/cpclib-visual-bndbuild/Cargo.toml +++ b/cpclib-visual-bndbuild/Cargo.toml @@ -14,7 +14,6 @@ homepage.workspace = true cpclib-bndbuild.workspace = true eframe = {workspace=true, features=["persistence", "wgpu"]} egui_file = "0.10" - gag = "1.0.0" serde.workspace = true itertools.workspace = true diff --git a/cpclib-visual-bndbuild/src/lib.rs b/cpclib-visual-bndbuild/src/lib.rs index bcb91349..81c58b9f 100644 --- a/cpclib-visual-bndbuild/src/lib.rs +++ b/cpclib-visual-bndbuild/src/lib.rs @@ -39,6 +39,9 @@ pub struct BndBuildApp { /// The provided filename by the user filename: Option, + /// Recently opened files + recent_files: Vec, + /// The content of the file loaded #[serde(skip)] file_content: Option, @@ -111,7 +114,8 @@ impl Default for BndBuildApp { gags: ( gag::BufferRedirect::stdout().unwrap(), gag::BufferRedirect::stderr().unwrap() - ) + ), + recent_files: Vec::new() } } } @@ -253,12 +257,19 @@ impl BndBuildApp { Ok(builder) => { self.filename = Some(path.into()); self.file_content = std::fs::read_to_string(self.filename.as_ref().unwrap()).ok(); // read a second time, but the file exists - self.builder_and_layers = BuilderAndCache::from(builder).into() + self.builder_and_layers = BuilderAndCache::from(builder).into(); + + + if let Some(position) = self.recent_files.iter().position(|elem| elem == path) { + self.recent_files.remove(position); + } + self.recent_files.push(path.to_path_buf()); } Err(err) => { self.file_error = Some(err.to_string()); } } + self.logs.clear(); } pub fn update_cache(&mut self) { @@ -289,6 +300,17 @@ impl BndBuildApp { ui.close_menu(); }; + if ! self.recent_files.is_empty() { + ui.menu_button("Open Recent", |ui| { + for fname in self.recent_files.clone().iter().rev() { + if ui.add(Button::new(fname.display().to_string()).wrap(false)).clicked() { + self.load(fname); + ui.close_menu(); + } + } + }); + } + if self.filename.is_some() { if ui .add(Button::new("Save").shortcut_text(ctx.format_shortcut(&CTRL_S))) @@ -520,14 +542,17 @@ impl eframe::App for BndBuildApp { else { None }; + if let Some(path) = p { self.load(path); + ctx.request_repaint_after(REFRESH_DURATION); // ensure progress will be displayed } // Handle reload if self.request_reload { self.request_reload = false; self.load(self.filename.clone().unwrap()); + ctx.request_repaint_after(REFRESH_DURATION); // ensure progress will be displayed } if self.request_save {