From b879c36588cd45ae382e152804e9421a5e8b9dd6 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Sun, 16 Oct 2022 03:10:30 -0400 Subject: [PATCH] refactor: move some state code around (#839) --- src/app/layout_manager.rs | 4 ++-- src/app/states.rs | 38 ++++--------------------------- src/app/widgets.rs | 9 ++++++++ src/app/widgets/battery_widget.rs | 5 ++++ src/app/widgets/mem_graph.rs | 15 ++++++++++++ src/app/widgets/net_graph.rs | 15 ++++++++++++ src/options.rs | 5 +++- 7 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 src/app/widgets/battery_widget.rs create mode 100644 src/app/widgets/mem_graph.rs create mode 100644 src/app/widgets/net_graph.rs diff --git a/src/app/layout_manager.rs b/src/app/layout_manager.rs index 9148ab5b..916a3d5b 100644 --- a/src/app/layout_manager.rs +++ b/src/app/layout_manager.rs @@ -872,11 +872,11 @@ pub struct BottomWidget { #[builder(default = None)] pub parent_reflector: Option<(WidgetDirection, u64)>, - /// Top left corner when drawn, for mouse click detection. (x, y) + /// Top left corner when drawn, for mouse click detection. (x, y) #[builder(default = None)] pub top_left_corner: Option<(u16, u16)>, - /// Bottom right corner when drawn, for mouse click detection. (x, y) + /// Bottom right corner when drawn, for mouse click detection. (x, y) #[builder(default = None)] pub bottom_right_corner: Option<(u16, u16)>, } diff --git a/src/app/states.rs b/src/app/states.rs index a1465640..dcecc15a 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -7,7 +7,10 @@ use crate::{ constants, }; -use super::widgets::{CpuWidgetState, DiskTableWidget, ProcWidget, TempWidgetState}; +use super::widgets::{ + BatteryWidgetState, CpuWidgetState, DiskTableWidget, MemWidgetState, NetWidgetState, + ProcWidget, TempWidgetState, +}; #[derive(Debug)] pub enum ScrollDirection { @@ -140,20 +143,6 @@ impl ProcState { } } -pub struct NetWidgetState { - pub current_display_time: u64, - pub autohide_timer: Option, -} - -impl NetWidgetState { - pub fn init(current_display_time: u64, autohide_timer: Option) -> Self { - NetWidgetState { - current_display_time, - autohide_timer, - } - } -} - pub struct NetState { pub force_update: Option, pub widget_states: HashMap, @@ -198,19 +187,6 @@ impl CpuState { } } -pub struct MemWidgetState { - pub current_display_time: u64, - pub autohide_timer: Option, -} - -impl MemWidgetState { - pub fn init(current_display_time: u64, autohide_timer: Option) -> Self { - MemWidgetState { - current_display_time, - autohide_timer, - } - } -} pub struct MemState { pub force_update: Option, pub widget_states: HashMap, @@ -281,12 +257,6 @@ pub struct BasicTableWidgetState { pub right_brc: Option<(u16, u16)>, } -#[derive(Default)] -pub struct BatteryWidgetState { - pub currently_selected_battery_index: usize, - pub tab_click_locs: Option>, -} - pub struct BatteryState { pub widget_states: HashMap, } diff --git a/src/app/widgets.rs b/src/app/widgets.rs index 6bdb0e55..2a00fa38 100644 --- a/src/app/widgets.rs +++ b/src/app/widgets.rs @@ -11,3 +11,12 @@ pub use disk_table::*; pub mod cpu_graph; pub use cpu_graph::*; + +pub mod net_graph; +pub use net_graph::*; + +pub mod mem_graph; +pub use mem_graph::*; + +pub mod battery_widget; +pub use battery_widget::*; diff --git a/src/app/widgets/battery_widget.rs b/src/app/widgets/battery_widget.rs new file mode 100644 index 00000000..68bbca79 --- /dev/null +++ b/src/app/widgets/battery_widget.rs @@ -0,0 +1,5 @@ +#[derive(Default)] +pub struct BatteryWidgetState { + pub currently_selected_battery_index: usize, + pub tab_click_locs: Option>, +} diff --git a/src/app/widgets/mem_graph.rs b/src/app/widgets/mem_graph.rs new file mode 100644 index 00000000..a86bd02a --- /dev/null +++ b/src/app/widgets/mem_graph.rs @@ -0,0 +1,15 @@ +use std::time::Instant; + +pub struct MemWidgetState { + pub current_display_time: u64, + pub autohide_timer: Option, +} + +impl MemWidgetState { + pub fn init(current_display_time: u64, autohide_timer: Option) -> Self { + MemWidgetState { + current_display_time, + autohide_timer, + } + } +} diff --git a/src/app/widgets/net_graph.rs b/src/app/widgets/net_graph.rs new file mode 100644 index 00000000..e3526210 --- /dev/null +++ b/src/app/widgets/net_graph.rs @@ -0,0 +1,15 @@ +use std::time::Instant; + +pub struct NetWidgetState { + pub current_display_time: u64, + pub autohide_timer: Option, +} + +impl NetWidgetState { + pub fn init(current_display_time: u64, autohide_timer: Option) -> Self { + NetWidgetState { + current_display_time, + autohide_timer, + } + } +} diff --git a/src/options.rs b/src/options.rs index 5213dcf0..ea94cc51 100644 --- a/src/options.rs +++ b/src/options.rs @@ -11,7 +11,10 @@ use std::{ use crate::{ app::{ layout_manager::*, - widgets::{CpuWidgetState, DiskTableWidget, ProcWidget, ProcWidgetMode, TempWidgetState}, + widgets::{ + BatteryWidgetState, CpuWidgetState, DiskTableWidget, MemWidgetState, NetWidgetState, + ProcWidget, ProcWidgetMode, TempWidgetState, + }, *, }, canvas::{canvas_colours::CanvasColours, ColourScheme},