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 a new effect. If |enabled| is false, the effect will remain
30  // deactivated until explicitly enabled.
31  // Note: No resources will be allocated until the effect is both
32  //       enabled and an overscroll event has occurred.
33  static scoped_ptr<OverscrollGlow> Create(bool enabled);
34
35  ~OverscrollGlow();
36
37  // Enable the effect. If the effect was previously disabled, it will remain
38  // dormant until subsequent calls to |OnOverscrolled()|.
39  void Enable();
40
41  // Deactivate and detach the effect. Subsequent calls to |OnOverscrolled()| or
42  // |Animate()| will have no effect.
43  void Disable();
44
45  // Effect layers will be attached to |overscrolling_layer| if necessary.
46  // |overscroll| is the accumulated overscroll for the current gesture.
47  // |velocity| is the instantaneous velocity for the overscroll.
48  // Returns true if the effect still needs animation ticks.
49  bool OnOverscrolled(cc::Layer* overscrolling_layer,
50                      base::TimeTicks current_time,
51                      gfx::Vector2dF overscroll,
52                      gfx::Vector2dF velocity);
53
54  // Returns true if the effect still needs animation ticks.
55  // Note: The effect will detach itself when no further animation is required.
56  bool Animate(base::TimeTicks current_time);
57
58  // Horizontal overscroll will be ignored when false.
59  void set_horizontal_overscroll_enabled(bool enabled) {
60    horizontal_overscroll_enabled_ = enabled;
61  }
62  // Vertical overscroll will be ignored when false.
63  void set_vertical_overscroll_enabled(bool enabled) {
64    vertical_overscroll_enabled_ = enabled;
65  }
66  // The size of the layer for which edges will be animated.
67  void set_size(gfx::SizeF size) {
68    size_ = size;
69  }
70
71 private:
72  enum Axis { AXIS_X, AXIS_Y };
73
74  OverscrollGlow(bool enabled);
75
76  // Returns whether the effect is initialized.
77  bool InitializeIfNecessary();
78  bool NeedsAnimate() const;
79  void UpdateLayerAttachment(cc::Layer* parent);
80  void Detach();
81  void Pull(base::TimeTicks current_time,
82            gfx::Vector2dF added_overscroll);
83  void Absorb(base::TimeTicks current_time,
84              gfx::Vector2dF velocity,
85              gfx::Vector2dF overscroll,
86              gfx::Vector2dF old_overscroll);
87  void Release(base::TimeTicks current_time);
88  void ReleaseAxis(Axis axis, base::TimeTicks current_time);
89
90  EdgeEffect* GetOppositeEdge(int edge_index);
91
92  scoped_ptr<EdgeEffect> edge_effects_[EdgeEffect::EDGE_COUNT];
93
94  bool enabled_;
95  bool initialized_;
96  gfx::SizeF size_;
97  gfx::Vector2dF old_overscroll_;
98  gfx::Vector2dF old_velocity_;
99  bool horizontal_overscroll_enabled_;
100  bool vertical_overscroll_enabled_;
101
102  scoped_refptr<cc::Layer> root_layer_;
103
104  DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
105};
106
107}  // namespace content
108
109#endif  // CONTENT_BROWSER_ANDROID_SCROLL_GLOW_H_
110