From 15041398d2cdfe765ea841b85731db826fbd4f8e Mon Sep 17 00:00:00 2001 From: Martin Stefcek Date: Fri, 30 Jul 2021 14:23:37 +0200 Subject: [PATCH] fix wallet resize --- .../src/ui/widgets/list_state.rs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/applications/tari_console_wallet/src/ui/widgets/list_state.rs b/applications/tari_console_wallet/src/ui/widgets/list_state.rs index 11d72e8827..c95f259d80 100644 --- a/applications/tari_console_wallet/src/ui/widgets/list_state.rs +++ b/applications/tari_console_wallet/src/ui/widgets/list_state.rs @@ -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 } @@ -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)); + } }