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/geometry/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
),
20 scroll_layer_(nullptr),
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
) {
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(),
49 ScrollbarLayerImplBase
* ScrollbarLayerImplBase::ToScrollbarLayer() {
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
)
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 // TODO(wjmaclean) We shouldn't need to exempt the scroll_layer from the
73 // scrollable() test below. https://crbug.com/367858.
74 if (current_layer
->scrollable() || current_layer
== container_layer
||
75 current_layer
== scroll_layer
)
76 (current_layer
->*operation
)(scrollbar
);
81 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
84 LayerImpl
* scroll_layer
= layer_tree_impl()->LayerById(scroll_layer_id
);
85 LayerImpl
* clip_layer
= layer_tree_impl()->LayerById(clip_layer_id
);
86 if (scroll_layer_
== scroll_layer
&& clip_layer_
== clip_layer
)
89 RegisterScrollbarWithLayers(
90 this, clip_layer_
, scroll_layer_
, &LayerImpl::RemoveScrollbar
);
91 scroll_layer_
= scroll_layer
;
92 clip_layer_
= clip_layer
;
93 RegisterScrollbarWithLayers(
94 this, clip_layer_
, scroll_layer_
, &LayerImpl::AddScrollbar
);
96 ScrollbarParametersDidChange(false);
99 gfx::Rect
ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
100 const gfx::RectF
& layer_rect
) const {
101 // Don't intersect with the bounds as in LayerRectToContentRect() because
102 // layer_rect here might be in coordinates of the containing layer.
103 gfx::RectF content_rect
= gfx::ScaleRect(layer_rect
,
106 return gfx::ToEnclosingRect(content_rect
);
109 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos
) {
110 if (current_pos_
== current_pos
)
112 current_pos_
= current_pos
;
113 NoteLayerPropertyChanged();
117 bool ScrollbarLayerImplBase::SetMaximum(int maximum
) {
118 if (maximum_
== maximum
)
121 NoteLayerPropertyChanged();
125 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
128 return scroll_layer_
->user_scrollable(orientation()) && (0 < maximum());
131 bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust
) {
132 if (vertical_adjust_
== vertical_adjust
)
134 vertical_adjust_
= vertical_adjust
;
135 NoteLayerPropertyChanged();
139 bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio
) {
140 if (!IsThumbResizable())
143 if (visible_to_total_length_ratio_
== ratio
)
145 visible_to_total_length_ratio_
= ratio
;
146 NoteLayerPropertyChanged();
150 bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor
) {
151 if (thumb_thickness_scale_factor_
== factor
)
153 thumb_thickness_scale_factor_
= factor
;
154 NoteLayerPropertyChanged();
158 gfx::Rect
ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
159 // Thumb extent is the length of the thumb in the scrolling direction, thumb
160 // thickness is in the perpendicular direction. Here's an example of a
161 // horizontal scrollbar - inputs are above the scrollbar, computed values
164 // |<------------------- track_length_ ------------------->|
166 // |--| <-- start_offset
168 // +--+----------------------------+------------------+-------+--+
169 // |<|| |##################| ||>|
170 // +--+----------------------------+------------------+-------+--+
172 // |<- thumb_length ->|
174 // |<------- thumb_offset -------->|
176 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
177 // have to compute it. The ratio of the thumb's length to the track's length
178 // is the same as that of the visible viewport to the total viewport, unless
179 // that would make the thumb's length less than its thickness.
181 // vertical_adjust_ is used when the layer geometry from the main thread is
182 // not in sync with what the user sees. For instance on Android scrolling the
183 // top bar controls out of view reveals more of the page content. We want the
184 // root layer scrollbars to reflect what the user sees even if we haven't
185 // received new layer geometry from the main thread. If the user has scrolled
186 // down by 50px and the initial viewport size was 950px the geometry would
187 // look something like this:
189 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
190 // Layer geometry: Desired thumb positions:
191 // +--------------------+-+ +----------------------+ <-- 0px
206 // +--------------------+-+ | #|
207 // | horizontal layer | | | #|
208 // +--------------------+-+ | #| <-- 950px
210 // | | |##################### |
211 // +----------------------+ +----------------------+ <-- 1000px
213 // The layer geometry is set up for a 950px tall viewport, but the user can
214 // actually see down to 1000px. Thus we have to move the quad for the
215 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
216 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
217 // means the quads may extend outside the layer's bounds.
219 // With the length known, we can compute the thumb's position.
220 float track_length
= TrackLength();
221 int thumb_length
= ThumbLength();
222 int thumb_thickness
= ThumbThickness();
224 // With the length known, we can compute the thumb's position.
225 float clamped_current_pos
=
226 std::min(std::max(current_pos_
, 0.f
), static_cast<float>(maximum_
));
228 int thumb_offset
= TrackStart();
230 float ratio
= clamped_current_pos
/ maximum_
;
231 float max_offset
= track_length
- thumb_length
;
232 thumb_offset
+= static_cast<int>(ratio
* max_offset
);
235 float thumb_thickness_adjustment
=
236 thumb_thickness
* (1.f
- thumb_thickness_scale_factor_
);
238 gfx::RectF thumb_rect
;
239 if (orientation_
== HORIZONTAL
) {
240 thumb_rect
= gfx::RectF(thumb_offset
,
241 vertical_adjust_
+ thumb_thickness_adjustment
,
243 thumb_thickness
- thumb_thickness_adjustment
);
245 thumb_rect
= gfx::RectF(
246 is_left_side_vertical_scrollbar_
247 ? bounds().width() - thumb_thickness
248 : thumb_thickness_adjustment
,
250 thumb_thickness
- thumb_thickness_adjustment
,
254 return ScrollbarLayerRectToContentRect(thumb_rect
);
257 void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize
) {
258 if (!clip_layer_
|| !scroll_layer_
)
261 scroll_layer_
->SetScrollbarPosition(this, clip_layer_
, on_resize
);