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