From fe53d4577a22973317595a64eddf24dfcab20881 Mon Sep 17 00:00:00 2001 From: Jaby Blubb Date: Tue, 15 Aug 2023 15:21:10 +0200 Subject: [PATCH] Cleanup selection code --- src/Tools/psxreadmap/src/lib.rs | 85 +++++++++++---------------------- 1 file changed, 27 insertions(+), 58 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 77055180..626f5d5a 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -52,55 +52,42 @@ impl MenuSelection { } pub struct ListSelection { - pub selection: Option + pub id: usize } impl ListSelection { pub fn increment(&mut self, max: usize) { - if let Some(cur_selection) = self.selection { - self.selection = Some({ - if cur_selection + 1 >= max { - 0 - } + let new_selection = self.id + 1; - else { - cur_selection + 1 - } - }); - } + self.id = { + if new_selection >= max { + 0 + } + + else { + new_selection + } + } } pub fn decrement(&mut self, max: usize) { - if let Some(mut cur_selection) = self.selection { - self.selection = Some({ - cur_selection = cur_selection - 1; - if cur_selection > max - 1 { - max - 1 - } + let new_selection = self.id - 1; - else { - cur_selection - } - }); + self.id = { + if new_selection >= max { + max - 1 + } + + else { + new_selection + } } } - - pub fn activate(&mut self) { - self.selection = Some(0); - } - - pub fn deactivate(&mut self) { - self.selection = None; - } - - pub fn is_active(&self) -> bool { - self.selection.is_some() - } } impl Default for ListSelection { fn default() -> Self { - Self{selection: None} + Self{id: 0} } } @@ -122,16 +109,8 @@ impl ConsoleUI { pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { self.data = ConsoleUIData::from(memory_map)?; + self.section_selection = ListSelection::default(); - if self.data.section_info.len() > 0 { - if !self.section_selection.is_active() { - self.section_selection.activate(); - } - } - - else { - self.section_selection.deactivate(); - } Ok(()) } @@ -199,16 +178,7 @@ impl ConsoleUI { } }; - self.first_section_id = { - if let Some(selection) = self.section_selection.selection { - selection - } - - else { - 0 - } - }; - + self.first_section_id = self.section_selection.id; input_result } @@ -245,8 +215,7 @@ impl ConsoleUI { frame.render_widget(menu_bar, main_layout[0]); match self.menu_selection { MenuSelection::Stats => { - // ToDo: v This is the same and we always have elements now - Self::render_stats(frame, main_layout[1], (&self.section_selection, self.first_section_id), &mut self.data); + Self::render_stats(frame, main_layout[1], &self.section_selection, &mut self.data); }, MenuSelection::Quit => { frame.render_widget(Self::create_exit_message(), main_layout[1]); @@ -264,7 +233,7 @@ impl ConsoleUI { Ok(()) } - fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: (&ListSelection, usize), data: &mut ConsoleUIData) { + fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) { let layout = Layout::default().direction(Direction::Vertical).constraints([ Constraint::Length(3), Constraint::Max(3), @@ -285,11 +254,11 @@ impl ConsoleUI { ) .highlight_style(Style::default().bg(Color::Cyan)); - let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[content_selection.1], Color::Cyan); + let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[content_selection.id], Color::Cyan); let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[0], Color::Red); let mut list_state = ListState::default(); - list_state.select(content_selection.0.selection); + list_state.select(Some(content_selection.id)); frame.render_stateful_widget(content_list, layout[0], &mut list_state); frame.render_widget(first_mem_gauge, layout[1]);