From cdcce02689cd95dd6fa8708325fb55532e8c7bb6 Mon Sep 17 00:00:00 2001 From: Lukas Himbert Date: Sat, 27 Jan 2024 19:08:45 +0100 Subject: [PATCH] recent files list for V2 schemas --- tbled/src/main.rs | 40 +++++++++++++++++++++++++++++++++------ tbled/src/recent_files.rs | 8 ++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/tbled/src/main.rs b/tbled/src/main.rs index dd8606d..1effbed 100644 --- a/tbled/src/main.rs +++ b/tbled/src/main.rs @@ -43,6 +43,7 @@ pub enum CustomSchemaVersion { V1, // CS1 V2, // CS2-5 RecentV1(usize), // index into the "most recent schemas (v1) queue" + RecentV2(usize), // same but v2 } #[derive(Clone, Debug)] @@ -108,6 +109,7 @@ pub struct AppData { warnings: std::vec::Vec, //edited: std::collections::HashSet<(i32, i32)>, // edited since last save recent_schemas_v1: recent_files::RecentFiles, + recent_schemas_v2: recent_files::RecentFiles, } struct App { @@ -416,6 +418,7 @@ impl App { path, warnings, recent_schemas_v1: _, + recent_schemas_v2: _, } = &mut self.state; tbl_history.clear_all(); selected_attributes.clear(); @@ -468,6 +471,7 @@ impl App { path } CustomSchemaVersion::RecentV1(i) => self.state.recent_schemas_v1.get(i).unwrap().to_owned(), + CustomSchemaVersion::RecentV2(i) => self.state.recent_schemas_v2.get(i).unwrap().to_owned(), }; let raw_schemas = match misc::import_schemas(&path) { Ok(s) => s, @@ -483,9 +487,11 @@ impl App { recent_files::save_recent_schemas_v1(&self.state.recent_schemas_v1); self.update_menu_recent_schemas_v1(); } - CustomSchemaVersion::V2 => { - //| CustomSchemaVersion::RecentV2(_) => { - todo!() + CustomSchemaVersion::V2 | CustomSchemaVersion::RecentV2(_) => { + self.state.tbl_version = tocs::tbl::Version::V2(raw_schemas); + self.state.recent_schemas_v2.mark_as_updated(&path); + recent_files::save_recent_schemas_v2(&self.state.recent_schemas_v2); + self.update_menu_recent_schemas_v2(); } } self.state.tbl_history.clear_all(); @@ -508,11 +514,13 @@ impl App { self.update_menu_recent_schemas_v1(); } _ => { - todo!() + self.state.recent_schemas_v2.mark_as_updated(&path); + recent_files::save_recent_schemas_v2(&self.state.recent_schemas_v2); + self.update_menu_recent_schemas_v2(); } }, // ok Err(e) => { - fltk::dialog::alert_default(&format!("error saving to {}: {:?}", path, e)); + fltk::dialog::alert_default(&format!("error saving to {path}: {e:?}")); } } } @@ -588,6 +596,7 @@ impl App { path, warnings, recent_schemas_v1: _, + recent_schemas_v2: _, } = &mut self.state; tbl_history.clear_all(); selected_attributes.clear(); @@ -718,8 +727,23 @@ impl App { fn update_menu_recent_schemas_v2(&mut self) { let i = self.widgets.menu.find_index("&Schemas/Import/Recent (CS2 onward)\t"); + self.widgets.menu.clear_submenu(i).unwrap(); let mut recent_menu = self.widgets.menu.at(i).unwrap(); - recent_menu.deactivate(); + if self.state.recent_schemas_v2.is_empty() { + recent_menu.deactivate(); + } else { + recent_menu.activate(); + } + drop(recent_menu); + for (i, path) in self.state.recent_schemas_v2.iter().enumerate() { + self.widgets.menu.add_emit( + &format!("&Schemas/Import/Recent (CS2 onward)\t/{}", misc::QuotedFltkMenuLabel::new(path.as_str())), + fltk::enums::Shortcut::None, + fltk::menu::MenuFlag::Normal, + self.sender.clone(), + Msg::SetCustomVersion(CustomSchemaVersion::RecentV2(i)), + ); + } } fn update_menu_recent_tbls(&mut self) { @@ -764,6 +788,7 @@ impl App { tbl_history, warnings, recent_schemas_v1: _, + recent_schemas_v2: _, } = &mut self.state; // if there are warnings, show button to display them @@ -867,6 +892,7 @@ fn main() { path, warnings: vec![], recent_schemas_v1: recent_files::load_recent_schemas_v1(), + recent_schemas_v2: recent_files::load_recent_schemas_v2(), }, tocs::tbl::ReadTblResult::Warn { tbl, warnings } => AppData { tbl_history: TblStateHistory::new(tbl), @@ -876,6 +902,7 @@ fn main() { path, warnings, recent_schemas_v1: recent_files::load_recent_schemas_v1(), + recent_schemas_v2: recent_files::load_recent_schemas_v2(), }, tocs::tbl::ReadTblResult::Err(e) => { log::error!("Unable to read file: {e}"); @@ -895,6 +922,7 @@ fn main() { path: Utf8PathBuf::new(), warnings: vec![], recent_schemas_v1: recent_files::load_recent_schemas_v1(), + recent_schemas_v2: recent_files::load_recent_schemas_v2(), }, }; diff --git a/tbled/src/recent_files.rs b/tbled/src/recent_files.rs index 9fd1ee6..174bb1d 100644 --- a/tbled/src/recent_files.rs +++ b/tbled/src/recent_files.rs @@ -150,3 +150,11 @@ pub fn load_recent_schemas_v1() -> RecentFiles { pub fn save_recent_schemas_v1(recent_files: &RecentFiles) -> Option<()> { PATHS.as_ref().and_then(|p| recent_files.write_to_file(&p.schemas_v1).ok()) } + +pub fn load_recent_schemas_v2() -> RecentFiles { + load_file(PATHS.as_ref().map(|p| p.schemas_v2.as_ref())) +} + +pub fn save_recent_schemas_v2(recent_files: &RecentFiles) -> Option<()> { + PATHS.as_ref().and_then(|p| recent_files.write_to_file(&p.schemas_v2).ok()) +} -- 2.45.2