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;