summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClementTsang <cjhtsang@uwaterloo.ca>2022-05-03 06:05:02 -0400
committerClementTsang <cjhtsang@uwaterloo.ca>2022-05-07 20:53:52 -0400
commit9e63642e9c918632d7803e687319e5a2a0e12ad6 (patch)
tree37b7a6f1293c755e7cce12cdd54e6ed542a83f1d
parentdf1a4183273f3940aeddd109c25414a056b1e010 (diff)
refactor: fix off-by-one bug with getting start position
-rw-r--r--src/app.rs41
-rw-r--r--src/canvas/components/text_table.rs6
-rw-r--r--src/canvas/drawing_utils.rs2
3 files changed, 24 insertions, 25 deletions
diff --git a/src/app.rs b/src/app.rs
index 8ea22fd4..975c733e 100644
--- a/src/app.rs
+++ b/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
}
}
}
diff --git a/src/canvas/components/text_table.rs b/src/canvas/components/text_table.rs
index 15dc15cf..c25972ee 100644
--- a/src/canvas/components/text_table.rs
+++ b/src/canvas/components/text_table.rs
@@ -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
diff --git a/src/canvas/drawing_utils.rs b/src/canvas/drawing_utils.rs
index 839017a9..ee21660a 100644
--- a/src/canvas/drawing_utils.rs
+++ b/src/canvas/drawing_utils.rs
@@ -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 {