| 1 | use ratatui::{ |
| 2 | Frame, |
| 3 | layout::{Alignment, Constraint, Direction, Layout, Rect}, |
| 4 | text::{Line, Span}, |
| 5 | widgets::Paragraph, |
| 6 | }; |
| 7 | |
| 8 | use crate::{ |
| 9 | app::App, |
| 10 | canvas::{ |
| 11 | Painter, |
| 12 | components::{ |
| 13 | data_table::{DrawInfo, SelectionState}, |
| 14 | search_input::build_query_spans, |
| 15 | }, |
| 16 | drawing_utils::widget_block, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | const SORT_MENU_WIDTH: u16 = 7; |
| 21 | |
| 22 | impl Painter { |
| 23 | /// Draws and handles all process-related drawing. Use this. |
| 24 | /// - `widget_id` here represents the widget ID of the process widget |
| 25 | /// itself! |
| 26 | pub fn draw_process( |
| 27 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 28 | ) { |
| 29 | if let Some(proc_widget_state) = app_state.states.proc_state.widget_states.get(&widget_id) { |
| 30 | let is_basic = app_state.app_config_fields.use_basic_mode; |
| 31 | let search_height = if !is_basic { 5 } else { 3 }; |
| 32 | let is_sort_open = proc_widget_state.is_sort_open; |
| 33 | |
| 34 | let mut proc_draw_loc = draw_loc; |
| 35 | if proc_widget_state.is_search_enabled() { |
| 36 | let processes_chunk = Layout::default() |
| 37 | .direction(Direction::Vertical) |
| 38 | .constraints([Constraint::Min(0), Constraint::Length(search_height)]) |
| 39 | .split(draw_loc); |
| 40 | proc_draw_loc = processes_chunk[0]; |
| 41 | |
| 42 | self.draw_search_field(f, app_state, processes_chunk[1], widget_id + 1); |
| 43 | } |
| 44 | |
| 45 | if is_sort_open { |
| 46 | let processes_chunk = Layout::default() |
| 47 | .direction(Direction::Horizontal) |
| 48 | .constraints([Constraint::Length(SORT_MENU_WIDTH + 4), Constraint::Min(0)]) |
| 49 | .split(proc_draw_loc); |
| 50 | proc_draw_loc = processes_chunk[1]; |
| 51 | |
| 52 | self.draw_sort_table(f, app_state, processes_chunk[0], widget_id + 2); |
| 53 | } |
| 54 | |
| 55 | self.draw_processes_table(f, app_state, proc_draw_loc, widget_id); |
| 56 | } |
| 57 | |
| 58 | if let Some(proc_widget_state) = app_state |
| 59 | .states |
| 60 | .proc_state |
| 61 | .widget_states |
| 62 | .get_mut(&widget_id) |
| 63 | { |
| 64 | // Reset redraw marker. |
| 65 | if proc_widget_state.force_rerender { |
| 66 | proc_widget_state.force_rerender = false; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Draws the process sort box. |
| 72 | /// - `widget_id` represents the widget ID of the process widget itself. |
| 73 | fn draw_processes_table( |
| 74 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 75 | ) { |
| 76 | let should_get_widget_bounds = app_state.should_get_widget_bounds(); |
| 77 | if let Some(proc_widget_state) = app_state |
| 78 | .states |
| 79 | .proc_state |
| 80 | .widget_states |
| 81 | .get_mut(&widget_id) |
| 82 | { |
| 83 | let recalculate_column_widths = |
| 84 | should_get_widget_bounds || proc_widget_state.force_rerender; |
| 85 | |
| 86 | let is_on_widget = widget_id == app_state.current_widget.widget_id; |
| 87 | |
| 88 | let draw_info = DrawInfo { |
| 89 | loc: draw_loc, |
| 90 | force_redraw: app_state.is_force_redraw, |
| 91 | recalculate_column_widths, |
| 92 | selection_state: SelectionState::new(app_state.is_expanded, is_on_widget), |
| 93 | }; |
| 94 | |
| 95 | proc_widget_state.table.draw( |
| 96 | f, |
| 97 | &draw_info, |
| 98 | app_state.widget_map.get_mut(&widget_id), |
| 99 | self, |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Draws the process search field. |
| 105 | /// - `widget_id` represents the widget ID of the search box itself --- NOT |
| 106 | /// the process widget state that is stored. |
| 107 | fn draw_search_field( |
| 108 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 109 | ) { |
| 110 | let is_basic = app_state.app_config_fields.use_basic_mode; |
| 111 | |
| 112 | if let Some(proc_widget_state) = app_state |
| 113 | .states |
| 114 | .proc_state |
| 115 | .widget_states |
| 116 | .get_mut(&(widget_id - 1)) |
| 117 | { |
| 118 | let is_selected = widget_id == app_state.current_widget.widget_id; |
| 119 | let num_columns = usize::from(draw_loc.width); |
| 120 | const SEARCH_TITLE: &str = "> "; |
| 121 | let offset = 4; |
| 122 | let available_width = if num_columns > (offset + 3) { |
| 123 | num_columns - offset |
| 124 | } else { |
| 125 | num_columns |
| 126 | }; |
| 127 | |
| 128 | proc_widget_state |
| 129 | .proc_search |
| 130 | .search_state |
| 131 | .input_field_state |
| 132 | .get_start_position(available_width, app_state.is_force_redraw); |
| 133 | |
| 134 | // TODO: [CURSOR] blinking cursor? |
| 135 | let query_with_cursor = build_query_spans( |
| 136 | &proc_widget_state.proc_search.search_state.input_field_state, |
| 137 | available_width, |
| 138 | is_selected, |
| 139 | self.styles.selected_text_style, |
| 140 | self.styles.text_style, |
| 141 | ); |
| 142 | |
| 143 | let mut search_text = vec![Line::from({ |
| 144 | let mut search_vec = vec![Span::styled( |
| 145 | SEARCH_TITLE, |
| 146 | if is_selected { |
| 147 | self.styles.table_header_style |
| 148 | } else { |
| 149 | self.styles.text_style |
| 150 | }, |
| 151 | )]; |
| 152 | search_vec.extend(query_with_cursor); |
| 153 | |
| 154 | search_vec |
| 155 | })]; |
| 156 | |
| 157 | // Text options shamelessly stolen from VS Code. |
| 158 | let case_style = if !proc_widget_state.proc_search.query_options.ignore_case { |
| 159 | self.styles.selected_text_style |
| 160 | } else { |
| 161 | self.styles.text_style |
| 162 | }; |
| 163 | |
| 164 | let whole_word_style = if proc_widget_state.proc_search.query_options.whole_word { |
| 165 | self.styles.selected_text_style |
| 166 | } else { |
| 167 | self.styles.text_style |
| 168 | }; |
| 169 | |
| 170 | let regex_style = if proc_widget_state.proc_search.query_options.use_regex { |
| 171 | self.styles.selected_text_style |
| 172 | } else { |
| 173 | self.styles.text_style |
| 174 | }; |
| 175 | |
| 176 | // TODO: [MOUSE] Mouse support for these in search |
| 177 | // TODO: [MOVEMENT] Movement support for these in search |
| 178 | let (case, whole, regex) = { |
| 179 | cfg_select! { |
| 180 | target_os = "macos" => { |
| 181 | ("Case(F1)", "Whole(F2)", "Regex(F3)") |
| 182 | } |
| 183 | _ => { |
| 184 | ("Case(Alt+C)", "Whole(Alt+W)", "Regex(Alt+R)") |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | let option_text = Line::from(vec![ |
| 189 | Span::styled(case, case_style), |
| 190 | Span::raw(" "), |
| 191 | Span::styled(whole, whole_word_style), |
| 192 | Span::raw(" "), |
| 193 | Span::styled(regex, regex_style), |
| 194 | ]); |
| 195 | |
| 196 | search_text.push(Line::from(Span::styled( |
| 197 | if let Some(err) = &proc_widget_state.proc_search.search_state.error_message { |
| 198 | err.as_str() |
| 199 | } else { |
| 200 | "" |
| 201 | }, |
| 202 | self.styles.invalid_query_style, |
| 203 | ))); |
| 204 | search_text.push(option_text); |
| 205 | |
| 206 | let current_border_style = |
| 207 | if proc_widget_state.proc_search.search_state.is_invalid_search { |
| 208 | self.styles.invalid_query_style |
| 209 | } else if is_selected { |
| 210 | self.styles.highlighted_border_style |
| 211 | } else { |
| 212 | self.styles.border_style |
| 213 | }; |
| 214 | |
| 215 | let process_search_block = { |
| 216 | let mut block = widget_block( |
| 217 | is_basic, |
| 218 | is_selected, |
| 219 | self.styles.border_type, |
| 220 | self.styles.general_widget_style, |
| 221 | ) |
| 222 | .border_style(current_border_style); |
| 223 | |
| 224 | if !is_basic { |
| 225 | block = block.title_top( |
| 226 | Line::styled(" Esc to close ", current_border_style).right_aligned(), |
| 227 | ) |
| 228 | } |
| 229 | |
| 230 | block |
| 231 | }; |
| 232 | |
| 233 | let margined_draw_loc = Layout::default() |
| 234 | .constraints([Constraint::Percentage(100)]) |
| 235 | .horizontal_margin(u16::from(is_basic && !is_selected)) |
| 236 | .direction(Direction::Horizontal) |
| 237 | .split(draw_loc)[0]; |
| 238 | |
| 239 | f.render_widget( |
| 240 | Paragraph::new(search_text) |
| 241 | .block(process_search_block) |
| 242 | .style(self.styles.text_style) |
| 243 | .alignment(Alignment::Left), |
| 244 | margined_draw_loc, |
| 245 | ); |
| 246 | |
| 247 | if app_state.should_get_widget_bounds() { |
| 248 | // Update draw loc in widget map |
| 249 | if let Some(widget) = app_state.widget_map.get_mut(&widget_id) { |
| 250 | widget.top_left_corner = Some((margined_draw_loc.x, margined_draw_loc.y)); |
| 251 | widget.bottom_right_corner = Some(( |
| 252 | margined_draw_loc.x + margined_draw_loc.width, |
| 253 | margined_draw_loc.y + margined_draw_loc.height, |
| 254 | )); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /// Draws the process sort box. |
| 261 | /// - `widget_id` represents the widget ID of the sort box itself --- NOT |
| 262 | /// the process widget state that is stored. |
| 263 | fn draw_sort_table( |
| 264 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 265 | ) { |
| 266 | let should_get_widget_bounds = app_state.should_get_widget_bounds(); |
| 267 | if let Some(pws) = app_state |
| 268 | .states |
| 269 | .proc_state |
| 270 | .widget_states |
| 271 | .get_mut(&(widget_id - 2)) |
| 272 | { |
| 273 | let recalculate_column_widths = should_get_widget_bounds || pws.force_rerender; |
| 274 | |
| 275 | let is_on_widget = widget_id == app_state.current_widget.widget_id; |
| 276 | |
| 277 | let draw_info = DrawInfo { |
| 278 | loc: draw_loc, |
| 279 | force_redraw: app_state.is_force_redraw, |
| 280 | recalculate_column_widths, |
| 281 | selection_state: SelectionState::new(app_state.is_expanded, is_on_widget), |
| 282 | }; |
| 283 | |
| 284 | pws.sort_table.draw( |
| 285 | f, |
| 286 | &draw_info, |
| 287 | app_state.widget_map.get_mut(&widget_id), |
| 288 | self, |
| 289 | ); |
| 290 | } |
| 291 | } |
| 292 | } |