Switch between selections
This commit is contained in:
parent
8840ca9147
commit
f74762f0b0
|
@ -26,7 +26,7 @@ pub enum UIState {
|
||||||
Terminated
|
Terminated
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum MenuSelection {
|
enum MenuSelection {
|
||||||
Stats,
|
Stats,
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
@ -51,15 +51,32 @@ impl MenuSelection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ListSelection {
|
#[derive(Copy, Clone)]
|
||||||
pub id: usize
|
enum SectionMode {
|
||||||
|
UpperSection,
|
||||||
|
LowerSection,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ListSelection {
|
impl SectionMode {
|
||||||
pub fn increment(&mut self, max: usize) {
|
pub fn as_id(&self) -> usize {
|
||||||
let new_selection = self.id + 1;
|
match self {
|
||||||
|
SectionMode::UpperSection => 0,
|
||||||
|
SectionMode::LowerSection => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.id = {
|
struct SectionSelection {
|
||||||
|
mode: SectionMode,
|
||||||
|
id: [usize; 2]
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SectionSelection {
|
||||||
|
fn increment(&mut self, max: usize) {
|
||||||
|
let cur_id = &mut self.id[self.mode.as_id()];
|
||||||
|
let new_selection = *cur_id + 1;
|
||||||
|
|
||||||
|
*cur_id = {
|
||||||
if new_selection >= max {
|
if new_selection >= max {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -70,10 +87,11 @@ impl ListSelection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrement(&mut self, max: usize) {
|
fn decrement(&mut self, max: usize) {
|
||||||
let new_selection = self.id - 1;
|
let cur_id = &mut self.id[self.mode.as_id()];
|
||||||
|
let new_selection = *cur_id - 1;
|
||||||
|
|
||||||
self.id = {
|
*cur_id = {
|
||||||
if new_selection >= max {
|
if new_selection >= max {
|
||||||
max - 1
|
max - 1
|
||||||
}
|
}
|
||||||
|
@ -83,11 +101,27 @@ impl ListSelection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn switch_section_to(&mut self, section_mode: SectionMode) {
|
||||||
|
self.mode = section_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_section_mode_id(&self) -> usize {
|
||||||
|
self.mode.as_id()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_selection_for(&self, section_mode: SectionMode) -> usize {
|
||||||
|
self.id[section_mode.as_id()]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_selection(&self) -> usize {
|
||||||
|
self.get_selection_for(self.mode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ListSelection {
|
impl Default for SectionSelection {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self{id: 0}
|
Self{mode: SectionMode::UpperSection, id: [0, 0]}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,19 +131,17 @@ pub struct ConsoleUI {
|
||||||
terminal: Terminal,
|
terminal: Terminal,
|
||||||
console_size: Rect,
|
console_size: Rect,
|
||||||
menu_selection: MenuSelection,
|
menu_selection: MenuSelection,
|
||||||
section_selection: ListSelection,
|
section_selection: SectionSelection,
|
||||||
first_section_id: usize,
|
|
||||||
second_section_id: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConsoleUI {
|
impl ConsoleUI {
|
||||||
pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI {
|
pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI {
|
||||||
ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: ListSelection::default(), first_section_id: 0, second_section_id: 0}
|
ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: SectionSelection::default()}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> {
|
pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> {
|
||||||
self.data = ConsoleUIData::from(memory_map)?;
|
self.data = ConsoleUIData::from(memory_map)?;
|
||||||
self.section_selection = ListSelection::default();
|
self.section_selection = SectionSelection::default();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -118,6 +150,28 @@ impl ConsoleUI {
|
||||||
let input_result = {
|
let input_result = {
|
||||||
match self.rx.recv()? {
|
match self.rx.recv()? {
|
||||||
Event::Input(event) => {
|
Event::Input(event) => {
|
||||||
|
if matches!(self.menu_selection, MenuSelection::Stats) {
|
||||||
|
match event.code {
|
||||||
|
KeyCode::Down => {
|
||||||
|
self.section_selection.increment(self.data.section_info.len());
|
||||||
|
return Ok(UIState::Render);
|
||||||
|
},
|
||||||
|
KeyCode::Up => {
|
||||||
|
self.section_selection.decrement(self.data.section_info.len());
|
||||||
|
return Ok(UIState::Render);
|
||||||
|
},
|
||||||
|
KeyCode::Char('a') => {
|
||||||
|
self.section_selection.switch_section_to(SectionMode::UpperSection);
|
||||||
|
return Ok(UIState::Render);
|
||||||
|
},
|
||||||
|
KeyCode::Char('s') => {
|
||||||
|
self.section_selection.switch_section_to(SectionMode::LowerSection);
|
||||||
|
return Ok(UIState::Render);
|
||||||
|
},
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match event.code {
|
match event.code {
|
||||||
KeyCode::Char('q') => Ok(UIState::Terminated),
|
KeyCode::Char('q') => Ok(UIState::Terminated),
|
||||||
KeyCode::Char('s') => {
|
KeyCode::Char('s') => {
|
||||||
|
@ -132,26 +186,6 @@ impl ConsoleUI {
|
||||||
self.menu_selection = self.menu_selection.next();
|
self.menu_selection = self.menu_selection.next();
|
||||||
Ok(UIState::Render)
|
Ok(UIState::Render)
|
||||||
},
|
},
|
||||||
KeyCode::Down => {
|
|
||||||
if matches!(self.menu_selection, MenuSelection::Stats) {
|
|
||||||
self.section_selection.increment(self.data.section_info.len());
|
|
||||||
Ok(UIState::Render)
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
Ok(UIState::Alive)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
KeyCode::Up => {
|
|
||||||
if matches!(self.menu_selection, MenuSelection::Stats) {
|
|
||||||
self.section_selection.decrement(self.data.section_info.len());
|
|
||||||
Ok(UIState::Render)
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
Ok(UIState::Alive)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
if matches!(self.menu_selection, MenuSelection::Quit) {
|
if matches!(self.menu_selection, MenuSelection::Quit) {
|
||||||
Ok(UIState::Terminated)
|
Ok(UIState::Terminated)
|
||||||
|
@ -178,7 +212,6 @@ impl ConsoleUI {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.first_section_id = self.section_selection.id;
|
|
||||||
input_result
|
input_result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +266,8 @@ impl ConsoleUI {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) {
|
fn render_stats(frame: &mut ConsoleFrame, layout: Rect, section_selection: &SectionSelection, data: &mut ConsoleUIData) {
|
||||||
|
const HIGHLIGHT_COLOR:[Color;2] = [Color::Cyan, Color::Red];
|
||||||
let layout = Layout::default().direction(Direction::Vertical).constraints([
|
let layout = Layout::default().direction(Direction::Vertical).constraints([
|
||||||
Constraint::Length(3),
|
Constraint::Length(3),
|
||||||
Constraint::Max(3),
|
Constraint::Max(3),
|
||||||
|
@ -252,13 +286,13 @@ impl ConsoleUI {
|
||||||
.style(Style::default().fg(Color::White))
|
.style(Style::default().fg(Color::White))
|
||||||
.border_type(BorderType::Plain)
|
.border_type(BorderType::Plain)
|
||||||
)
|
)
|
||||||
.highlight_style(Style::default().bg(Color::Cyan));
|
.highlight_style(Style::default().bg(HIGHLIGHT_COLOR[section_selection.get_section_mode_id()]));
|
||||||
|
|
||||||
let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[content_selection.id], Color::Cyan);
|
let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::UpperSection)], HIGHLIGHT_COLOR[0]);
|
||||||
let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[0], Color::Red);
|
let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::LowerSection)], HIGHLIGHT_COLOR[1]);
|
||||||
let mut list_state = ListState::default();
|
let mut list_state = ListState::default();
|
||||||
|
|
||||||
list_state.select(Some(content_selection.id));
|
list_state.select(Some(section_selection.get_current_selection()));
|
||||||
|
|
||||||
frame.render_stateful_widget(content_list, layout[0], &mut list_state);
|
frame.render_stateful_widget(content_list, layout[0], &mut list_state);
|
||||||
frame.render_widget(first_mem_gauge, layout[1]);
|
frame.render_widget(first_mem_gauge, layout[1]);
|
||||||
|
|
Loading…
Reference in New Issue