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_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_
6 #define CC_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_
7 
8 #include <memory>
9 
10 #include "base/time/time.h"
11 #include "cc/cc_export.h"
12 #include "cc/input/scrollbar.h"
13 #include "cc/layers/layer_impl.h"
14 #include "cc/layers/scrollbar_layer_impl_base.h"
15 #include "ui/gfx/geometry/vector2d_f.h"
16 
17 namespace cc {
18 
19 class ScrollbarAnimationControllerClient;
20 
21 // ScrollbarAnimationControllerThinning for one scrollbar
22 class CC_EXPORT SingleScrollbarAnimationControllerThinning {
23  public:
24   static constexpr float kIdleThicknessScale = 0.4f;
25   static constexpr float kMouseMoveDistanceToTriggerExpand = 25.f;
26 
27   static std::unique_ptr<SingleScrollbarAnimationControllerThinning> Create(
28       ElementId scroll_element_id,
29       ScrollbarOrientation orientation,
30       ScrollbarAnimationControllerClient* client,
31       base::TimeDelta thinning_duration);
32 
33   SingleScrollbarAnimationControllerThinning(
34       const SingleScrollbarAnimationControllerThinning&) = delete;
35   ~SingleScrollbarAnimationControllerThinning() = default;
36 
37   SingleScrollbarAnimationControllerThinning& operator=(
38       const SingleScrollbarAnimationControllerThinning&) = delete;
39 
mouse_is_over_scrollbar_thumb()40   bool mouse_is_over_scrollbar_thumb() const {
41     return mouse_is_over_scrollbar_thumb_;
42   }
mouse_is_near_scrollbar_thumb()43   bool mouse_is_near_scrollbar_thumb() const {
44     return mouse_is_near_scrollbar_thumb_;
45   }
mouse_is_near_scrollbar_track()46   bool mouse_is_near_scrollbar_track() const {
47     return mouse_is_near_scrollbar_track_;
48   }
49 
captured()50   bool captured() const { return captured_; }
51 
52   bool Animate(base::TimeTicks now);
53   void StartAnimation();
54   void StopAnimation();
55 
56   void UpdateThumbThicknessScale();
57 
58   void DidMouseDown();
59   void DidMouseUp();
60   void DidMouseLeave();
61   void DidMouseMove(const gfx::PointF& device_viewport_point);
62 
63  private:
64   SingleScrollbarAnimationControllerThinning(
65       ElementId scroll_element_id,
66       ScrollbarOrientation orientation,
67       ScrollbarAnimationControllerClient* client,
68       base::TimeDelta thinning_duration);
69 
70   ScrollbarLayerImplBase* GetScrollbar() const;
71   float AnimationProgressAtTime(base::TimeTicks now);
72   void RunAnimationFrame(float progress);
73   const base::TimeDelta& Duration();
74 
75   // Describes whether the current animation should INCREASE (thicken)
76   // a bar or DECREASE it (thin).
77   enum AnimationChange { NONE, INCREASE, DECREASE };
78   float ThumbThicknessScaleAt(float progress);
79 
80   float AdjustScale(float new_value,
81                     float current_value,
82                     AnimationChange animation_change,
83                     float min_value,
84                     float max_value);
85   void ApplyThumbThicknessScale(float thumb_thickness_scale);
86 
87   ScrollbarAnimationControllerClient* client_;
88 
89   base::TimeTicks last_awaken_time_;
90   bool is_animating_;
91 
92   ElementId scroll_element_id_;
93 
94   ScrollbarOrientation orientation_;
95   bool captured_;
96   bool mouse_is_over_scrollbar_thumb_;
97   bool mouse_is_near_scrollbar_thumb_;
98   bool mouse_is_near_scrollbar_track_;
99   // Are we narrowing or thickening the bars.
100   AnimationChange thickness_change_;
101 
102   base::TimeDelta thinning_duration_;
103 };
104 
105 }  // namespace cc
106 
107 #endif  // CC_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_
108