refactor: fix off-by-one bug with getting start position
This commit is contained in:
parent
df1a418327
commit
9e63642e9c
41
src/app.rs
41
src/app.rs
|
@ -1111,12 +1111,8 @@ impl App {
|
|||
&self.current_widget.bottom_right_corner,
|
||||
) {
|
||||
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
|
||||
let header_gap_offset = 1 + if self.is_drawing_gap(&self.current_widget) {
|
||||
self.app_config_fields.table_gap
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let height = brc_y - tlc_y - 2 * border_offset - header_gap_offset;
|
||||
let header_offset = self.header_offset(&self.current_widget);
|
||||
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
|
||||
self.change_position_count(-(height as i64));
|
||||
}
|
||||
}
|
||||
|
@ -1138,12 +1134,8 @@ impl App {
|
|||
&self.current_widget.bottom_right_corner,
|
||||
) {
|
||||
let border_offset = if self.is_drawing_border() { 1 } else { 0 };
|
||||
let header_gap_offset = 1 + if self.is_drawing_gap(&self.current_widget) {
|
||||
self.app_config_fields.table_gap
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let height = brc_y - tlc_y - 2 * border_offset - header_gap_offset;
|
||||
let header_offset = self.header_offset(&self.current_widget);
|
||||
let height = brc_y - tlc_y - 2 * border_offset - header_offset;
|
||||
self.change_position_count(height as i64);
|
||||
}
|
||||
}
|
||||
|
@ -2908,13 +2900,8 @@ impl App {
|
|||
| BottomWidgetType::Disk => {
|
||||
// Get our index...
|
||||
let clicked_entry = y - *tlc_y;
|
||||
// + 1 so we start at 0.
|
||||
let header_gap_offset = 1 + if self.is_drawing_gap(&self.current_widget) {
|
||||
self.app_config_fields.table_gap
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let offset = border_offset + header_gap_offset;
|
||||
let header_offset = self.header_offset(&self.current_widget);
|
||||
let offset = border_offset + header_offset;
|
||||
if clicked_entry >= offset {
|
||||
let offset_clicked_entry = clicked_entry - offset;
|
||||
match &self.current_widget.widget_type {
|
||||
|
@ -3083,13 +3070,23 @@ impl App {
|
|||
self.is_expanded || !self.app_config_fields.use_basic_mode
|
||||
}
|
||||
|
||||
fn is_drawing_gap(&self, widget: &BottomWidget) -> bool {
|
||||
fn header_offset(&self, widget: &BottomWidget) -> u16 {
|
||||
if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) =
|
||||
(widget.top_left_corner, widget.bottom_right_corner)
|
||||
{
|
||||
brc_y - tlc_y >= constants::TABLE_GAP_HEIGHT_LIMIT
|
||||
let height_diff = brc_y - tlc_y;
|
||||
if height_diff >= constants::TABLE_GAP_HEIGHT_LIMIT {
|
||||
1 + self.app_config_fields.table_gap
|
||||
} else {
|
||||
let min_height_for_header = if self.is_drawing_border() { 3 } else { 1 };
|
||||
if height_diff > min_height_for_header {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.app_config_fields.table_gap == 0
|
||||
1 + self.app_config_fields.table_gap
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -151,7 +151,7 @@ impl<'a> TextTable<'a> {
|
|||
state.current_scroll_position,
|
||||
self.is_force_redraw,
|
||||
);
|
||||
let end = min(table_data.data.len(), start + num_rows + 1);
|
||||
let end = min(table_data.data.len(), start + num_rows);
|
||||
state
|
||||
.table_state
|
||||
.select(Some(state.current_scroll_position.saturating_sub(start)));
|
||||
|
@ -277,7 +277,7 @@ pub fn get_start_position(
|
|||
} else if currently_selected_position >= num_rows {
|
||||
// Else if the current position past the last element visible in the list, omit
|
||||
// until we can see that element
|
||||
*scroll_position_bar = currently_selected_position - num_rows;
|
||||
*scroll_position_bar = currently_selected_position - num_rows + 1;
|
||||
*scroll_position_bar
|
||||
} else {
|
||||
// Else, if it is not past the last element visible, do not omit anything
|
||||
|
@ -289,7 +289,7 @@ pub fn get_start_position(
|
|||
// If it's past the first element, then show from that element downwards
|
||||
*scroll_position_bar = currently_selected_position;
|
||||
} else if currently_selected_position >= *scroll_position_bar + num_rows {
|
||||
*scroll_position_bar = currently_selected_position - num_rows;
|
||||
*scroll_position_bar = currently_selected_position - num_rows + 1;
|
||||
}
|
||||
// Else, don't change what our start position is from whatever it is set to!
|
||||
*scroll_position_bar
|
||||
|
|
|
@ -162,6 +162,8 @@ pub fn get_start_position(
|
|||
*scroll_position_bar = 0;
|
||||
}
|
||||
|
||||
// FIXME: Note that num_rows is WRONG here! It assumes the number of rows - 1... oops.
|
||||
|
||||
match scroll_direction {
|
||||
app::ScrollDirection::Down => {
|
||||
if currently_selected_position < *scroll_position_bar + num_rows {
|
||||
|
|
Loading…
Reference in New Issue