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.
4
5 #include "cc/layers/scrollbar_layer_impl_base.h"
6
7 #include <algorithm>
8
9 #include "base/numerics/ranges.h"
10 #include "cc/trees/effect_node.h"
11 #include "cc/trees/layer_tree_impl.h"
12 #include "cc/trees/scroll_node.h"
13 #include "ui/gfx/geometry/rect_conversions.h"
14
15 namespace cc {
16
ScrollbarLayerImplBase(LayerTreeImpl * tree_impl,int id,ScrollbarOrientation orientation,bool is_left_side_vertical_scrollbar,bool is_overlay)17 ScrollbarLayerImplBase::ScrollbarLayerImplBase(
18 LayerTreeImpl* tree_impl,
19 int id,
20 ScrollbarOrientation orientation,
21 bool is_left_side_vertical_scrollbar,
22 bool is_overlay)
23 : LayerImpl(tree_impl, id),
24 is_overlay_scrollbar_(is_overlay),
25 thumb_thickness_scale_factor_(1.f),
26 current_pos_(0.f),
27 clip_layer_length_(0.f),
28 scroll_layer_length_(0.f),
29 orientation_(orientation),
30 is_left_side_vertical_scrollbar_(is_left_side_vertical_scrollbar),
31 vertical_adjust_(0.f) {}
32
~ScrollbarLayerImplBase()33 ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {
34 layer_tree_impl()->UnregisterScrollbar(this);
35 }
36
PushPropertiesTo(LayerImpl * layer)37 void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) {
38 LayerImpl::PushPropertiesTo(layer);
39 DCHECK(layer->IsScrollbarLayer());
40 ScrollbarLayerImplBase* scrollbar_layer = ToScrollbarLayer(layer);
41 scrollbar_layer->set_is_overlay_scrollbar(is_overlay_scrollbar_);
42 scrollbar_layer->SetScrollElementId(scroll_element_id());
43 }
44
IsScrollbarLayer() const45 bool ScrollbarLayerImplBase::IsScrollbarLayer() const {
46 return true;
47 }
48
SetScrollElementId(ElementId scroll_element_id)49 void ScrollbarLayerImplBase::SetScrollElementId(ElementId scroll_element_id) {
50 if (scroll_element_id_ == scroll_element_id)
51 return;
52
53 layer_tree_impl()->UnregisterScrollbar(this);
54 scroll_element_id_ = scroll_element_id;
55 layer_tree_impl()->RegisterScrollbar(this);
56 }
57
SetCurrentPos(float current_pos)58 bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos) {
59 if (current_pos_ == current_pos)
60 return false;
61 current_pos_ = current_pos;
62 NoteLayerPropertyChanged();
63 return true;
64 }
65
current_pos() const66 float ScrollbarLayerImplBase::current_pos() const {
67 DCHECK(!layer_tree_impl()->ScrollbarGeometriesNeedUpdate());
68 return current_pos_;
69 }
70
clip_layer_length() const71 float ScrollbarLayerImplBase::clip_layer_length() const {
72 DCHECK(!layer_tree_impl()->ScrollbarGeometriesNeedUpdate());
73 return clip_layer_length_;
74 }
75
scroll_layer_length() const76 float ScrollbarLayerImplBase::scroll_layer_length() const {
77 DCHECK(!layer_tree_impl()->ScrollbarGeometriesNeedUpdate());
78 return scroll_layer_length_;
79 }
80
vertical_adjust() const81 float ScrollbarLayerImplBase::vertical_adjust() const {
82 DCHECK(!layer_tree_impl()->ScrollbarGeometriesNeedUpdate());
83 return vertical_adjust_;
84 }
85
CanScrollOrientation() const86 bool ScrollbarLayerImplBase::CanScrollOrientation() const {
87 PropertyTrees* property_trees = layer_tree_impl()->property_trees();
88 const auto* scroll_node =
89 property_trees->scroll_tree.FindNodeFromElementId(scroll_element_id_);
90 DCHECK(scroll_node);
91 // TODO(bokan): Looks like we sometimes get here without a ScrollNode. It
92 // should be safe to just return false here (we don't use scroll_element_id_
93 // anywhere else) so we can merge the fix. Once merged, will investigate the
94 // underlying cause. https://crbug.com/924068.
95 if (!scroll_node)
96 return false;
97
98 if (orientation() == ScrollbarOrientation::HORIZONTAL) {
99 if (!scroll_node->user_scrollable_horizontal)
100 return false;
101 } else {
102 if (!scroll_node->user_scrollable_vertical)
103 return false;
104 }
105
106 // Ensure the clip_layer_length and scroll_layer_length values are up-to-date.
107 // TODO(pdr): Instead of using the clip and scroll layer lengths which require
108 // an update, refactor to use the scroll tree (ScrollTree::MaxScrollOffset
109 // as in LayerTreeHostImpl::TryScroll).
110 layer_tree_impl()->UpdateScrollbarGeometries();
111
112 // Ensure clip_layer_length is smaller than scroll_layer_length, not including
113 // small deltas due to floating point error.
114 return !MathUtil::IsFloatNearlyTheSame(clip_layer_length(),
115 scroll_layer_length()) &&
116 clip_layer_length() < scroll_layer_length();
117 }
118
SetVerticalAdjust(float vertical_adjust)119 void ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
120 if (vertical_adjust_ == vertical_adjust)
121 return;
122 vertical_adjust_ = vertical_adjust;
123 NoteLayerPropertyChanged();
124 }
125
SetClipLayerLength(float clip_layer_length)126 void ScrollbarLayerImplBase::SetClipLayerLength(float clip_layer_length) {
127 if (clip_layer_length_ == clip_layer_length)
128 return;
129 clip_layer_length_ = clip_layer_length;
130 NoteLayerPropertyChanged();
131 }
132
SetScrollLayerLength(float scroll_layer_length)133 void ScrollbarLayerImplBase::SetScrollLayerLength(float scroll_layer_length) {
134 if (scroll_layer_length_ == scroll_layer_length)
135 return;
136 scroll_layer_length_ = scroll_layer_length;
137 NoteLayerPropertyChanged();
138 return;
139 }
140
SetThumbThicknessScaleFactor(float factor)141 void ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) {
142 if (thumb_thickness_scale_factor_ == factor)
143 return;
144 thumb_thickness_scale_factor_ = factor;
145 NoteLayerPropertyChanged();
146 }
147
ComputeThumbQuadRectWithThumbThicknessScale(float thumb_thickness_scale_factor) const148 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRectWithThumbThicknessScale(
149 float thumb_thickness_scale_factor) const {
150 // Thumb extent is the length of the thumb in the scrolling direction, thumb
151 // thickness is in the perpendicular direction. Here's an example of a
152 // horizontal scrollbar - inputs are above the scrollbar, computed values
153 // below:
154 //
155 // |<------------------- track_length_ ------------------->|
156 //
157 // |--| <-- start_offset
158 //
159 // +--+----------------------------+------------------+-------+--+
160 // |<|| |##################| ||>|
161 // +--+----------------------------+------------------+-------+--+
162 //
163 // |<- thumb_length ->|
164 //
165 // |<------- thumb_offset -------->|
166 //
167 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
168 // have to compute it. The ratio of the thumb's length to the track's length
169 // is the same as that of the visible viewport to the total viewport, unless
170 // that would make the thumb's length less than its thickness.
171 //
172 // vertical_adjust_ is used when the layer geometry from the main thread is
173 // not in sync with what the user sees. For instance on Android scrolling the
174 // top bar controls out of view reveals more of the page content. We want the
175 // root layer scrollbars to reflect what the user sees even if we haven't
176 // received new layer geometry from the main thread. If the user has scrolled
177 // down by 50px and the initial viewport size was 950px the geometry would
178 // look something like this:
179 //
180 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
181 // Layer geometry: Desired thumb positions:
182 // +--------------------+-+ +----------------------+ <-- 0px
183 // | |v| | #|
184 // | |e| | #|
185 // | |r| | #|
186 // | |t| | #|
187 // | |i| | #|
188 // | |c| | #|
189 // | |a| | #|
190 // | |l| | #|
191 // | | | | #|
192 // | |l| | #|
193 // | |a| | #|
194 // | |y| | #|
195 // | |e| | #|
196 // | |r| | #|
197 // +--------------------+-+ | #|
198 // | horizontal layer | | | #|
199 // +--------------------+-+ | #| <-- 950px
200 // | | | #|
201 // | | |##################### |
202 // +----------------------+ +----------------------+ <-- 1000px
203 //
204 // The layer geometry is set up for a 950px tall viewport, but the user can
205 // actually see down to 1000px. Thus we have to move the quad for the
206 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
207 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
208 // means the quads may extend outside the layer's bounds.
209
210 // With the length known, we can compute the thumb's position.
211 float track_length = TrackLength();
212 int thumb_length = ThumbLength();
213 int thumb_thickness = ThumbThickness();
214 float maximum = scroll_layer_length() - clip_layer_length();
215
216 // With the length known, we can compute the thumb's position.
217 float clamped_current_pos = base::ClampToRange(current_pos(), 0.0f, maximum);
218
219 int thumb_offset = TrackStart();
220 if (maximum > 0) {
221 float ratio = clamped_current_pos / maximum;
222 float _max_offset = track_length - thumb_length;
223 thumb_offset += static_cast<int>(ratio * _max_offset);
224 }
225
226 float thumb_thickness_adjustment =
227 thumb_thickness * (1.f - thumb_thickness_scale_factor);
228
229 gfx::RectF thumb_rect;
230 if (orientation_ == ScrollbarOrientation::HORIZONTAL) {
231 thumb_rect = gfx::RectF(thumb_offset,
232 vertical_adjust_ + thumb_thickness_adjustment,
233 thumb_length,
234 thumb_thickness - thumb_thickness_adjustment);
235 } else {
236 thumb_rect = gfx::RectF(
237 is_left_side_vertical_scrollbar_
238 ? bounds().width() - thumb_thickness
239 : thumb_thickness_adjustment,
240 thumb_offset,
241 thumb_thickness - thumb_thickness_adjustment,
242 thumb_length);
243 }
244
245 return gfx::ToEnclosingRect(thumb_rect);
246 }
247
ComputeExpandedThumbQuadRect() const248 gfx::Rect ScrollbarLayerImplBase::ComputeExpandedThumbQuadRect() const {
249 DCHECK(is_overlay_scrollbar());
250 return ComputeThumbQuadRectWithThumbThicknessScale(1.f);
251 }
252
ComputeThumbQuadRect() const253 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
254 return ComputeThumbQuadRectWithThumbThicknessScale(
255 thumb_thickness_scale_factor_);
256 }
257
SetOverlayScrollbarLayerOpacityAnimated(float opacity)258 void ScrollbarLayerImplBase::SetOverlayScrollbarLayerOpacityAnimated(
259 float opacity) {
260 DCHECK(is_overlay_scrollbar());
261 if (!layer_tree_impl())
262 return;
263
264 PropertyTrees* property_trees = layer_tree_impl()->property_trees();
265
266 EffectNode* node = property_trees->effect_tree.Node(effect_tree_index());
267 if (node->opacity == opacity)
268 return;
269
270 node->opacity = opacity;
271 node->effect_changed = true;
272 property_trees->changed = true;
273 property_trees->effect_tree.set_needs_update(true);
274 layer_tree_impl()->set_needs_update_draw_properties();
275 }
276
277 LayerTreeSettings::ScrollbarAnimator
GetScrollbarAnimator() const278 ScrollbarLayerImplBase::GetScrollbarAnimator() const {
279 return layer_tree_impl()->settings().scrollbar_animator;
280 }
281
HasFindInPageTickmarks() const282 bool ScrollbarLayerImplBase::HasFindInPageTickmarks() const {
283 return false;
284 }
285
OverlayScrollbarOpacity() const286 float ScrollbarLayerImplBase::OverlayScrollbarOpacity() const {
287 return Opacity();
288 }
289
SupportsDragSnapBack() const290 bool ScrollbarLayerImplBase::SupportsDragSnapBack() const {
291 return false;
292 }
293
JumpOnTrackClick() const294 bool ScrollbarLayerImplBase::JumpOnTrackClick() const {
295 return false;
296 }
297
BackButtonRect() const298 gfx::Rect ScrollbarLayerImplBase::BackButtonRect() const {
299 return gfx::Rect(0, 0);
300 }
301
ForwardButtonRect() const302 gfx::Rect ScrollbarLayerImplBase::ForwardButtonRect() const {
303 return gfx::Rect(0, 0);
304 }
305
BackTrackRect() const306 gfx::Rect ScrollbarLayerImplBase::BackTrackRect() const {
307 return gfx::Rect(0, 0);
308 }
309
ForwardTrackRect() const310 gfx::Rect ScrollbarLayerImplBase::ForwardTrackRect() const {
311 return gfx::Rect(0, 0);
312 }
313
314 // This manages identifying which part of a composited scrollbar got hit based
315 // on the position_in_widget.
IdentifyScrollbarPart(const gfx::PointF position_in_widget) const316 ScrollbarPart ScrollbarLayerImplBase::IdentifyScrollbarPart(
317 const gfx::PointF position_in_widget) const {
318 const gfx::Point pointer_location(position_in_widget.x(),
319 position_in_widget.y());
320 if (BackButtonRect().Contains(pointer_location))
321 return ScrollbarPart::BACK_BUTTON;
322
323 if (ForwardButtonRect().Contains(pointer_location))
324 return ScrollbarPart::FORWARD_BUTTON;
325
326 if (ComputeThumbQuadRect().Contains(pointer_location))
327 return ScrollbarPart::THUMB;
328
329 if (BackTrackRect().Contains(pointer_location))
330 return ScrollbarPart::BACK_TRACK;
331
332 if (ForwardTrackRect().Contains(pointer_location))
333 return ScrollbarPart::FORWARD_TRACK;
334
335 // TODO(arakeri): Once crbug.com/952314 is fixed, add a DCHECK to verify that
336 // the point that is passed in is within the TrackRect. Also, please note that
337 // hit testing other scrollbar parts is not yet implemented.
338 return ScrollbarPart::NO_PART;
339 }
340
341 } // namespace cc
342