From 67c6984406dd30a8f516a08c8e250e4da1e27d68 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 19 Feb 2020 19:56:04 -0500 Subject: [PATCH] Pretty sure I fixed scroll resizing... --- src/app.rs | 41 +++++++------------------------------ src/canvas.rs | 21 +++++++++++++++++-- src/canvas/drawing_utils.rs | 17 ++++++++------- 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/src/app.rs b/src/app.rs index 06212c71..6bf65675 100644 --- a/src/app.rs +++ b/src/app.rs @@ -233,6 +233,7 @@ pub struct App { pub help_dialog_state: AppHelpDialogState, pub app_config_fields: AppConfigFields, pub is_expanded: bool, + pub is_resized: bool, pub cpu_state: CpuState, pub mem_state: MemState, pub net_state: NetworkState, @@ -275,6 +276,7 @@ impl App { show_disabled_data, }, is_expanded: false, + is_resized: false, cpu_state: CpuState::default(), mem_state: MemState::default(), net_state: NetworkState::default(), @@ -325,6 +327,7 @@ impl App { } } else if self.is_expanded { self.is_expanded = false; + self.is_resized = true; } } @@ -540,43 +543,13 @@ impl App { } else if !self.is_in_dialog() { // Pop-out mode. We ignore if in process search. - // TODO: [FIX] This is a temporary workaround for scroll not being proper with expanded (and resizing overall). match self.current_widget_selected { - WidgetPosition::Process => { - self.app_scroll_positions - .process_scroll_state - .current_scroll_position = 0; - self.app_scroll_positions - .process_scroll_state - .previous_scroll_position = 0; + WidgetPosition::ProcessSearch => {} + _ => { + self.is_expanded = true; + self.is_resized = true; } - WidgetPosition::Cpu => { - self.app_scroll_positions - .cpu_scroll_state - .current_scroll_position = 0; - self.app_scroll_positions - .cpu_scroll_state - .previous_scroll_position = 0; - } - WidgetPosition::Temp => { - self.app_scroll_positions - .temp_scroll_state - .current_scroll_position = 0; - self.app_scroll_positions - .temp_scroll_state - .previous_scroll_position = 0; - } - WidgetPosition::Disk => { - self.app_scroll_positions - .disk_scroll_state - .current_scroll_position = 0; - self.app_scroll_positions - .disk_scroll_state - .previous_scroll_position = 0; - } - _ => {} } - self.is_expanded = true; } } diff --git a/src/canvas.rs b/src/canvas.rs index 85293c89..cef90a66 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -83,8 +83,8 @@ pub struct DisplayableData { #[derive(Default)] /// Handles the canvas' state. TODO: [OPT] implement this. pub struct Painter { - height: f64, - width: f64, + height: u16, + width: u16, vertical_dialog_chunk: Vec, middle_dialog_chunk: Vec, vertical_chunks: Vec, @@ -146,6 +146,17 @@ impl Painter { pub fn draw_data( &mut self, terminal: &mut Terminal, app_state: &mut app::App, ) -> error::Result<()> { + let terminal_size = terminal.size()?; + let current_height = terminal_size.height; + let current_width = terminal_size.width; + + if self.height == 0 && self.width == 0 { + self.height = current_height; + self.width = current_width; + } else if self.height != current_height || self.width != current_width { + app_state.is_resized = true; + } + terminal.autoresize()?; terminal.draw(|mut f| { if app_state.help_dialog_state.is_showing_help { @@ -519,6 +530,8 @@ impl Painter { } })?; + app_state.is_resized = false; + Ok(()) } @@ -612,6 +625,7 @@ impl Painter { .app_scroll_positions .cpu_scroll_state .current_scroll_position, + app_state.is_resized, ); let sliced_cpu_data = &cpu_data[start_position as usize..]; @@ -950,6 +964,7 @@ impl Painter { .app_scroll_positions .temp_scroll_state .current_scroll_position, + app_state.is_resized, ); let sliced_vec = &(temp_sensor_data[start_position as usize..]); @@ -1045,6 +1060,7 @@ impl Painter { .app_scroll_positions .disk_scroll_state .current_scroll_position, + app_state.is_resized, ); let sliced_vec = &disk_data[start_position as usize..]; @@ -1285,6 +1301,7 @@ impl Painter { .app_scroll_positions .process_scroll_state .current_scroll_position, + app_state.is_resized, ); // Sanity check diff --git a/src/canvas/drawing_utils.rs b/src/canvas/drawing_utils.rs index 5629eb93..ef4a441f 100644 --- a/src/canvas/drawing_utils.rs +++ b/src/canvas/drawing_utils.rs @@ -72,16 +72,12 @@ pub fn get_variable_intrinsic_widths( pub fn get_start_position( num_rows: u64, scroll_direction: &app::ScrollDirection, scroll_position_bar: &mut u64, - currently_selected_position: u64, + currently_selected_position: u64, is_resized: bool, ) -> u64 { - // TODO: [FIX] Scroll does NOT work with expanded (and resizing overall). - // if currently_selected_position >= *scroll_position_bar - // && num_rows > (currently_selected_position - *scroll_position_bar + num_rows) - // { - // *scroll_position_bar = - // std::cmp::max(0, currently_selected_position as i64 - num_rows as i64) as u64; - // debug!("Scroll bar: {}", *scroll_position_bar); - // } + if is_resized { + *scroll_position_bar = 0; + } + match scroll_direction { app::ScrollDirection::DOWN => { if currently_selected_position < *scroll_position_bar + num_rows { @@ -103,6 +99,9 @@ pub fn get_start_position( // If it's past the first element, then show from that element downwards *scroll_position_bar = currently_selected_position; *scroll_position_bar + } else if currently_selected_position >= *scroll_position_bar + num_rows { + *scroll_position_bar = currently_selected_position - num_rows; + *scroll_position_bar } else { // Else, don't change what our start position is from whatever it is set to! *scroll_position_bar