[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 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 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 8 | #include "base/callback.h" |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 11 | #include "base/time/time.h" |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 12 | #include "ui/gfx/size_f.h" |
| 13 | #include "ui/gfx/vector2d_f.h" |
| 14 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 15 | namespace cc { |
| 16 | class Layer; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | namespace content { |
| 20 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 21 | class EdgeEffectBase; |
| 22 | |
[email protected] | bb4b8b79 | 2013-07-26 10:54:55 | [diff] [blame] | 23 | /* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java. |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 24 | * Conscious tradeoffs were made to align this as closely as possible with the |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 25 | * original Android Java version. |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 26 | */ |
| 27 | class OverscrollGlow { |
| 28 | public: |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 29 | 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] | a4766fb | 2013-06-06 02:42:47 | [diff] [blame] | 38 | |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 39 | ~OverscrollGlow(); |
| 40 | |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 41 | // Enable the effect. If the effect was previously disabled, it will remain |
| 42 | // dormant until subsequent calls to |OnOverscrolled()|. |
| 43 | void Enable(); |
[email protected] | c724a0a | 2013-06-22 00:23:44 | [diff] [blame] | 44 | |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 45 | // 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] | 5b19833 | 2014-04-29 09:37:57 | [diff] [blame] | 50 | // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while |
| 51 | // |velocity| is in device pixels / second. |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 52 | // Returns true if the effect still needs animation ticks. |
| 53 | bool OnOverscrolled(cc::Layer* overscrolling_layer, |
| 54 | base::TimeTicks current_time, |
[email protected] | 5b19833 | 2014-04-29 09:37:57 | [diff] [blame] | 55 | gfx::Vector2dF accumulated_overscroll, |
| 56 | gfx::Vector2dF overscroll_delta, |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 57 | gfx::Vector2dF velocity, |
| 58 | gfx::Vector2dF overscroll_location); |
[email protected] | c724a0a | 2013-06-22 00:23:44 | [diff] [blame] | 59 | |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 60 | // Returns true if the effect still needs animation ticks. |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 61 | // Note: The effect will detach itself when no further animation is required. |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 62 | bool Animate(base::TimeTicks current_time); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 63 | |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 64 | // 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] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 69 | float edge_offsets[EDGE_COUNT]; |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 70 | }; |
| 71 | void UpdateDisplayParameters(const DisplayParameters& params); |
| 72 | |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 73 | private: |
| 74 | enum Axis { AXIS_X, AXIS_Y }; |
| 75 | |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 76 | // Returns whether the effect is initialized. |
| 77 | bool InitializeIfNecessary(); |
| 78 | bool NeedsAnimate() const; |
| 79 | void UpdateLayerAttachment(cc::Layer* parent); |
| 80 | void Detach(); |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 81 | void Pull(base::TimeTicks current_time, |
| 82 | const gfx::Vector2dF& overscroll_delta, |
| 83 | const gfx::Vector2dF& overscroll_location); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 84 | void Absorb(base::TimeTicks current_time, |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 85 | const gfx::Vector2dF& velocity, |
[email protected] | 5b19833 | 2014-04-29 09:37:57 | [diff] [blame] | 86 | bool x_overscroll_started, |
| 87 | bool y_overscroll_started); |
[email protected] | e553503 | 2013-10-15 19:13:33 | [diff] [blame] | 88 | void Release(base::TimeTicks current_time); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 89 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 90 | EdgeEffectBase* GetOppositeEdge(int edge_index); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 91 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame^] | 92 | EdgeEffectProvider edge_effect_provider_; |
| 93 | scoped_ptr<EdgeEffectBase> edge_effects_[EDGE_COUNT]; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 94 | |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 95 | DisplayParameters display_params_; |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 96 | bool enabled_; |
| 97 | bool initialized_; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 98 | |
| 99 | scoped_refptr<cc::Layer> root_layer_; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(OverscrollGlow); |
| 102 | }; |
| 103 | |
| 104 | } // namespace content |
| 105 | |
| 106 | #endif // CONTENT_BROWSER_ANDROID_SCROLL_GLOW_H_ |