Skip to content

Commit

Permalink
[visualbnd] Add an open recent file menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
rgiot committed Aug 13, 2023
1 parent 4373a7a commit 76722d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 0 additions & 1 deletion cpclib-visual-bndbuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions cpclib-visual-bndbuild/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct BndBuildApp {
/// The provided filename by the user
filename: Option<std::path::PathBuf>,

/// Recently opened files
recent_files: Vec<std::path::PathBuf>,

/// The content of the file loaded
#[serde(skip)]
file_content: Option<String>,
Expand Down Expand Up @@ -111,7 +114,8 @@ impl Default for BndBuildApp {
gags: (
gag::BufferRedirect::stdout().unwrap(),
gag::BufferRedirect::stderr().unwrap()
)
),
recent_files: Vec::new()
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 76722d4

Please sign in to comment.