Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_impl_base.cc
blobbeac79f6cca24c68dc17253f1193a6918f5998a2
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 LayerImpl::PushPropertiesTo(layer);
37 layer->SetOpacity(active_opacity);
38 DCHECK(layer->ToScrollbarLayer());
39 layer->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_);
40 PushScrollClipPropertiesTo(layer);
43 void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl* layer) {
44 DCHECK(layer->ToScrollbarLayer());
45 layer->ToScrollbarLayer()->SetScrollLayerAndClipLayerByIds(ScrollLayerId(),
46 ClipLayerId());
49 ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() {
50 return this;
53 namespace {
55 typedef void (LayerImpl::*ScrollbarRegistrationOperation)(
56 ScrollbarLayerImplBase*);
58 void RegisterScrollbarWithLayers(ScrollbarLayerImplBase* scrollbar,
59 LayerImpl* container_layer,
60 LayerImpl* scroll_layer,
61 ScrollbarRegistrationOperation operation) {
62 if (!container_layer || !scroll_layer)
63 return;
65 DCHECK(scrollbar);
67 // Scrollbars must be notifed of changes to their scroll and container layers
68 // and all scrollable layers in between.
69 for (LayerImpl* current_layer = scroll_layer;
70 current_layer && current_layer != container_layer->parent();
71 current_layer = current_layer->parent()) {
72 (current_layer->*operation)(scrollbar);
75 } // namespace
77 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
78 int scroll_layer_id,
79 int clip_layer_id) {
80 LayerImpl* scroll_layer = layer_tree_impl()->LayerById(scroll_layer_id);
81 LayerImpl* clip_layer = layer_tree_impl()->LayerById(clip_layer_id);
82 if (scroll_layer_ == scroll_layer && clip_layer_ == clip_layer)
83 return;
85 RegisterScrollbarWithLayers(
86 this, clip_layer_, scroll_layer_, &LayerImpl::RemoveScrollbar);
87 scroll_layer_ = scroll_layer;
88 clip_layer_ = clip_layer;
89 RegisterScrollbarWithLayers(
90 this, clip_layer_, scroll_layer_, &LayerImpl::AddScrollbar);
92 ScrollbarParametersDidChange(false);
95 gfx::Rect ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
96 const gfx::RectF& layer_rect) const {
97 // Don't intersect with the bounds as in LayerRectToContentRect() because
98 // layer_rect here might be in coordinates of the containing layer.
99 gfx::RectF content_rect = gfx::ScaleRect(layer_rect,
100 contents_scale_x(),
101 contents_scale_y());
102 return gfx::ToEnclosingRect(content_rect);
105 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos) {
106 if (current_pos_ == current_pos)
107 return false;
108 current_pos_ = current_pos;
109 NoteLayerPropertyChanged();
110 return true;
113 bool ScrollbarLayerImplBase::SetMaximum(int maximum) {
114 if (maximum_ == maximum)
115 return false;
116 maximum_ = maximum;
117 NoteLayerPropertyChanged();
118 return true;
121 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
122 if (!scroll_layer_)
123 return false;
124 return scroll_layer_->user_scrollable(orientation()) && (0 < maximum());
127 bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
128 if (vertical_adjust_ == vertical_adjust)
129 return false;
130 vertical_adjust_ = vertical_adjust;
131 NoteLayerPropertyChanged();
132 return true;
135 bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) {
136 if (!IsThumbResizable())
137 return false;
139 if (visible_to_total_length_ratio_ == ratio)
140 return false;
141 visible_to_total_length_ratio_ = ratio;
142 NoteLayerPropertyChanged();
143 return true;
146 bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) {
147 if (thumb_thickness_scale_factor_ == factor)
148 return false;
149 thumb_thickness_scale_factor_ = factor;
150 NoteLayerPropertyChanged();
151 return true;
154 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
155 // Thumb extent is the length of the thumb in the scrolling direction, thumb
156 // thickness is in the perpendicular direction. Here's an example of a
157 // horizontal scrollbar - inputs are above the scrollbar, computed values
158 // below:
160 // |<------------------- track_length_ ------------------->|
162 // |--| <-- start_offset
164 // +--+----------------------------+------------------+-------+--+
165 // |<|| |##################| ||>|
166 // +--+----------------------------+------------------+-------+--+
168 // |<- thumb_length ->|
170 // |<------- thumb_offset -------->|
172 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
173 // have to compute it. The ratio of the thumb's length to the track's length
174 // is the same as that of the visible viewport to the total viewport, unless
175 // that would make the thumb's length less than its thickness.
177 // vertical_adjust_ is used when the layer geometry from the main thread is
178 // not in sync with what the user sees. For instance on Android scrolling the
179 // top bar controls out of view reveals more of the page content. We want the
180 // root layer scrollbars to reflect what the user sees even if we haven't
181 // received new layer geometry from the main thread. If the user has scrolled
182 // down by 50px and the initial viewport size was 950px the geometry would
183 // look something like this:
185 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
186 // Layer geometry: Desired thumb positions:
187 // +--------------------+-+ +----------------------+ <-- 0px
188 // | |v| | #|
189 // | |e| | #|
190 // | |r| | #|
191 // | |t| | #|
192 // | |i| | #|
193 // | |c| | #|
194 // | |a| | #|
195 // | |l| | #|
196 // | | | | #|
197 // | |l| | #|
198 // | |a| | #|
199 // | |y| | #|
200 // | |e| | #|
201 // | |r| | #|
202 // +--------------------+-+ | #|
203 // | horizontal layer | | | #|
204 // +--------------------+-+ | #| <-- 950px
205 // | | | #|
206 // | | |##################### |
207 // +----------------------+ +----------------------+ <-- 1000px
209 // The layer geometry is set up for a 950px tall viewport, but the user can
210 // actually see down to 1000px. Thus we have to move the quad for the
211 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
212 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
213 // means the quads may extend outside the layer's bounds.
215 // With the length known, we can compute the thumb's position.
216 float track_length = TrackLength();
217 int thumb_length = ThumbLength();
218 int thumb_thickness = ThumbThickness();
220 // With the length known, we can compute the thumb's position.
221 float clamped_current_pos =
222 std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_));
224 int thumb_offset = TrackStart();
225 if (maximum_ > 0) {
226 float ratio = clamped_current_pos / maximum_;
227 float max_offset = track_length - thumb_length;
228 thumb_offset += static_cast<int>(ratio * max_offset);
231 float thumb_thickness_adjustment =
232 thumb_thickness * (1.f - thumb_thickness_scale_factor_);
234 gfx::RectF thumb_rect;
235 if (orientation_ == HORIZONTAL) {
236 thumb_rect = gfx::RectF(thumb_offset,
237 vertical_adjust_ + thumb_thickness_adjustment,
238 thumb_length,
239 thumb_thickness - thumb_thickness_adjustment);
240 } else {
241 thumb_rect = gfx::RectF(
242 is_left_side_vertical_scrollbar_
243 ? bounds().width() - thumb_thickness
244 : thumb_thickness_adjustment,
245 thumb_offset,
246 thumb_thickness - thumb_thickness_adjustment,
247 thumb_length);
250 return ScrollbarLayerRectToContentRect(thumb_rect);
253 void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize) {
254 if (!clip_layer_ || !scroll_layer_)
255 return;
257 scroll_layer_->SetScrollbarPosition(this, clip_layer_, on_resize);
260 } // namespace cc