Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Unified Diff: ui/views/controls/scroll_view.cc

Issue 1680613002: Adding momentum/overscroll to views:: ScrollViews Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Tableview layout. aaaand I think we are done Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/scroll_view.h ('k') | ui/views/controls/scroll_view_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/scroll_view.cc
diff --git a/ui/views/controls/scroll_view.cc b/ui/views/controls/scroll_view.cc
index a3e1e9787a2ae32b3118c2d0ed9837d808f94d25..8eb4337c087fff66933d6c1d1d480b24db154e2e 100644
--- a/ui/views/controls/scroll_view.cc
+++ b/ui/views/controls/scroll_view.cc
@@ -6,12 +6,14 @@
#include "base/logging.h"
#include "base/macros.h"
+#include "ui/compositor/overscroll/ui_scroll_input_manager.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/native_theme/native_theme.h"
+#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/style/platform_style.h"
-#include "ui/views/widget/root_view.h"
+#include "ui/views/widget/widget.h"
namespace views {
@@ -64,15 +66,23 @@ int CheckScrollBounds(int viewport_size, int content_size, int current_pos) {
}
// Make sure the content is not scrolled out of bounds
-void CheckScrollBounds(View* viewport, View* view) {
+void CheckScrollBounds(View* viewport, View* container, View* view) {
if (!view)
return;
- int x = CheckScrollBounds(viewport->width(), view->width(), -view->x());
- int y = CheckScrollBounds(viewport->height(), view->height(), -view->y());
+ gfx::ScrollOffset offset = container
+ ? container->layer()->CurrentScrollOffset()
+ : gfx::ScrollOffset(-view->x(), -view->y());
- // This is no op if bounds are the same
- view->SetBounds(-x, -y, view->width(), view->height());
+ int x = CheckScrollBounds(viewport->width(), view->width(), offset.x());
+ int y = CheckScrollBounds(viewport->height(), view->height(), offset.y());
+
+ if (container) {
+ container->layer()->SetScrollOffset(gfx::ScrollOffset(x, y));
+ } else {
+ // This is no op if bounds are the same
+ view->SetBounds(-x, -y, view->width(), view->height());
+ }
}
// Used by ScrollToPosition() to make sure the new position fits within the
@@ -89,6 +99,21 @@ int AdjustPosition(int current_position,
return (new_position > max_position) ? max_position : new_position;
}
+class ScrollViewContainer : public View {
+ public:
+ ScrollViewContainer() {}
+
+ // View:
+ void ChildPreferredSizeChanged(View* child) override {
+ PreferredSizeChanged();
+ }
+
+ // TODO: Add ScrollRectToVisible
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ScrollViewContainer);
+};
+
} // namespace
// Viewport contains the contents View of the ScrollView.
@@ -105,9 +130,17 @@ class ScrollView::Viewport : public View {
View* contents = child_at(0);
gfx::Rect scroll_rect(rect);
+
+ ScrollView* scroll_view = static_cast<ScrollView*>(parent());
+ if (contents == scroll_view->contents_container_) {
+ // With layer scrolling, there's no need to "undo" the offset done in the
+ // child's View::ScrollRectToVisible() before it calls this.
+ DCHECK_EQ(0, contents->x());
+ DCHECK_EQ(0, contents->y());
+ }
+
scroll_rect.Offset(-contents->x(), -contents->y());
- static_cast<ScrollView*>(parent())->ScrollContentsRegionToBeVisible(
- scroll_rect);
+ scroll_view->ScrollContentsRegionToBeVisible(scroll_rect);
}
void ChildPreferredSizeChanged(View* child) override {
@@ -121,6 +154,7 @@ class ScrollView::Viewport : public View {
ScrollView::ScrollView()
: contents_(NULL),
+ contents_container_(new ScrollViewContainer()),
contents_viewport_(new Viewport()),
header_(NULL),
header_viewport_(new Viewport()),
@@ -133,6 +167,7 @@ ScrollView::ScrollView()
set_notify_enter_exit_on_child(true);
AddChildView(contents_viewport_);
+ contents_viewport_->AddChildView(contents_container_);
AddChildView(header_viewport_);
// Don't add the scrollbars as children until we discover we need them
@@ -142,6 +177,18 @@ ScrollView::ScrollView()
vert_sb_->SetVisible(false);
vert_sb_->set_controller(this);
corner_view_->SetVisible(false);
+
+ contents_viewport_->SetPaintToLayer(true);
+ contents_viewport_->set_background(
+ Background::CreateSolidBackground(SK_ColorWHITE));
+ contents_viewport_->layer()->SetMasksToBounds(true);
+
+ contents_container_->SetPaintToLayer(true);
+ contents_container_->set_background(
+ Background::CreateSolidBackground(SK_ColorWHITE));
+ contents_container_->layer()->SetScrollable(
+ contents_viewport_->layer(), true /* can_overscroll */,
+ base::Bind(&ScrollView::OnLayerScrolled, base::Unretained(this)));
}
ScrollView::~ScrollView() {
@@ -158,7 +205,7 @@ ScrollView* ScrollView::CreateScrollViewWithBorder() {
}
void ScrollView::SetContents(View* a_view) {
- SetHeaderOrContents(contents_viewport_, a_view, &contents_);
+ SetHeaderOrContents(contents_container_, a_view, &contents_);
}
void ScrollView::SetHeader(View* header) {
@@ -168,8 +215,9 @@ void ScrollView::SetHeader(View* header) {
gfx::Rect ScrollView::GetVisibleRect() const {
if (!contents_)
return gfx::Rect();
- return gfx::Rect(-contents_->x(), -contents_->y(),
- contents_viewport_->width(), contents_viewport_->height());
+ gfx::ScrollOffset offset = CurrentOffset();
+ return gfx::Rect(offset.x(), offset.y(), contents_viewport_->width(),
+ contents_viewport_->height());
}
void ScrollView::ClipHeightTo(int min_height, int max_height) {
@@ -267,10 +315,14 @@ void ScrollView::Layout() {
// Update the bounds right now so the inner views can fit in it.
contents_viewport_->SetBoundsRect(viewport_bounds);
- // Give |contents_| a chance to update its bounds if it depends on the
- // viewport.
- if (contents_)
+ // Give |contents_| a chance to update its bounds if it depends on its parent
+ // bounds.
+ if (contents_) {
+ // The container bounds may need to be made larger later, but use the
+ // viewport bounds to layout the contents.
+ contents_container_->SetBoundsRect(viewport_bounds);
contents_->Layout();
+ }
bool should_layout_contents = false;
bool horiz_sb_required = false;
@@ -326,16 +378,26 @@ void ScrollView::Layout() {
// Update to the real client size with the visible scrollbars.
contents_viewport_->SetBoundsRect(viewport_bounds);
- if (should_layout_contents && contents_)
+ if (should_layout_contents && contents_) {
+ contents_container_->SetBoundsRect(viewport_bounds);
contents_->Layout();
+ }
+
+ // Even when |contents_| needs to scroll, it can still be narrower or wider
+ // the viewport. So ensure the scrolling layer can fill the viewport, so that
+ // events will correctly hit it, and overscroll looks correct.
+ gfx::Size container_size = contents_ ? contents_->size() : gfx::Size();
+ container_size.SetToMax(viewport_bounds.size());
+ contents_container_->SetBoundsRect(gfx::Rect(container_size));
header_viewport_->SetBounds(contents_x, contents_y,
viewport_bounds.width(), header_height);
if (header_)
header_->Layout();
- CheckScrollBounds(header_viewport_, header_);
- CheckScrollBounds(contents_viewport_, contents_);
+ CheckScrollBounds(header_viewport_, nullptr, header_);
+ CheckScrollBounds(contents_viewport_, contents_container_, contents_);
+
SchedulePaint();
UpdateScrollBarPositions();
}
@@ -379,6 +441,13 @@ void ScrollView::OnMouseExited(const ui::MouseEvent& event) {
vert_sb_->OnMouseExitedScrollView(event);
}
+void ScrollView::OnScrollEvent(ui::ScrollEvent* event) {
+ ui::UIScrollInputManager* compositor_scroller =
+ GetWidget()->GetCompositor()->scroll_input_manager();
+ if (compositor_scroller)
+ compositor_scroller->OnScrollEvent(*event);
+}
+
void ScrollView::OnGestureEvent(ui::GestureEvent* event) {
// If the event happened on one of the scrollbars, then those events are
// sent directly to the scrollbars. Otherwise, only scroll events are sent to
@@ -402,27 +471,31 @@ const char* ScrollView::GetClassName() const {
return kViewClassName;
}
+ScrollView* ScrollView::EnclosingScrollView() {
+ return this;
+}
+
void ScrollView::ScrollToPosition(ScrollBar* source, int position) {
if (!contents_)
return;
+ gfx::ScrollOffset offset = CurrentOffset();
if (source == horiz_sb_ && horiz_sb_->visible()) {
- position = AdjustPosition(contents_->x(), position, contents_->width(),
+ position = AdjustPosition(offset.x(), position, contents_->width(),
contents_viewport_->width());
- if (-contents_->x() == position)
+ if (offset.x() == position)
return;
- contents_->SetX(-position);
- if (header_) {
- header_->SetX(-position);
- header_->SchedulePaintInRect(header_->GetVisibleBounds());
- }
+ offset.set_x(position);
} else if (source == vert_sb_ && vert_sb_->visible()) {
- position = AdjustPosition(contents_->y(), position, contents_->height(),
+ position = AdjustPosition(offset.y(), position, contents_->height(),
contents_viewport_->height());
- if (-contents_->y() == position)
+ if (offset.y() == position)
return;
- contents_->SetY(-position);
+ offset.set_y(position);
}
+ ScrollToOffset(offset);
+
+ // TODO(tapted): Remove. Not needed for layered scrolling.
contents_->SchedulePaintInRect(contents_->GetVisibleBounds());
}
@@ -504,10 +577,9 @@ void ScrollView::ScrollContentsRegionToBeVisible(const gfx::Rect& rect) {
(vis_rect.y() > y) ? y : std::max(0, max_y -
contents_viewport_->height());
- contents_->SetX(-new_x);
- if (header_)
- header_->SetX(-new_x);
- contents_->SetY(-new_y);
+ ScrollToOffset(gfx::ScrollOffset(new_x, new_y));
+
+ // TODO(tapted): Remove. Handled in OnLayerScrolled() with layered scrolling.
UpdateScrollBarPositions();
}
@@ -555,20 +627,36 @@ void ScrollView::UpdateScrollBarPositions() {
if (!contents_)
return;
+ const gfx::ScrollOffset offset = CurrentOffset();
if (horiz_sb_->visible()) {
int vw = contents_viewport_->width();
int cw = contents_->width();
- int origin = contents_->x();
- horiz_sb_->Update(vw, cw, -origin);
+ horiz_sb_->Update(vw, cw, offset.x());
}
if (vert_sb_->visible()) {
int vh = contents_viewport_->height();
int ch = contents_->height();
- int origin = contents_->y();
- vert_sb_->Update(vh, ch, -origin);
+ vert_sb_->Update(vh, ch, offset.y());
}
}
+gfx::ScrollOffset ScrollView::CurrentOffset() const {
+ return contents_container_->layer()->CurrentScrollOffset();
+}
+
+void ScrollView::ScrollToOffset(const gfx::ScrollOffset& offset) {
+ contents_container_->layer()->SetScrollOffset(offset);
+ OnLayerScrolled();
+}
+
+void ScrollView::OnLayerScrolled() {
+ if (header_) {
+ header_->SetX(-CurrentOffset().x());
+ header_->SchedulePaintInRect(header_->GetVisibleBounds());
+ }
+ UpdateScrollBarPositions();
+}
+
// VariableRowHeightScrollHelper ----------------------------------------------
VariableRowHeightScrollHelper::VariableRowHeightScrollHelper(
« no previous file with comments | « ui/views/controls/scroll_view.h ('k') | ui/views/controls/scroll_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698