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"
8 #include "cc/trees/layer_tree_impl.h"
9 #include "ui/gfx/rect_conversions.h"
13 ScrollbarLayerImplBase::ScrollbarLayerImplBase(
14 LayerTreeImpl
* tree_impl
,
16 ScrollbarOrientation orientation
,
17 bool is_left_side_vertical_scrollbar
,
19 : LayerImpl(tree_impl
, id
),
22 is_overlay_scrollbar_(is_overlay
),
23 thumb_thickness_scale_factor_(1.f
),
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
) {}
31 ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {}
33 void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl
* layer
) {
34 float active_opacity
= layer
->opacity();
35 LayerImpl::PushPropertiesTo(layer
);
36 layer
->SetOpacity(active_opacity
);
37 DCHECK(layer
->ToScrollbarLayer());
38 layer
->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_
);
39 PushScrollClipPropertiesTo(layer
);
42 void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl
* layer
) {
43 DCHECK(layer
->ToScrollbarLayer());
44 layer
->ToScrollbarLayer()->SetScrollLayerAndClipLayerByIds(ScrollLayerId(),
48 ScrollbarLayerImplBase
* ScrollbarLayerImplBase::ToScrollbarLayer() {
54 typedef void (LayerImpl::*ScrollbarRegistrationOperation
)(
55 ScrollbarLayerImplBase
*);
57 void RegisterScrollbarWithLayers(ScrollbarLayerImplBase
* scrollbar
,
58 LayerImpl
* container_layer
,
59 LayerImpl
* scroll_layer
,
60 ScrollbarRegistrationOperation operation
) {
61 if (!container_layer
|| !scroll_layer
)
66 // Scrollbars must be notifed of changes to their scroll and container layers
67 // and all scrollable layers in between.
68 for (LayerImpl
* current_layer
= scroll_layer
;
69 current_layer
&& current_layer
!= container_layer
->parent();
70 current_layer
= current_layer
->parent()) {
71 // TODO(wjmaclean) We shouldn't need to exempt the scroll_layer from the
72 // scrollable() test below. https://crbug.com/367858.
73 if (current_layer
->scrollable() || current_layer
== container_layer
||
74 current_layer
== scroll_layer
)
75 (current_layer
->*operation
)(scrollbar
);
80 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
83 LayerImpl
* scroll_layer
= layer_tree_impl()->LayerById(scroll_layer_id
);
84 LayerImpl
* clip_layer
= layer_tree_impl()->LayerById(clip_layer_id
);
85 if (scroll_layer_
== scroll_layer
&& clip_layer_
== clip_layer
)
88 RegisterScrollbarWithLayers(
89 this, clip_layer_
, scroll_layer_
, &LayerImpl::RemoveScrollbar
);
90 scroll_layer_
= scroll_layer
;
91 clip_layer_
= clip_layer
;
92 RegisterScrollbarWithLayers(
93 this, clip_layer_
, scroll_layer_
, &LayerImpl::AddScrollbar
);
95 ScrollbarParametersDidChange();
98 gfx::Rect
ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
99 const gfx::RectF
& layer_rect
) const {
100 // Don't intersect with the bounds as in LayerRectToContentRect() because
101 // layer_rect here might be in coordinates of the containing layer.
102 gfx::RectF content_rect
= gfx::ScaleRect(layer_rect
,
105 return gfx::ToEnclosingRect(content_rect
);
108 void ScrollbarLayerImplBase::SetCurrentPos(float current_pos
) {
109 if (current_pos_
== current_pos
)
111 current_pos_
= current_pos
;
112 NoteLayerPropertyChanged();
115 void ScrollbarLayerImplBase::SetMaximum(int maximum
) {
116 if (maximum_
== maximum
)
119 NoteLayerPropertyChanged();
122 void ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust
) {
123 if (vertical_adjust_
== vertical_adjust
)
125 vertical_adjust_
= vertical_adjust
;
126 NoteLayerPropertyChanged();
129 void ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio
) {
130 if (!IsThumbResizable())
133 if (visible_to_total_length_ratio_
== ratio
)
135 visible_to_total_length_ratio_
= ratio
;
136 NoteLayerPropertyChanged();
139 void ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor
) {
140 if (thumb_thickness_scale_factor_
== factor
)
142 thumb_thickness_scale_factor_
= factor
;
143 NoteLayerPropertyChanged();
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
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
194 // +--------------------+-+ | #|
195 // | horizontal layer | | | #|
196 // +--------------------+-+ | #| <-- 950px
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_
));
215 float ratio
= clamped_current_pos
/ maximum_
;
216 float max_offset
= track_length
- thumb_length
;
217 int thumb_offset
= static_cast<int>(ratio
* max_offset
) + TrackStart();
219 float thumb_thickness_adjustment
=
220 thumb_thickness
* (1.f
- thumb_thickness_scale_factor_
);
222 gfx::RectF thumb_rect
;
223 if (orientation_
== HORIZONTAL
) {
224 thumb_rect
= gfx::RectF(thumb_offset
,
225 vertical_adjust_
+ thumb_thickness_adjustment
,
227 thumb_thickness
- thumb_thickness_adjustment
);
229 thumb_rect
= gfx::RectF(
230 is_left_side_vertical_scrollbar_
231 ? bounds().width() - thumb_thickness
232 : thumb_thickness_adjustment
,
234 thumb_thickness
- thumb_thickness_adjustment
,
238 return ScrollbarLayerRectToContentRect(thumb_rect
);
241 void ScrollbarLayerImplBase::ScrollbarParametersDidChange() {
242 if (!clip_layer_
|| !scroll_layer_
)
245 scroll_layer_
->SetScrollbarPosition(this, clip_layer_
);