| 1 | use rustc_hash::FxHashMap as HashMap; |
| 2 | |
| 3 | use crate::{ |
| 4 | app::layout_manager::BottomWidgetType, |
| 5 | constants, |
| 6 | utils::input::InputFieldState, |
| 7 | widgets::{ |
| 8 | BatteryWidgetState, CpuWidgetState, DiskIoGraphWidgetState, DiskTableWidget, |
| 9 | MemWidgetState, NetWidgetState, ProcWidgetState, TempGraphWidgetState, TempWidgetState, |
| 10 | query::ProcessQuery, |
| 11 | }, |
| 12 | }; |
| 13 | |
| 14 | pub struct AppWidgetStates { |
| 15 | pub cpu_state: CpuState, |
| 16 | pub mem_state: MemState, |
| 17 | pub net_state: NetState, |
| 18 | pub proc_state: ProcState, |
| 19 | pub temp_state: TempState, |
| 20 | pub temp_graph_state: TempGraphStates, |
| 21 | pub disk_state: DiskState, |
| 22 | pub disk_io_graph_state: DiskIoGraphStates, |
| 23 | pub battery_state: AppBatteryState, |
| 24 | pub basic_table_widget_state: Option<BasicTableWidgetState>, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug)] |
| 28 | pub enum CursorDirection { |
| 29 | Left, |
| 30 | Right, |
| 31 | } |
| 32 | |
| 33 | pub struct AppHelpDialogState { |
| 34 | pub is_showing_help: bool, |
| 35 | pub height: u16, |
| 36 | pub scroll_state: ParagraphScrollState, |
| 37 | pub index_shortcuts: Vec<u16>, |
| 38 | is_searching: bool, |
| 39 | pub search_input_state: InputFieldState, |
| 40 | } |
| 41 | |
| 42 | impl Default for AppHelpDialogState { |
| 43 | fn default() -> Self { |
| 44 | AppHelpDialogState { |
| 45 | is_showing_help: false, |
| 46 | height: 0, |
| 47 | scroll_state: ParagraphScrollState::default(), |
| 48 | index_shortcuts: vec![0; constants::HELP_TEXT.len()], |
| 49 | is_searching: false, |
| 50 | search_input_state: InputFieldState::default(), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl AppHelpDialogState { |
| 56 | pub fn is_searching(&self) -> bool { |
| 57 | self.is_searching |
| 58 | } |
| 59 | |
| 60 | pub fn is_help_searching(&self) -> bool { |
| 61 | self.is_showing_help && self.is_searching |
| 62 | } |
| 63 | |
| 64 | pub fn open_search(&mut self) { |
| 65 | self.is_searching = true; |
| 66 | } |
| 67 | |
| 68 | pub fn close_search(&mut self) { |
| 69 | self.is_searching = false; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// AppSearchState deals with generic searching (I might do this in the future). |
| 74 | #[derive(Default)] |
| 75 | pub struct AppSearchState { |
| 76 | pub is_enabled: bool, |
| 77 | pub is_invalid_search: bool, |
| 78 | pub input_field_state: InputFieldState, |
| 79 | |
| 80 | /// The query. TODO: Merge this as one enum. |
| 81 | pub query: Option<ProcessQuery>, |
| 82 | pub error_message: Option<String>, |
| 83 | } |
| 84 | |
| 85 | impl AppSearchState { |
| 86 | /// Resets the [`AppSearchState`] to its default state, albeit still |
| 87 | /// enabled. |
| 88 | pub fn reset(&mut self) { |
| 89 | *self = AppSearchState { |
| 90 | is_enabled: self.is_enabled, |
| 91 | ..AppSearchState::default() |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Returns whether the [`AppSearchState`] has an invalid or blank search. |
| 96 | pub fn is_invalid_or_blank_search(&self) -> bool { |
| 97 | self.input_field_state.current_query().is_empty() || self.is_invalid_search |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | pub struct ProcState { |
| 102 | pub widget_states: HashMap<u64, ProcWidgetState>, |
| 103 | } |
| 104 | |
| 105 | impl ProcState { |
| 106 | pub fn init(widget_states: HashMap<u64, ProcWidgetState>) -> Self { |
| 107 | ProcState { widget_states } |
| 108 | } |
| 109 | |
| 110 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut ProcWidgetState> { |
| 111 | self.widget_states.get_mut(&widget_id) |
| 112 | } |
| 113 | |
| 114 | pub fn get_widget_state(&self, widget_id: u64) -> Option<&ProcWidgetState> { |
| 115 | self.widget_states.get(&widget_id) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub struct NetState { |
| 120 | pub widget_states: HashMap<u64, NetWidgetState>, |
| 121 | } |
| 122 | |
| 123 | impl NetState { |
| 124 | pub fn init(widget_states: HashMap<u64, NetWidgetState>) -> Self { |
| 125 | NetState { widget_states } |
| 126 | } |
| 127 | |
| 128 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut NetWidgetState> { |
| 129 | self.widget_states.get_mut(&widget_id) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub struct CpuState { |
| 134 | pub widget_states: HashMap<u64, CpuWidgetState>, |
| 135 | } |
| 136 | |
| 137 | impl CpuState { |
| 138 | pub fn init(widget_states: HashMap<u64, CpuWidgetState>) -> Self { |
| 139 | CpuState { widget_states } |
| 140 | } |
| 141 | |
| 142 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut CpuWidgetState> { |
| 143 | self.widget_states.get_mut(&widget_id) |
| 144 | } |
| 145 | |
| 146 | pub fn get_widget_state(&self, widget_id: u64) -> Option<&CpuWidgetState> { |
| 147 | self.widget_states.get(&widget_id) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | pub struct MemState { |
| 152 | pub widget_states: HashMap<u64, MemWidgetState>, |
| 153 | } |
| 154 | |
| 155 | impl MemState { |
| 156 | pub fn init(widget_states: HashMap<u64, MemWidgetState>) -> Self { |
| 157 | MemState { widget_states } |
| 158 | } |
| 159 | |
| 160 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut MemWidgetState> { |
| 161 | self.widget_states.get_mut(&widget_id) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub struct TempState { |
| 166 | pub widget_states: HashMap<u64, TempWidgetState>, |
| 167 | } |
| 168 | |
| 169 | impl TempState { |
| 170 | pub fn init(widget_states: HashMap<u64, TempWidgetState>) -> Self { |
| 171 | TempState { widget_states } |
| 172 | } |
| 173 | |
| 174 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut TempWidgetState> { |
| 175 | self.widget_states.get_mut(&widget_id) |
| 176 | } |
| 177 | |
| 178 | pub fn get_widget_state(&self, widget_id: u64) -> Option<&TempWidgetState> { |
| 179 | self.widget_states.get(&widget_id) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | pub struct TempGraphStates { |
| 184 | pub widget_states: HashMap<u64, TempGraphWidgetState>, |
| 185 | } |
| 186 | |
| 187 | impl TempGraphStates { |
| 188 | pub fn init(widget_states: HashMap<u64, TempGraphWidgetState>) -> Self { |
| 189 | TempGraphStates { widget_states } |
| 190 | } |
| 191 | |
| 192 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut TempGraphWidgetState> { |
| 193 | self.widget_states.get_mut(&widget_id) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// Holds per-widget state for all disk I/O graph instances in the layout. |
| 198 | pub struct DiskIoGraphStates { |
| 199 | pub widget_states: HashMap<u64, DiskIoGraphWidgetState>, |
| 200 | } |
| 201 | |
| 202 | impl DiskIoGraphStates { |
| 203 | pub fn init(widget_states: HashMap<u64, DiskIoGraphWidgetState>) -> Self { |
| 204 | DiskIoGraphStates { widget_states } |
| 205 | } |
| 206 | |
| 207 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut DiskIoGraphWidgetState> { |
| 208 | self.widget_states.get_mut(&widget_id) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | pub struct DiskState { |
| 213 | pub widget_states: HashMap<u64, DiskTableWidget>, |
| 214 | } |
| 215 | |
| 216 | impl DiskState { |
| 217 | pub fn init(widget_states: HashMap<u64, DiskTableWidget>) -> Self { |
| 218 | DiskState { widget_states } |
| 219 | } |
| 220 | |
| 221 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut DiskTableWidget> { |
| 222 | self.widget_states.get_mut(&widget_id) |
| 223 | } |
| 224 | |
| 225 | pub fn get_widget_state(&self, widget_id: u64) -> Option<&DiskTableWidget> { |
| 226 | self.widget_states.get(&widget_id) |
| 227 | } |
| 228 | } |
| 229 | pub struct BasicTableWidgetState { |
| 230 | // Since this is intended (currently) to only be used for ONE widget, that's |
| 231 | // how it's going to be written. If we want to allow for multiple of these, |
| 232 | // then we can expand outwards with a normal BasicTableState and a hashmap |
| 233 | pub currently_displayed_widget_type: BottomWidgetType, |
| 234 | pub currently_displayed_widget_id: u64, |
| 235 | pub left_tlc: Option<(u16, u16)>, |
| 236 | pub left_brc: Option<(u16, u16)>, |
| 237 | pub right_tlc: Option<(u16, u16)>, |
| 238 | pub right_brc: Option<(u16, u16)>, |
| 239 | } |
| 240 | |
| 241 | pub struct AppBatteryState { |
| 242 | pub widget_states: HashMap<u64, BatteryWidgetState>, |
| 243 | } |
| 244 | |
| 245 | impl AppBatteryState { |
| 246 | pub fn init(widget_states: HashMap<u64, BatteryWidgetState>) -> Self { |
| 247 | AppBatteryState { widget_states } |
| 248 | } |
| 249 | |
| 250 | pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut BatteryWidgetState> { |
| 251 | self.widget_states.get_mut(&widget_id) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | #[derive(Default)] |
| 256 | pub struct ParagraphScrollState { |
| 257 | pub current_scroll_index: u16, |
| 258 | pub max_scroll_index: u16, |
| 259 | } |