Roll src/third_party/WebKit d0dd976:1cfa05a (svn 202498:202500)
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_impl_base.cc
blob1d07a8349e4604d715df1a339011643df1460b7d
1 // Copyright 2013 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.
5 #include "cc/layers/scrollbar_layer_impl_base.h"
7 #include <algorithm>
8 #include "cc/trees/layer_tree_impl.h"
9 #include "ui/gfx/geometry/rect_conversions.h"
11 namespace cc {
13 ScrollbarLayerImplBase::ScrollbarLayerImplBase(
14 LayerTreeImpl* tree_impl,
15 int id,
16 ScrollbarOrientation orientation,
17 bool is_left_side_vertical_scrollbar,
18 bool is_overlay)
19 : LayerImpl(tree_impl, id),
20 scroll_layer_(nullptr),
21 clip_layer_(nullptr),
22 is_overlay_scrollbar_(is_overlay),
23 thumb_thickness_scale_factor_(1.f),
24 current_pos_(0.f),
25 maximum_(0),
26 orientation_(orientation),
27 is_left_side_vertical_scrollbar_(is_left_side_vertical_scrollbar),
28 vertical_adjust_(0.f),
29 visible_to_total_length_ratio_(1.f) {
32 ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {}
34 void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) {
35 float active_opacity = layer->opacity();
36 bool active_hidden = layer->hide_layer_and_subtree();
37 LayerImpl::PushPropertiesTo(layer);
38 layer->SetOpacity(active_opacity);
39 layer->SetHideLayerAndSubtree(active_hidden);
40 DCHECK(layer->ToScrollbarLayer());
41 layer->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_);
42 PushScrollClipPropertiesTo(layer);
45 void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl* layer) {
46 DCHECK(layer->ToScrollbarLayer());
47 layer->ToScrollbarLayer()->SetScrollLayerAndClipLayerByIds(ScrollLayerId(),
48 ClipLayerId());
51 ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() {
52 return this;
55 namespace {
57 typedef void (LayerImpl::*ScrollbarRegistrationOperation)(
58 ScrollbarLayerImplBase*);
60 void RegisterScrollbarWithLayers(ScrollbarLayerImplBase* scrollbar,
61 LayerImpl* container_layer,
62 LayerImpl* scroll_layer,
63 ScrollbarRegistrationOperation operation) {
64 if (!container_layer || !scroll_layer)
65 return;
67 DCHECK(scrollbar);
69 // Scrollbars must be notifed of changes to their scroll and container layers
70 // and all scrollable layers in between.
71 for (LayerImpl* current_layer = scroll_layer;
72 current_layer && current_layer != container_layer->parent();
73 current_layer = current_layer->parent()) {
74 (current_layer->*operation)(scrollbar);
77 } // namespace
79 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
80 int scroll_layer_id,
81 int clip_layer_id) {
82 LayerImpl* scroll_layer = layer_tree_impl()->LayerById(scroll_layer_id);
83 LayerImpl* clip_layer = layer_tree_impl()->LayerById(clip_layer_id);
84 if (scroll_layer_ == scroll_layer && clip_layer_ == clip_layer)
85 return;
87 RegisterScrollbarWithLayers(
88 this, clip_layer_, scroll_layer_, &LayerImpl::RemoveScrollbar);
89 scroll_layer_ = scroll_layer;
90 clip_layer_ = clip_layer;
91 RegisterScrollbarWithLayers(
92 this, clip_layer_, scroll_layer_, &LayerImpl::AddScrollbar);
94 ScrollbarParametersDidChange(false);
97 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos) {
98 if (current_pos_ == current_pos)
99 return false;
100 current_pos_ = current_pos;
101 NoteLayerPropertyChanged();
102 return true;
105 bool ScrollbarLayerImplBase::SetMaximum(int maximum) {
106 if (maximum_ == maximum)
107 return false;
108 maximum_ = maximum;
109 NoteLayerPropertyChanged();
110 return true;
113 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
114 if (!scroll_layer_)
115 return false;
116 return scroll_layer_->user_scrollable(orientation()) && (0 < maximum());
119 bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
120 if (vertical_adjust_ == vertical_adjust)
121 return false;
122 vertical_adjust_ = vertical_adjust;
123 NoteLayerPropertyChanged();
124 return true;
127 bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) {
128 if (!IsThumbResizable())
129 return false;
131 if (visible_to_total_length_ratio_ == ratio)
132 return false;
133 visible_to_total_length_ratio_ = ratio;
134 NoteLayerPropertyChanged();
135 return true;
138 bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) {
139 if (thumb_thickness_scale_factor_ == factor)
140 return false;
141 thumb_thickness_scale_factor_ = factor;
142 NoteLayerPropertyChanged();
143 return true;
146 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
147 // Thumb extent is the length of the thumb in the scrolling direction, thumb
148 // thickness is in the perpendicular direction. Here's an example of a
149 // horizontal scrollbar - inputs are above the scrollbar, computed values
150 // below:
152 // |<------------------- track_length_ ------------------->|
154 // |--| <-- start_offset
156 // +--+----------------------------+------------------+-------+--+
157 // |<|| |##################| ||>|
158 // +--+----------------------------+------------------+-------+--+
160 // |<- thumb_length ->|
162 // |<------- thumb_offset -------->|
164 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
165 // have to compute it. The ratio of the thumb's length to the track's length
166 // is the same as that of the visible viewport to the total viewport, unless
167 // that would make the thumb's length less than its thickness.
169 // vertical_adjust_ is used when the layer geometry from the main thread is
170 // not in sync with what the user sees. For instance on Android scrolling the
171 // top bar controls out of view reveals more of the page content. We want the
172 // root layer scrollbars to reflect what the user sees even if we haven't
173 // received new layer geometry from the main thread. If the user has scrolled
174 // down by 50px and the initial viewport size was 950px the geometry would
175 // look something like this:
177 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
178 // Layer geometry: Desired thumb positions:
179 // +--------------------+-+ +----------------------+ <-- 0px
180 // | |v| | #|
181 // | |e| | #|
182 // | |r| | #|
183 // | |t| | #|
184 // | |i| | #|
185 // | |c| | #|
186 // | |a| | #|
187 // | |l| | #|
188 // | | | | #|
189 // | |l| | #|
190 // | |a| | #|
191 // | |y| | #|
192 // | |e| | #|
193 // | |r| | #|
194 // +--------------------+-+ | #|
195 // | horizontal layer | | | #|
196 // +--------------------+-+ | #| <-- 950px
197 // | | | #|
198 // | | |##################### |
199 // +----------------------+ +----------------------+ <-- 1000px
201 // The layer geometry is set up for a 950px tall viewport, but the user can
202 // actually see down to 1000px. Thus we have to move the quad for the
203 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
204 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
205 // means the quads may extend outside the layer's bounds.
207 // With the length known, we can compute the thumb's position.
208 float track_length = TrackLength();
209 int thumb_length = ThumbLength();
210 int thumb_thickness = ThumbThickness();
212 // With the length known, we can compute the thumb's position.
213 float clamped_current_pos =
214 std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_));
216 int thumb_offset = TrackStart();
217 if (maximum_ > 0) {
218 float ratio = clamped_current_pos / maximum_;
219 float max_offset = track_length - thumb_length;
220 thumb_offset += static_cast<int>(ratio * max_offset);
223 float thumb_thickness_adjustment =
224 thumb_thickness * (1.f - thumb_thickness_scale_factor_);
226 gfx::RectF thumb_rect;
227 if (orientation_ == HORIZONTAL) {
228 thumb_rect = gfx::RectF(thumb_offset,
229 vertical_adjust_ + thumb_thickness_adjustment,
230 thumb_length,
231 thumb_thickness - thumb_thickness_adjustment);
232 } else {
233 thumb_rect = gfx::RectF(
234 is_left_side_vertical_scrollbar_
235 ? bounds().width() - thumb_thickness
236 : thumb_thickness_adjustment,
237 thumb_offset,
238 thumb_thickness - thumb_thickness_adjustment,
239 thumb_length);
242 return gfx::ToEnclosingRect(thumb_rect);
245 void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize) {
246 if (!clip_layer_ || !scroll_layer_)
247 return;
249 scroll_layer_->SetScrollbarPosition(this, clip_layer_, on_resize);
252 } // namespace cc