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 "content/browser/android/edge_effect_base.h"
12 #include "ui/gfx/geometry/size_f.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
21 // Provides lazy, customized EdgeEffect creation.
22 class OverscrollGlowClient
{
24 virtual ~OverscrollGlowClient() {}
26 // Called lazily, after the initial overscrolling event.
27 virtual scoped_ptr
<EdgeEffectBase
> CreateEdgeEffect() = 0;
30 /* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
31 * Conscious tradeoffs were made to align this as closely as possible with the
32 * original Android Java version.
34 class OverscrollGlow
{
36 // |client| must be valid for the duration of the effect's lifetime.
37 // The effect is enabled by default, but will remain dormant until the first
39 explicit OverscrollGlow(OverscrollGlowClient
* client
);
42 // Called when the root content layer overscrolls.
43 // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
44 // |velocity| is in device pixels / second.
45 // |overscroll_location| is the coordinate of the causal overscrolling event.
46 // Returns true if the effect still needs animation ticks.
47 bool OnOverscrolled(base::TimeTicks current_time
,
48 const gfx::Vector2dF
& accumulated_overscroll
,
49 gfx::Vector2dF overscroll_delta
,
50 gfx::Vector2dF velocity
,
51 const gfx::Vector2dF
& overscroll_location
);
53 // Returns true if the effect still needs animation ticks, with effect layers
54 // attached to |parent_layer| if necessary.
55 // Note: The effect will detach itself when no further animation is required.
56 bool Animate(base::TimeTicks current_time
, cc::Layer
* parent_layer
);
58 // Update the effect according to the most recent display parameters,
59 // Note: All dimensions are in device pixels.
60 void OnFrameUpdated(const gfx::SizeF
& viewport_size
,
61 const gfx::SizeF
& content_size
,
62 const gfx::Vector2dF
& content_scroll_offset
);
64 // Reset the effect to its inactive state, clearing any active effects.
67 // Whether the effect is active, either being pulled or receding.
68 bool IsActive() const;
70 // The maximum alpha value (in the range [0,1]) of any animated edge layers.
71 // If the effect is inactive, this will be 0.
72 float GetVisibleAlpha() const;
75 enum Axis
{ AXIS_X
, AXIS_Y
};
76 enum { EDGE_COUNT
= EdgeEffectBase::EDGE_COUNT
};
78 // Returns whether the effect has been properly initialized.
79 bool InitializeIfNecessary();
80 bool CheckNeedsAnimate();
81 void UpdateLayerAttachment(cc::Layer
* parent
);
83 void Pull(base::TimeTicks current_time
,
84 const gfx::Vector2dF
& overscroll_delta
,
85 const gfx::Vector2dF
& overscroll_location
);
86 void Absorb(base::TimeTicks current_time
,
87 const gfx::Vector2dF
& velocity
,
88 bool x_overscroll_started
,
89 bool y_overscroll_started
);
90 void Release(base::TimeTicks current_time
);
92 EdgeEffectBase
* GetOppositeEdge(int edge_index
);
94 OverscrollGlowClient
* const client_
;
95 scoped_ptr
<EdgeEffectBase
> edge_effects_
[EDGE_COUNT
];
97 gfx::SizeF viewport_size_
;
98 float edge_offsets_
[EDGE_COUNT
];
100 bool allow_horizontal_overscroll_
;
101 bool allow_vertical_overscroll_
;
103 scoped_refptr
<cc::Layer
> root_layer_
;
105 DISALLOW_COPY_AND_ASSIGN(OverscrollGlow
);
108 } // namespace content
110 #endif // CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_