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"
22 // Provides lazy, customized EdgeEffect creation.
23 class OverscrollGlowClient
{
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
{
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
42 explicit OverscrollGlow(OverscrollGlowClient
* client
);
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 // |overscroll_location| is the coordinate of the causal overscrolling event.
49 // Returns true if the effect still needs animation ticks.
50 bool OnOverscrolled(base::TimeTicks current_time
,
51 const gfx::Vector2dF
& accumulated_overscroll
,
52 gfx::Vector2dF overscroll_delta
,
53 gfx::Vector2dF velocity
,
54 const gfx::Vector2dF
& overscroll_location
);
56 // Returns true if the effect still needs animation ticks, with effect layers
57 // attached to |parent_layer| if necessary.
58 // Note: The effect will detach itself when no further animation is required.
59 bool Animate(base::TimeTicks current_time
, cc::Layer
* parent_layer
);
61 // Update the effect according to the most recent display parameters,
62 // Note: All dimensions are in device pixels.
63 void OnFrameUpdated(const gfx::SizeF
& viewport_size
,
64 const gfx::SizeF
& content_size
,
65 const gfx::Vector2dF
& content_scroll_offset
);
67 // Reset the effect to its inactive state, clearing any active effects.
70 // Whether the effect is active, either being pulled or receding.
71 bool IsActive() const;
73 // The maximum alpha value (in the range [0,1]) of any animated edge layers.
74 // If the effect is inactive, this will be 0.
75 float GetVisibleAlpha() const;
78 enum Axis
{ AXIS_X
, AXIS_Y
};
80 // Returns whether the effect has been properly initialized.
81 bool InitializeIfNecessary();
82 bool CheckNeedsAnimate();
83 void UpdateLayerAttachment(cc::Layer
* parent
);
85 void Pull(base::TimeTicks current_time
,
86 const gfx::Vector2dF
& overscroll_delta
,
87 const gfx::Vector2dF
& overscroll_location
);
88 void Absorb(base::TimeTicks current_time
,
89 const gfx::Vector2dF
& velocity
,
90 bool x_overscroll_started
,
91 bool y_overscroll_started
);
92 void Release(base::TimeTicks current_time
);
94 EdgeEffectBase
* GetOppositeEdge(int edge_index
);
96 OverscrollGlowClient
* const client_
;
97 scoped_ptr
<EdgeEffectBase
> edge_effects_
[EDGE_COUNT
];
99 gfx::SizeF viewport_size_
;
100 float edge_offsets_
[EDGE_COUNT
];
102 bool allow_horizontal_overscroll_
;
103 bool allow_vertical_overscroll_
;
105 scoped_refptr
<cc::Layer
> root_layer_
;
107 DISALLOW_COPY_AND_ASSIGN(OverscrollGlow
);
110 } // namespace content
112 #endif // CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_