Commit 5eaac482 authored by Allen Bauer's avatar Allen Bauer Committed by Commit Bot

Ensure the ScrollView is scrolled to reveal any control that acquires focus.

Bug: 790555
Change-Id: Id64b81c88908adce55d47537ddd0744bfdefc857
Reviewed-on: https://chromium-review.googlesource.com/806256Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522112}
parent 75ac1cc6
......@@ -99,6 +99,8 @@ class FixedView : public View {
SetBounds(x(), y(), pref.width(), pref.height());
}
void SetFocus() { Focus(); }
private:
DISALLOW_COPY_AND_ASSIGN(FixedView);
};
......@@ -633,6 +635,34 @@ TEST_F(ScrollViewTest, ScrollRectToVisible) {
EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y());
}
// Verifies that child scrolls into view when it's focused.
TEST_F(ScrollViewTest, ScrollChildToVisibleOnFocus) {
ScrollViewTestApi test_api(&scroll_view_);
CustomView* contents = new CustomView;
scroll_view_.SetContents(contents);
contents->SetPreferredSize(gfx::Size(500, 1000));
FixedView* child = new FixedView;
child->SetPreferredSize(gfx::Size(10, 10));
child->SetPosition(gfx::Point(0, 405));
contents->AddChildView(child);
scroll_view_.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
scroll_view_.Layout();
EXPECT_EQ(gfx::Point(), test_api.IntegralViewOffset());
// Set focus to the child control. This should cause the control to scroll to
// y=405 height=10. Like the above test, this should make the y position of
// the content at (405 + 10) - viewport_height (scroll region bottom aligned).
child->SetFocus();
const int viewport_height = test_api.contents_viewport()->height();
// Expect there to be a horizontal scrollbar, making the viewport shorter.
EXPECT_EQ(100 - scroll_view_.GetScrollBarLayoutHeight(), viewport_height);
gfx::ScrollOffset offset = test_api.CurrentOffset();
EXPECT_EQ(415 - viewport_height, offset.y());
}
// Verifies ClipHeightTo() uses the height of the content when it is between the
// minimum and maximum height values.
TEST_F(ScrollViewTest, ClipHeightToNormalContentHeight) {
......
......@@ -1478,6 +1478,10 @@ void View::ScrollRectToVisible(const gfx::Rect& rect) {
}
}
void View::ScrollViewToVisible() {
ScrollRectToVisible(GetLocalBounds());
}
int View::GetPageScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
......@@ -1797,6 +1801,7 @@ void View::OnBlur() {
void View::Focus() {
OnFocus();
ScrollViewToVisible();
}
void View::Blur() {
......
......@@ -1108,6 +1108,10 @@ class VIEWS_EXPORT View : public ui::LayerDelegate,
// the child view, such as Viewport, to override appropriately.
virtual void ScrollRectToVisible(const gfx::Rect& rect);
// Scrolls the view's bounds or some subset thereof to be visible. By default
// this function calls ScrollRectToVisible(GetLocalBounds()).
virtual void ScrollViewToVisible();
// The following methods are used by ScrollView to determine the amount
// to scroll relative to the visible bounds of the view. For example, a
// return value of 10 indicates the scrollview should scroll 10 pixels in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment