From a42c20443dc8c53e42dbf473c5d2cd7ef4b806d0 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 30 Sep 2020 18:58:48 -0400 Subject: [PATCH] Create components for easier UI creation --- .vscode/settings.json | 1 + src/app.rs | 8 +- src/app/process_killer.rs | 1 + src/app/states.rs | 47 +++++----- src/canvas/canvas_colours.rs | 7 +- src/canvas/drawing_utils.rs | 8 +- src/canvas/screens/config_screen.rs | 47 ++++------ src/clap.rs | 4 +- src/components/base_traits/base_widget.rs | 51 +++++++++++ src/components/base_traits/drawable.rs | 9 ++ src/components/base_traits/events.rs | 25 ++++++ src/components/base_traits/mod.rs | 7 ++ src/components/mod.rs | 5 ++ src/components/scrollable_table.rs | 97 ++++++++++++++++++++ src/constants.rs | 1 + src/lib.rs | 4 +- src/options.rs | 105 ++++++++++++++++++++++ 17 files changed, 360 insertions(+), 67 deletions(-) create mode 100644 src/components/base_traits/base_widget.rs create mode 100644 src/components/base_traits/drawable.rs create mode 100644 src/components/base_traits/events.rs create mode 100644 src/components/base_traits/mod.rs create mode 100644 src/components/mod.rs create mode 100644 src/components/scrollable_table.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index dd8c5e7aa..85611a5b1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -42,6 +42,7 @@ "cmdline", "commandline", "concat", + "coord", "crossterm", "curr", "cvar", diff --git a/src/app.rs b/src/app.rs index 01899c002..0783b2b90 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,7 +11,9 @@ use layout_manager::*; pub use states::*; use crate::{ - canvas, constants, + canvas, + components::ScrollDirection, + constants, options::Config, options::ConfigFlags, options::WidgetIdEnabled, @@ -126,6 +128,7 @@ pub struct App { pub filters: DataFilters, pub config: Config, pub config_path: Option, + pub config_page_settings: Vec, } impl App { @@ -1236,7 +1239,7 @@ impl App { } } 'C' => { - // self.open_config(), + // self.open_config_screen(); } 'c' => { if let BottomWidgetType::Proc = self.current_widget.widget_type { @@ -2593,6 +2596,7 @@ impl App { } // Now handle click propagation down to widget. + // self.current_widget.on_click(MouseButton::Left, Coordinate {x, y}); if let Some((_tlc_x, tlc_y)) = &self.current_widget.top_left_corner { match &self.current_widget.widget_type { BottomWidgetType::Proc diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index e367d99c8..8a3646852 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -36,6 +36,7 @@ pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { if cfg!(target_family = "unix") { #[cfg(any(target_family = "unix"))] { + // FIXME: [KILL] Forbid negative PID killing in unix let output = unsafe { libc::kill(pid as i32, libc::SIGTERM) }; if output != 0 { // We had an error... diff --git a/src/app/states.rs b/src/app/states.rs index dea49b185..46de947e9 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -6,25 +6,12 @@ use tui::widgets::TableState; use crate::{ app::{layout_manager::BottomWidgetType, query::*}, + components::ScrollDirection, constants, data_harvester::processes::{self, ProcessSorting}, }; use ProcessSorting::*; -#[derive(Debug)] -pub enum ScrollDirection { - // UP means scrolling up --- this usually DECREMENTS - Up, - // DOWN means scrolling down --- this usually INCREMENTS - Down, -} - -impl Default for ScrollDirection { - fn default() -> Self { - ScrollDirection::Down - } -} - #[derive(Debug)] pub enum CursorDirection { Left, @@ -34,6 +21,8 @@ pub enum CursorDirection { /// AppScrollWidgetState deals with fields for a scrollable app's current state. #[derive(Default)] pub struct AppScrollWidgetState { + pub column_headers: Vec, + pub column_contents: Vec>, pub current_scroll_position: usize, pub previous_scroll_position: usize, pub scroll_direction: ScrollDirection, @@ -802,6 +791,7 @@ impl BatteryState { } } +// FIXME: [REFACTOR] Unify scroll state tracking implementations under one struct. #[derive(Default)] pub struct ParagraphScrollState { pub current_scroll_index: u16, @@ -809,17 +799,22 @@ pub struct ParagraphScrollState { } #[derive(Default)] -pub struct ConfigState { - pub current_category_index: usize, - pub category_list: Vec, -} - -#[derive(Default)] -pub struct ConfigCategory { +pub struct MainConfigState { pub category_name: &'static str, - pub options_list: Vec, -} - -pub struct ConfigOption { - pub set_function: Box anyhow::Result<()>>, + pub config_sub_options: Vec, +} + +pub struct ConfigSubOptions { + /// The option's name. + pub option_name: &'static str, + /// The option's description. + pub description: &'static str, + /// How it handles drawing in an additional Block. If this does is None, + /// then nothing additional will be drawn here. + pub draw_fn: Option>, + /// How it handles inputs. For example, scroll states, mouse inputs, keyboard inputs, etc. + /// must be handled by the option itself. + pub input_fn: Box, + /// How it updates the config. Must be handled by the option itself. + pub update_fn: Box, } diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs index 2876e24b0..8205ba77d 100644 --- a/src/canvas/canvas_colours.rs +++ b/src/canvas/canvas_colours.rs @@ -25,7 +25,7 @@ pub struct CanvasColours { pub text_style: Style, pub widget_title_style: Style, pub graph_style: Style, - // Full, Medium, Low + // Order is full, medium, and low pub battery_bar_styles: Vec