diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-04-01 20:31:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-01 20:31:43 -0400 |
commit | 0b1d84fdf590bf8cacc5d0f94b67f63daa9b768a (patch) | |
tree | d2ff261148f9ef21f2e860af43f795df911c5c79 /src/canvas/widgets/disk_table.rs | |
parent | c1a19f960fc1b348dd4465fe9a4d698bed229c77 (diff) |
Add modularity to widget placement and inclusion (#95)
Diffstat (limited to 'src/canvas/widgets/disk_table.rs')
-rw-r--r-- | src/canvas/widgets/disk_table.rs | 194 |
1 files changed, 96 insertions, 98 deletions
diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index 2e4dbd2a..9b201bd9 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -8,7 +8,7 @@ use tui::{ }; use crate::{ - app::{self, WidgetPosition}, + app::{self}, canvas::{ drawing_utils::{get_start_position, get_variable_intrinsic_widths}, Painter, @@ -28,42 +28,38 @@ lazy_static! { pub trait DiskTableWidget { fn draw_disk_table<B: Backend>( &self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, draw_border: bool, + widget_id: u64, ); } impl DiskTableWidget for Painter { fn draw_disk_table<B: Backend>( &self, f: &mut Frame<'_, B>, app_state: &mut app::App, draw_loc: Rect, draw_border: bool, + widget_id: u64, ) { - let disk_data: &[Vec<String>] = &app_state.canvas_data.disk_data; - let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64; - let start_position = get_start_position( - num_rows, - &app_state.app_scroll_positions.scroll_direction, - &mut app_state - .app_scroll_positions - .disk_scroll_state - .previous_scroll_position, - app_state - .app_scroll_positions - .disk_scroll_state - .current_scroll_position, - app_state.is_resized, - ); + if let Some(disk_widget_state) = app_state.disk_state.widget_states.get_mut(&widget_id) { + let disk_data: &mut [Vec<String>] = &mut app_state.canvas_data.disk_data; + let num_rows = max(0, i64::from(draw_loc.height) - 5) as u64; + let start_position = get_start_position( + num_rows, + &disk_widget_state.scroll_state.scroll_direction, + &mut disk_widget_state.scroll_state.previous_scroll_position, + disk_widget_state.scroll_state.current_scroll_position, + app_state.is_resized, + ); - let sliced_vec = &disk_data[start_position as usize..]; - let mut disk_counter: i64 = 0; + let sliced_vec = &mut disk_data[start_position as usize..]; + let mut disk_counter: i64 = 0; - let disk_rows = sliced_vec.iter().map(|disk| { - Row::StyledData( - disk.iter(), - match app_state.current_widget_selected { - WidgetPosition::Disk => { + let current_widget_id = app_state.current_widget.widget_id; + let disk_rows = sliced_vec.iter().map(|disk| { + Row::StyledData( + disk.iter(), + if current_widget_id == widget_id + && disk_widget_state.scroll_state.current_scroll_position >= start_position + { if disk_counter as u64 - == app_state - .app_scroll_positions - .disk_scroll_state - .current_scroll_position + == disk_widget_state.scroll_state.current_scroll_position - start_position { disk_counter = -1; @@ -74,82 +70,84 @@ impl DiskTableWidget for Painter { } self.colours.text_style } - } - _ => self.colours.text_style, - }, - ) - }); + } else { + self.colours.text_style + }, + ) + }); - // Calculate widths - // TODO: [PRETTY] Ellipsis on strings? - let width = f64::from(draw_loc.width); - let width_ratios = [0.2, 0.15, 0.13, 0.13, 0.13, 0.13, 0.13]; - let variable_intrinsic_results = - get_variable_intrinsic_widths(width as u16, &width_ratios, &DISK_HEADERS_LENS); - let intrinsic_widths = &variable_intrinsic_results.0[0..variable_intrinsic_results.1]; + // Calculate widths + // TODO: [PRETTY] Ellipsis on strings? + let width = f64::from(draw_loc.width); + let width_ratios = [0.2, 0.15, 0.13, 0.13, 0.13, 0.13, 0.13]; + let variable_intrinsic_results = + get_variable_intrinsic_widths(width as u16, &width_ratios, &DISK_HEADERS_LENS); + let intrinsic_widths = &variable_intrinsic_results.0[0..variable_intrinsic_results.1]; - let title = if app_state.is_expanded { - const TITLE_BASE: &str = " Disk ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( - " Disk ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - result_title - } else if app_state.app_config_fields.use_basic_mode { - String::new() - } else { - " Disk ".to_string() - }; + let title = if app_state.is_expanded { + const TITLE_BASE: &str = " Disk ── Esc to go back "; + let repeat_num = max( + 0, + draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, + ); + let result_title = format!( + " Disk ─{}─ Esc to go back ", + "─".repeat(repeat_num as usize) + ); + result_title + } else if app_state.app_config_fields.use_basic_mode { + String::new() + } else { + " Disk ".to_string() + }; + + let border_and_title_style = if app_state.current_widget.widget_id == widget_id { + self.colours.highlighted_border_style + } else { + self.colours.border_style + }; - let disk_block = if draw_border { - Block::default() - .title(&title) - .title_style(if app_state.is_expanded { - match app_state.current_widget_selected { - WidgetPosition::Disk => self.colours.highlighted_border_style, - _ => self.colours.border_style, - } - } else { - self.colours.widget_title_style - }) - .borders(Borders::ALL) - .border_style(match app_state.current_widget_selected { - WidgetPosition::Disk => self.colours.highlighted_border_style, - _ => self.colours.border_style, - }) - } else { - match app_state.current_widget_selected { - WidgetPosition::Disk => Block::default() + let disk_block = if draw_border { + Block::default() + .title(&title) + .title_style(if app_state.is_expanded { + border_and_title_style + } else { + self.colours.widget_title_style + }) + .borders(Borders::ALL) + .border_style(border_and_title_style) + } else if app_state.current_widget.widget_id == widget_id { + Block::default() .borders(*SIDE_BORDERS) - .border_style(self.colours.highlighted_border_style), - _ => Block::default().borders(Borders::NONE), - } - }; + .border_style(self.colours.highlighted_border_style) + } else { + Block::default().borders(Borders::NONE) + }; - let margined_draw_loc = Layout::default() - .constraints([Constraint::Percentage(100)].as_ref()) - .horizontal_margin(match app_state.current_widget_selected { - WidgetPosition::Disk => 0, - _ if !draw_border => 1, - _ => 0, - }) - .direction(Direction::Horizontal) - .split(draw_loc); + let margined_draw_loc = Layout::default() + .constraints([Constraint::Percentage(100)].as_ref()) + .horizontal_margin( + if app_state.current_widget.widget_id == widget_id || draw_border { + 0 + } else { + 1 + }, + ) + .direction(Direction::Horizontal) + .split(draw_loc); - // Draw! - Table::new(DISK_HEADERS.iter(), disk_rows) - .block(disk_block) - .header_style(self.colours.table_header_style) - .widths( - &(intrinsic_widths - .iter() - .map(|calculated_width| Constraint::Length(*calculated_width as u16)) - .collect::<Vec<_>>()), - ) - .render(f, margined_draw_loc[0]); + // Draw! + Table::new(DISK_HEADERS.iter(), disk_rows) + .block(disk_block) + .header_style(self.colours.table_header_style) + .widths( + &(intrinsic_widths + .iter() + .map(|calculated_width| Constraint::Length(*calculated_width as u16)) + .collect::<Vec<_>>()), + ) + .render(f, margined_draw_loc[0]); + } } } |