blob: 878d39aec1288b4ed12c68a4933b529ad5673d51 [file] [log] [blame]
[email protected]c6de4d8e2013-05-17 17:51:511// 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
[email protected]cffc3282014-08-15 23:38:248#include "base/callback.h"
[email protected]c6de4d8e2013-05-17 17:51:519#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
[email protected]a43858f2013-06-28 15:18:3711#include "base/time/time.h"
[email protected]c6de4d8e2013-05-17 17:51:5112#include "ui/gfx/size_f.h"
13#include "ui/gfx/vector2d_f.h"
14
[email protected]cffc3282014-08-15 23:38:2415namespace cc {
16class Layer;
[email protected]c6de4d8e2013-05-17 17:51:5117}
18
19namespace content {
20
[email protected]cffc3282014-08-15 23:38:2421class EdgeEffectBase;
22
[email protected]bb4b8b792013-07-26 10:54:5523/* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
[email protected]c6de4d8e2013-05-17 17:51:5124 * Conscious tradeoffs were made to align this as closely as possible with the
[email protected]afaaf902014-04-11 23:43:0925 * original Android Java version.
[email protected]c6de4d8e2013-05-17 17:51:5126 */
27class OverscrollGlow {
28 public:
[email protected]cffc3282014-08-15 23:38:2429 enum Edge { EDGE_TOP = 0, EDGE_LEFT, EDGE_BOTTOM, EDGE_RIGHT, EDGE_COUNT };
30
31 // Allows lazy creation of the edge effects.
32 typedef base::Callback<scoped_ptr<EdgeEffectBase>(void)> EdgeEffectProvider;
33
34 // |edge_effect_provider| must be valid for the duration of the effect's
35 // lifetime. The effect is enabled by default, but will remain dormant until
36 // the first overscroll event.
37 explicit OverscrollGlow(const EdgeEffectProvider& edge_effect_provider);
[email protected]a4766fb2013-06-06 02:42:4738
[email protected]c6de4d8e2013-05-17 17:51:5139 ~OverscrollGlow();
40
[email protected]580d3362013-12-07 02:56:4341 // Enable the effect. If the effect was previously disabled, it will remain
42 // dormant until subsequent calls to |OnOverscrolled()|.
43 void Enable();
[email protected]c724a0a2013-06-22 00:23:4444
[email protected]580d3362013-12-07 02:56:4345 // Deactivate and detach the effect. Subsequent calls to |OnOverscrolled()| or
46 // |Animate()| will have no effect.
47 void Disable();
48
49 // Effect layers will be attached to |overscrolling_layer| if necessary.
[email protected]5b198332014-04-29 09:37:5750 // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
51 // |velocity| is in device pixels / second.
[email protected]580d3362013-12-07 02:56:4352 // Returns true if the effect still needs animation ticks.
53 bool OnOverscrolled(cc::Layer* overscrolling_layer,
54 base::TimeTicks current_time,
[email protected]5b198332014-04-29 09:37:5755 gfx::Vector2dF accumulated_overscroll,
56 gfx::Vector2dF overscroll_delta,
[email protected]cffc3282014-08-15 23:38:2457 gfx::Vector2dF velocity,
58 gfx::Vector2dF overscroll_location);
[email protected]c724a0a2013-06-22 00:23:4459
[email protected]c6de4d8e2013-05-17 17:51:5160 // Returns true if the effect still needs animation ticks.
[email protected]580d3362013-12-07 02:56:4361 // Note: The effect will detach itself when no further animation is required.
[email protected]c6de4d8e2013-05-17 17:51:5162 bool Animate(base::TimeTicks current_time);
[email protected]c6de4d8e2013-05-17 17:51:5163
[email protected]afaaf902014-04-11 23:43:0964 // Update the effect according to the most recent display parameters,
65 // Note: All dimensions are in device pixels.
66 struct DisplayParameters {
67 DisplayParameters();
68 gfx::SizeF size;
[email protected]cffc3282014-08-15 23:38:2469 float edge_offsets[EDGE_COUNT];
[email protected]afaaf902014-04-11 23:43:0970 };
71 void UpdateDisplayParameters(const DisplayParameters& params);
72
[email protected]c6de4d8e2013-05-17 17:51:5173 private:
74 enum Axis { AXIS_X, AXIS_Y };
75
[email protected]580d3362013-12-07 02:56:4376 // Returns whether the effect is initialized.
77 bool InitializeIfNecessary();
78 bool NeedsAnimate() const;
79 void UpdateLayerAttachment(cc::Layer* parent);
80 void Detach();
[email protected]cffc3282014-08-15 23:38:2481 void Pull(base::TimeTicks current_time,
82 const gfx::Vector2dF& overscroll_delta,
83 const gfx::Vector2dF& overscroll_location);
[email protected]c6de4d8e2013-05-17 17:51:5184 void Absorb(base::TimeTicks current_time,
[email protected]cffc3282014-08-15 23:38:2485 const gfx::Vector2dF& velocity,
[email protected]5b198332014-04-29 09:37:5786 bool x_overscroll_started,
87 bool y_overscroll_started);
[email protected]e5535032013-10-15 19:13:3388 void Release(base::TimeTicks current_time);
[email protected]c6de4d8e2013-05-17 17:51:5189
[email protected]cffc3282014-08-15 23:38:2490 EdgeEffectBase* GetOppositeEdge(int edge_index);
[email protected]c6de4d8e2013-05-17 17:51:5191
[email protected]cffc3282014-08-15 23:38:2492 EdgeEffectProvider edge_effect_provider_;
93 scoped_ptr<EdgeEffectBase> edge_effects_[EDGE_COUNT];
[email protected]c6de4d8e2013-05-17 17:51:5194
[email protected]afaaf902014-04-11 23:43:0995 DisplayParameters display_params_;
[email protected]afaaf902014-04-11 23:43:0996 bool enabled_;
97 bool initialized_;
[email protected]c6de4d8e2013-05-17 17:51:5198
99 scoped_refptr<cc::Layer> root_layer_;
[email protected]c6de4d8e2013-05-17 17:51:51100
101 DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
102};
103
104} // namespace content
105
106#endif // CONTENT_BROWSER_ANDROID_SCROLL_GLOW_H_