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 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(),
51 ScrollbarLayerImplBase
* ScrollbarLayerImplBase::ToScrollbarLayer() {
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
)
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
);
79 void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds(
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
)
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 gfx::Rect
ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
98 const gfx::RectF
& layer_rect
) const {
99 // Don't intersect with the bounds as in LayerRectToContentRect() because
100 // layer_rect here might be in coordinates of the containing layer.
101 gfx::RectF content_rect
= gfx::ScaleRect(layer_rect
,
104 return gfx::ToEnclosingRect(content_rect
);
107 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos
) {
108 if (current_pos_
== current_pos
)
110 current_pos_
= current_pos
;
111 NoteLayerPropertyChanged();
115 bool ScrollbarLayerImplBase::SetMaximum(int maximum
) {
116 if (maximum_
== maximum
)
119 NoteLayerPropertyChanged();
123 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
126 return scroll_layer_
->user_scrollable(orientation()) && (0 < maximum());
129 bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust
) {
130 if (vertical_adjust_
== vertical_adjust
)
132 vertical_adjust_
= vertical_adjust
;
133 NoteLayerPropertyChanged();
137 bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio
) {
138 if (!IsThumbResizable())
141 if (visible_to_total_length_ratio_
== ratio
)
143 visible_to_total_length_ratio_
= ratio
;
144 NoteLayerPropertyChanged();
148 bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor
) {
149 if (thumb_thickness_scale_factor_
== factor
)
151 thumb_thickness_scale_factor_
= factor
;
152 NoteLayerPropertyChanged();
156 gfx::Rect
ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
157 // Thumb extent is the length of the thumb in the scrolling direction, thumb
158 // thickness is in the perpendicular direction. Here's an example of a
159 // horizontal scrollbar - inputs are above the scrollbar, computed values
162 // |<------------------- track_length_ ------------------->|
164 // |--| <-- start_offset
166 // +--+----------------------------+------------------+-------+--+
167 // |<|| |##################| ||>|
168 // +--+----------------------------+------------------+-------+--+
170 // |<- thumb_length ->|
172 // |<------- thumb_offset -------->|
174 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
175 // have to compute it. The ratio of the thumb's length to the track's length
176 // is the same as that of the visible viewport to the total viewport, unless
177 // that would make the thumb's length less than its thickness.
179 // vertical_adjust_ is used when the layer geometry from the main thread is
180 // not in sync with what the user sees. For instance on Android scrolling the
181 // top bar controls out of view reveals more of the page content. We want the
182 // root layer scrollbars to reflect what the user sees even if we haven't
183 // received new layer geometry from the main thread. If the user has scrolled
184 // down by 50px and the initial viewport size was 950px the geometry would
185 // look something like this:
187 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
188 // Layer geometry: Desired thumb positions:
189 // +--------------------+-+ +----------------------+ <-- 0px
204 // +--------------------+-+ | #|
205 // | horizontal layer | | | #|
206 // +--------------------+-+ | #| <-- 950px
208 // | | |##################### |
209 // +----------------------+ +----------------------+ <-- 1000px
211 // The layer geometry is set up for a 950px tall viewport, but the user can
212 // actually see down to 1000px. Thus we have to move the quad for the
213 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
214 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
215 // means the quads may extend outside the layer's bounds.
217 // With the length known, we can compute the thumb's position.
218 float track_length
= TrackLength();
219 int thumb_length
= ThumbLength();
220 int thumb_thickness
= ThumbThickness();
222 // With the length known, we can compute the thumb's position.
223 float clamped_current_pos
=
224 std::min(std::max(current_pos_
, 0.f
), static_cast<float>(maximum_
));
226 int thumb_offset
= TrackStart();
228 float ratio
= clamped_current_pos
/ maximum_
;
229 float max_offset
= track_length
- thumb_length
;
230 thumb_offset
+= static_cast<int>(ratio
* max_offset
);
233 float thumb_thickness_adjustment
=
234 thumb_thickness
* (1.f
- thumb_thickness_scale_factor_
);
236 gfx::RectF thumb_rect
;
237 if (orientation_
== HORIZONTAL
) {
238 thumb_rect
= gfx::RectF(thumb_offset
,
239 vertical_adjust_
+ thumb_thickness_adjustment
,
241 thumb_thickness
- thumb_thickness_adjustment
);
243 thumb_rect
= gfx::RectF(
244 is_left_side_vertical_scrollbar_
245 ? bounds().width() - thumb_thickness
246 : thumb_thickness_adjustment
,
248 thumb_thickness
- thumb_thickness_adjustment
,
252 return ScrollbarLayerRectToContentRect(thumb_rect
);
255 void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize
) {
256 if (!clip_layer_
|| !scroll_layer_
)
259 scroll_layer_
->SetScrollbarPosition(this, clip_layer_
, on_resize
);