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 (current_layer
->*operation
)(scrollbar
);
77 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
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
)
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
,
102 return gfx::ToEnclosingRect(content_rect
);
105 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos
) {
106 if (current_pos_
== current_pos
)
108 current_pos_
= current_pos
;
109 NoteLayerPropertyChanged();
113 bool ScrollbarLayerImplBase::SetMaximum(int maximum
) {
114 if (maximum_
== maximum
)
117 NoteLayerPropertyChanged();
121 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
124 return scroll_layer_
->user_scrollable(orientation()) && (0 < maximum());
127 bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust
) {
128 if (vertical_adjust_
== vertical_adjust
)
130 vertical_adjust_
= vertical_adjust
;
131 NoteLayerPropertyChanged();
135 bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio
) {
136 if (!IsThumbResizable())
139 if (visible_to_total_length_ratio_
== ratio
)
141 visible_to_total_length_ratio_
= ratio
;
142 NoteLayerPropertyChanged();
146 bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor
) {
147 if (thumb_thickness_scale_factor_
== factor
)
149 thumb_thickness_scale_factor_
= factor
;
150 NoteLayerPropertyChanged();
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
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
202 // +--------------------+-+ | #|
203 // | horizontal layer | | | #|
204 // +--------------------+-+ | #| <-- 950px
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();
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
,
239 thumb_thickness
- thumb_thickness_adjustment
);
241 thumb_rect
= gfx::RectF(
242 is_left_side_vertical_scrollbar_
243 ? bounds().width() - thumb_thickness
244 : thumb_thickness_adjustment
,
246 thumb_thickness
- thumb_thickness_adjustment
,
250 return ScrollbarLayerRectToContentRect(thumb_rect
);
253 void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize
) {
254 if (!clip_layer_
|| !scroll_layer_
)
257 scroll_layer_
->SetScrollbarPosition(this, clip_layer_
, on_resize
);