From bf865dc5365eb6883574d2a2b50082903b60061a Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 26 Feb 2024 17:19:12 +0000 Subject: [PATCH] Codechange: Add `GetVisibleRangeIterators()` to `Scrollbar`. --- src/widget_type.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/widget_type.h b/src/widget_type.h index 3f28ffc1d9..b7a8393fca 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -833,6 +833,20 @@ public: int GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const; + /** + * Get a pair of iterators for the range of visible elements in a container. + * @param container Container of elements represented by the scrollbar. + * @returns Pair of iterators of visible elements. + */ + template + auto GetVisibleRangeIterators(Tcontainer &container) const + { + assert(this->GetCount() == container.size()); // Scrollbar and container size must match. + auto first = std::next(std::begin(container), this->GetPosition()); + auto last = std::next(first, std::min(this->GetCapacity(), this->GetCount() - this->GetPosition())); + return std::make_pair(first, last); + } + /** * Return an iterator pointing to the element of a scrolled widget that a user clicked in. * @param container Container of elements represented by the scrollbar.