refactor: Better responsiveness when resizing cpu basic widget (#292)

Gives better and less jank responsiveness when resizing the window in narrow sizes for the cpu basic widget.
This commit is contained in:
Clement Tsang 2020-11-02 23:41:57 -05:00 committed by GitHub
parent e8358f8f47
commit 1d35e1c8b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 32 deletions

View File

@ -61,41 +61,86 @@ impl CpuBasicWidget for Painter {
.direction(Direction::Horizontal)
.split(draw_loc);
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
const CPU_NAME_SPACE: usize = 3;
const BAR_BOUND_SPACE: usize = 2;
const PERCENTAGE_SPACE: usize = 4;
const MARGIN_SPACE: usize = 2;
let remaining_width = usize::from(draw_loc.width)
.saturating_sub((9 + MARGIN_SPACE) * REQUIRED_COLUMNS);
let bar_length = remaining_width / REQUIRED_COLUMNS;
const COMBINED_SPACING: usize =
CPU_NAME_SPACE + BAR_BOUND_SPACE + PERCENTAGE_SPACE + MARGIN_SPACE;
const REDUCED_SPACING: usize = CPU_NAME_SPACE + PERCENTAGE_SPACE + MARGIN_SPACE;
let chunk_width = chunks[0].width as usize;
// CPU (and RAM) percent bars are, uh, "heavily" inspired from htop.
let cpu_bars = (0..num_cpus)
.map(|cpu_index| {
let use_percentage =
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
0.0
};
let num_bars = calculate_basic_use_bars(use_percentage, bar_length);
format!(
"{:3}[{}{}{:3.0}%]",
if app_state.app_config_fields.show_average_cpu {
if cpu_index == 0 {
"AVG".to_string()
// Inspired by htop.
// We do +4 as if it's too few bars in the bar length, it's kinda pointless.
let cpu_bars = if chunk_width >= COMBINED_SPACING + 4 {
let bar_length = chunk_width - COMBINED_SPACING;
(0..num_cpus)
.map(|cpu_index| {
let use_percentage =
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
(cpu_index - 1).to_string()
}
} else {
cpu_index.to_string()
},
"|".repeat(num_bars),
" ".repeat(bar_length - num_bars),
use_percentage.round(),
)
})
.collect::<Vec<_>>();
0.0
};
let num_bars = calculate_basic_use_bars(use_percentage, bar_length);
format!(
"{:3}[{}{}{:3.0}%]",
if app_state.app_config_fields.show_average_cpu {
if cpu_index == 0 {
"AVG".to_string()
} else {
(cpu_index - 1).to_string()
}
} else {
cpu_index.to_string()
},
"|".repeat(num_bars),
" ".repeat(bar_length - num_bars),
use_percentage.round(),
)
})
.collect::<Vec<_>>()
} else if chunk_width >= REDUCED_SPACING {
(0..num_cpus)
.map(|cpu_index| {
let use_percentage =
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
0.0
};
format!(
"{:3} {:3.0}%",
if app_state.app_config_fields.show_average_cpu {
if cpu_index == 0 {
"AVG".to_string()
} else {
(cpu_index - 1).to_string()
}
} else {
cpu_index.to_string()
},
use_percentage.round(),
)
})
.collect::<Vec<_>>()
} else {
(0..num_cpus)
.map(|cpu_index| {
let use_percentage =
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
0.0
};
format!("{:3.0}%", use_percentage.round(),)
})
.collect::<Vec<_>>()
};
let mut row_counter = num_cpus;
let mut start_index = 0;

View File

@ -486,7 +486,7 @@ pub fn tree_process_data(
single_process_data: &[ConvertedProcessData], is_using_command: bool,
sort_type: &ProcessSorting, is_sort_descending: bool,
) -> Vec<ConvertedProcessData> {
// TODO: [TREE] Allow for collapsing entries.
// FIXME: [TREE] Allow for collapsing entries.
// TODO: [TREE] Option to sort usage by total branch usage or individual value usage?
// Let's first build up a (really terrible) parent -> child mapping...