| 1 | use ratatui::{ |
| 2 | Frame, |
| 3 | layout::{Alignment, Constraint, Direction, Layout, Rect}, |
| 4 | text::{Line, Span}, |
| 5 | widgets::{Block, Paragraph}, |
| 6 | }; |
| 7 | |
| 8 | use crate::{ |
| 9 | app::{App, layout_manager::BottomWidgetType}, |
| 10 | canvas::Painter, |
| 11 | }; |
| 12 | |
| 13 | impl Painter { |
| 14 | pub fn draw_basic_table_arrows( |
| 15 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 16 | ) { |
| 17 | if let Some(current_table) = app_state.widget_map.get(&widget_id) { |
| 18 | let current_table = if let BottomWidgetType::ProcSort = current_table.widget_type { |
| 19 | current_table |
| 20 | .right_neighbour |
| 21 | .and_then(|id| app_state.widget_map.get(&id)) |
| 22 | .expect("id must exist in widget mapping") |
| 23 | } else { |
| 24 | current_table |
| 25 | }; |
| 26 | |
| 27 | let (left_table, right_table) = ( |
| 28 | { |
| 29 | current_table |
| 30 | .left_neighbour |
| 31 | .map(|left_widget_id| { |
| 32 | app_state |
| 33 | .widget_map |
| 34 | .get(&left_widget_id) |
| 35 | .map(|left_widget| { |
| 36 | if left_widget.widget_type == BottomWidgetType::ProcSort { |
| 37 | left_widget |
| 38 | .left_neighbour |
| 39 | .map(|left_left_widget_id| { |
| 40 | app_state.widget_map.get(&left_left_widget_id).map( |
| 41 | |left_left_widget| { |
| 42 | &left_left_widget.widget_type |
| 43 | }, |
| 44 | ) |
| 45 | }) |
| 46 | .unwrap_or(Some(&BottomWidgetType::Temp)) |
| 47 | .unwrap_or(&BottomWidgetType::Temp) |
| 48 | } else { |
| 49 | &left_widget.widget_type |
| 50 | } |
| 51 | }) |
| 52 | .unwrap_or(&BottomWidgetType::Temp) |
| 53 | }) |
| 54 | .unwrap_or(&BottomWidgetType::Temp) |
| 55 | }, |
| 56 | { |
| 57 | current_table |
| 58 | .right_neighbour |
| 59 | .map(|right_widget_id| { |
| 60 | app_state |
| 61 | .widget_map |
| 62 | .get(&right_widget_id) |
| 63 | .map(|right_widget| { |
| 64 | if right_widget.widget_type == BottomWidgetType::ProcSort { |
| 65 | right_widget |
| 66 | .right_neighbour |
| 67 | .map(|right_right_widget_id| { |
| 68 | app_state |
| 69 | .widget_map |
| 70 | .get(&right_right_widget_id) |
| 71 | .map(|right_right_widget| { |
| 72 | &right_right_widget.widget_type |
| 73 | }) |
| 74 | }) |
| 75 | .unwrap_or(Some(&BottomWidgetType::Disk)) |
| 76 | .unwrap_or(&BottomWidgetType::Disk) |
| 77 | } else { |
| 78 | &right_widget.widget_type |
| 79 | } |
| 80 | }) |
| 81 | .unwrap_or(&BottomWidgetType::Disk) |
| 82 | }) |
| 83 | .unwrap_or(&BottomWidgetType::Disk) |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | // TODO: I can do this text effect as just a border now! |
| 88 | let left_name = left_table.get_pretty_name(); |
| 89 | let right_name = right_table.get_pretty_name(); |
| 90 | let num_spaces = |
| 91 | usize::from(draw_loc.width).saturating_sub(6 + left_name.len() + right_name.len()); |
| 92 | let carousel_text_style = if widget_id == app_state.current_widget.widget_id { |
| 93 | self.styles.highlighted_border_style |
| 94 | } else { |
| 95 | self.styles.text_style |
| 96 | }; |
| 97 | |
| 98 | let left_arrow_text = vec![ |
| 99 | Line::default(), |
| 100 | Line::from(Span::styled(format!("◄ {left_name}"), carousel_text_style)), |
| 101 | ]; |
| 102 | |
| 103 | let right_arrow_text = vec![ |
| 104 | Line::default(), |
| 105 | Line::from(Span::styled(format!("{right_name} ►"), carousel_text_style)), |
| 106 | ]; |
| 107 | |
| 108 | let margined_draw_loc = Layout::default() |
| 109 | .direction(Direction::Horizontal) |
| 110 | .constraints([ |
| 111 | Constraint::Length(2 + left_name.len() as u16), |
| 112 | Constraint::Length(num_spaces as u16), |
| 113 | Constraint::Length(2 + right_name.len() as u16), |
| 114 | ]) |
| 115 | .horizontal_margin(1) |
| 116 | .split(draw_loc); |
| 117 | |
| 118 | // Done like this for now since it's easier to just manually paint instead of |
| 119 | // dealing with blocks. |
| 120 | f.buffer_mut() |
| 121 | .set_style(draw_loc, self.styles.general_widget_style); |
| 122 | |
| 123 | f.render_widget( |
| 124 | Paragraph::new(left_arrow_text).block(Block::default()), |
| 125 | margined_draw_loc[0], |
| 126 | ); |
| 127 | f.render_widget( |
| 128 | Paragraph::new(right_arrow_text) |
| 129 | .block(Block::default()) |
| 130 | .alignment(Alignment::Right), |
| 131 | margined_draw_loc[2], |
| 132 | ); |
| 133 | |
| 134 | if app_state.should_get_widget_bounds() { |
| 135 | // Some explanations for future readers: |
| 136 | // - The "height" as of writing of this entire widget is 2. If it's 1, it |
| 137 | // occasionally doesn't draw. |
| 138 | // - As such, the buttons are only on the lower part of this 2-high widget. |
| 139 | // - So, we want to only check at one location, the `draw_loc.y + 1`, and that's |
| 140 | // it. |
| 141 | // - But why is it "+2" then? Well, it's because I have a REALLY ugly hack |
| 142 | // for mouse button checking, since most button checks are of the form `(draw_loc.y + draw_loc.height)`, |
| 143 | // and the same for the x and width. Unfortunately, if you check using >= and <=, the outer bound is |
| 144 | // actually too large - so, we assume all of them are one too big and check via < (see |
| 145 | // https://github.com/ClementTsang/bottom/pull/459 for details). |
| 146 | // - So in other words, to make it simple, we keep this to a standard and |
| 147 | // overshoot by one here. |
| 148 | if let Some(basic_table) = &mut app_state.states.basic_table_widget_state { |
| 149 | basic_table.left_tlc = |
| 150 | Some((margined_draw_loc[0].x, margined_draw_loc[0].y + 1)); |
| 151 | basic_table.left_brc = Some(( |
| 152 | margined_draw_loc[0].x + margined_draw_loc[0].width, |
| 153 | margined_draw_loc[0].y + 2, |
| 154 | )); |
| 155 | basic_table.right_tlc = |
| 156 | Some((margined_draw_loc[2].x, margined_draw_loc[2].y + 1)); |
| 157 | basic_table.right_brc = Some(( |
| 158 | margined_draw_loc[2].x + margined_draw_loc[2].width, |
| 159 | margined_draw_loc[2].y + 2, |
| 160 | )); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |