From 2be126430d2cfaa2331629a25469fe6e088731be Mon Sep 17 00:00:00 2001 From: Thomas Frans Date: Wed, 14 Sep 2022 13:33:25 +0200 Subject: [PATCH] Fixed pagination bug that caused items not to load --- src/ui/listview.rs | 35 +++++++++++++++++++++++++++++------ src/ui/pagination.rs | 7 +++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/ui/listview.rs b/src/ui/listview.rs index 4998f39..db9992f 100644 --- a/src/ui/listview.rs +++ b/src/ui/listview.rs @@ -59,7 +59,7 @@ impl Scroller for ListView { impl ListView { pub fn new(content: Arc>>, queue: Arc, library: Arc) -> Self { - Self { + let result = Self { content, last_content_len: 0, selected: 0, @@ -72,7 +72,9 @@ impl ListView { library, pagination: Pagination::default(), title: "".to_string(), - } + }; + result.try_paginate(); + result } pub fn with_title(mut self, title: &str) -> Self { @@ -99,10 +101,30 @@ impl ListView { } } + /// Return wether there are still items that aren't shown in the listview. + /// + /// `true` if there are unloaded items + /// `false` if all items are loaded fn can_paginate(&self) -> bool { self.get_pagination().max_content().unwrap_or(0) > self.get_pagination().loaded_content() } + /// Try to load more items into the list if neccessary. + #[inline] + fn try_paginate(&self) { + // Paginate if there are more items + // AND + // The selected item is the current last item (keyboard scrolling) + // OR + // The scroller can't scroll further down (mouse scrolling) + if self.can_paginate() + && (self.selected == self.content.read().unwrap().len().saturating_sub(1) + || !self.scroller.can_scroll_down()) + { + self.pagination.call(&self.content, self.library.clone()); + } + } + pub fn get_selected_index(&self) -> usize { self.selected } @@ -297,7 +319,10 @@ impl View for ListView { Event::Mouse { event: MouseEvent::WheelDown, .. - } => self.scroller.scroll_down(3), + } => { + self.scroller.scroll_down(3); + self.try_paginate(); + } Event::Mouse { event: MouseEvent::Press(MouseButton::Left), position, @@ -529,9 +554,7 @@ impl ViewExt for ListView { MoveAmount::Integer(amount) => self.move_focus(*amount), } } - if self.selected == last_idx && self.can_paginate() { - self.pagination.call(&self.content, self.library.clone()); - } + self.try_paginate(); return Ok(CommandResult::Consumed(None)); } _ => return Ok(CommandResult::Consumed(None)), diff --git a/src/ui/pagination.rs b/src/ui/pagination.rs index 3205e86..6367059 100644 --- a/src/ui/pagination.rs +++ b/src/ui/pagination.rs @@ -85,6 +85,13 @@ impl ApiResult { pub type Paginator = Box>>) + Send + Sync>; +/// Manages the loading of ListItems, to increase performance and decrease +/// memory usage. +/// +/// `loaded_content`: The amount of currently loaded items +/// `max_content`: The maximum amount of items +/// `callback`: TODO: document +/// `busy`: TODO: document pub struct Pagination { loaded_content: Arc>, max_content: Arc>>,