Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / overscroll_glow.h
blob80a1b106cf4ace93b5ea0a5dcc3ef5e45e43190c
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.
5 #ifndef CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
6 #define CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "ui/gfx/geometry/size_f.h"
12 #include "ui/gfx/geometry/vector2d_f.h"
14 namespace cc {
15 class Layer;
18 namespace content {
20 class EdgeEffectBase;
22 // Provides lazy, customized EdgeEffect creation.
23 class OverscrollGlowClient {
24 public:
25 virtual ~OverscrollGlowClient() {}
27 // Called lazily, after the initial overscrolling event.
28 virtual scoped_ptr<EdgeEffectBase> CreateEdgeEffect() = 0;
31 /* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
32 * Conscious tradeoffs were made to align this as closely as possible with the
33 * original Android Java version.
35 class OverscrollGlow {
36 public:
37 enum Edge { EDGE_TOP = 0, EDGE_LEFT, EDGE_BOTTOM, EDGE_RIGHT, EDGE_COUNT };
39 // |client| must be valid for the duration of the effect's lifetime.
40 // The effect is enabled by default, but will remain dormant until the first
41 // overscroll event.
42 explicit OverscrollGlow(OverscrollGlowClient* client);
43 ~OverscrollGlow();
45 // Called when the root content layer overscrolls.
46 // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
47 // |velocity| is in device pixels / second.
48 // Returns true if the effect still needs animation ticks.
49 bool OnOverscrolled(base::TimeTicks current_time,
50 gfx::Vector2dF accumulated_overscroll,
51 gfx::Vector2dF overscroll_delta,
52 gfx::Vector2dF velocity,
53 gfx::Vector2dF overscroll_location);
55 // Returns true if the effect still needs animation ticks, with effect layers
56 // attached to |parent_layer| if necessary.
57 // Note: The effect will detach itself when no further animation is required.
58 bool Animate(base::TimeTicks current_time, cc::Layer* parent_layer);
60 // Update the effect according to the most recent display parameters,
61 // Note: All dimensions are in device pixels.
62 void UpdateDisplay(const gfx::SizeF& viewport_size,
63 const gfx::SizeF& content_size,
64 const gfx::Vector2dF& content_scroll_offset);
66 // Reset the effect to its inactive state, clearing any active effects.
67 void Reset();
69 // Whether the effect is active, either being pulled or receding.
70 bool IsActive() const;
72 // The maximum alpha value (in the range [0,1]) of any animated edge layers.
73 // If the effect is inactive, this will be 0.
74 float GetVisibleAlpha() const;
76 private:
77 enum Axis { AXIS_X, AXIS_Y };
79 // Returns whether the effect has been properly initialized.
80 bool InitializeIfNecessary();
81 bool CheckNeedsAnimate();
82 void UpdateLayerAttachment(cc::Layer* parent);
83 void Detach();
84 void Pull(base::TimeTicks current_time,
85 const gfx::Vector2dF& overscroll_delta,
86 const gfx::Vector2dF& overscroll_location);
87 void Absorb(base::TimeTicks current_time,
88 const gfx::Vector2dF& velocity,
89 bool x_overscroll_started,
90 bool y_overscroll_started);
91 void Release(base::TimeTicks current_time);
93 EdgeEffectBase* GetOppositeEdge(int edge_index);
95 OverscrollGlowClient* const client_;
96 scoped_ptr<EdgeEffectBase> edge_effects_[EDGE_COUNT];
98 gfx::SizeF viewport_size_;
99 float edge_offsets_[EDGE_COUNT];
100 bool initialized_;
102 scoped_refptr<cc::Layer> root_layer_;
104 DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
107 } // namespace content
109 #endif // CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_