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) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-22 14:45:17 +02:00
1 parent b6f79261a2
commit 0dc5812f32
2 files changed
+26

No files matched your search

+19
View File
@@ -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<ScrollView *>(group);
if (scrollView) {
return scrollView;
}
}
return nullptr;
}
void ScrollView::ScrollRelative(float distance) {
scrollTarget_ = scrollPos_ + distance;
scrollToTarget_ = true;