Debian Codegraph @

src/app.rs

raw symbols SCIP
1 pub mod data;
2 pub mod filter;
3 pub mod layout_manager;
4 pub mod states;
5
6 use std::time::Instant;
7
8 use data::*;
9 use filter::*;
10 use layout_manager::*;
11 use rustc_hash::FxHashMap as HashMap;
12 pub use states::*;
13
14 use crate::{
15 canvas::{
16 components::{data_table::SortOrder, time_series::LegendPosition},
17 dialogs::process_kill_dialog::ProcessKillDialog,
18 },
19 components::time_series::TimeseriesState,
20 constants,
21 options::config::flags::TableGap,
22 utils::data_units::DataUnit,
23 widgets::{
24 DiskWidgetColumn, ProcWidgetColumn, ProcWidgetMode, TempWidgetColumn, TreeCollapsed,
25 },
26 };
27
28 #[derive(Debug, Clone, Eq, PartialEq, Default, Copy)]
29 pub enum AxisScaling {
30 #[default]
31 Log,
32 Linear,
33 }
34
35 /// AppConfigFields is meant to cover basic fields that would normally be set
36 /// by config files or launch options.
37 ///
38 /// TODO: Clean this up, we probably don't need to have this duplicated.
39 #[derive(Debug, Default)]
40 #[cfg_attr(test, derive(PartialEq, Eq))]
41 pub struct AppConfigFields {
42 pub update_rate: u64,
43 pub temperature_type: TemperatureType,
44 pub use_dot: bool,
45 pub cpu_left_legend: bool,
46 pub show_average_cpu: bool, // TODO: Unify this in CPU options
47 pub show_cpu_decimal: bool,
48 pub use_current_cpu_total: bool,
49 pub unnormalized_cpu: bool,
50 pub get_process_threads: bool,
51 pub use_basic_mode: bool,
52 pub default_time_value: u64,
53 pub time_interval: u64,
54 pub hide_time: bool,
55 pub autohide_time: bool,
56 pub use_old_network_legend: bool,
57 pub table_gap: TableGap,
58 pub disable_click: bool,
59 pub disable_keys: bool,
60 pub enable_gpu: bool,
61 pub short_gpu_names: bool,
62 pub enable_cache_memory: bool,
63 pub show_table_scroll_position: bool,
64 pub show_table_scroll_bar: bool,
65 #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
66 pub is_advanced_kill: bool,
67 pub is_read_only: bool,
68 #[cfg(target_os = "linux")]
69 pub hide_k_threads: bool,
70 #[cfg(feature = "zfs")]
71 pub free_arc: bool,
72 pub memory_legend_position: Option<LegendPosition>,
73 // TODO: Remove these, move network details state-side.
74 pub network_unit_type: DataUnit,
75 pub network_legend_position: Option<LegendPosition>,
76 pub network_scale_type: AxisScaling,
77 pub network_use_binary_prefix: bool,
78 pub network_show_packets: bool,
79 pub network_start_zeroed: bool,
80 pub retention_ms: u64,
81 pub dedicated_average_row: bool,
82 pub default_tree_collapse: bool,
83 pub default_temp_sort_column: Option<TempWidgetColumn>,
84 pub default_temp_sort_order: SortOrder,
85 pub default_disk_sort_column: Option<DiskWidgetColumn>,
86 pub default_disk_sort_order: SortOrder,
87 pub temperature_legend_position: Option<LegendPosition>,
88 pub disk_io_legend_position: Option<LegendPosition>,
89 pub disk_show_unmounted: bool,
90 pub disk_io_graph_show_unmounted: bool,
91 }
92
93 /// For filtering out information
94 #[derive(Debug, Clone, Default)]
95 pub struct DataFilters {
96 pub disk_filter: Option<Filter>,
97 pub mount_filter: Option<Filter>,
98 pub temp_filter: Option<Filter>,
99 pub temp_graph_filter: Option<Filter>,
100 pub disk_io_graph_filter: Option<Filter>,
101 pub net_filter: Option<Filter>,
102 }
103
104 pub struct App {
105 awaiting_second_char: bool,
106 second_char: Option<char>,
107 pub data_store: DataStore,
108 last_key_press: Instant,
109 pub(crate) process_kill_dialog: ProcessKillDialog,
110 pub help_dialog_state: AppHelpDialogState,
111 pub is_expanded: bool,
112 pub is_force_redraw: bool,
113 pub is_determining_widget_boundary: bool,
114 pub basic_mode_use_percent: bool,
115 pub states: AppWidgetStates,
116 pub app_config_fields: AppConfigFields,
117 pub widget_map: HashMap<u64, BottomWidget>,
118 pub current_widget: BottomWidget,
119 pub used_widgets: UsedWidgets,
120 pub filters: DataFilters,
121 }
122
123 impl App {
124 /// Create a new [`App`].
125 pub fn new(
126 app_config_fields: AppConfigFields, states: AppWidgetStates,
127 widget_map: HashMap<u64, BottomWidget>, current_widget: BottomWidget,
128 used_widgets: UsedWidgets, filters: DataFilters, is_expanded: bool,
129 ) -> Self {
130 let mut data_store = DataStore::new(used_widgets);
131 data_store.set_filters(filters.clone());
132
133 Self {
134 awaiting_second_char: false,
135 second_char: None,
136 data_store,
137 last_key_press: Instant::now(),
138 process_kill_dialog: ProcessKillDialog::default(),
139 help_dialog_state: AppHelpDialogState::default(),
140 is_expanded,
141 is_force_redraw: false,
142 is_determining_widget_boundary: false,
143 basic_mode_use_percent: false,
144 states,
145 app_config_fields,
146 widget_map,
147 current_widget,
148 used_widgets,
149 filters,
150 }
151 }
152
153 /// Update the data in the [`App`].
154 pub fn update_data(&mut self) {
155 let data_source = self.data_store.get_data();
156
157 // FIXME: (points_rework_v1) maybe separate PR but would it make more sense to
158 // store references of data? Would it also make more sense to move the
159 // "data set" step to the draw step, and make it only set if force
160 // update is set here?
161 for proc in self.states.proc_state.widget_states.values_mut() {
162 if proc.force_update_data {
163 proc.set_table_data(data_source);
164 }
165 }
166
167 for temp in self.states.temp_state.widget_states.values_mut() {
168 if temp.force_update_data {
169 temp.set_table_data(&data_source.temp_data);
170 }
171 }
172
173 for cpu in self.states.cpu_state.widget_states.values_mut() {
174 if cpu.force_update_data {
175 cpu.set_legend_data(&data_source.cpu_harvest);
176 }
177 }
178
179 for disk in self.states.disk_state.widget_states.values_mut() {
180 if disk.force_update_data {
181 disk.set_table_data(data_source);
182 }
183 }
184 }
185
186 pub fn reset(&mut self) {
187 // Reset multi
188 self.reset_multi_tap_keys();
189
190 // Reset dialog state
191 self.help_dialog_state.is_showing_help = false;
192 self.process_kill_dialog.reset();
193
194 // Close all searches and reset it
195 self.states
196 .proc_state
197 .widget_states
198 .values_mut()
199 .for_each(|state| {
200 state.proc_search.search_state.reset();
201 });
202
203 self.data_store.reset();
204
205 // Reset zoom.
206 // TODO: Make this suck less... should just make it so that calling reset fixes this all (including above too).
207 for widget_state in self.states.cpu_state.widget_states.values_mut() {
208 widget_state.graph.state_mut().reset_zoom();
209 }
210
211 for widget_state in self.states.mem_state.widget_states.values_mut() {
212 widget_state.graph.state_mut().reset_zoom();
213 }
214
215 for widget_state in self.states.net_state.widget_states.values_mut() {
216 widget_state.graph.state_mut().reset_zoom();
217 }
218
219 for widget_state in self.states.temp_graph_state.widget_states.values_mut() {
220 widget_state.graph.state_mut().reset_zoom();
221 }
222 }
223
224 pub fn should_get_widget_bounds(&self) -> bool {
225 self.is_force_redraw || self.is_determining_widget_boundary
226 }
227
228 pub fn on_esc(&mut self) {
229 self.reset_multi_tap_keys();
230
231 if self.process_kill_dialog.is_open() {
232 self.process_kill_dialog.on_esc();
233 self.is_force_redraw = true;
234 } else if self.help_dialog_state.is_showing_help {
235 if self.help_dialog_state.is_searching() {
236 self.help_dialog_state.close_search();
237 self.is_force_redraw = true;
238 } else {
239 self.help_dialog_state.is_showing_help = false;
240 self.help_dialog_state.scroll_state.current_scroll_index = 0;
241 self.is_force_redraw = true;
242 }
243 } else {
244 match self.current_widget.widget_type {
245 BottomWidgetType::Proc => {
246 if let Some(pws) = self
247 .states
248 .proc_state
249 .get_mut_widget_state(self.current_widget.widget_id)
250 && (pws.is_search_enabled() || pws.is_sort_open)
251 {
252 pws.proc_search.search_state.is_enabled = false;
253 pws.is_sort_open = false;
254 self.is_force_redraw = true;
255 return;
256 }
257 }
258 BottomWidgetType::ProcSearch => {
259 if let Some(pws) = self
260 .states
261 .proc_state
262 .get_mut_widget_state(self.current_widget.widget_id - 1)
263 && pws.is_search_enabled()
264 {
265 pws.proc_search.search_state.is_enabled = false;
266 self.move_widget_selection(&WidgetDirection::Up);
267 self.is_force_redraw = true;
268 return;
269 }
270 }
271 BottomWidgetType::ProcSort => {
272 if let Some(pws) = self
273 .states
274 .proc_state
275 .get_mut_widget_state(self.current_widget.widget_id - 2)
276 && pws.is_sort_open
277 {
278 pws.is_sort_open = false;
279 self.move_widget_selection(&WidgetDirection::Right);
280 self.is_force_redraw = true;
281 return;
282 }
283 }
284 _ => {}
285 }
286
287 if self.is_expanded {
288 self.is_expanded = false;
289 self.is_force_redraw = true;
290 }
291 }
292 }
293
294 pub fn is_in_search_widget(&self) -> bool {
295 matches!(
296 self.current_widget.widget_type,
297 BottomWidgetType::ProcSearch
298 )
299 }
300
301 pub fn is_in_any_search(&self) -> bool {
302 // TODO: This is really hacky, but is fine until we do some smarter things like putting event catching at a per-widget/dialog state.
303 self.is_in_search_widget() || (self.help_dialog_state.is_help_searching())
304 }
305
306 fn reset_multi_tap_keys(&mut self) {
307 self.awaiting_second_char = false;
308 self.second_char = None;
309 }
310
311 fn is_in_dialog(&self) -> bool {
312 self.help_dialog_state.is_showing_help || self.process_kill_dialog.is_open()
313 }
314
315 fn ignore_normal_keybinds(&self) -> bool {
316 self.is_in_dialog()
317 }
318
319 pub fn on_tab(&mut self) {
320 // Allow usage whilst only in processes
321
322 if !self.ignore_normal_keybinds()
323 && let BottomWidgetType::Proc = self.current_widget.widget_type
324 && let Some(proc_widget_state) = self
325 .states
326 .proc_state
327 .get_mut_widget_state(self.current_widget.widget_id)
328 {
329 proc_widget_state.toggle_tab();
330 }
331 }
332
333 pub fn on_slash(&mut self) {
334 if !self.ignore_normal_keybinds() {
335 match &self.current_widget.widget_type {
336 BottomWidgetType::Proc | BottomWidgetType::ProcSort => {
337 // Toggle on
338 if let Some(proc_widget_state) = self.states.proc_state.get_mut_widget_state(
339 self.current_widget.widget_id
340 - match &self.current_widget.widget_type {
341 BottomWidgetType::ProcSort => 2,
342 _ => 0,
343 },
344 ) {
345 proc_widget_state.proc_search.search_state.is_enabled = true;
346 self.move_widget_selection(&WidgetDirection::Down);
347 self.is_force_redraw = true;
348 }
349 }
350 _ => {}
351 }
352 } else if self.help_dialog_state.is_showing_help {
353 self.help_dialog_state.open_search();
354 self.is_force_redraw = true;
355 }
356 }
357
358 pub fn toggle_sort_menu(&mut self) {
359 let widget_id = self.current_widget.widget_id
360 - match &self.current_widget.widget_type {
361 BottomWidgetType::Proc => 0,
362 BottomWidgetType::ProcSort => 2,
363 _ => 0,
364 };
365
366 if let Some(pws) = self.states.proc_state.get_mut_widget_state(widget_id) {
367 pws.is_sort_open = !pws.is_sort_open;
368 pws.force_rerender = true;
369
370 // If the sort is now open, move left. Otherwise, if the proc sort was selected,
371 // force move right.
372 if pws.is_sort_open {
373 pws.sort_table.set_position(pws.table.sort_index());
374 self.move_widget_selection(&WidgetDirection::Left);
375 } else if let BottomWidgetType::ProcSort = self.current_widget.widget_type {
376 self.move_widget_selection(&WidgetDirection::Right);
377 }
378 self.is_force_redraw = true;
379 }
380 }
381
382 pub fn invert_sort(&mut self) {
383 match &self.current_widget.widget_type {
384 BottomWidgetType::Proc | BottomWidgetType::ProcSort => {
385 let widget_id = self.current_widget.widget_id
386 - match &self.current_widget.widget_type {
387 BottomWidgetType::Proc => 0,
388 BottomWidgetType::ProcSort => 2,
389 _ => 0,
390 };
391
392 if let Some(pws) = self.states.proc_state.get_mut_widget_state(widget_id) {
393 pws.table.toggle_order();
394 pws.force_data_update();
395 }
396 }
397 _ => {}
398 }
399 }
400
401 pub fn toggle_percentages(&mut self) {
402 match &self.current_widget.widget_type {
403 BottomWidgetType::BasicMem => {
404 self.basic_mode_use_percent = !self.basic_mode_use_percent; // Oh god this is so lazy.
405 }
406 BottomWidgetType::Proc => {
407 if let Some(proc_widget_state) = self
408 .states
409 .proc_state
410 .widget_states
411 .get_mut(&self.current_widget.widget_id)
412 {
413 proc_widget_state.toggle_mem_percentage();
414 }
415 }
416
417 _ => {}
418 }
419 }
420
421 pub fn toggle_ignore_case(&mut self) {
422 let is_in_search_widget = self.is_in_search_widget();
423 if let Some(proc_widget_state) = self
424 .states
425 .proc_state
426 .widget_states
427 .get_mut(&(self.current_widget.widget_id - 1))
428 && is_in_search_widget
429 && proc_widget_state.is_search_enabled()
430 {
431 proc_widget_state.proc_search.search_toggle_ignore_case();
432 proc_widget_state.update_query();
433 }
434 }
435
436 pub fn toggle_search_whole_word(&mut self) {
437 let is_in_search_widget = self.is_in_search_widget();
438 if let Some(proc_widget_state) = self
439 .states
440 .proc_state
441 .widget_states
442 .get_mut(&(self.current_widget.widget_id - 1))
443 && is_in_search_widget
444 && proc_widget_state.is_search_enabled()
445 {
446 proc_widget_state.proc_search.search_toggle_whole_word();
447 proc_widget_state.update_query();
448 }
449 }
450
451 pub fn toggle_search_regex(&mut self) {
452 let is_in_search_widget = self.is_in_search_widget();
453 if let Some(proc_widget_state) = self
454 .states
455 .proc_state
456 .widget_states
457 .get_mut(&(self.current_widget.widget_id - 1))
458 && is_in_search_widget
459 && proc_widget_state.is_search_enabled()
460 {
461 proc_widget_state.proc_search.search_toggle_regex();
462 proc_widget_state.update_query();
463 }
464 }
465
466 pub fn toggle_tree_mode(&mut self) {
467 if let Some(proc_widget_state) = self
468 .states
469 .proc_state
470 .widget_states
471 .get_mut(&(self.current_widget.widget_id))
472 {
473 match proc_widget_state.mode {
474 ProcWidgetMode::Tree { .. } => {
475 proc_widget_state.mode = ProcWidgetMode::Normal;
476 proc_widget_state.force_rerender_and_update();
477 }
478 ProcWidgetMode::Normal => {
479 proc_widget_state.mode = ProcWidgetMode::Tree(TreeCollapsed::new(
480 self.app_config_fields.default_tree_collapse,
481 ));
482 proc_widget_state.force_rerender_and_update();
483 }
484 ProcWidgetMode::Grouped => {}
485 }
486 }
487 }
488
489 /// One of two functions allowed to run while in a dialog...
490 pub fn on_enter(&mut self) {
491 if self.process_kill_dialog.is_open() {
492 // Not the best way of doing things for now but works as glue.
493 self.process_kill_dialog.on_enter();
494 } else if self.help_dialog_state.is_help_searching() {
495 self.help_dialog_state.close_search();
496 self.is_force_redraw = true;
497 } else if !self.is_in_dialog() {
498 match self.current_widget.widget_type {
499 BottomWidgetType::ProcSearch => {
500 if let Some(proc_widget_state) = self
501 .states
502 .proc_state
503 .get_mut_widget_state(self.current_widget.widget_id - 1)
504 && proc_widget_state.is_search_enabled()
505 {
506 proc_widget_state.proc_search.search_state.is_enabled = false;
507 self.move_widget_selection(&WidgetDirection::Up);
508 self.is_force_redraw = true;
509 }
510 }
511 BottomWidgetType::ProcSort => {
512 if let Some(proc_widget_state) = self
513 .states
514 .proc_state
515 .get_mut_widget_state(self.current_widget.widget_id - 2)
516 && proc_widget_state.is_sort_open
517 {
518 proc_widget_state.use_sort_table_value();
519 self.move_widget_selection(&WidgetDirection::Right);
520 self.is_force_redraw = true;
521 }
522 }
523 _ => {}
524 }
525 }
526 }
527
528 pub fn on_delete(&mut self) {
529 if self.help_dialog_state.is_help_searching() {
530 self.help_dialog_state.search_input_state.delete_at_cursor();
531 self.is_force_redraw = true;
532 return;
533 }
534 match self.current_widget.widget_type {
535 BottomWidgetType::ProcSearch => {
536 let is_in_search_widget = self.is_in_search_widget();
537 if let Some(proc_widget_state) = self
538 .states
539 .proc_state
540 .widget_states
541 .get_mut(&(self.current_widget.widget_id - 1))
542 && is_in_search_widget
543 && proc_widget_state.proc_search.search_state.is_enabled
544 {
545 proc_widget_state
546 .proc_search
547 .search_state
548 .input_field_state
549 .delete_at_cursor();
550
551 proc_widget_state.update_query();
552 }
553 }
554 BottomWidgetType::Proc => {
555 self.kill_current_process();
556 }
557 _ => {}
558 }
559 }
560
561 pub fn on_backspace(&mut self) {
562 if self.help_dialog_state.is_help_searching() {
563 self.help_dialog_state
564 .search_input_state
565 .delete_behind_cursor();
566 self.is_force_redraw = true;
567 } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type {
568 let is_in_search_widget = self.is_in_search_widget();
569 if let Some(proc_widget_state) = self
570 .states
571 .proc_state
572 .widget_states
573 .get_mut(&(self.current_widget.widget_id - 1))
574 && is_in_search_widget
575 && proc_widget_state.proc_search.search_state.is_enabled
576 {
577 proc_widget_state
578 .proc_search
579 .search_state
580 .input_field_state
581 .delete_behind_cursor();
582
583 proc_widget_state.update_query();
584 }
585 }
586 }
587
588 pub fn on_up_key(&mut self) {
589 if !self.is_in_dialog() {
590 self.decrement_position_count();
591 self.reset_multi_tap_keys();
592 } else if self.help_dialog_state.is_showing_help {
593 self.help_scroll_up();
594 self.reset_multi_tap_keys();
595 } else if self.process_kill_dialog.is_open() {
596 self.process_kill_dialog.on_up_key();
597 }
598 }
599
600 pub fn on_down_key(&mut self) {
601 if !self.is_in_dialog() {
602 self.increment_position_count();
603 self.reset_multi_tap_keys();
604 } else if self.help_dialog_state.is_showing_help {
605 self.help_scroll_down();
606 self.reset_multi_tap_keys();
607 } else if self.process_kill_dialog.is_open() {
608 self.process_kill_dialog.on_down_key();
609 }
610 }
611
612 pub fn on_left_key(&mut self) {
613 if self.help_dialog_state.is_help_searching() {
614 self.help_dialog_state.search_input_state.move_left();
615 return;
616 }
617
618 if !self.is_in_dialog() {
619 match self.current_widget.widget_type {
620 BottomWidgetType::Proc => {
621 if let Some(proc_widget_state) = self
622 .states
623 .proc_state
624 .get_mut_widget_state(self.current_widget.widget_id)
625 {
626 proc_widget_state.collapse_current_tree_branch_entry();
627 }
628 }
629 BottomWidgetType::ProcSearch => {
630 let is_in_search_widget = self.is_in_search_widget();
631 if let Some(proc_widget_state) = self
632 .states
633 .proc_state
634 .get_mut_widget_state(self.current_widget.widget_id - 1)
635 && is_in_search_widget
636 {
637 proc_widget_state
638 .proc_search
639 .search_state
640 .input_field_state
641 .move_left();
642 }
643 }
644 BottomWidgetType::Battery => {
645 #[cfg(feature = "battery")]
646 if self.data_store.get_data().battery_harvest.len() > 1
647 && let Some(battery_widget_state) = self
648 .states
649 .battery_state
650 .get_mut_widget_state(self.current_widget.widget_id)
651 && battery_widget_state.currently_selected_battery_index > 0
652 {
653 battery_widget_state.currently_selected_battery_index -= 1;
654 }
655 }
656 _ => {}
657 }
658 } else if self.process_kill_dialog.is_open() {
659 self.process_kill_dialog.on_left_key();
660 }
661 }
662
663 pub fn on_right_key(&mut self) {
664 if self.help_dialog_state.is_help_searching() {
665 self.help_dialog_state.search_input_state.move_right();
666 return;
667 }
668
669 if !self.is_in_dialog() {
670 match self.current_widget.widget_type {
671 BottomWidgetType::Proc => {
672 if let Some(proc_widget_state) = self
673 .states
674 .proc_state
675 .get_mut_widget_state(self.current_widget.widget_id)
676 {
677 proc_widget_state.expand_current_tree_branch_entry();
678 }
679 }
680 BottomWidgetType::ProcSearch => {
681 let is_in_search_widget = self.is_in_search_widget();
682 if let Some(proc_widget_state) = self
683 .states
684 .proc_state
685 .get_mut_widget_state(self.current_widget.widget_id - 1)
686 && is_in_search_widget
687 {
688 proc_widget_state
689 .proc_search
690 .search_state
691 .input_field_state
692 .move_right();
693 }
694 }
695 BottomWidgetType::Battery => {
696 #[cfg(feature = "battery")]
697 {
698 let battery_count = self.data_store.get_data().battery_harvest.len();
699 if battery_count > 1
700 && let Some(battery_widget_state) = self
701 .states
702 .battery_state
703 .get_mut_widget_state(self.current_widget.widget_id)
704 && battery_widget_state.currently_selected_battery_index
705 < battery_count - 1
706 {
707 battery_widget_state.currently_selected_battery_index += 1;
708 }
709 }
710 }
711 _ => {}
712 }
713 } else if self.process_kill_dialog.is_open() {
714 self.process_kill_dialog.on_right_key();
715 }
716 }
717
718 pub fn on_space_key(&mut self) {
719 if !self.is_in_dialog() {
720 if self.current_widget.widget_type == BottomWidgetType::Proc
721 && let Some(proc_widget_state) = self
722 .states
723 .proc_state
724 .get_mut_widget_state(self.current_widget.widget_id)
725 {
726 proc_widget_state.toggle_current_tree_branch_entry();
727 }
728 } else if self.help_dialog_state.is_help_searching() {
729 self.on_char_key(' ');
730 }
731 }
732
733 pub fn on_page_up(&mut self) {
734 if self.process_kill_dialog.is_open() {
735 self.process_kill_dialog.on_page_up();
736 } else if self.help_dialog_state.is_showing_help {
737 let current = &mut self.help_dialog_state.scroll_state.current_scroll_index;
738 let amount = self.help_dialog_state.height;
739 *current = current.saturating_sub(amount);
740 } else if self.current_widget.widget_type.is_widget_table()
741 && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = (
742 &self.current_widget.top_left_corner,
743 &self.current_widget.bottom_right_corner,
744 )
745 {
746 let border_offset = u16::from(self.is_drawing_border());
747 let header_offset = self.header_offset(&self.current_widget);
748 let height = brc_y - tlc_y - 2 * border_offset - header_offset;
749 self.change_position_count(-(height as i64));
750 }
751 }
752
753 pub fn on_page_down(&mut self) {
754 if self.process_kill_dialog.is_open() {
755 self.process_kill_dialog.on_page_down();
756 } else if self.help_dialog_state.is_showing_help {
757 let current = self.help_dialog_state.scroll_state.current_scroll_index;
758 let amount = self.help_dialog_state.height;
759
760 self.help_scroll_to_or_max(current + amount);
761 } else if self.current_widget.widget_type.is_widget_table()
762 && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = (
763 &self.current_widget.top_left_corner,
764 &self.current_widget.bottom_right_corner,
765 )
766 {
767 let border_offset = u16::from(self.is_drawing_border());
768 let header_offset = self.header_offset(&self.current_widget);
769 let height = brc_y - tlc_y - 2 * border_offset - header_offset;
770 self.change_position_count(height as i64);
771 }
772 }
773
774 pub fn scroll_half_page_up(&mut self) {
775 if self.help_dialog_state.is_showing_help {
776 let current = &mut self.help_dialog_state.scroll_state.current_scroll_index;
777 let amount = self.help_dialog_state.height / 2;
778
779 *current = current.saturating_sub(amount);
780 } else if self.current_widget.widget_type.is_widget_table()
781 && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = (
782 &self.current_widget.top_left_corner,
783 &self.current_widget.bottom_right_corner,
784 )
785 {
786 let border_offset = u16::from(self.is_drawing_border());
787 let header_offset = self.header_offset(&self.current_widget);
788 let height = brc_y - tlc_y - 2 * border_offset - header_offset;
789 self.change_position_count(-(height as i64) / 2);
790 }
791 }
792
793 pub fn scroll_half_page_down(&mut self) {
794 if self.help_dialog_state.is_showing_help {
795 let current = self.help_dialog_state.scroll_state.current_scroll_index;
796 let amount = self.help_dialog_state.height / 2;
797
798 self.help_scroll_to_or_max(current + amount);
799 } else if self.current_widget.widget_type.is_widget_table()
800 && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = (
801 &self.current_widget.top_left_corner,
802 &self.current_widget.bottom_right_corner,
803 )
804 {
805 let border_offset = u16::from(self.is_drawing_border());
806 let header_offset = self.header_offset(&self.current_widget);
807 let height = brc_y - tlc_y - 2 * border_offset - header_offset;
808 self.change_position_count(height as i64 / 2);
809 }
810 }
811
812 pub fn skip_cursor_beginning(&mut self) {
813 if self.help_dialog_state.is_help_searching() {
814 self.help_dialog_state
815 .search_input_state
816 .skip_to_beginning();
817 self.is_force_redraw = true;
818 } else if !self.ignore_normal_keybinds()
819 && let BottomWidgetType::ProcSearch = self.current_widget.widget_type
820 {
821 let is_in_search_widget = self.is_in_search_widget();
822 if let Some(proc_widget_state) = self
823 .states
824 .proc_state
825 .widget_states
826 .get_mut(&(self.current_widget.widget_id - 1))
827 && is_in_search_widget
828 {
829 proc_widget_state
830 .proc_search
831 .search_state
832 .input_field_state
833 .skip_to_beginning();
834 }
835 }
836 }
837
838 pub fn skip_cursor_end(&mut self) {
839 if self.help_dialog_state.is_help_searching() {
840 self.help_dialog_state.search_input_state.skip_to_end();
841 self.is_force_redraw = true;
842 } else if !self.ignore_normal_keybinds()
843 && let BottomWidgetType::ProcSearch = self.current_widget.widget_type
844 {
845 let is_in_search_widget = self.is_in_search_widget();
846 if let Some(proc_widget_state) = self
847 .states
848 .proc_state
849 .widget_states
850 .get_mut(&(self.current_widget.widget_id - 1))
851 && is_in_search_widget
852 {
853 proc_widget_state
854 .proc_search
855 .search_state
856 .input_field_state
857 .skip_to_end();
858 }
859 }
860 }
861
862 pub fn clear_search(&mut self) {
863 if self.help_dialog_state.is_help_searching() {
864 self.help_dialog_state.search_input_state = Default::default();
865 self.is_force_redraw = true;
866 } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type
867 && let Some(proc_widget_state) = self
868 .states
869 .proc_state
870 .widget_states
871 .get_mut(&(self.current_widget.widget_id - 1))
872 {
873 proc_widget_state.clear_search();
874 }
875 }
876
877 pub fn clear_previous_word(&mut self) {
878 if self.help_dialog_state.is_help_searching() {
879 self.help_dialog_state
880 .search_input_state
881 .delete_previous_word();
882 self.is_force_redraw = true;
883 } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type
884 && let Some(proc_widget_state) = self
885 .states
886 .proc_state
887 .widget_states
888 .get_mut(&(self.current_widget.widget_id - 1))
889 {
890 proc_widget_state
891 .proc_search
892 .search_state
893 .input_field_state
894 .delete_previous_word();
895
896 proc_widget_state.update_query();
897 }
898 }
899
900 pub fn on_char_key(&mut self, caught_char: char) {
901 // Skip control code chars
902 if caught_char.is_control() {
903 return;
904 }
905
906 const MAX_KEY_TIMEOUT_IN_MILLISECONDS: u64 = 1000;
907
908 // Forbid any char key presses when showing a dialog box...
909 if !self.ignore_normal_keybinds() {
910 let current_key_press_inst = Instant::now();
911 if current_key_press_inst
912 .duration_since(self.last_key_press)
913 .as_millis()
914 > MAX_KEY_TIMEOUT_IN_MILLISECONDS.into()
915 {
916 self.reset_multi_tap_keys();
917 }
918 self.last_key_press = current_key_press_inst;
919
920 if let BottomWidgetType::ProcSearch = self.current_widget.widget_type {
921 let is_in_search_widget = self.is_in_search_widget();
922 if let Some(proc_widget_state) = self
923 .states
924 .proc_state
925 .widget_states
926 .get_mut(&(self.current_widget.widget_id - 1))
927 && is_in_search_widget
928 && proc_widget_state.is_search_enabled()
929 {
930 proc_widget_state
931 .proc_search
932 .search_state
933 .input_field_state
934 .insert_char(caught_char);
935
936 proc_widget_state.update_query();
937
938 return;
939 }
940 }
941 self.handle_char(caught_char);
942 } else if self.help_dialog_state.is_showing_help {
943 if self.help_dialog_state.is_searching() {
944 self.help_dialog_state
945 .search_input_state
946 .insert_char(caught_char);
947 self.is_force_redraw = true;
948 } else {
949 match caught_char {
950 '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
951 let potential_index = caught_char.to_digit(10);
952 if let Some(potential_index) = potential_index {
953 let potential_index = potential_index as usize;
954 if (potential_index) < self.help_dialog_state.index_shortcuts.len() {
955 self.help_scroll_to_or_max(
956 self.help_dialog_state.index_shortcuts[potential_index],
957 );
958 }
959 }
960 }
961 'j' | 'k' | 'g' | 'G' => self.handle_char(caught_char),
962 '/' => {
963 self.help_dialog_state.open_search();
964 self.is_force_redraw = true;
965 }
966 _ => {}
967 }
968 }
969 } else if self.process_kill_dialog.is_open() {
970 self.process_kill_dialog.on_char(caught_char);
971 }
972 }
973
974 /// Kill the currently selected process if we are in the process widget.
975 ///
976 /// TODO: This ideally gets abstracted out into a separate widget.
977 pub(crate) fn kill_current_process(&mut self) {
978 if self.app_config_fields.is_read_only {
979 return;
980 }
981
982 if let Some(pws) = self
983 .states
984 .proc_state
985 .widget_states
986 .get(&self.current_widget.widget_id)
987 && let Some(current) = pws.table.current_item()
988 {
989 let id = current.id.to_string();
990 if let Some(pids) = pws
991 .id_pid_map
992 .get(&id)
993 .cloned()
994 .or_else(|| Some(vec![current.pid]))
995 {
996 let current_process = (id, pids);
997
998 let use_simple_selection = {
999 cfg_select! {
1000 any(target_os = "linux", target_os = "macos", target_os = "freebsd") => {
1001 !self.app_config_fields.is_advanced_kill
1002 }
1003 _ => {
1004 true
1005 }
1006 }
1007 };
1008
1009 self.process_kill_dialog.start_process_kill(
1010 current_process.0,
1011 current_process.1,
1012 use_simple_selection,
1013 );
1014
1015 // TODO: I don't think most of this is needed.
1016 self.is_determining_widget_boundary = true;
1017 }
1018 }
1019 }
1020
1021 // FIXME: Refactor this system...
1022 fn handle_char(&mut self, caught_char: char) {
1023 match caught_char {
1024 '/' => {
1025 self.on_slash();
1026 }
1027 'd' => {
1028 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1029 let mut is_first_d = true;
1030 if let Some(second_char) = self.second_char
1031 && self.awaiting_second_char
1032 && second_char == 'd'
1033 {
1034 is_first_d = false;
1035 self.awaiting_second_char = false;
1036 self.second_char = None;
1037
1038 self.reset_multi_tap_keys();
1039
1040 self.kill_current_process();
1041 }
1042
1043 if is_first_d {
1044 self.awaiting_second_char = true;
1045 self.second_char = Some('d');
1046 }
1047 } else if let Some(disk) = self
1048 .states
1049 .disk_state
1050 .get_mut_widget_state(self.current_widget.widget_id)
1051 {
1052 disk.set_index(0);
1053 }
1054 }
1055 'g' => {
1056 let mut is_first_g = true;
1057 if let Some(second_char) = self.second_char
1058 && self.awaiting_second_char
1059 && second_char == 'g'
1060 {
1061 is_first_g = false;
1062 self.awaiting_second_char = false;
1063 self.second_char = None;
1064 self.skip_to_first();
1065 }
1066
1067 if is_first_g {
1068 self.awaiting_second_char = true;
1069 self.second_char = Some('g');
1070 }
1071 }
1072 'G' => self.skip_to_last(),
1073 'k' => self.on_up_key(),
1074 'j' => self.on_down_key(),
1075 'f' => self.data_store.toggle_frozen(),
1076 'c' => {
1077 if let BottomWidgetType::Proc = self.current_widget.widget_type
1078 && let Some(proc_widget_state) = self
1079 .states
1080 .proc_state
1081 .get_mut_widget_state(self.current_widget.widget_id)
1082 {
1083 proc_widget_state.select_column(ProcWidgetColumn::Cpu);
1084 }
1085 }
1086 'm' => {
1087 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1088 if let Some(proc_widget_state) = self
1089 .states
1090 .proc_state
1091 .get_mut_widget_state(self.current_widget.widget_id)
1092 {
1093 proc_widget_state.select_column(ProcWidgetColumn::Mem);
1094 }
1095 } else if let Some(disk) = self
1096 .states
1097 .disk_state
1098 .get_mut_widget_state(self.current_widget.widget_id)
1099 {
1100 disk.set_index(1);
1101 }
1102 }
1103 'p' => {
1104 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1105 if let Some(proc_widget_state) = self
1106 .states
1107 .proc_state
1108 .get_mut_widget_state(self.current_widget.widget_id)
1109 {
1110 proc_widget_state.select_column(ProcWidgetColumn::PidOrCount);
1111 }
1112 } else if let Some(disk) = self
1113 .states
1114 .disk_state
1115 .get_mut_widget_state(self.current_widget.widget_id)
1116 {
1117 disk.set_index(5);
1118 }
1119 }
1120 'P' => {
1121 if let BottomWidgetType::Proc = self.current_widget.widget_type
1122 && let Some(proc_widget_state) = self
1123 .states
1124 .proc_state
1125 .get_mut_widget_state(self.current_widget.widget_id)
1126 {
1127 proc_widget_state.toggle_command();
1128 }
1129 }
1130 'n' => {
1131 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1132 if let Some(proc_widget_state) = self
1133 .states
1134 .proc_state
1135 .get_mut_widget_state(self.current_widget.widget_id)
1136 {
1137 proc_widget_state.select_column(ProcWidgetColumn::ProcNameOrCommand);
1138 }
1139 } else if let Some(disk) = self
1140 .states
1141 .disk_state
1142 .get_mut_widget_state(self.current_widget.widget_id)
1143 {
1144 disk.set_index(3);
1145 }
1146 }
1147 #[cfg(feature = "gpu")]
1148 'M' => {
1149 if let BottomWidgetType::Proc = self.current_widget.widget_type
1150 && let Some(proc_widget_state) = self
1151 .states
1152 .proc_state
1153 .get_mut_widget_state(self.current_widget.widget_id)
1154 {
1155 proc_widget_state.select_column(ProcWidgetColumn::GpuMem);
1156 }
1157 }
1158 #[cfg(feature = "gpu")]
1159 'C' => {
1160 if let BottomWidgetType::Proc = self.current_widget.widget_type
1161 && let Some(proc_widget_state) = self
1162 .states
1163 .proc_state
1164 .get_mut_widget_state(self.current_widget.widget_id)
1165 {
1166 proc_widget_state.select_column(ProcWidgetColumn::GpuUtil);
1167 }
1168 }
1169 '?' => {
1170 self.help_dialog_state.is_showing_help = true;
1171 self.is_force_redraw = true;
1172 }
1173 'H' | 'A' => self.move_widget_selection(&WidgetDirection::Left),
1174 'L' | 'D' => self.move_widget_selection(&WidgetDirection::Right),
1175 'K' | 'W' => self.move_widget_selection(&WidgetDirection::Up),
1176 'J' | 'S' => self.move_widget_selection(&WidgetDirection::Down),
1177 't' => {
1178 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1179 self.toggle_tree_mode()
1180 } else if let Some(temp) = self
1181 .states
1182 .temp_state
1183 .get_mut_widget_state(self.current_widget.widget_id)
1184 {
1185 temp.table.set_sort_index(1);
1186 temp.force_data_update();
1187 } else if let Some(disk) = self
1188 .states
1189 .disk_state
1190 .get_mut_widget_state(self.current_widget.widget_id)
1191 {
1192 disk.set_index(4);
1193 }
1194 }
1195 '+' => self.on_plus(),
1196 '-' => self.on_minus(),
1197 '=' => self.reset_zoom(),
1198 'e' => self.toggle_expand_widget(),
1199 's' => {
1200 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1201 self.toggle_sort_menu()
1202 } else if let Some(temp) = self
1203 .states
1204 .temp_state
1205 .get_mut_widget_state(self.current_widget.widget_id)
1206 {
1207 temp.table.set_sort_index(0);
1208 temp.force_data_update();
1209 self.is_force_redraw = true;
1210 }
1211 }
1212 'u' => {
1213 if let Some(disk) = self
1214 .states
1215 .disk_state
1216 .get_mut_widget_state(self.current_widget.widget_id)
1217 {
1218 disk.set_index(2);
1219 }
1220 }
1221 'r' => {
1222 if let Some(disk) = self
1223 .states
1224 .disk_state
1225 .get_mut_widget_state(self.current_widget.widget_id)
1226 {
1227 disk.set_index(6);
1228 }
1229 }
1230 'w' => {
1231 if let Some(disk) = self
1232 .states
1233 .disk_state
1234 .get_mut_widget_state(self.current_widget.widget_id)
1235 {
1236 disk.set_index(7);
1237 }
1238 }
1239 'I' => self.invert_sort(),
1240 '%' => self.toggle_percentages(),
1241 #[cfg(target_os = "linux")]
1242 'z' => {
1243 if let BottomWidgetType::Proc = self.current_widget.widget_type
1244 && let Some(proc_widget_state) = self
1245 .states
1246 .proc_state
1247 .widget_states
1248 .get_mut(&self.current_widget.widget_id)
1249 {
1250 proc_widget_state.toggle_k_thread();
1251 }
1252 }
1253 _ => {}
1254 }
1255
1256 if let Some(second_char) = self.second_char
1257 && self.awaiting_second_char
1258 && caught_char != second_char
1259 {
1260 self.awaiting_second_char = false;
1261 }
1262 }
1263
1264 fn toggle_expand_widget(&mut self) {
1265 if self.is_expanded {
1266 self.is_expanded = false;
1267 self.is_force_redraw = true;
1268 } else {
1269 self.expand_widget();
1270 }
1271 }
1272
1273 fn expand_widget(&mut self) {
1274 // TODO: [BASIC] Expansion in basic mode.
1275 if !self.ignore_normal_keybinds() && !self.app_config_fields.use_basic_mode {
1276 // Pop-out mode. We ignore if in process search.
1277
1278 match self.current_widget.widget_type {
1279 BottomWidgetType::ProcSearch => {}
1280 _ => {
1281 self.is_expanded = true;
1282 self.is_force_redraw = true;
1283 }
1284 }
1285 }
1286 }
1287
1288 pub fn move_widget_selection(&mut self, direction: &WidgetDirection) {
1289 // Since we only want to call reset once, we do it like this to avoid
1290 // redundant calls on recursion.
1291 self.move_widget_selection_logic(direction);
1292 self.reset_multi_tap_keys();
1293 }
1294
1295 fn move_widget_selection_logic(&mut self, direction: &WidgetDirection) {
1296 // The actual logic for widget movement.
1297
1298 // We follow these following steps:
1299 // 1. Send a movement signal in `direction`.
1300 // 2. Check if this new widget we've landed on is hidden. If not, halt.
1301 // 3. If it hidden, loop and either send:
1302 // - A signal equal to the current direction, if it is opposite of the
1303 // reflection.
1304 // - Reflection direction.
1305
1306 if !self.ignore_normal_keybinds() && !self.is_expanded {
1307 if let Some(new_widget_id) = &(match direction {
1308 WidgetDirection::Left => self.current_widget.left_neighbour,
1309 WidgetDirection::Right => self.current_widget.right_neighbour,
1310 WidgetDirection::Up => self.current_widget.up_neighbour,
1311 WidgetDirection::Down => self.current_widget.down_neighbour,
1312 }) && let Some(new_widget) = self.widget_map.get(new_widget_id)
1313 {
1314 match &new_widget.widget_type {
1315 BottomWidgetType::Temp
1316 | BottomWidgetType::Proc
1317 | BottomWidgetType::ProcSort
1318 | BottomWidgetType::Disk
1319 | BottomWidgetType::Battery
1320 if self.states.basic_table_widget_state.is_some()
1321 && (*direction == WidgetDirection::Left
1322 || *direction == WidgetDirection::Right) =>
1323 {
1324 // Gotta do this for the sort widget
1325 if let BottomWidgetType::ProcSort = new_widget.widget_type {
1326 if let Some(proc_widget_state) = self
1327 .states
1328 .proc_state
1329 .widget_states
1330 .get(&(new_widget_id - 2))
1331 {
1332 if proc_widget_state.is_sort_open {
1333 self.current_widget = new_widget.clone();
1334 } else if let Some(next_new_widget_id) = match direction {
1335 WidgetDirection::Left => new_widget.left_neighbour,
1336 _ => new_widget.right_neighbour,
1337 } && let Some(next_new_widget) =
1338 self.widget_map.get(&next_new_widget_id)
1339 {
1340 self.current_widget = next_new_widget.clone();
1341 }
1342 }
1343 } else {
1344 self.current_widget = new_widget.clone();
1345 }
1346
1347 if let Some(basic_table_widget_state) =
1348 &mut self.states.basic_table_widget_state
1349 {
1350 basic_table_widget_state.currently_displayed_widget_id =
1351 self.current_widget.widget_id;
1352 basic_table_widget_state.currently_displayed_widget_type =
1353 self.current_widget.widget_type.clone();
1354 }
1355
1356 // And let's not forget:
1357 self.is_determining_widget_boundary = true;
1358 }
1359 BottomWidgetType::BasicTables => {
1360 match &direction {
1361 WidgetDirection::Up => {
1362 // Note this case would fail if it moved up into a hidden
1363 // widget, but it's for basic so whatever, it's all hard-coded
1364 // right now anyways...
1365 if let Some(next_new_widget_id) = new_widget.up_neighbour
1366 && let Some(next_new_widget) =
1367 self.widget_map.get(&next_new_widget_id)
1368 {
1369 self.current_widget = next_new_widget.clone();
1370 }
1371 }
1372 WidgetDirection::Down => {
1373 // Assuming we're in basic mode (BasicTables), then
1374 // we want to move DOWN to the currently shown widget.
1375 if let Some(basic_table_widget_state) =
1376 &mut self.states.basic_table_widget_state
1377 {
1378 // We also want to move towards Proc if we had set it to
1379 // ProcSort.
1380 if let BottomWidgetType::ProcSort =
1381 basic_table_widget_state.currently_displayed_widget_type
1382 {
1383 basic_table_widget_state.currently_displayed_widget_type =
1384 BottomWidgetType::Proc;
1385 basic_table_widget_state.currently_displayed_widget_id -= 2;
1386 }
1387
1388 if let Some(next_new_widget) = self.widget_map.get(
1389 &basic_table_widget_state.currently_displayed_widget_id,
1390 ) {
1391 self.current_widget = next_new_widget.clone();
1392 }
1393 }
1394 }
1395 _ => self.current_widget = new_widget.clone(),
1396 }
1397 }
1398 _ if new_widget.parent_reflector.is_some() => {
1399 // It may be hidden...
1400 if let Some((parent_direction, offset)) = &new_widget.parent_reflector {
1401 if direction.is_opposite(parent_direction) {
1402 // Keep going in the current direction if hidden...
1403 // unless we hit a wall of sorts.
1404 let option_next_neighbour_id = match &direction {
1405 WidgetDirection::Left => new_widget.left_neighbour,
1406 WidgetDirection::Right => new_widget.right_neighbour,
1407 WidgetDirection::Up => new_widget.up_neighbour,
1408 WidgetDirection::Down => new_widget.down_neighbour,
1409 };
1410 match &new_widget.widget_type {
1411 BottomWidgetType::CpuLegend => {
1412 if let Some(cpu_widget_state) = self
1413 .states
1414 .cpu_state
1415 .widget_states
1416 .get(&(new_widget_id - *offset))
1417 {
1418 if cpu_widget_state.is_legend_hidden {
1419 if let Some(next_neighbour_id) =
1420 option_next_neighbour_id
1421 && let Some(next_neighbour_widget) =
1422 self.widget_map.get(&next_neighbour_id)
1423 {
1424 self.current_widget =
1425 next_neighbour_widget.clone();
1426 }
1427 } else {
1428 self.current_widget = new_widget.clone();
1429 }
1430 }
1431 }
1432 BottomWidgetType::ProcSearch | BottomWidgetType::ProcSort => {
1433 if let Some(proc_widget_state) = self
1434 .states
1435 .proc_state
1436 .widget_states
1437 .get(&(new_widget_id - *offset))
1438 {
1439 match &new_widget.widget_type {
1440 BottomWidgetType::ProcSearch =>
1441 {
1442 #[allow(clippy::collapsible_match)]
1443 if !proc_widget_state.is_search_enabled() {
1444 if let Some(next_neighbour_id) =
1445 option_next_neighbour_id
1446 && let Some(next_neighbour_widget) =
1447 self.widget_map
1448 .get(&next_neighbour_id)
1449 {
1450 self.current_widget =
1451 next_neighbour_widget.clone();
1452 }
1453 } else {
1454 self.current_widget = new_widget.clone();
1455 }
1456 }
1457 BottomWidgetType::ProcSort => {
1458 #[allow(clippy::collapsible_match)]
1459 if !proc_widget_state.is_sort_open {
1460 if let Some(next_neighbour_id) =
1461 option_next_neighbour_id
1462 && let Some(next_neighbour_widget) =
1463 self.widget_map
1464 .get(&next_neighbour_id)
1465 {
1466 self.current_widget =
1467 next_neighbour_widget.clone();
1468 }
1469 } else {
1470 self.current_widget = new_widget.clone();
1471 }
1472 }
1473 _ => {
1474 self.current_widget = new_widget.clone();
1475 }
1476 }
1477 }
1478 }
1479 _ => {
1480 self.current_widget = new_widget.clone();
1481 }
1482 }
1483 } else {
1484 // Reflect
1485 match &new_widget.widget_type {
1486 BottomWidgetType::CpuLegend => {
1487 if let Some(cpu_widget_state) = self
1488 .states
1489 .cpu_state
1490 .widget_states
1491 .get(&(new_widget_id - *offset))
1492 {
1493 if cpu_widget_state.is_legend_hidden {
1494 if let Some(parent_cpu_widget) =
1495 self.widget_map.get(&(new_widget_id - *offset))
1496 {
1497 self.current_widget = parent_cpu_widget.clone();
1498 }
1499 } else {
1500 self.current_widget = new_widget.clone();
1501 }
1502 }
1503 }
1504 BottomWidgetType::ProcSearch | BottomWidgetType::ProcSort => {
1505 if let Some(proc_widget_state) = self
1506 .states
1507 .proc_state
1508 .widget_states
1509 .get(&(new_widget_id - *offset))
1510 {
1511 match &new_widget.widget_type {
1512 BottomWidgetType::ProcSearch =>
1513 {
1514 #[allow(clippy::collapsible_match)]
1515 if !proc_widget_state.is_search_enabled() {
1516 if let Some(parent_proc_widget) = self
1517 .widget_map
1518 .get(&(new_widget_id - *offset))
1519 {
1520 self.current_widget =
1521 parent_proc_widget.clone();
1522 }
1523 } else {
1524 self.current_widget = new_widget.clone();
1525 }
1526 }
1527 BottomWidgetType::ProcSort => {
1528 #[allow(clippy::collapsible_match)]
1529 if !proc_widget_state.is_sort_open {
1530 if let Some(parent_proc_widget) = self
1531 .widget_map
1532 .get(&(new_widget_id - *offset))
1533 {
1534 self.current_widget =
1535 parent_proc_widget.clone();
1536 }
1537 } else {
1538 self.current_widget = new_widget.clone();
1539 }
1540 }
1541 _ => {
1542 self.current_widget = new_widget.clone();
1543 }
1544 }
1545 }
1546 }
1547 _ => {
1548 self.current_widget = new_widget.clone();
1549 }
1550 }
1551 }
1552 }
1553 }
1554 _ => {
1555 // Cannot be hidden, does not special treatment.
1556 self.current_widget = new_widget.clone();
1557 }
1558 }
1559
1560 let mut reflection_dir: Option<WidgetDirection> = None;
1561 if let Some((parent_direction, offset)) = &self.current_widget.parent_reflector {
1562 match &self.current_widget.widget_type {
1563 BottomWidgetType::CpuLegend => {
1564 if let Some(cpu_widget_state) = self
1565 .states
1566 .cpu_state
1567 .widget_states
1568 .get(&(self.current_widget.widget_id - *offset))
1569 && cpu_widget_state.is_legend_hidden
1570 {
1571 reflection_dir = Some(parent_direction.clone());
1572 }
1573 }
1574 BottomWidgetType::ProcSearch | BottomWidgetType::ProcSort => {
1575 if let Some(proc_widget_state) = self
1576 .states
1577 .proc_state
1578 .widget_states
1579 .get(&(self.current_widget.widget_id - *offset))
1580 {
1581 match &self.current_widget.widget_type {
1582 BottomWidgetType::ProcSearch
1583 if !proc_widget_state.is_search_enabled() =>
1584 {
1585 reflection_dir = Some(parent_direction.clone());
1586 }
1587 BottomWidgetType::ProcSort
1588 if !proc_widget_state.is_sort_open =>
1589 {
1590 reflection_dir = Some(parent_direction.clone());
1591 }
1592 _ => {}
1593 }
1594 }
1595 }
1596 _ => {}
1597 }
1598 }
1599
1600 if let Some(ref_dir) = &reflection_dir {
1601 self.move_widget_selection_logic(ref_dir);
1602 }
1603 }
1604 } else {
1605 match direction {
1606 WidgetDirection::Left => self.handle_left_expanded_movement(),
1607 WidgetDirection::Right => self.handle_right_expanded_movement(),
1608 WidgetDirection::Up => {
1609 if let BottomWidgetType::ProcSearch = self.current_widget.widget_type
1610 && let Some(current_widget) =
1611 self.widget_map.get(&self.current_widget.widget_id)
1612 && let Some(new_widget_id) = current_widget.up_neighbour
1613 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1614 {
1615 self.current_widget = new_widget.clone();
1616 }
1617 }
1618 WidgetDirection::Down => match &self.current_widget.widget_type {
1619 BottomWidgetType::Proc | BottomWidgetType::ProcSort => {
1620 let widget_id = self.current_widget.widget_id
1621 - match &self.current_widget.widget_type {
1622 BottomWidgetType::ProcSort => 2,
1623 _ => 0,
1624 };
1625 if let Some(current_widget) = self.widget_map.get(&widget_id)
1626 && let Some(new_widget_id) = current_widget.down_neighbour
1627 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1628 && let Some(proc_widget_state) =
1629 self.states.proc_state.get_widget_state(widget_id)
1630 && proc_widget_state.is_search_enabled()
1631 {
1632 self.current_widget = new_widget.clone();
1633 }
1634 }
1635 _ => {}
1636 },
1637 }
1638 }
1639 }
1640
1641 fn handle_left_expanded_movement(&mut self) {
1642 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1643 if let Some(new_widget_id) = self.current_widget.left_neighbour
1644 && let Some(proc_widget_state) = self
1645 .states
1646 .proc_state
1647 .widget_states
1648 .get(&self.current_widget.widget_id)
1649 && proc_widget_state.is_sort_open
1650 && let Some(proc_sort_widget) = self.widget_map.get(&new_widget_id)
1651 {
1652 self.current_widget = proc_sort_widget.clone(); // TODO: Could I remove this clone w/ static references?
1653 }
1654 } else if self.app_config_fields.cpu_left_legend {
1655 if let BottomWidgetType::Cpu = self.current_widget.widget_type
1656 && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id)
1657 && let Some(cpu_widget_state) = self
1658 .states
1659 .cpu_state
1660 .widget_states
1661 .get(&self.current_widget.widget_id)
1662 && !cpu_widget_state.is_legend_hidden
1663 && let Some(new_widget_id) = current_widget.left_neighbour
1664 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1665 {
1666 self.current_widget = new_widget.clone();
1667 }
1668 } else if let BottomWidgetType::CpuLegend = self.current_widget.widget_type
1669 && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id)
1670 && let Some(new_widget_id) = current_widget.left_neighbour
1671 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1672 {
1673 self.current_widget = new_widget.clone();
1674 }
1675 }
1676
1677 fn handle_right_expanded_movement(&mut self) {
1678 if let BottomWidgetType::ProcSort = self.current_widget.widget_type {
1679 if let Some(new_widget_id) = self.current_widget.right_neighbour
1680 && let Some(proc_sort_widget) = self.widget_map.get(&new_widget_id)
1681 {
1682 self.current_widget = proc_sort_widget.clone();
1683 }
1684 } else if self.app_config_fields.cpu_left_legend {
1685 if let BottomWidgetType::CpuLegend = self.current_widget.widget_type
1686 && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id)
1687 && let Some(new_widget_id) = current_widget.right_neighbour
1688 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1689 {
1690 self.current_widget = new_widget.clone();
1691 }
1692 } else if let BottomWidgetType::Cpu = self.current_widget.widget_type
1693 && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id)
1694 && let Some(cpu_widget_state) = self
1695 .states
1696 .cpu_state
1697 .widget_states
1698 .get(&self.current_widget.widget_id)
1699 && !cpu_widget_state.is_legend_hidden
1700 && let Some(new_widget_id) = current_widget.right_neighbour
1701 && let Some(new_widget) = self.widget_map.get(&new_widget_id)
1702 {
1703 self.current_widget = new_widget.clone();
1704 }
1705 }
1706
1707 pub fn skip_to_first(&mut self) {
1708 if !self.ignore_normal_keybinds() {
1709 match self.current_widget.widget_type {
1710 BottomWidgetType::Proc => {
1711 if let Some(proc_widget_state) = self
1712 .states
1713 .proc_state
1714 .get_mut_widget_state(self.current_widget.widget_id)
1715 {
1716 proc_widget_state.table.scroll_to_first();
1717 }
1718 }
1719 BottomWidgetType::ProcSort => {
1720 if let Some(proc_widget_state) = self
1721 .states
1722 .proc_state
1723 .get_mut_widget_state(self.current_widget.widget_id - 2)
1724 {
1725 proc_widget_state.sort_table.scroll_to_first();
1726 }
1727 }
1728 BottomWidgetType::Temp => {
1729 if let Some(temp_widget_state) = self
1730 .states
1731 .temp_state
1732 .get_mut_widget_state(self.current_widget.widget_id)
1733 {
1734 temp_widget_state.table.scroll_to_first();
1735 }
1736 }
1737 BottomWidgetType::Disk => {
1738 if let Some(disk_widget_state) = self
1739 .states
1740 .disk_state
1741 .get_mut_widget_state(self.current_widget.widget_id)
1742 {
1743 disk_widget_state.table.scroll_to_first();
1744 }
1745 }
1746 BottomWidgetType::CpuLegend => {
1747 if let Some(cpu_widget_state) = self
1748 .states
1749 .cpu_state
1750 .get_mut_widget_state(self.current_widget.widget_id - 1)
1751 {
1752 cpu_widget_state.table.scroll_to_first();
1753 }
1754 }
1755
1756 _ => {}
1757 }
1758 self.reset_multi_tap_keys();
1759 } else if self.help_dialog_state.is_showing_help {
1760 self.help_dialog_state.scroll_state.current_scroll_index = 0;
1761 } else if self.process_kill_dialog.is_open() {
1762 self.process_kill_dialog.go_to_first();
1763 }
1764 }
1765
1766 pub fn skip_to_last(&mut self) {
1767 if !self.ignore_normal_keybinds() {
1768 match self.current_widget.widget_type {
1769 BottomWidgetType::Proc => {
1770 if let Some(proc_widget_state) = self
1771 .states
1772 .proc_state
1773 .get_mut_widget_state(self.current_widget.widget_id)
1774 {
1775 proc_widget_state.table.scroll_to_last();
1776 }
1777 }
1778 BottomWidgetType::ProcSort => {
1779 if let Some(proc_widget_state) = self
1780 .states
1781 .proc_state
1782 .get_mut_widget_state(self.current_widget.widget_id - 2)
1783 {
1784 proc_widget_state.sort_table.scroll_to_last();
1785 }
1786 }
1787 BottomWidgetType::Temp => {
1788 if let Some(temp_widget_state) = self
1789 .states
1790 .temp_state
1791 .get_mut_widget_state(self.current_widget.widget_id)
1792 {
1793 temp_widget_state.table.scroll_to_last();
1794 }
1795 }
1796 BottomWidgetType::Disk => {
1797 if let Some(disk_widget_state) = self
1798 .states
1799 .disk_state
1800 .get_mut_widget_state(self.current_widget.widget_id)
1801 && !self.data_store.get_data().disk_harvest.is_empty()
1802 {
1803 disk_widget_state.table.scroll_to_last();
1804 }
1805 }
1806 BottomWidgetType::CpuLegend => {
1807 if let Some(cpu_widget_state) = self
1808 .states
1809 .cpu_state
1810 .get_mut_widget_state(self.current_widget.widget_id - 1)
1811 {
1812 cpu_widget_state.table.scroll_to_last();
1813 }
1814 }
1815 _ => {}
1816 }
1817 self.reset_multi_tap_keys();
1818 } else if self.help_dialog_state.is_showing_help {
1819 self.help_dialog_state.scroll_state.current_scroll_index =
1820 self.help_dialog_state.scroll_state.max_scroll_index;
1821 } else if self.process_kill_dialog.is_open() {
1822 self.process_kill_dialog.go_to_last();
1823 }
1824 }
1825
1826 pub fn decrement_position_count(&mut self) {
1827 self.change_position_count(-1);
1828 }
1829
1830 pub fn increment_position_count(&mut self) {
1831 self.change_position_count(1);
1832 }
1833
1834 fn change_position_count(&mut self, amount: i64) {
1835 if !self.ignore_normal_keybinds() {
1836 match self.current_widget.widget_type {
1837 BottomWidgetType::Proc => {
1838 self.change_process_position(amount);
1839 }
1840 BottomWidgetType::ProcSort => self.change_process_sort_position(amount),
1841 BottomWidgetType::Temp => self.change_temp_position(amount),
1842 BottomWidgetType::Disk => self.change_disk_position(amount),
1843 BottomWidgetType::CpuLegend => self.change_cpu_legend_position(amount),
1844 _ => {}
1845 }
1846 }
1847 }
1848
1849 fn change_process_sort_position(&mut self, num_to_change_by: i64) {
1850 if let Some(proc_widget_state) = self
1851 .states
1852 .proc_state
1853 .get_mut_widget_state(self.current_widget.widget_id - 2)
1854 {
1855 proc_widget_state
1856 .sort_table
1857 .increment_position(num_to_change_by);
1858 }
1859 }
1860
1861 fn change_cpu_legend_position(&mut self, num_to_change_by: i64) {
1862 if let Some(cpu_widget_state) = self
1863 .states
1864 .cpu_state
1865 .widget_states
1866 .get_mut(&(self.current_widget.widget_id - 1))
1867 {
1868 cpu_widget_state.table.increment_position(num_to_change_by);
1869 }
1870 }
1871
1872 /// Returns the new position.
1873 fn change_process_position(&mut self, num_to_change_by: i64) -> Option<usize> {
1874 if let Some(proc_widget_state) = self
1875 .states
1876 .proc_state
1877 .get_mut_widget_state(self.current_widget.widget_id)
1878 {
1879 proc_widget_state.table.increment_position(num_to_change_by)
1880 } else {
1881 None
1882 }
1883 }
1884
1885 fn change_temp_position(&mut self, num_to_change_by: i64) {
1886 if let Some(temp_widget_state) = self
1887 .states
1888 .temp_state
1889 .widget_states
1890 .get_mut(&self.current_widget.widget_id)
1891 {
1892 temp_widget_state.table.increment_position(num_to_change_by);
1893 }
1894 }
1895
1896 fn change_disk_position(&mut self, num_to_change_by: i64) {
1897 if let Some(disk_widget_state) = self
1898 .states
1899 .disk_state
1900 .widget_states
1901 .get_mut(&self.current_widget.widget_id)
1902 {
1903 disk_widget_state.table.increment_position(num_to_change_by);
1904 }
1905 }
1906
1907 fn help_scroll_up(&mut self) {
1908 if self.help_dialog_state.scroll_state.current_scroll_index > 0 {
1909 self.help_dialog_state.scroll_state.current_scroll_index -= 1;
1910 }
1911 }
1912
1913 fn help_scroll_down(&mut self) {
1914 if self.help_dialog_state.scroll_state.current_scroll_index
1915 < self.help_dialog_state.scroll_state.max_scroll_index
1916 {
1917 self.help_dialog_state.scroll_state.current_scroll_index += 1;
1918 }
1919 }
1920
1921 fn help_scroll_to_or_max(&mut self, new_position: u16) {
1922 if new_position <= self.help_dialog_state.scroll_state.max_scroll_index {
1923 self.help_dialog_state.scroll_state.current_scroll_index = new_position;
1924 } else {
1925 self.help_dialog_state.scroll_state.current_scroll_index =
1926 self.help_dialog_state.scroll_state.max_scroll_index;
1927 }
1928 }
1929
1930 pub fn handle_scroll_up(&mut self) {
1931 if self.process_kill_dialog.is_open() {
1932 self.process_kill_dialog.on_scroll_up();
1933 } else if self.help_dialog_state.is_showing_help {
1934 self.help_scroll_up();
1935 } else if self.current_widget.widget_type.is_widget_graph() {
1936 self.zoom_in();
1937 } else if self.current_widget.widget_type.is_widget_table() {
1938 self.decrement_position_count();
1939 }
1940 }
1941
1942 pub fn handle_scroll_down(&mut self) {
1943 if self.process_kill_dialog.is_open() {
1944 self.process_kill_dialog.on_scroll_down();
1945 } else if self.help_dialog_state.is_showing_help {
1946 self.help_scroll_down();
1947 } else if self.current_widget.widget_type.is_widget_graph() {
1948 self.zoom_out();
1949 } else if self.current_widget.widget_type.is_widget_table() {
1950 self.increment_position_count();
1951 }
1952 }
1953
1954 fn on_plus(&mut self) {
1955 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1956 // Toggle collapsing if tree
1957 self.toggle_collapsing_process_branch();
1958 } else {
1959 self.zoom_in();
1960 }
1961 }
1962
1963 fn on_minus(&mut self) {
1964 if let BottomWidgetType::Proc = self.current_widget.widget_type {
1965 // Toggle collapsing if tree
1966 self.toggle_collapsing_process_branch();
1967 } else {
1968 self.zoom_out();
1969 }
1970 }
1971
1972 fn toggle_collapsing_process_branch(&mut self) {
1973 if let Some(pws) = self
1974 .states
1975 .proc_state
1976 .widget_states
1977 .get_mut(&self.current_widget.widget_id)
1978 {
1979 pws.toggle_current_tree_branch_entry();
1980 }
1981 }
1982
1983 #[inline]
1984 fn current_ts_state(&mut self) -> Option<&mut TimeseriesState> {
1985 match self.current_widget.widget_type {
1986 BottomWidgetType::Cpu
1987 if let Some(widget_state) = self
1988 .states
1989 .cpu_state
1990 .get_mut_widget_state(self.current_widget.widget_id) =>
1991 {
1992 Some(widget_state.graph.state_mut())
1993 }
1994 BottomWidgetType::Mem
1995 if let Some(widget_state) = self
1996 .states
1997 .mem_state
1998 .get_mut_widget_state(self.current_widget.widget_id) =>
1999 {
2000 Some(widget_state.graph.state_mut())
2001 }
2002 BottomWidgetType::Net
2003 if let Some(widget_state) = self
2004 .states
2005 .net_state
2006 .get_mut_widget_state(self.current_widget.widget_id) =>
2007 {
2008 Some(widget_state.graph.state_mut())
2009 }
2010 BottomWidgetType::TempGraph
2011 if let Some(widget_state) = self
2012 .states
2013 .temp_graph_state
2014 .get_mut_widget_state(self.current_widget.widget_id) =>
2015 {
2016 Some(widget_state.graph.state_mut())
2017 }
2018 BottomWidgetType::DiskIoGraph
2019 if let Some(widget_state) = self
2020 .states
2021 .disk_io_graph_state
2022 .get_mut_widget_state(self.current_widget.widget_id) =>
2023 {
2024 Some(widget_state.graph.state_mut())
2025 }
2026 _ => None,
2027 }
2028 }
2029
2030 fn zoom_out(&mut self) {
2031 if let Some(ts_state) = self.current_ts_state() {
2032 ts_state.zoom_out();
2033 }
2034 }
2035
2036 fn zoom_in(&mut self) {
2037 if let Some(ts_state) = self.current_ts_state() {
2038 ts_state.zoom_in();
2039 }
2040 }
2041
2042 fn reset_zoom(&mut self) {
2043 if let Some(ts_state) = self.current_ts_state() {
2044 ts_state.reset_zoom();
2045 }
2046 }
2047
2048 /// Moves the mouse to the widget that was clicked on, then propagates the
2049 /// click down to be handled by the widget specifically.
2050 pub fn on_left_mouse_up(&mut self, x: u16, y: u16) {
2051 // Pretty dead simple - iterate through the widget map and go to the widget
2052 // where the click is within.
2053
2054 // TODO: [REFACTOR] might want to refactor this, it's really ugly.
2055 // TODO: [REFACTOR] Might wanna refactor ALL state things in general, currently
2056 // everything is grouped up as an app state. We should separate stuff
2057 // like event state and gui state and etc.
2058
2059 // TODO: [MOUSE] double click functionality...? We would do this above all
2060 // other actions and SC if needed.
2061
2062 // Short circuit if we're in basic table... we might have to handle the basic
2063 // table arrow case here...
2064
2065 if let Some(bt) = &mut self.states.basic_table_widget_state
2066 && let (
2067 Some((left_tlc_x, left_tlc_y)),
2068 Some((left_brc_x, left_brc_y)),
2069 Some((right_tlc_x, right_tlc_y)),
2070 Some((right_brc_x, right_brc_y)),
2071 ) = (bt.left_tlc, bt.left_brc, bt.right_tlc, bt.right_brc)
2072 {
2073 if (x >= left_tlc_x && y >= left_tlc_y) && (x < left_brc_x && y < left_brc_y) {
2074 // Case for the left "button" in the simple arrow.
2075 if let Some(new_widget) = self.widget_map.get(&(bt.currently_displayed_widget_id)) {
2076 // We have to move to the current table widget first...
2077 self.current_widget = new_widget.clone();
2078
2079 if let BottomWidgetType::Proc = &new_widget.widget_type
2080 && let Some(proc_widget_state) = self
2081 .states
2082 .proc_state
2083 .get_widget_state(new_widget.widget_id)
2084 && proc_widget_state.is_sort_open
2085 {
2086 self.move_widget_selection(&WidgetDirection::Left);
2087 }
2088 self.move_widget_selection(&WidgetDirection::Left);
2089 return;
2090 }
2091 } else if (x >= right_tlc_x && y >= right_tlc_y) && (x < right_brc_x && y < right_brc_y)
2092 {
2093 // Case for the right "button" in the simple arrow.
2094 if let Some(new_widget) = self.widget_map.get(&(bt.currently_displayed_widget_id)) {
2095 // We have to move to the current table widget first...
2096 self.current_widget = new_widget.clone();
2097
2098 if let BottomWidgetType::ProcSort = &new_widget.widget_type
2099 && let Some(proc_widget_state) = self
2100 .states
2101 .proc_state
2102 .get_widget_state(new_widget.widget_id - 2)
2103 && proc_widget_state.is_sort_open
2104 {
2105 self.move_widget_selection(&WidgetDirection::Right);
2106 }
2107 }
2108 self.move_widget_selection(&WidgetDirection::Right);
2109 // Bit extra logic to ensure you always land on a proc widget, not the sort
2110 if let BottomWidgetType::ProcSort = &self.current_widget.widget_type {
2111 self.move_widget_selection(&WidgetDirection::Right);
2112 }
2113 return;
2114 }
2115 }
2116
2117 // Second short circuit --- are we in the dd dialog state? If so, only check
2118 // yes/no/signals and bail after.
2119 if self.process_kill_dialog.is_open() && self.process_kill_dialog.on_click(x, y) {
2120 return;
2121 }
2122
2123 let mut failed_to_get = true;
2124 for (new_widget_id, widget) in &self.widget_map {
2125 if let (Some((tlc_x, tlc_y)), Some((brc_x, brc_y))) =
2126 (widget.top_left_corner, widget.bottom_right_corner)
2127 && (x >= tlc_x && y >= tlc_y)
2128 && (x < brc_x && y < brc_y)
2129 && let Some(new_widget) = self.widget_map.get(new_widget_id)
2130 {
2131 self.current_widget = new_widget.clone();
2132 match &self.current_widget.widget_type {
2133 BottomWidgetType::Temp
2134 | BottomWidgetType::Proc
2135 | BottomWidgetType::ProcSort
2136 | BottomWidgetType::Disk
2137 | BottomWidgetType::Battery => {
2138 if let Some(basic_table_widget_state) =
2139 &mut self.states.basic_table_widget_state
2140 {
2141 basic_table_widget_state.currently_displayed_widget_id =
2142 self.current_widget.widget_id;
2143 basic_table_widget_state.currently_displayed_widget_type =
2144 self.current_widget.widget_type.clone();
2145 }
2146 }
2147 _ => {}
2148 }
2149
2150 failed_to_get = false;
2151 break;
2152 }
2153 }
2154
2155 if failed_to_get {
2156 return;
2157 }
2158
2159 // Now handle click propagation down to widget.
2160 if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = (
2161 &self.current_widget.top_left_corner,
2162 &self.current_widget.bottom_right_corner,
2163 ) {
2164 let border_offset = u16::from(self.is_drawing_border());
2165
2166 // This check ensures the click isn't actually just clicking on the bottom
2167 // border.
2168 if y < (brc_y - border_offset) {
2169 match &self.current_widget.widget_type {
2170 BottomWidgetType::Proc
2171 | BottomWidgetType::ProcSort
2172 | BottomWidgetType::CpuLegend
2173 | BottomWidgetType::Temp
2174 | BottomWidgetType::Disk => {
2175 // Get our index...
2176 let clicked_entry = y - *tlc_y;
2177 let header_offset = self.header_offset(&self.current_widget);
2178 let offset = border_offset + header_offset;
2179 if clicked_entry >= offset {
2180 let offset_clicked_entry = clicked_entry - offset;
2181 match &self.current_widget.widget_type {
2182 BottomWidgetType::Proc => {
2183 if let Some(proc_widget_state) = self
2184 .states
2185 .proc_state
2186 .get_widget_state(self.current_widget.widget_id)
2187 && let Some(visual_index) =
2188 proc_widget_state.table.ratatui_selected()
2189 {
2190 let is_tree_mode = matches!(
2191 proc_widget_state.mode,
2192 ProcWidgetMode::Tree { .. }
2193 );
2194 let change =
2195 offset_clicked_entry as i64 - visual_index as i64;
2196
2197 self.change_process_position(change);
2198
2199 // If in tree mode, also check to see if this click is
2200 // on
2201 // the same entry as the already selected one - if it
2202 // is,
2203 // then we minimize.
2204 if is_tree_mode && change == 0 {
2205 self.toggle_collapsing_process_branch();
2206 }
2207 }
2208 }
2209 BottomWidgetType::ProcSort => {
2210 // TODO: [Feature] This could sort if you double click!
2211 if let Some(proc_widget_state) = self
2212 .states
2213 .proc_state
2214 .get_widget_state(self.current_widget.widget_id - 2)
2215 && let Some(visual_index) =
2216 proc_widget_state.sort_table.ratatui_selected()
2217 {
2218 self.change_process_sort_position(
2219 offset_clicked_entry as i64 - visual_index as i64,
2220 );
2221 }
2222 }
2223 BottomWidgetType::CpuLegend => {
2224 if let Some(cpu_widget_state) = self
2225 .states
2226 .cpu_state
2227 .get_widget_state(self.current_widget.widget_id - 1)
2228 && let Some(visual_index) =
2229 cpu_widget_state.table.ratatui_selected()
2230 {
2231 self.change_cpu_legend_position(
2232 offset_clicked_entry as i64 - visual_index as i64,
2233 );
2234 }
2235 }
2236 BottomWidgetType::Temp => {
2237 if let Some(temp_widget_state) = self
2238 .states
2239 .temp_state
2240 .get_widget_state(self.current_widget.widget_id)
2241 && let Some(visual_index) =
2242 temp_widget_state.table.ratatui_selected()
2243 {
2244 self.change_temp_position(
2245 offset_clicked_entry as i64 - visual_index as i64,
2246 );
2247 }
2248 }
2249 BottomWidgetType::Disk => {
2250 if let Some(disk_widget_state) = self
2251 .states
2252 .disk_state
2253 .get_widget_state(self.current_widget.widget_id)
2254 && let Some(visual_index) =
2255 disk_widget_state.table.ratatui_selected()
2256 {
2257 self.change_disk_position(
2258 offset_clicked_entry as i64 - visual_index as i64,
2259 );
2260 }
2261 }
2262 _ => {}
2263 }
2264 } else {
2265 // We might have clicked on a header! Check if we only exceeded the
2266 // table + border offset, and it's implied
2267 // we exceeded the gap offset.
2268 if clicked_entry == border_offset {
2269 match &self.current_widget.widget_type {
2270 BottomWidgetType::Proc => {
2271 if let Some(state) = self
2272 .states
2273 .proc_state
2274 .get_mut_widget_state(self.current_widget.widget_id)
2275 && state.table.try_select_location(x, y).is_some()
2276 {
2277 state.force_data_update();
2278 }
2279 }
2280 BottomWidgetType::Temp => {
2281 if let Some(temp) = self
2282 .states
2283 .temp_state
2284 .get_mut_widget_state(self.current_widget.widget_id)
2285 && temp.table.try_select_location(x, y).is_some()
2286 {
2287 temp.force_data_update();
2288 }
2289 }
2290 BottomWidgetType::Disk => {
2291 if let Some(disk) = self
2292 .states
2293 .disk_state
2294 .get_mut_widget_state(self.current_widget.widget_id)
2295 && disk.table.try_select_location(x, y).is_some()
2296 {
2297 disk.force_data_update();
2298 }
2299 }
2300 _ => (),
2301 }
2302 }
2303 }
2304 }
2305 BottomWidgetType::Battery => {
2306 #[cfg(feature = "battery")]
2307 if let Some(battery_widget_state) = self
2308 .states
2309 .battery_state
2310 .get_mut_widget_state(self.current_widget.widget_id)
2311 && let Some(tab_spacing) = &battery_widget_state.tab_click_locs
2312 {
2313 for (itx, ((tlc_x, tlc_y), (brc_x, brc_y))) in
2314 tab_spacing.iter().enumerate()
2315 {
2316 if (x >= *tlc_x && y >= *tlc_y) && (x <= *brc_x && y <= *brc_y) {
2317 let num_batteries =
2318 self.data_store.get_data().battery_harvest.len();
2319 if itx >= num_batteries {
2320 // range check to keep within current data
2321 battery_widget_state.currently_selected_battery_index =
2322 num_batteries - 1;
2323 } else {
2324 battery_widget_state.currently_selected_battery_index = itx;
2325 }
2326 break;
2327 }
2328 }
2329 }
2330 }
2331 _ => {}
2332 }
2333 }
2334 }
2335 }
2336
2337 fn is_drawing_border(&self) -> bool {
2338 self.is_expanded || !self.app_config_fields.use_basic_mode
2339 }
2340
2341 fn header_offset(&self, widget: &BottomWidget) -> u16 {
2342 if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) =
2343 (widget.top_left_corner, widget.bottom_right_corner)
2344 {
2345 let height_diff = brc_y - tlc_y;
2346 if height_diff >= constants::TABLE_GAP_HEIGHT_LIMIT {
2347 1 + self.app_config_fields.table_gap.height()
2348 } else {
2349 let min_height_for_header = if self.is_drawing_border() { 3 } else { 1 };
2350 u16::from(height_diff > min_height_for_header)
2351 }
2352 } else {
2353 1 + self.app_config_fields.table_gap.height()
2354 }
2355 }
2356
2357 /// A quick and dirty way to handle paste events.
2358 pub fn handle_paste(&mut self, paste: String) {
2359 let is_in_search_widget = self.is_in_search_widget();
2360 if let Some(proc_widget_state) = self
2361 .states
2362 .proc_state
2363 .widget_states
2364 .get_mut(&(self.current_widget.widget_id - 1))
2365 && is_in_search_widget
2366 && proc_widget_state.is_search_enabled()
2367 {
2368 proc_widget_state
2369 .proc_search
2370 .search_state
2371 .input_field_state
2372 .insert_string(paste);
2373
2374 proc_widget_state.update_query();
2375 }
2376 }
2377 }