| 1 | //! Code related to drawing. |
| 2 | //! |
| 3 | //! Note that eventually this should not contain any widget-specific draw code, |
| 4 | //! but rather just generic code or components. |
| 5 | |
| 6 | pub mod components; |
| 7 | pub mod dialogs; |
| 8 | mod drawing_utils; |
| 9 | mod widgets; |
| 10 | |
| 11 | use ratatui::{ |
| 12 | Frame, Terminal, |
| 13 | backend::Backend, |
| 14 | layout::{Constraint, Direction, Flex, Layout, Position, Rect}, |
| 15 | style::Style, |
| 16 | symbols::Marker, |
| 17 | text::Span, |
| 18 | widgets::Paragraph, |
| 19 | }; |
| 20 | |
| 21 | use crate::{ |
| 22 | app::{ |
| 23 | App, |
| 24 | layout_manager::{BottomColRow, BottomLayout, BottomWidgetType}, |
| 25 | }, |
| 26 | constants::*, |
| 27 | options::config::style::Styles, |
| 28 | }; |
| 29 | |
| 30 | /// Handles the canvas' state. |
| 31 | pub struct Painter { |
| 32 | pub styles: Styles, |
| 33 | |
| 34 | /// Used to know whether to invalidate things. |
| 35 | previous_height: u16, |
| 36 | |
| 37 | /// Used to know whether to invalidate things. |
| 38 | previous_width: u16, |
| 39 | |
| 40 | /// The layout. |
| 41 | layout: BottomLayout, |
| 42 | } |
| 43 | |
| 44 | impl Painter { |
| 45 | pub fn init(layout: BottomLayout, styling: Styles) -> anyhow::Result<Self> { |
| 46 | let painter = Painter { |
| 47 | styles: styling, |
| 48 | previous_height: 0, |
| 49 | previous_width: 0, |
| 50 | layout, |
| 51 | }; |
| 52 | |
| 53 | Ok(painter) |
| 54 | } |
| 55 | |
| 56 | /// Determines the border style. |
| 57 | pub fn get_border_style(&self, widget_id: u64, selected_widget_id: u64) -> Style { |
| 58 | let is_on_widget = widget_id == selected_widget_id; |
| 59 | if is_on_widget { |
| 60 | self.styles.highlighted_border_style |
| 61 | } else { |
| 62 | self.styles.border_style |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub(crate) fn get_marker(&self, use_dot: bool) -> Marker { |
| 67 | if use_dot { |
| 68 | Marker::Dot |
| 69 | } else { |
| 70 | Marker::Braille |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn draw_frozen_indicator(&self, f: &mut Frame<'_>, draw_loc: Rect) { |
| 75 | f.render_widget( |
| 76 | Paragraph::new(Span::styled( |
| 77 | "Frozen, press 'f' to unfreeze", |
| 78 | self.styles.selected_text_style, |
| 79 | )), |
| 80 | Layout::default() |
| 81 | .horizontal_margin(1) |
| 82 | .constraints([Constraint::Length(1)]) |
| 83 | .split(draw_loc)[0], |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | pub fn draw_data<B: Backend>( |
| 88 | &mut self, terminal: &mut Terminal<B>, app_state: &mut App, |
| 89 | ) -> Result<(), B::Error> { |
| 90 | use BottomWidgetType::*; |
| 91 | |
| 92 | terminal.draw(|f| { |
| 93 | let (terminal_size, frozen_draw_loc) = if app_state.data_store.is_frozen() { |
| 94 | // TODO: Remove built-in cache? |
| 95 | let split_loc = Layout::default() |
| 96 | .constraints([Constraint::Min(0), Constraint::Length(1)]) |
| 97 | .split(f.area()); |
| 98 | (split_loc[0], Some(split_loc[1])) |
| 99 | } else { |
| 100 | (f.area(), None) |
| 101 | }; |
| 102 | let terminal_height = terminal_size.height; |
| 103 | let terminal_width = terminal_size.width; |
| 104 | |
| 105 | if (self.previous_height == 0 && self.previous_width == 0) |
| 106 | || (self.previous_height != terminal_height |
| 107 | || self.previous_width != terminal_width) |
| 108 | { |
| 109 | app_state.is_force_redraw = true; |
| 110 | self.previous_height = terminal_height; |
| 111 | self.previous_width = terminal_width; |
| 112 | } |
| 113 | |
| 114 | // TODO: We should probably remove this or make it done elsewhere, not the |
| 115 | // responsibility of the app. |
| 116 | if app_state.should_get_widget_bounds() { |
| 117 | // If we're force drawing, reset ALL mouse boundaries. |
| 118 | for widget in app_state.widget_map.values_mut() { |
| 119 | widget.top_left_corner = None; |
| 120 | widget.bottom_right_corner = None; |
| 121 | } |
| 122 | |
| 123 | // Reset process kill dialog button locations... |
| 124 | app_state.process_kill_dialog.handle_redraw(); |
| 125 | |
| 126 | // Reset battery dialog button locations... |
| 127 | for battery_widget in app_state.states.battery_state.widget_states.values_mut() { |
| 128 | battery_widget.tab_click_locs = None; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // TODO: Make drawing dialog generic. |
| 133 | if app_state.help_dialog_state.is_showing_help { |
| 134 | let area = f.area(); |
| 135 | f.buffer_mut() |
| 136 | .set_style(area, self.styles.general_widget_style); |
| 137 | |
| 138 | let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3; |
| 139 | let border_len = terminal_height.saturating_sub(gen_help_len) / 2; |
| 140 | let [_, vertical_dialog_chunk, _] = Layout::default() |
| 141 | .direction(Direction::Vertical) |
| 142 | .constraints([ |
| 143 | Constraint::Length(border_len), |
| 144 | Constraint::Length(gen_help_len), |
| 145 | Constraint::Length(border_len), |
| 146 | ]) |
| 147 | .areas(terminal_size); |
| 148 | |
| 149 | // An approximate proxy for the max line length to use. |
| 150 | const MAX_TEXT_LENGTH: u16 = const { |
| 151 | let mut max = 0; |
| 152 | |
| 153 | let mut i = 0; |
| 154 | while i < HELP_TEXT.len() { |
| 155 | let section = HELP_TEXT[i]; |
| 156 | let mut j = 0; |
| 157 | while j < section.len() { |
| 158 | let line = section[j]; |
| 159 | if line.len() > max { |
| 160 | max = line.len(); |
| 161 | } |
| 162 | |
| 163 | j += 1; |
| 164 | } |
| 165 | |
| 166 | i += 1; |
| 167 | } |
| 168 | |
| 169 | max as u16 |
| 170 | }; |
| 171 | |
| 172 | let dialog_width = vertical_dialog_chunk.width; |
| 173 | let [middle_dialog_chunk] = if dialog_width < MAX_TEXT_LENGTH { |
| 174 | Layout::default() |
| 175 | .direction(Direction::Horizontal) |
| 176 | .constraints([Constraint::Percentage(100)]) |
| 177 | .areas(vertical_dialog_chunk) |
| 178 | } else { |
| 179 | // We calculate this so that the margins never have to split an odd number. |
| 180 | let len = if (dialog_width.saturating_sub(MAX_TEXT_LENGTH)) % 2 == 0 { |
| 181 | MAX_TEXT_LENGTH |
| 182 | } else { |
| 183 | // It can only be 1 if the difference is greater than 1, so this is fine. |
| 184 | MAX_TEXT_LENGTH + 1 |
| 185 | }; |
| 186 | |
| 187 | Layout::default() |
| 188 | .direction(Direction::Horizontal) |
| 189 | .constraints([Constraint::Length(len)]) |
| 190 | .flex(Flex::SpaceAround) |
| 191 | .areas(vertical_dialog_chunk) |
| 192 | }; |
| 193 | |
| 194 | self.draw_help_dialog(f, app_state, middle_dialog_chunk); |
| 195 | } else if app_state.process_kill_dialog.is_open() { |
| 196 | let area = f.area(); |
| 197 | f.buffer_mut() |
| 198 | .set_style(area, self.styles.general_widget_style); |
| 199 | |
| 200 | // FIXME: For width, just limit to a max size or full width. For height, not |
| 201 | // sure. Maybe pass max and let child handle? |
| 202 | let horizontal_padding = if terminal_width < 100 { 0 } else { 5 }; |
| 203 | let vertical_padding = if terminal_height < 100 { 0 } else { 5 }; |
| 204 | |
| 205 | let vertical_dialog_chunk = Layout::default() |
| 206 | .direction(Direction::Vertical) |
| 207 | .constraints([ |
| 208 | Constraint::Length(vertical_padding), |
| 209 | Constraint::Fill(1), |
| 210 | Constraint::Length(vertical_padding), |
| 211 | ]) |
| 212 | .areas::<3>(terminal_size)[1]; |
| 213 | |
| 214 | let dialog_draw_area = Layout::default() |
| 215 | .direction(Direction::Horizontal) |
| 216 | .constraints([ |
| 217 | Constraint::Length(horizontal_padding), |
| 218 | Constraint::Fill(1), |
| 219 | Constraint::Length(horizontal_padding), |
| 220 | ]) |
| 221 | .areas::<3>(vertical_dialog_chunk)[1]; |
| 222 | |
| 223 | app_state |
| 224 | .process_kill_dialog |
| 225 | .draw(f, dialog_draw_area, &self.styles); |
| 226 | } else if app_state.is_expanded { |
| 227 | if let Some(frozen_draw_loc) = frozen_draw_loc { |
| 228 | self.draw_frozen_indicator(f, frozen_draw_loc); |
| 229 | } |
| 230 | |
| 231 | let rect = Layout::default() |
| 232 | .margin(0) |
| 233 | .constraints([Constraint::Percentage(100)]) |
| 234 | .split(terminal_size); |
| 235 | match &app_state.current_widget.widget_type { |
| 236 | Cpu => self.draw_cpu(f, app_state, rect[0], app_state.current_widget.widget_id), |
| 237 | CpuLegend => self.draw_cpu( |
| 238 | f, |
| 239 | app_state, |
| 240 | rect[0], |
| 241 | app_state.current_widget.widget_id - 1, |
| 242 | ), |
| 243 | Mem | BasicMem => self.draw_memory_graph( |
| 244 | f, |
| 245 | app_state, |
| 246 | rect[0], |
| 247 | app_state.current_widget.widget_id, |
| 248 | ), |
| 249 | Disk => self.draw_disk_table( |
| 250 | f, |
| 251 | app_state, |
| 252 | rect[0], |
| 253 | app_state.current_widget.widget_id, |
| 254 | ), |
| 255 | Temp => self.draw_temp_table( |
| 256 | f, |
| 257 | app_state, |
| 258 | rect[0], |
| 259 | app_state.current_widget.widget_id, |
| 260 | ), |
| 261 | Net => { |
| 262 | self.draw_network(f, app_state, rect[0], app_state.current_widget.widget_id) |
| 263 | } |
| 264 | Proc | ProcSearch | ProcSort => { |
| 265 | let widget_id = app_state.current_widget.widget_id |
| 266 | - match &app_state.current_widget.widget_type { |
| 267 | ProcSearch => 1, |
| 268 | ProcSort => 2, |
| 269 | _ => 0, |
| 270 | }; |
| 271 | |
| 272 | self.draw_process(f, app_state, rect[0], widget_id); |
| 273 | } |
| 274 | Battery => |
| 275 | { |
| 276 | #[cfg(feature = "battery")] |
| 277 | self.draw_battery(f, app_state, rect[0], app_state.current_widget.widget_id) |
| 278 | } |
| 279 | TempGraph => self.draw_temperature_graph( |
| 280 | f, |
| 281 | app_state, |
| 282 | rect[0], |
| 283 | app_state.current_widget.widget_id, |
| 284 | ), |
| 285 | DiskIoGraph => self.draw_disk_io_graph( |
| 286 | f, |
| 287 | app_state, |
| 288 | rect[0], |
| 289 | app_state.current_widget.widget_id, |
| 290 | ), |
| 291 | _ => {} |
| 292 | } |
| 293 | } else if app_state.app_config_fields.use_basic_mode { |
| 294 | // Basic mode. This basically removes all graphs but otherwise |
| 295 | // the same info. |
| 296 | if let Some(frozen_draw_loc) = frozen_draw_loc { |
| 297 | self.draw_frozen_indicator(f, frozen_draw_loc); |
| 298 | } |
| 299 | |
| 300 | let data = app_state.data_store.get_data(); |
| 301 | let actual_cpu_data_len = data.cpu_harvest.len(); |
| 302 | |
| 303 | // This fixes #397, apparently if the height is 1, it can't render the CPU |
| 304 | // bars... |
| 305 | let cpu_height = { |
| 306 | let c = (actual_cpu_data_len / 4) as u16 |
| 307 | + u16::from(!actual_cpu_data_len.is_multiple_of(4)) |
| 308 | + u16::from( |
| 309 | app_state.app_config_fields.show_average_cpu |
| 310 | && app_state.app_config_fields.dedicated_average_row |
| 311 | && !actual_cpu_data_len.saturating_sub(1).is_multiple_of(4), |
| 312 | ); |
| 313 | |
| 314 | if c <= 1 { 1 } else { c } |
| 315 | }; |
| 316 | |
| 317 | let mut mem_rows = 1; |
| 318 | |
| 319 | if data.swap_harvest.is_some() { |
| 320 | mem_rows += 1; // add row for swap |
| 321 | } |
| 322 | |
| 323 | #[cfg(feature = "zfs")] |
| 324 | { |
| 325 | if data.arc_harvest.is_some() { |
| 326 | mem_rows += 1; // add row for arc |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #[cfg(not(target_os = "windows"))] |
| 331 | { |
| 332 | if data.cache_harvest.is_some() { |
| 333 | mem_rows += 1; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | #[cfg(feature = "gpu")] |
| 338 | { |
| 339 | mem_rows += data.gpu_harvest.len() as u16; // add row(s) for gpu |
| 340 | } |
| 341 | |
| 342 | let network_rows = if app_state.app_config_fields.network_show_packets { |
| 343 | 4 // 4 rows for RX/TX and Packet Rates (Avg sizes moved to right side) |
| 344 | } else { |
| 345 | 2 // 2 rows for RX and TX |
| 346 | }; |
| 347 | |
| 348 | if mem_rows < network_rows { |
| 349 | mem_rows += network_rows - mem_rows; // min rows |
| 350 | } |
| 351 | |
| 352 | let vertical_chunks = Layout::default() |
| 353 | .direction(Direction::Vertical) |
| 354 | .margin(0) |
| 355 | .constraints([ |
| 356 | Constraint::Length(cpu_height), |
| 357 | Constraint::Length(mem_rows), |
| 358 | Constraint::Length(2), |
| 359 | Constraint::Min(5), |
| 360 | ]) |
| 361 | .split(terminal_size); |
| 362 | |
| 363 | let middle_chunks = Layout::default() |
| 364 | .direction(Direction::Horizontal) |
| 365 | .constraints([Constraint::Percentage(50), Constraint::Percentage(50)]) |
| 366 | .split(vertical_chunks[1]); |
| 367 | |
| 368 | if vertical_chunks[0].width >= 2 { |
| 369 | self.draw_basic_cpu(f, app_state, vertical_chunks[0], 1); |
| 370 | } |
| 371 | if middle_chunks[0].width >= 2 { |
| 372 | self.draw_basic_memory(f, app_state, middle_chunks[0], 2); |
| 373 | } |
| 374 | if middle_chunks[1].width >= 2 { |
| 375 | self.draw_basic_network(f, app_state, middle_chunks[1], 3); |
| 376 | } |
| 377 | |
| 378 | let mut later_widget_id: Option<u64> = None; |
| 379 | if let Some(basic_table_widget_state) = &app_state.states.basic_table_widget_state { |
| 380 | let widget_id = basic_table_widget_state.currently_displayed_widget_id; |
| 381 | later_widget_id = Some(widget_id); |
| 382 | if vertical_chunks[3].width >= 2 { |
| 383 | match basic_table_widget_state.currently_displayed_widget_type { |
| 384 | Disk => { |
| 385 | self.draw_disk_table(f, app_state, vertical_chunks[3], widget_id) |
| 386 | } |
| 387 | Proc | ProcSort => { |
| 388 | let wid = widget_id |
| 389 | - match basic_table_widget_state.currently_displayed_widget_type |
| 390 | { |
| 391 | ProcSearch => 1, |
| 392 | ProcSort => 2, |
| 393 | _ => 0, |
| 394 | }; |
| 395 | self.draw_process(f, app_state, vertical_chunks[3], wid); |
| 396 | } |
| 397 | Temp => { |
| 398 | self.draw_temp_table(f, app_state, vertical_chunks[3], widget_id) |
| 399 | } |
| 400 | Battery => |
| 401 | { |
| 402 | #[cfg(feature = "battery")] |
| 403 | self.draw_battery(f, app_state, vertical_chunks[3], widget_id) |
| 404 | } |
| 405 | TempGraph => self.draw_temperature_graph( |
| 406 | f, |
| 407 | app_state, |
| 408 | vertical_chunks[3], |
| 409 | widget_id, |
| 410 | ), |
| 411 | DiskIoGraph => { |
| 412 | self.draw_disk_io_graph(f, app_state, vertical_chunks[3], widget_id) |
| 413 | } |
| 414 | _ => {} |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if let Some(widget_id) = later_widget_id { |
| 420 | self.draw_basic_table_arrows(f, app_state, vertical_chunks[2], widget_id); |
| 421 | } |
| 422 | } else { |
| 423 | // Draws using the passed in (or default) layout. |
| 424 | if let Some(frozen_draw_loc) = frozen_draw_loc { |
| 425 | self.draw_frozen_indicator(f, frozen_draw_loc); |
| 426 | } |
| 427 | |
| 428 | // A two-pass algorithm - get layouts using constraints (first pass), |
| 429 | // then pass each layout to the corresponding widget (second pass). |
| 430 | // Note that layouts are already cached in ratatui, so we don't need |
| 431 | // to do it manually! |
| 432 | let base = Layout::vertical(self.layout.rows.iter().map(|r| r.constraint)) |
| 433 | .split(terminal_size); |
| 434 | |
| 435 | for (br, base) in self.layout.rows.iter().zip(base.iter()) { |
| 436 | let base = |
| 437 | Layout::horizontal(br.children.iter().map(|bc| bc.constraint)).split(*base); |
| 438 | |
| 439 | for (bc, base) in br.children.iter().zip(base.iter()) { |
| 440 | let base = Layout::vertical(bc.children.iter().map(|bcr| bcr.constraint)) |
| 441 | .split(*base); |
| 442 | |
| 443 | for (widgets, base) in bc.children.iter().zip(base.iter()) { |
| 444 | let widget_draw_locs = |
| 445 | Layout::horizontal(widgets.children.iter().map(|bw| bw.constraint)) |
| 446 | .split(*base); |
| 447 | |
| 448 | self.draw_widgets_with_constraints( |
| 449 | f, |
| 450 | app_state, |
| 451 | widgets, |
| 452 | &widget_draw_locs, |
| 453 | ); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | })?; |
| 459 | |
| 460 | // We also move it back to the origin to try rand avoid wasting CPU cycles for things like kitty's |
| 461 | // `cursor_trail` calculations. |
| 462 | let backend = terminal.backend_mut(); |
| 463 | backend.set_cursor_position(Position::ORIGIN)?; |
| 464 | backend.flush()?; |
| 465 | |
| 466 | if let Some(updated_current_widget) = app_state |
| 467 | .widget_map |
| 468 | .get(&app_state.current_widget.widget_id) |
| 469 | { |
| 470 | app_state.current_widget = updated_current_widget.clone(); |
| 471 | } |
| 472 | |
| 473 | app_state.is_force_redraw = false; |
| 474 | app_state.is_determining_widget_boundary = false; |
| 475 | |
| 476 | Ok(()) |
| 477 | } |
| 478 | |
| 479 | fn draw_widgets_with_constraints( |
| 480 | &self, f: &mut Frame<'_>, app_state: &mut App, widgets: &BottomColRow, |
| 481 | widget_draw_locs: &[Rect], |
| 482 | ) { |
| 483 | use BottomWidgetType::*; |
| 484 | for (widget, draw_loc) in widgets.children.iter().zip(widget_draw_locs) { |
| 485 | if draw_loc.width >= 2 && draw_loc.height >= 2 { |
| 486 | match &widget.widget_type { |
| 487 | Cpu => self.draw_cpu(f, app_state, *draw_loc, widget.widget_id), |
| 488 | Mem => self.draw_memory_graph(f, app_state, *draw_loc, widget.widget_id), |
| 489 | Net => self.draw_network(f, app_state, *draw_loc, widget.widget_id), |
| 490 | Temp => self.draw_temp_table(f, app_state, *draw_loc, widget.widget_id), |
| 491 | Disk => self.draw_disk_table(f, app_state, *draw_loc, widget.widget_id), |
| 492 | Proc => self.draw_process(f, app_state, *draw_loc, widget.widget_id), |
| 493 | Battery => |
| 494 | { |
| 495 | #[cfg(feature = "battery")] |
| 496 | self.draw_battery(f, app_state, *draw_loc, widget.widget_id) |
| 497 | } |
| 498 | TempGraph => { |
| 499 | self.draw_temperature_graph(f, app_state, *draw_loc, widget.widget_id) |
| 500 | } |
| 501 | DiskIoGraph => { |
| 502 | self.draw_disk_io_graph(f, app_state, *draw_loc, widget.widget_id) |
| 503 | } |
| 504 | _ => {} |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |