Skip to content

Commit

Permalink
fix(wallet): fix resize panic (#3149)
Browse files Browse the repository at this point in the history
## Description
Fixes the panic during resize.

## How Has This Been Tested?
Manually, and also added a cargo test.

## Checklist:
* [x] I'm merging against the `development` branch.
* [x] I have squashed my commits into a single commit.
  • Loading branch information
aviator-app[bot] committed Aug 9, 2021
2 parents 778f951 + 1504139 commit 33af084
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion applications/tari_console_wallet/src/ui/widgets/list_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ impl WindowedListState {
self.offset = self.start;
list_state.select(Some(selected - self.start));
}

// If the window was resized make sure we are within bounds of the list.
if self.end > self.num_items {
let diff = self.end - self.num_items;
self.start -= diff;
self.end -= diff;
if let Some(selected) = self.selected {
list_state.select(Some(selected - self.start));
}
self.offset = self.start;
}
list_state
}

Expand Down Expand Up @@ -193,4 +202,22 @@ mod test {
let window = list_state.get_start_end();
assert_eq!(window, (5, 9));
}

#[test]
fn test_console_resize() {
let mut list_state = WindowedListState::new();
// Start with 20 items and console size of 5
list_state.set_num_items(20);
// Go to the last item (2 times previous).
list_state.previous();
list_state.previous();
list_state.get_list_state(5);
assert_eq!(list_state.get_start_end(), (15, 20));
// Resize to 10.
list_state.get_list_state(10);
assert_eq!(list_state.get_start_end(), (10, 20));
// Resize to 50.
list_state.get_list_state(50);
assert_eq!(list_state.get_start_end(), (0, 20));
}
}

0 comments on commit 33af084

Please sign in to comment.