diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-11-02 23:41:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-02 23:41:57 -0500 |
commit | 1d35e1c8b4cf2978333e79a38619772cd5d781e3 (patch) | |
tree | cd75542f3872a9c8eedf5f610a5fcc628a36b750 /src/canvas | |
parent | e8358f8f47f707b2af99e3b69dd1d7c3d2ca0ef3 (diff) |
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.
Diffstat (limited to 'src/canvas')
-rw-r--r-- | src/canvas/widgets/cpu_basic.rs | 111 |
1 files changed, 78 insertions, 33 deletions
diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index 8e6b332f..12b9949a 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -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; - - // 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() + + 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; + + // 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 { + 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 { - (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 + }; + + 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; |