From a2120328f137212c561cbc007883a541b0ddf9e1 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Fri, 28 Oct 2022 02:35:42 +0300 Subject: [PATCH] somewhat limit scrolling extents --- src/ListView.cpp | 7 +++++++ src/ScrollContainer.cpp | 4 ++-- src/main.cpp | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ListView.cpp b/src/ListView.cpp index 2d18115..fbbc491 100644 --- a/src/ListView.cpp +++ b/src/ListView.cpp @@ -43,9 +43,16 @@ void ListView::on_mouse_button(MouseButtonEvent &event) { } } else if (event.did_scroll_down()) { m_scroll += m_scroll_step; + auto items_size = m_item_height * elements.size(); + if (m_scroll > items_size) { + m_scroll = items_size; + } repaint(); } else if (event.did_scroll_up()) { m_scroll -= m_scroll_step; + if (m_scroll < 0) { + m_scroll = 0; + } repaint(); } } diff --git a/src/ScrollContainer.cpp b/src/ScrollContainer.cpp index 51d359f..26b4d83 100644 --- a/src/ScrollContainer.cpp +++ b/src/ScrollContainer.cpp @@ -26,9 +26,9 @@ std::shared_ptr ScrollContainer::make_target() { void ScrollContainer::on_mouse_button(MouseButtonEvent &event) { if (event.did_scroll_down()) { - set_scroll(Point(m_scroll.x(), m_scroll.y() + m_scroll_step)); + set_scroll(Point(m_scroll.x(), std::min(m_scroll.y() + m_scroll_step, m_target->rect().height()))); } else if (event.did_scroll_up()) { - set_scroll(Point(m_scroll.x(), m_scroll.y() - m_scroll_step)); + set_scroll(Point(m_scroll.x(), std::max(0.0, m_scroll.y() - m_scroll_step))); } } diff --git a/src/main.cpp b/src/main.cpp index 753fbd7..58da265 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ int main() { test_layout->set_margin(69); test_layout->set_spacing(10); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 50; i++) { test_widget->add("Hello: " + std::to_string(i)); } @@ -104,7 +104,7 @@ int main() { selected_label->set_text("Selected: " + item); }; - int i = 1000; + int i = 100; while (i --> 0) { list_view->elements.push_back("Item " + std::to_string(i)); }