| 1 | use ratatui::{ |
| 2 | Frame, |
| 3 | layout::{Constraint, Direction, Layout, Rect}, |
| 4 | }; |
| 5 | |
| 6 | use crate::{ |
| 7 | app::{App, data::InnerData, layout_manager::WidgetDirection}, |
| 8 | canvas::{ |
| 9 | Painter, |
| 10 | components::{ |
| 11 | data_table::{DrawInfo, SelectionState}, |
| 12 | time_series::GraphData, |
| 13 | }, |
| 14 | drawing_utils::should_hide_x_label, |
| 15 | }, |
| 16 | collection::cpu::CpuData, |
| 17 | components::time_series::GraphDrawCtx, |
| 18 | widgets::CpuWidgetState, |
| 19 | }; |
| 20 | |
| 21 | const AVG_POSITION: usize = 1; |
| 22 | const ALL_POSITION: usize = 0; |
| 23 | |
| 24 | impl Painter { |
| 25 | pub fn draw_cpu(&self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64) { |
| 26 | let legend_width = (draw_loc.width as f64 * 0.15) as u16; |
| 27 | |
| 28 | if legend_width < 6 { |
| 29 | // Skip drawing legend |
| 30 | if app_state.current_widget.widget_id == (widget_id + 1) { |
| 31 | if app_state.app_config_fields.cpu_left_legend { |
| 32 | app_state.move_widget_selection(&WidgetDirection::Right); |
| 33 | } else { |
| 34 | app_state.move_widget_selection(&WidgetDirection::Left); |
| 35 | } |
| 36 | } |
| 37 | self.draw_cpu_graph(f, app_state, draw_loc, widget_id); |
| 38 | if let Some(cpu_widget_state) = |
| 39 | app_state.states.cpu_state.widget_states.get_mut(&widget_id) |
| 40 | { |
| 41 | cpu_widget_state.is_legend_hidden = true; |
| 42 | } |
| 43 | |
| 44 | // Update draw loc in widget map |
| 45 | if app_state.should_get_widget_bounds() |
| 46 | && let Some(bottom_widget) = app_state.widget_map.get_mut(&widget_id) |
| 47 | { |
| 48 | bottom_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); |
| 49 | bottom_widget.bottom_right_corner = |
| 50 | Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); |
| 51 | } |
| 52 | } else { |
| 53 | let graph_width = draw_loc.width - legend_width; |
| 54 | let (graph_index, legend_index, constraints) = |
| 55 | if app_state.app_config_fields.cpu_left_legend { |
| 56 | ( |
| 57 | 1, |
| 58 | 0, |
| 59 | [ |
| 60 | Constraint::Length(legend_width), |
| 61 | Constraint::Length(graph_width), |
| 62 | ], |
| 63 | ) |
| 64 | } else { |
| 65 | ( |
| 66 | 0, |
| 67 | 1, |
| 68 | [ |
| 69 | Constraint::Length(graph_width), |
| 70 | Constraint::Length(legend_width), |
| 71 | ], |
| 72 | ) |
| 73 | }; |
| 74 | |
| 75 | let partitioned_draw_loc = Layout::default() |
| 76 | .margin(0) |
| 77 | .direction(Direction::Horizontal) |
| 78 | .constraints(constraints) |
| 79 | .split(draw_loc); |
| 80 | |
| 81 | self.draw_cpu_graph(f, app_state, partitioned_draw_loc[graph_index], widget_id); |
| 82 | self.draw_cpu_legend( |
| 83 | f, |
| 84 | app_state, |
| 85 | partitioned_draw_loc[legend_index], |
| 86 | widget_id + 1, |
| 87 | ); |
| 88 | |
| 89 | if app_state.should_get_widget_bounds() { |
| 90 | // Update draw loc in widget map |
| 91 | if let Some(cpu_widget) = app_state.widget_map.get_mut(&widget_id) { |
| 92 | cpu_widget.top_left_corner = Some(( |
| 93 | partitioned_draw_loc[graph_index].x, |
| 94 | partitioned_draw_loc[graph_index].y, |
| 95 | )); |
| 96 | cpu_widget.bottom_right_corner = Some(( |
| 97 | partitioned_draw_loc[graph_index].x |
| 98 | + partitioned_draw_loc[graph_index].width, |
| 99 | partitioned_draw_loc[graph_index].y |
| 100 | + partitioned_draw_loc[graph_index].height, |
| 101 | )); |
| 102 | } |
| 103 | |
| 104 | if let Some(legend_widget) = app_state.widget_map.get_mut(&(widget_id + 1)) { |
| 105 | legend_widget.top_left_corner = Some(( |
| 106 | partitioned_draw_loc[legend_index].x, |
| 107 | partitioned_draw_loc[legend_index].y, |
| 108 | )); |
| 109 | legend_widget.bottom_right_corner = Some(( |
| 110 | partitioned_draw_loc[legend_index].x |
| 111 | + partitioned_draw_loc[legend_index].width, |
| 112 | partitioned_draw_loc[legend_index].y |
| 113 | + partitioned_draw_loc[legend_index].height, |
| 114 | )); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn generate_points<'a>( |
| 121 | &self, cpu_widget_state: &'a CpuWidgetState, data: &'a InnerData, show_avg_cpu: bool, |
| 122 | ) -> Vec<GraphData<'a>> { |
| 123 | let show_avg_offset = if show_avg_cpu { AVG_POSITION } else { 0 }; |
| 124 | let current_scroll_position = cpu_widget_state.table.state.current_index; |
| 125 | let cpu_entries = &data.cpu_harvest; |
| 126 | let cpu_points = &data.time_series_data.cpu; |
| 127 | let time = &data.time_series_data.time; |
| 128 | |
| 129 | if current_scroll_position == ALL_POSITION { |
| 130 | // This case ensures the other cases cannot have the position be equal to 0. |
| 131 | |
| 132 | cpu_points |
| 133 | .iter() |
| 134 | .enumerate() |
| 135 | .map(|(itx, values)| { |
| 136 | let style = if show_avg_cpu && itx == 0 { |
| 137 | self.styles.avg_cpu_colour |
| 138 | } else { |
| 139 | self.styles.cpu_colour_styles |
| 140 | [(itx - show_avg_offset) % self.styles.cpu_colour_styles.len()] |
| 141 | }; |
| 142 | |
| 143 | GraphData::default().style(style).time(time).values(values) |
| 144 | }) |
| 145 | .rev() |
| 146 | .collect() |
| 147 | } else if let Some(CpuData { .. }) = cpu_entries.get(current_scroll_position - 1) { |
| 148 | // We generally subtract one from current scroll position because of the all |
| 149 | // entry. TODO: Do this a bit better (e.g. we can just do if let |
| 150 | // Some(_) = cpu_points.get()) |
| 151 | |
| 152 | let style = if show_avg_cpu && current_scroll_position == AVG_POSITION { |
| 153 | self.styles.avg_cpu_colour |
| 154 | } else { |
| 155 | let offset_position = current_scroll_position - 1; |
| 156 | self.styles.cpu_colour_styles |
| 157 | [(offset_position - show_avg_offset) % self.styles.cpu_colour_styles.len()] |
| 158 | }; |
| 159 | |
| 160 | vec![ |
| 161 | GraphData::default() |
| 162 | .style(style) |
| 163 | .time(time) |
| 164 | .values(&cpu_points[current_scroll_position - 1]), |
| 165 | ] |
| 166 | } else { |
| 167 | vec![] |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn draw_cpu_graph( |
| 172 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 173 | ) { |
| 174 | if let Some(cpu_widget_state) = app_state.states.cpu_state.widget_states.get_mut(&widget_id) |
| 175 | { |
| 176 | let data = app_state.data_store.get_data(); |
| 177 | |
| 178 | let hide_x_labels = should_hide_x_label( |
| 179 | app_state.app_config_fields.hide_time, |
| 180 | app_state.app_config_fields.autohide_time, |
| 181 | cpu_widget_state.graph.state_mut().autohide_timer_mut(), |
| 182 | draw_loc, |
| 183 | ); |
| 184 | |
| 185 | let graph_data = self.generate_points( |
| 186 | cpu_widget_state, |
| 187 | data, |
| 188 | app_state.app_config_fields.show_average_cpu, |
| 189 | ); |
| 190 | |
| 191 | // TODO: Maybe hide load avg if too long? Or maybe the CPU part. |
| 192 | let title = { |
| 193 | #[cfg(unix)] |
| 194 | { |
| 195 | let load_avg = &data.load_avg_harvest; |
| 196 | let load_avg_str = format!( |
| 197 | "─ {:.2} {:.2} {:.2} ", |
| 198 | load_avg[0], load_avg[1], load_avg[2] |
| 199 | ); |
| 200 | |
| 201 | concat_string::concat_string!(" CPU ", load_avg_str).into() |
| 202 | } |
| 203 | #[cfg(not(target_family = "unix"))] |
| 204 | { |
| 205 | " CPU ".into() |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id); |
| 210 | let marker = self.get_marker(app_state.app_config_fields.use_dot); |
| 211 | |
| 212 | cpu_widget_state.graph.draw( |
| 213 | f, |
| 214 | draw_loc, |
| 215 | GraphDrawCtx { |
| 216 | title, |
| 217 | border_style, |
| 218 | title_style: self.styles.widget_title_style, |
| 219 | graph_style: self.styles.graph_style, |
| 220 | general_widget_style: self.styles.general_widget_style, |
| 221 | border_type: self.styles.border_type, |
| 222 | marker, |
| 223 | hide_x_labels, |
| 224 | is_selected: app_state.current_widget.widget_id == widget_id, |
| 225 | is_expanded: app_state.is_expanded, |
| 226 | legend_position: None, |
| 227 | legend_constraints: None, |
| 228 | }, |
| 229 | graph_data, |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | fn draw_cpu_legend( |
| 235 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 236 | ) { |
| 237 | let recalculate_column_widths = app_state.should_get_widget_bounds(); |
| 238 | if let Some(cpu_widget_state) = app_state |
| 239 | .states |
| 240 | .cpu_state |
| 241 | .widget_states |
| 242 | .get_mut(&(widget_id - 1)) |
| 243 | { |
| 244 | // TODO: This line (and the one above, see caller) is pretty dumb but I guess |
| 245 | // needed for now. Refactor if possible! |
| 246 | cpu_widget_state.is_legend_hidden = false; |
| 247 | |
| 248 | let is_on_widget = widget_id == app_state.current_widget.widget_id; |
| 249 | |
| 250 | let draw_info = DrawInfo { |
| 251 | loc: draw_loc, |
| 252 | force_redraw: app_state.is_force_redraw, |
| 253 | recalculate_column_widths, |
| 254 | // TODO: Bug with this, shouldn't be selected on expand! |
| 255 | selection_state: SelectionState::new(app_state.is_expanded, is_on_widget), |
| 256 | }; |
| 257 | |
| 258 | cpu_widget_state.table.draw( |
| 259 | f, |
| 260 | &draw_info, |
| 261 | app_state.widget_map.get_mut(&widget_id), |
| 262 | self, |
| 263 | ); |
| 264 | } |
| 265 | } |
| 266 | } |