overscroll_glow.h revision 558790d6acca3451cf3a6b497803a5f07d0bec58
1// Copyright (c) 2012 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 CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
6#define CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/time/time.h"
11#include "content/browser/android/edge_effect.h"
12#include "ui/gfx/size_f.h"
13#include "ui/gfx/vector2d_f.h"
14
15class SkBitmap;
16
17namespace cc {
18class Layer;
19}
20
21namespace content {
22
23/* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
24 * Conscious tradeoffs were made to align this as closely as possible with the
25 * original Android java version.
26 */
27class OverscrollGlow {
28 public:
29  // Create and initialize a new effect with the necessary resources.
30  // If |enabled| is false, the effect will be be deactivated until
31  // SetEnabled(true) is called.
32  // The caller should attach |root_layer| to the desired layer tree.
33  static scoped_ptr<OverscrollGlow> Create(bool enabled);
34
35  // Force loading of any necessary resources.  This function is thread-safe.
36  static void EnsureResources();
37
38  ~OverscrollGlow();
39
40  // If false, the glow will be deactivated, and subsequent calls to
41  // OnOverscrolled or Animate will have no effect.
42  void SetEnabled(bool enabled);
43
44  // |overscroll| is the accumulated overscroll for the current gesture.
45  // |velocity| is the instantaneous velocity for the overscroll.
46  void OnOverscrolled(base::TimeTicks current_time,
47                      gfx::Vector2dF overscroll,
48                      gfx::Vector2dF velocity);
49
50  // Triggers glow recession for any active edges.
51  // Note: This does not actually release any resources; the name mirrors that
52  //       in Android's OverscrollGlow class.
53  void Release(base::TimeTicks current_time);
54
55  // Returns true if the effect still needs animation ticks.
56  bool Animate(base::TimeTicks current_time);
57
58  // Returns true if the effect needs animation ticks.
59  bool NeedsAnimate() const;
60
61  // The root layer of the effect (not necessarily of the tree).
62  scoped_refptr<cc::Layer> root_layer() const {
63    return root_layer_;
64  }
65
66  // Horizontal overscroll will be ignored when false.
67  void set_horizontal_overscroll_enabled(bool enabled) {
68    horizontal_overscroll_enabled_ = enabled;
69  }
70  // Vertical overscroll will be ignored when false.
71  void set_vertical_overscroll_enabled(bool enabled) {
72    vertical_overscroll_enabled_ = enabled;
73  }
74  // The size of the layer for which edges will be animated.
75  void set_size(gfx::SizeF size) {
76    size_ = size;
77  }
78
79 private:
80  enum Axis { AXIS_X, AXIS_Y };
81
82  OverscrollGlow(bool enabled, const SkBitmap& edge, const SkBitmap& glow);
83
84  void Pull(base::TimeTicks current_time,
85            gfx::Vector2dF added_overscroll);
86  void Absorb(base::TimeTicks current_time,
87              gfx::Vector2dF velocity,
88              gfx::Vector2dF overscroll,
89              gfx::Vector2dF old_overscroll);
90
91  void ReleaseAxis(Axis axis, base::TimeTicks current_time);
92
93  EdgeEffect* GetOppositeEdge(int edge_index);
94
95  scoped_ptr<EdgeEffect> edge_effects_[EdgeEffect::EDGE_COUNT];
96
97  bool enabled_;
98  gfx::SizeF size_;
99  gfx::Vector2dF old_overscroll_;
100  gfx::Vector2dF old_velocity_;
101  bool horizontal_overscroll_enabled_;
102  bool vertical_overscroll_enabled_;
103
104  scoped_refptr<cc::Layer> root_layer_;
105
106  DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
107};
108
109}  // namespace content
110
111#endif  // CONTENT_BROWSER_ANDROID_SCROLL_GLOW_H_
112