From 265597dd34c28dfffb5f9e837b9a84aecaa80771 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 9 Oct 2023 20:53:32 -0400 Subject: [PATCH] Cleanup proc tree expand/collapse functionality * On click, continue to toggle tree branch expand/collapse * On `Left` and `-`, always collapse * On `Right` and `+`, always expand Also, a minor clippy fix to pass the CI --- docs/content/usage/widgets/process.md | 2 +- src/app.rs | 16 +++++++++----- .../disks/unix/linux/partition.rs | 2 +- src/constants.rs | 5 +++-- src/widgets/process_table.rs | 22 +++++++++++++++---- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/docs/content/usage/widgets/process.md b/docs/content/usage/widgets/process.md index a0936f61f..ab1ff2346 100644 --- a/docs/content/usage/widgets/process.md +++ b/docs/content/usage/widgets/process.md @@ -83,7 +83,7 @@ Pressing ++t++ or ++f5++ in the table toggles tree mode in the process widget, d A picture of tree mode in a process widget. -A process in tree mode can also be "collapsed", hiding its children and any descendants, using either the ++minus++ or ++plus++ keys, or double clicking on an entry. +A process in tree mode can also be "collapsed", hiding its children and any descendants, using either the ++minus++ or ++Left++ keys, or clicking on an entry. To expand, use the ++plus++ or ++Right++ keys, or click on the entry again. Lastly, note that in tree mode, processes cannot be grouped together due to the behaviour of the two modes somewhat clashing. diff --git a/src/app.rs b/src/app.rs index 468eeed88..1d457d887 100644 --- a/src/app.rs +++ b/src/app.rs @@ -705,6 +705,9 @@ impl App { } } } + BottomWidgetType::Proc => { + self.toggle_collapsing_process_branch(Some(false)); + } _ => {} } } else if self.delete_dialog_state.is_showing_dd { @@ -769,6 +772,9 @@ impl App { } } } + BottomWidgetType::Proc => { + self.toggle_collapsing_process_branch(Some(true)); + } _ => {} } } else if self.delete_dialog_state.is_showing_dd { @@ -2138,7 +2144,7 @@ impl App { fn on_plus(&mut self) { if let BottomWidgetType::Proc = self.current_widget.widget_type { // Toggle collapsing if tree - self.toggle_collapsing_process_branch(); + self.toggle_collapsing_process_branch(Some(true)); } else { self.zoom_in(); } @@ -2147,20 +2153,20 @@ impl App { fn on_minus(&mut self) { if let BottomWidgetType::Proc = self.current_widget.widget_type { // Toggle collapsing if tree - self.toggle_collapsing_process_branch(); + self.toggle_collapsing_process_branch(Some(false)); } else { self.zoom_out(); } } - fn toggle_collapsing_process_branch(&mut self) { + fn toggle_collapsing_process_branch(&mut self, force_expand: Option) { if let Some(pws) = self .states .proc_state .widget_states .get_mut(&self.current_widget.widget_id) { - pws.toggle_current_tree_branch_entry(); + pws.toggle_current_tree_branch_entry(force_expand); } } @@ -2579,7 +2585,7 @@ impl App { // the same entry as the already selected one - if it is, // then we minimize. if is_tree_mode && change == 0 { - self.toggle_collapsing_process_branch(); + self.toggle_collapsing_process_branch(None); } } } diff --git a/src/app/data_harvester/disks/unix/linux/partition.rs b/src/app/data_harvester/disks/unix/linux/partition.rs index 0e215747e..1507ae88d 100644 --- a/src/app/data_harvester/disks/unix/linux/partition.rs +++ b/src/app/data_harvester/disks/unix/linux/partition.rs @@ -110,7 +110,7 @@ impl FromStr for Partition { let mut parts = line.splitn(5, ' '); let device = match parts.next() { - Some(device) if device == "none" => None, + Some("none") => None, Some(device) => Some(device.to_string()), None => { bail!("missing device"); diff --git a/src/constants.rs b/src/constants.rs index bf73529a1..84462a9a3 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -327,7 +327,7 @@ pub const CPU_HELP_TEXT: [&str; 2] = [ "Mouse scroll Scrolling over an CPU core/average shows only that entry on the chart", ]; -pub const PROCESS_HELP_TEXT: [&str; 15] = [ +pub const PROCESS_HELP_TEXT: [&str; 16] = [ "3 - Process widget", "dd, F9 Kill the selected process", "c Sort by CPU usage, press again to reverse", @@ -341,7 +341,8 @@ pub const PROCESS_HELP_TEXT: [&str; 15] = [ "I Invert current sort", "% Toggle between values and percentages for memory usage", "t, F5 Toggle tree mode", - "+, -, click Collapse/expand a branch while in tree mode", + "-, Left, click Collapse a branch while in tree mode", + "+, Right, click Expand a branch while in tree mode", "click on header Sorts the entries by that column, click again to invert the sort", ]; diff --git a/src/widgets/process_table.rs b/src/widgets/process_table.rs index 41db87066..5f12b48e3 100644 --- a/src/widgets/process_table.rs +++ b/src/widgets/process_table.rs @@ -794,15 +794,29 @@ impl ProcWidgetState { } } - pub fn toggle_current_tree_branch_entry(&mut self) { + /// Change the state of a tree branch: + /// None - toggle the branch between collapsed and expanded + /// Some(true) - expand the branch, or do nothing if it's already expanded + /// Some(false) - collapse the branch, or do nothing if it's already collapsed + pub fn toggle_current_tree_branch_entry(&mut self, force_expand: Option) { if let ProcWidgetMode::Tree { collapsed_pids } = &mut self.mode { if let Some(process) = self.table.current_item() { let pid = process.pid; - if !collapsed_pids.remove(&pid) { - collapsed_pids.insert(pid); + let refresh = match force_expand { + Some(true) => collapsed_pids.remove(&pid), + Some(false) => collapsed_pids.insert(pid), + None => { + if !collapsed_pids.remove(&pid) { + collapsed_pids.insert(pid); + } + true + } + }; + + if refresh { + self.force_data_update(); } - self.force_data_update(); } } }