diff options
author | ClementTsang <34804052+ClementTsang@users.noreply.github.com> | 2022-08-15 00:50:27 -0400 |
---|---|---|
committer | ClementTsang <34804052+ClementTsang@users.noreply.github.com> | 2022-08-15 00:50:27 -0400 |
commit | 658b8c720f7f0b6646843282a419194882bdccdd (patch) | |
tree | 38243485b21dd2bbc00dbe8e3daa7fe3531c1238 /src | |
parent | f5e2b7242adef293cf11c8e32310bee81a306148 (diff) |
refactor: minor tweak to update_position code
Diffstat (limited to 'src')
-rw-r--r-- | src/components/text_table/state.rs | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/components/text_table/state.rs b/src/components/text_table/state.rs index 14173bc1..ef3163ac 100644 --- a/src/components/text_table/state.rs +++ b/src/components/text_table/state.rs @@ -472,9 +472,12 @@ impl<H: TableComponentHeader> TableComponentState<H> { /// Updates the position if possible, and if there is a valid change, returns the new position. pub fn update_position(&mut self, change: i64, num_entries: usize) -> Option<usize> { + let min_index = 0; + let max_index = num_entries.saturating_sub(1); + if change == 0 - || (change > 0 && self.current_scroll_position == num_entries.saturating_sub(1)) - || (change < 0 && self.current_scroll_position == 0) + || (change > 0 && self.current_scroll_position == max_index) + || (change < 0 && self.current_scroll_position == min_index) { return None; } @@ -482,7 +485,7 @@ impl<H: TableComponentHeader> TableComponentState<H> { let csp: Result<i64, _> = self.current_scroll_position.try_into(); if let Ok(csp) = csp { self.current_scroll_position = - (csp + change).clamp(0, num_entries.saturating_sub(1) as i64) as usize; + (csp + change).clamp(min_index as i64, max_index as i64) as usize; if change < 0 { self.scroll_direction = ScrollDirection::Up; @@ -490,10 +493,10 @@ impl<H: TableComponentHeader> TableComponentState<H> { self.scroll_direction = ScrollDirection::Down; } - return Some(self.current_scroll_position); + Some(self.current_scroll_position) + } else { + None } - - None } } |