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 #ifndef CC_LAYERS_SCROLLBAR_LAYER_IMPL_BASE_H_
6 #define CC_LAYERS_SCROLLBAR_LAYER_IMPL_BASE_H_
7 
8 #include "base/containers/flat_set.h"
9 #include "cc/cc_export.h"
10 #include "cc/input/scrollbar.h"
11 #include "cc/layers/layer.h"
12 #include "cc/layers/layer_impl.h"
13 #include "cc/trees/layer_tree_settings.h"
14 
15 namespace cc {
16 
17 class LayerTreeImpl;
18 
19 class CC_EXPORT ScrollbarLayerImplBase : public LayerImpl {
20  public:
21   ScrollbarLayerImplBase(const ScrollbarLayerImplBase&) = delete;
22   ScrollbarLayerImplBase& operator=(const ScrollbarLayerImplBase&) = delete;
23 
scroll_element_id()24   ElementId scroll_element_id() const { return scroll_element_id_; }
25   void SetScrollElementId(ElementId scroll_element_id);
26 
27   // The following setters should be called when updating scrollbar geometries
28   // (see: LayerTreeImpl::UpdateScrollbarGeometries).
29   bool SetCurrentPos(float current_pos);
30   void SetClipLayerLength(float clip_layer_length);
31   void SetScrollLayerLength(float scroll_layer_length);
32   void SetVerticalAdjust(float vertical_adjust);
33 
34   float current_pos() const;
35   float clip_layer_length() const;
36   float scroll_layer_length() const;
37   float vertical_adjust() const;
38 
is_overlay_scrollbar()39   bool is_overlay_scrollbar() const { return is_overlay_scrollbar_; }
set_is_overlay_scrollbar(bool is_overlay)40   void set_is_overlay_scrollbar(bool is_overlay) {
41     is_overlay_scrollbar_ = is_overlay;
42   }
43 
orientation()44   ScrollbarOrientation orientation() const { return orientation_; }
is_left_side_vertical_scrollbar()45   bool is_left_side_vertical_scrollbar() {
46     return is_left_side_vertical_scrollbar_;
47   }
48 
49   bool CanScrollOrientation() const;
50 
51   void PushPropertiesTo(LayerImpl* layer) override;
52 
53   // Thumb quad rect in layer space.
54   gfx::Rect ComputeThumbQuadRect() const;
55   gfx::Rect ComputeExpandedThumbQuadRect() const;
56 
thumb_thickness_scale_factor()57   float thumb_thickness_scale_factor() {
58     return thumb_thickness_scale_factor_;
59   }
60   void SetThumbThicknessScaleFactor(float thumb_thickness_scale_factor);
61 
62   virtual int ThumbThickness() const = 0;
63 
64   void SetOverlayScrollbarLayerOpacityAnimated(float opacity);
65 
66   virtual LayerTreeSettings::ScrollbarAnimator GetScrollbarAnimator() const;
67 
68   virtual float TrackLength() const = 0;
69   virtual int ThumbLength() const = 0;
70   virtual gfx::Rect BackButtonRect() const;
71   virtual gfx::Rect ForwardButtonRect() const;
72   virtual gfx::Rect BackTrackRect() const;
73   virtual gfx::Rect ForwardTrackRect() const;
74   virtual bool SupportsDragSnapBack() const;
75   virtual bool JumpOnTrackClick() const;
76   virtual ScrollbarPart IdentifyScrollbarPart(
77       const gfx::PointF position_in_widget) const;
78   // Only PaintedOverlayScrollbar(Aura Overlay Scrollbar) need to know
79   // tickmarks's state.
80   virtual bool HasFindInPageTickmarks() const;
81 
82   // Mac overlay scrollbars are faded during paint but the compositor layer is
83   // always fully opaque where as Aura scrollbars fade by animating the layer
84   // opacity. This method will return the user visible opacity of an overlay
85   // scrollbar regardless of the underlying mechanism or platform.
86   virtual float OverlayScrollbarOpacity() const;
87 
88  protected:
89   ScrollbarLayerImplBase(LayerTreeImpl* tree_impl,
90                          int id,
91                          ScrollbarOrientation orientation,
92                          bool is_left_side_vertical_scrollbar,
93                          bool is_overlay);
94   ~ScrollbarLayerImplBase() override;
95 
96   virtual int TrackStart() const = 0;
97   // Indicates whether the thumb length can be changed without going back to the
98   // main thread.
99   virtual bool IsThumbResizable() const = 0;
100 
101  private:
102   bool IsScrollbarLayer() const final;
103 
104   gfx::Rect ComputeThumbQuadRectWithThumbThicknessScale(
105       float thumb_thickness_scale_factor) const;
106 
107   ElementId scroll_element_id_;
108   bool is_overlay_scrollbar_;
109 
110   float thumb_thickness_scale_factor_;
111   float current_pos_;
112   float clip_layer_length_;
113   float scroll_layer_length_;
114   ScrollbarOrientation orientation_;
115   bool is_left_side_vertical_scrollbar_;
116 
117   // Difference between the clip layer's height and the visible viewport
118   // height (which may differ in the presence of top-controls hiding).
119   float vertical_adjust_;
120 
121   FRIEND_TEST_ALL_PREFIXES(ScrollbarLayerTest,
122                            ScrollElementIdPushedAcrossCommit);
123 };
124 
ToScrollbarLayer(LayerImpl * layer)125 inline ScrollbarLayerImplBase* ToScrollbarLayer(LayerImpl* layer) {
126   DCHECK(layer->IsScrollbarLayer());
127   return static_cast<ScrollbarLayerImplBase*>(layer);
128 }
129 
ToScrollbarLayer(const LayerImpl * layer)130 inline const ScrollbarLayerImplBase* ToScrollbarLayer(const LayerImpl* layer) {
131   DCHECK(layer->IsScrollbarLayer());
132   return static_cast<const ScrollbarLayerImplBase*>(layer);
133 }
134 
135 using ScrollbarSet = base::flat_set<ScrollbarLayerImplBase*>;
136 
137 }  // namespace cc
138 
139 #endif  // CC_LAYERS_SCROLLBAR_LAYER_IMPL_BASE_H_
140