[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 | |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 5 | #ifndef UI_ANDROID_OVERSCROLL_GLOW_H_ |
| 6 | #define UI_ANDROID_OVERSCROLL_GLOW_H_ |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 7 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 12 | #include "base/time/time.h" |
Andrew Grieve | 68dd21b4 | 2020-07-16 17:54:07 | [diff] [blame] | 13 | #include "ui/android/edge_effect.h" |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 14 | #include "ui/android/ui_android_export.h" |
tfarina | 93bfa91 | 2014-12-05 14:23:15 | [diff] [blame] | 15 | #include "ui/gfx/geometry/size_f.h" |
| 16 | #include "ui/gfx/geometry/vector2d_f.h" |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 17 | |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame] | 18 | namespace cc { |
| 19 | class Layer; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 20 | } |
| 21 | |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 22 | namespace ui { |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 23 | |
jdduke | 0fa51299 | 2014-11-21 03:18:45 | [diff] [blame] | 24 | // Provides lazy, customized EdgeEffect creation. |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 25 | class UI_ANDROID_EXPORT OverscrollGlowClient { |
jdduke | 0fa51299 | 2014-11-21 03:18:45 | [diff] [blame] | 26 | public: |
| 27 | virtual ~OverscrollGlowClient() {} |
| 28 | |
| 29 | // Called lazily, after the initial overscrolling event. |
Andrew Grieve | 68dd21b4 | 2020-07-16 17:54:07 | [diff] [blame] | 30 | virtual std::unique_ptr<EdgeEffect> CreateEdgeEffect() = 0; |
jdduke | 0fa51299 | 2014-11-21 03:18:45 | [diff] [blame] | 31 | }; |
| 32 | |
[email protected] | bb4b8b79 | 2013-07-26 10:54:55 | [diff] [blame] | 33 | /* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java. |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 34 | * Conscious tradeoffs were made to align this as closely as possible with the |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 35 | * original Android Java version. |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 36 | */ |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 37 | class UI_ANDROID_EXPORT OverscrollGlow { |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 38 | public: |
jdduke | 0fa51299 | 2014-11-21 03:18:45 | [diff] [blame] | 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); |
Nico Weber | a5d3d21 | 2017-09-01 05:43:43 | [diff] [blame] | 43 | virtual ~OverscrollGlow(); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 44 | |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 45 | // Called when the root content layer overscrolls. |
[email protected] | 5b19833 | 2014-04-29 09:37:57 | [diff] [blame] | 46 | // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while |
| 47 | // |velocity| is in device pixels / second. |
jdduke | 3367dde | 2015-07-25 01:23:47 | [diff] [blame] | 48 | // |overscroll_location| is the coordinate of the causal overscrolling event. |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 49 | // Returns true if the effect still needs animation ticks. |
sunyunjia | bbea8a9 | 2017-08-31 11:18:54 | [diff] [blame] | 50 | // This method is made virtual for mocking. |
| 51 | virtual bool OnOverscrolled(base::TimeTicks current_time, |
| 52 | gfx::Vector2dF accumulated_overscroll, |
| 53 | gfx::Vector2dF overscroll_delta, |
| 54 | gfx::Vector2dF velocity, |
| 55 | gfx::Vector2dF overscroll_location); |
[email protected] | c724a0a | 2013-06-22 00:23:44 | [diff] [blame] | 56 | |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 57 | // Returns true if the effect still needs animation ticks, with effect layers |
| 58 | // attached to |parent_layer| if necessary. |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 59 | // Note: The effect will detach itself when no further animation is required. |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 60 | bool Animate(base::TimeTicks current_time, cc::Layer* parent_layer); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 61 | |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 62 | // Update the effect according to the most recent display parameters, |
| 63 | // Note: All dimensions are in device pixels. |
jdduke | b0037755 | 2015-04-15 01:55:08 | [diff] [blame] | 64 | void OnFrameUpdated(const gfx::SizeF& viewport_size, |
| 65 | const gfx::SizeF& content_size, |
| 66 | const gfx::Vector2dF& content_scroll_offset); |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 67 | |
| 68 | // Reset the effect to its inactive state, clearing any active effects. |
| 69 | void Reset(); |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 70 | |
jdduke | 2dbb7e8 | 2015-02-11 20:58:23 | [diff] [blame] | 71 | // Whether the effect is active, either being pulled or receding. |
| 72 | bool IsActive() const; |
| 73 | |
jdduke | 9349a45e | 2015-03-17 21:50:23 | [diff] [blame] | 74 | // The maximum alpha value (in the range [0,1]) of any animated edge layers. |
| 75 | // If the effect is inactive, this will be 0. |
| 76 | float GetVisibleAlpha() const; |
| 77 | |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 78 | private: |
| 79 | enum Axis { AXIS_X, AXIS_Y }; |
Andrew Grieve | 68dd21b4 | 2020-07-16 17:54:07 | [diff] [blame] | 80 | enum { EDGE_COUNT = EdgeEffect::EDGE_COUNT }; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 81 | |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 82 | // Returns whether the effect has been properly initialized. |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 83 | bool InitializeIfNecessary(); |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 84 | bool CheckNeedsAnimate(); |
[email protected] | 580d336 | 2013-12-07 02:56:43 | [diff] [blame] | 85 | void UpdateLayerAttachment(cc::Layer* parent); |
| 86 | void Detach(); |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame] | 87 | void Pull(base::TimeTicks current_time, |
| 88 | const gfx::Vector2dF& overscroll_delta, |
| 89 | const gfx::Vector2dF& overscroll_location); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 90 | void Absorb(base::TimeTicks current_time, |
[email protected] | cffc328 | 2014-08-15 23:38:24 | [diff] [blame] | 91 | const gfx::Vector2dF& velocity, |
[email protected] | 5b19833 | 2014-04-29 09:37:57 | [diff] [blame] | 92 | bool x_overscroll_started, |
| 93 | bool y_overscroll_started); |
[email protected] | e553503 | 2013-10-15 19:13:33 | [diff] [blame] | 94 | void Release(base::TimeTicks current_time); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 95 | |
Andrew Grieve | 68dd21b4 | 2020-07-16 17:54:07 | [diff] [blame] | 96 | EdgeEffect* GetOppositeEdge(int edge_index); |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 97 | |
jdduke | 0fa51299 | 2014-11-21 03:18:45 | [diff] [blame] | 98 | OverscrollGlowClient* const client_; |
Andrew Grieve | 68dd21b4 | 2020-07-16 17:54:07 | [diff] [blame] | 99 | std::unique_ptr<EdgeEffect> edge_effects_[EDGE_COUNT]; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 100 | |
jdduke | 9db1b9d | 2014-11-15 00:49:52 | [diff] [blame] | 101 | gfx::SizeF viewport_size_; |
| 102 | float edge_offsets_[EDGE_COUNT]; |
[email protected] | afaaf90 | 2014-04-11 23:43:09 | [diff] [blame] | 103 | bool initialized_; |
jdduke | 3367dde | 2015-07-25 01:23:47 | [diff] [blame] | 104 | bool allow_horizontal_overscroll_; |
| 105 | bool allow_vertical_overscroll_; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 106 | |
| 107 | scoped_refptr<cc::Layer> root_layer_; |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(OverscrollGlow); |
| 110 | }; |
| 111 | |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 112 | } // namespace ui |
[email protected] | c6de4d8e | 2013-05-17 17:51:51 | [diff] [blame] | 113 | |
sievers | a964cb74 | 2015-10-23 18:06:38 | [diff] [blame] | 114 | #endif // UI_ANDROID_OVERSCROLL_GLOW_H_ |