Skip to content

Commit

Permalink
Pretty sure I fixed scroll resizing...
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Feb 20, 2020
1 parent 8cf5b42 commit 67c6984
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
41 changes: 7 additions & 34 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -325,6 +327,7 @@ impl App {
}
} else if self.is_expanded {
self.is_expanded = false;
self.is_resized = true;
}
}

Expand Down Expand Up @@ -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::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;
WidgetPosition::ProcessSearch => {}
_ => {
self.is_expanded = true;
self.is_resized = true;
}
_ => {}
}
self.is_expanded = true;
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rect>,
middle_dialog_chunk: Vec<Rect>,
vertical_chunks: Vec<Rect>,
Expand Down Expand Up @@ -146,6 +146,17 @@ impl Painter {
pub fn draw_data<B: backend::Backend>(
&mut self, terminal: &mut Terminal<B>, 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 {
Expand Down Expand Up @@ -519,6 +530,8 @@ impl Painter {
}
})?;

app_state.is_resized = false;

Ok(())
}

Expand Down Expand Up @@ -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..];
Expand Down Expand Up @@ -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..]);
Expand Down Expand Up @@ -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..];
Expand Down Expand Up @@ -1285,6 +1301,7 @@ impl Painter {
.app_scroll_positions
.process_scroll_state
.current_scroll_position,
app_state.is_resized,
);

// Sanity check
Expand Down
17 changes: 8 additions & 9 deletions src/canvas/drawing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 67c6984

Please sign in to comment.