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

Side by Side Diff: cc/layers/viewport.cc

Issue 986443003: Move viewport scrolling logic into separate class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: aelias feedback Created 5 years, 9 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 unified diff | Download patch
« no previous file with comments | « cc/layers/viewport.h ('k') | cc/trees/layer_tree_host_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cc/layers/viewport.h"
6
7 #include "base/logging.h"
8 #include "cc/input/top_controls_manager.h"
9 #include "cc/trees/layer_tree_host_impl.h"
10 #include "cc/trees/layer_tree_impl.h"
11 #include "ui/gfx/geometry/vector2d_conversions.h"
12 #include "ui/gfx/geometry/vector2d_f.h"
13
14 namespace cc {
15
16 // static
17 scoped_ptr<Viewport> Viewport::Create(
18 LayerTreeHostImpl* host_impl) {
19 return make_scoped_ptr(new Viewport(host_impl));
20 }
21
22 Viewport::Viewport(LayerTreeHostImpl* host_impl)
23 : host_impl_(host_impl) {
24 DCHECK(host_impl_);
25 }
26
27 Viewport::ScrollResult Viewport::ScrollBy(const gfx::Vector2dF& delta,
28 const gfx::Point& viewport_point,
29 bool is_wheel_scroll) {
30 gfx::Vector2dF content_delta = delta;
31 ScrollResult result;
32
33 if (ShouldTopControlsConsumeScroll(delta)) {
34 result.top_controls_applied_delta = ScrollTopControls(delta);
35 content_delta -= result.top_controls_applied_delta;
36 }
37
38 gfx::Vector2dF pending_content_delta = content_delta;
39
40 if (OuterScrollLayer()) {
41 pending_content_delta -= host_impl_->ScrollLayer(OuterScrollLayer(),
42 pending_content_delta,
43 viewport_point,
44 is_wheel_scroll);
45 }
46
47 // TODO(bokan): This shouldn't be needed but removing it causes subtle
48 // viewport movement during top controls manipulation.
49 if (!gfx::ToRoundedVector2d(pending_content_delta).IsZero()) {
50 pending_content_delta -= host_impl_->ScrollLayer(InnerScrollLayer(),
51 pending_content_delta,
52 viewport_point,
53 is_wheel_scroll);
54 result.unused_scroll_delta = AdjustOverscroll(pending_content_delta);
55 }
56
57
58 result.applied_delta = content_delta - pending_content_delta;
59 return result;
60 }
61
62 gfx::Vector2dF Viewport::ScrollTopControls(const gfx::Vector2dF& delta) {
63 gfx::Vector2dF excess_delta =
64 host_impl_->top_controls_manager()->ScrollBy(delta);
65
66 return delta - excess_delta;
67 }
68
69 bool Viewport::ShouldTopControlsConsumeScroll(
70 const gfx::Vector2dF& scroll_delta) const {
71 // Always consume if it's in the direction to show the top controls.
72 if (scroll_delta.y() < 0)
73 return true;
74
75 if (TotalScrollOffset().y() < MaxTotalScrollOffset().y())
76 return true;
77
78 return false;
79 }
80
81 gfx::Vector2dF Viewport::AdjustOverscroll(const gfx::Vector2dF& delta) const {
82 const float kEpsilon = 0.1f;
83 gfx::Vector2dF adjusted = delta;
84
85 if (std::abs(adjusted.x()) < kEpsilon)
86 adjusted.set_x(0.0f);
87 if (std::abs(adjusted.y()) < kEpsilon)
88 adjusted.set_y(0.0f);
89
90 // Disable overscroll on axes which are impossible to scroll.
91 if (host_impl_->settings().report_overscroll_only_for_scrollable_axes) {
92 if (std::abs(MaxTotalScrollOffset().x()) <= kEpsilon ||
93 !InnerScrollLayer()->user_scrollable_horizontal())
94 adjusted.set_x(0.0f);
95 if (std::abs(MaxTotalScrollOffset().y()) <= kEpsilon ||
96 !InnerScrollLayer()->user_scrollable_vertical())
97 adjusted.set_y(0.0f);
98 }
99
100 return adjusted;
101 }
102
103 gfx::ScrollOffset Viewport::MaxTotalScrollOffset() const {
104 gfx::ScrollOffset offset;
105
106 offset += InnerScrollLayer()->MaxScrollOffset();
107
108 if (OuterScrollLayer())
109 offset += OuterScrollLayer()->MaxScrollOffset();
110
111 return offset;
112 }
113
114 gfx::ScrollOffset Viewport::TotalScrollOffset() const {
115 gfx::ScrollOffset offset;
116
117 offset += InnerScrollLayer()->CurrentScrollOffset();
118
119 if (OuterScrollLayer())
120 offset += OuterScrollLayer()->CurrentScrollOffset();
121
122 return offset;
123 }
124
125 LayerImpl* Viewport::InnerScrollLayer() const {
126 return host_impl_->InnerViewportScrollLayer();
127 }
128
129 LayerImpl* Viewport::OuterScrollLayer() const {
130 return host_impl_->OuterViewportScrollLayer();
131 }
132
133 } // namespace cc
OLDNEW
« no previous file with comments | « cc/layers/viewport.h ('k') | cc/trees/layer_tree_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698