diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2021-09-26 00:30:16 -0400 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2021-09-26 00:30:16 -0400 |
commit | 35ec66eaa70383cda591eeeaccc6560cfa921317 (patch) | |
tree | 6f1e350df90c1964f3ef0b71943269344db2783f /src/app/widgets | |
parent | abcca77c1ddcd465906549263b27dc41e769a30a (diff) |
refactor: cover almost all keybinds except killing processes
Diffstat (limited to 'src/app/widgets')
-rw-r--r-- | src/app/widgets/base/text_input.rs | 38 | ||||
-rw-r--r-- | src/app/widgets/bottom_widgets/carousel.rs | 25 | ||||
-rw-r--r-- | src/app/widgets/bottom_widgets/empty.rs | 8 | ||||
-rw-r--r-- | src/app/widgets/bottom_widgets/process.rs | 20 |
4 files changed, 61 insertions, 30 deletions
diff --git a/src/app/widgets/base/text_input.rs b/src/app/widgets/base/text_input.rs index 1ca60910..32106a15 100644 --- a/src/app/widgets/base/text_input.rs +++ b/src/app/widgets/base/text_input.rs @@ -285,6 +285,24 @@ impl TextInput { area, ); } + + fn move_left(&mut self) -> ComponentEventResult { + let original_cursor = self.cursor.cur_cursor(); + if self.move_back() == original_cursor { + ComponentEventResult::NoRedraw + } else { + ComponentEventResult::Redraw + } + } + + fn move_right(&mut self) -> ComponentEventResult { + let original_cursor = self.cursor.cur_cursor(); + if self.move_forward() == original_cursor { + ComponentEventResult::NoRedraw + } else { + ComponentEventResult::Redraw + } + } } impl Component for TextInput { @@ -299,22 +317,8 @@ impl Component for TextInput { fn handle_key_event(&mut self, event: KeyEvent) -> ComponentEventResult { if event.modifiers.is_empty() || event.modifiers == KeyModifiers::SHIFT { match event.code { - KeyCode::Left => { - let original_cursor = self.cursor.cur_cursor(); - if self.move_back() == original_cursor { - ComponentEventResult::NoRedraw - } else { - ComponentEventResult::Redraw - } - } - KeyCode::Right => { - let original_cursor = self.cursor.cur_cursor(); - if self.move_forward() == original_cursor { - ComponentEventResult::NoRedraw - } else { - ComponentEventResult::Redraw - } - } + KeyCode::Left => self.move_left(), + KeyCode::Right => self.move_right(), KeyCode::Backspace => self.clear_previous_grapheme(), KeyCode::Delete => self.clear_current_grapheme(), KeyCode::Char(c) => self.insert_character(c), @@ -349,6 +353,8 @@ impl Component for TextInput { match event.code { KeyCode::Char('b') => self.move_word_back(), KeyCode::Char('f') => self.move_word_forward(), + KeyCode::Char('h') => self.move_left(), + KeyCode::Char('l') => self.move_right(), _ => ComponentEventResult::Unhandled, } } else { diff --git a/src/app/widgets/bottom_widgets/carousel.rs b/src/app/widgets/bottom_widgets/carousel.rs index 92d3614c..3788cf97 100644 --- a/src/app/widgets/bottom_widgets/carousel.rs +++ b/src/app/widgets/bottom_widgets/carousel.rs @@ -12,14 +12,15 @@ use tui::{ use crate::{ app::{ - does_bound_intersect_coordinate, event::ComponentEventResult, Component, SelectableType, - Widget, + does_bound_intersect_coordinate, + event::{ComponentEventResult, SelectionAction}, + Component, Widget, }, canvas::Painter, options::layout_options::LayoutRule, }; -/// A container that "holds"" multiple [`BottomWidget`]s through their [`NodeId`]s. +/// A container that "holds" multiple [`BottomWidget`]s through their [`NodeId`]s. #[derive(PartialEq, Eq)] pub struct Carousel { index: usize, @@ -109,7 +110,7 @@ impl Carousel { .direction(tui::layout::Direction::Vertical) .split(area); - self.set_bounds(split_area[0]); + self.set_bounds(area); if let Some((_prev_id, prev_element_name)) = self.get_prev() { let prev_arrow_text = Spans::from(Span::styled( @@ -198,11 +199,15 @@ impl Widget for Carousel { self.height } - fn selectable_type(&self) -> SelectableType { - if let Some(node) = self.get_currently_selected() { - SelectableType::Redirect(node) - } else { - SelectableType::Unselectable - } + fn handle_widget_selection_left(&mut self) -> SelectionAction { + // Override to move to the left widget + self.decrement_index(); + SelectionAction::Handled + } + + fn handle_widget_selection_right(&mut self) -> SelectionAction { + // Override to move to the right widget + self.increment_index(); + SelectionAction::Handled } } diff --git a/src/app/widgets/bottom_widgets/empty.rs b/src/app/widgets/bottom_widgets/empty.rs index 4c33523b..e444ac2b 100644 --- a/src/app/widgets/bottom_widgets/empty.rs +++ b/src/app/widgets/bottom_widgets/empty.rs @@ -1,7 +1,7 @@ use tui::layout::Rect; use crate::{ - app::{Component, Widget}, + app::{Component, SelectableType, Widget}, options::layout_options::LayoutRule, }; @@ -35,8 +35,6 @@ impl Empty { impl Component for Empty { fn bounds(&self) -> Rect { - // TODO: Maybe think of how to store this without making it available for clicking. Separate bounds out to the layout? Might - // need to keep the bounds calculations for some components, so maybe implement it specifically for them. Rect::default() } @@ -55,4 +53,8 @@ impl Widget for Empty { fn height(&self) -> LayoutRule { self.height } + + fn selectable_type(&self) -> SelectableType { + SelectableType::Unselectable + } } diff --git a/src/app/widgets/bottom_widgets/process.rs b/src/app/widgets/bottom_widgets/process.rs index 8a704848..3851bfcd 100644 --- a/src/app/widgets/bottom_widgets/process.rs +++ b/src/app/widgets/bottom_widgets/process.rs @@ -984,6 +984,24 @@ impl ProcessManager { ComponentEventResult::Signal(ReturnSignal::Update) } + fn toggle_memory(&mut self) -> ComponentEventResult { + if matches!( + self.process_table.columns()[3].sort_type, + ProcessSortType::MemPercent + ) { + self.process_table + .set_column(ProcessSortColumn::new(ProcessSortType::Mem), 3); + } else { + self.process_table + .set_column(ProcessSortColumn::new(ProcessSortType::MemPercent), 3); + } + + // Invalidate row cache. + self.process_table.invalidate_cached_columns(); // TODO: This should be automatically called somehow after sets/removes to avoid forgetting it - maybe do a queue system? + + ComponentEventResult::Signal(ReturnSignal::Update) + } + fn hide_sort(&mut self) { self.show_sort = false; if let ProcessManagerSelection::Sort = self.selected { @@ -1081,7 +1099,7 @@ impl Component for ProcessManager { return self.open_search(); } KeyCode::Char('%') => { - // Handle switching memory usage type + return self.toggle_memory(); } KeyCode::Char('+') => { // Expand a branch |