From 0dc5812f320c85d4d441d66581d8604333e99a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 22 Oct 2026 14:45:17 +0200 Subject: [PATCH] UI: Add ScrollView::ScrollToCenter and GetEnclosingScrollView For bringing a given view into the middle of the scroll view it's in, rather than just into sight like focusing does. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/UI/ScrollView.cpp | 19 +++++++++++++++++++ Common/UI/ScrollView.h | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/Common/UI/ScrollView.cpp b/Common/UI/ScrollView.cpp index 4b92a70adb..3123074b1c 100644 --- a/Common/UI/ScrollView.cpp +++ b/Common/UI/ScrollView.cpp @@ -468,6 +468,25 @@ void ScrollView::ScrollTo(float newScrollPos) { scrollToTarget_ = true; } +void ScrollView::ScrollToCenter(const View *view) { + const Bounds &vBounds = view->GetBounds(); + if (orientation_ == ORIENT_VERTICAL) { + ScrollTo(layoutScrollPos_ + vBounds.centerY() - bounds_.y - bounds_.h * 0.5f); + } else { + ScrollTo(layoutScrollPos_ + vBounds.centerX() - bounds_.x - bounds_.w * 0.5f); + } +} + +ScrollView *GetEnclosingScrollView(View *view) { + for (ViewGroup *group = view->Parent(); group; group = group->Parent()) { + ScrollView *scrollView = dynamic_cast(group); + if (scrollView) { + return scrollView; + } + } + return nullptr; +} + void ScrollView::ScrollRelative(float distance) { scrollTarget_ = scrollPos_ + distance; scrollToTarget_ = true; diff --git a/Common/UI/ScrollView.h b/Common/UI/ScrollView.h index 73036ddb92..e336a1d2b0 100644 --- a/Common/UI/ScrollView.h +++ b/Common/UI/ScrollView.h @@ -5,6 +5,11 @@ namespace UI { +class ScrollView; + +// The nearest scroll view the view is in, or null. +ScrollView *GetEnclosingScrollView(View *view); + // A scrollview usually contains just a single child - a linear layout or similar. class ScrollView : public ViewGroup { public: @@ -21,6 +26,8 @@ public: void ScrollTo(float newScrollPos); void ScrollToBottom(); + // Scrolls so that the view (which must be laid out) ends up in the middle. + void ScrollToCenter(const View *view); void ScrollRelative(float distance); bool CanScroll() const; void Update() override;