blob: e00d4fc1eee6f653651dd40d8740662ffbfaa5f3 [file] [log] [blame]
[email protected]c6de4d8e2013-05-17 17:51:511// 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
sieversa964cb742015-10-23 18:06:385#ifndef UI_ANDROID_OVERSCROLL_GLOW_H_
6#define UI_ANDROID_OVERSCROLL_GLOW_H_
[email protected]c6de4d8e2013-05-17 17:51:517
danakj25c52c32016-04-12 21:51:088#include <memory>
9
sieversa964cb742015-10-23 18:06:3810#include "base/macros.h"
[email protected]c6de4d8e2013-05-17 17:51:5111#include "base/memory/ref_counted.h"
[email protected]a43858f2013-06-28 15:18:3712#include "base/time/time.h"
Andrew Grieve68dd21b42020-07-16 17:54:0713#include "ui/android/edge_effect.h"
sieversa964cb742015-10-23 18:06:3814#include "ui/android/ui_android_export.h"
tfarina93bfa912014-12-05 14:23:1515#include "ui/gfx/geometry/size_f.h"
16#include "ui/gfx/geometry/vector2d_f.h"
[email protected]c6de4d8e2013-05-17 17:51:5117
[email protected]cffc3282014-08-15 23:38:2418namespace cc {
19class Layer;
[email protected]c6de4d8e2013-05-17 17:51:5120}
21
sieversa964cb742015-10-23 18:06:3822namespace ui {
[email protected]c6de4d8e2013-05-17 17:51:5123
jdduke0fa512992014-11-21 03:18:4524// Provides lazy, customized EdgeEffect creation.
sieversa964cb742015-10-23 18:06:3825class UI_ANDROID_EXPORT OverscrollGlowClient {
jdduke0fa512992014-11-21 03:18:4526 public:
27 virtual ~OverscrollGlowClient() {}
28
29 // Called lazily, after the initial overscrolling event.
Andrew Grieve68dd21b42020-07-16 17:54:0730 virtual std::unique_ptr<EdgeEffect> CreateEdgeEffect() = 0;
jdduke0fa512992014-11-21 03:18:4531};
32
[email protected]bb4b8b792013-07-26 10:54:5533/* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
[email protected]c6de4d8e2013-05-17 17:51:5134 * Conscious tradeoffs were made to align this as closely as possible with the
[email protected]afaaf902014-04-11 23:43:0935 * original Android Java version.
[email protected]c6de4d8e2013-05-17 17:51:5136 */
sieversa964cb742015-10-23 18:06:3837class UI_ANDROID_EXPORT OverscrollGlow {
[email protected]c6de4d8e2013-05-17 17:51:5138 public:
jdduke0fa512992014-11-21 03:18:4539 // |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 Webera5d3d212017-09-01 05:43:4343 virtual ~OverscrollGlow();
[email protected]c6de4d8e2013-05-17 17:51:5144
jdduke9db1b9d2014-11-15 00:49:5245 // Called when the root content layer overscrolls.
[email protected]5b198332014-04-29 09:37:5746 // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
47 // |velocity| is in device pixels / second.
jdduke3367dde2015-07-25 01:23:4748 // |overscroll_location| is the coordinate of the causal overscrolling event.
[email protected]580d3362013-12-07 02:56:4349 // Returns true if the effect still needs animation ticks.
sunyunjiabbea8a92017-08-31 11:18:5450 // 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]c724a0a2013-06-22 00:23:4456
jdduke9db1b9d2014-11-15 00:49:5257 // Returns true if the effect still needs animation ticks, with effect layers
58 // attached to |parent_layer| if necessary.
[email protected]580d3362013-12-07 02:56:4359 // Note: The effect will detach itself when no further animation is required.
jdduke9db1b9d2014-11-15 00:49:5260 bool Animate(base::TimeTicks current_time, cc::Layer* parent_layer);
[email protected]c6de4d8e2013-05-17 17:51:5161
[email protected]afaaf902014-04-11 23:43:0962 // Update the effect according to the most recent display parameters,
63 // Note: All dimensions are in device pixels.
jddukeb00377552015-04-15 01:55:0864 void OnFrameUpdated(const gfx::SizeF& viewport_size,
65 const gfx::SizeF& content_size,
66 const gfx::Vector2dF& content_scroll_offset);
jdduke9db1b9d2014-11-15 00:49:5267
68 // Reset the effect to its inactive state, clearing any active effects.
69 void Reset();
[email protected]afaaf902014-04-11 23:43:0970
jdduke2dbb7e82015-02-11 20:58:2371 // Whether the effect is active, either being pulled or receding.
72 bool IsActive() const;
73
jdduke9349a45e2015-03-17 21:50:2374 // 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]c6de4d8e2013-05-17 17:51:5178 private:
79 enum Axis { AXIS_X, AXIS_Y };
Andrew Grieve68dd21b42020-07-16 17:54:0780 enum { EDGE_COUNT = EdgeEffect::EDGE_COUNT };
[email protected]c6de4d8e2013-05-17 17:51:5181
jdduke9db1b9d2014-11-15 00:49:5282 // Returns whether the effect has been properly initialized.
[email protected]580d3362013-12-07 02:56:4383 bool InitializeIfNecessary();
jdduke9db1b9d2014-11-15 00:49:5284 bool CheckNeedsAnimate();
[email protected]580d3362013-12-07 02:56:4385 void UpdateLayerAttachment(cc::Layer* parent);
86 void Detach();
[email protected]cffc3282014-08-15 23:38:2487 void Pull(base::TimeTicks current_time,
88 const gfx::Vector2dF& overscroll_delta,
89 const gfx::Vector2dF& overscroll_location);
[email protected]c6de4d8e2013-05-17 17:51:5190 void Absorb(base::TimeTicks current_time,
[email protected]cffc3282014-08-15 23:38:2491 const gfx::Vector2dF& velocity,
[email protected]5b198332014-04-29 09:37:5792 bool x_overscroll_started,
93 bool y_overscroll_started);
[email protected]e5535032013-10-15 19:13:3394 void Release(base::TimeTicks current_time);
[email protected]c6de4d8e2013-05-17 17:51:5195
Andrew Grieve68dd21b42020-07-16 17:54:0796 EdgeEffect* GetOppositeEdge(int edge_index);
[email protected]c6de4d8e2013-05-17 17:51:5197
jdduke0fa512992014-11-21 03:18:4598 OverscrollGlowClient* const client_;
Andrew Grieve68dd21b42020-07-16 17:54:0799 std::unique_ptr<EdgeEffect> edge_effects_[EDGE_COUNT];
[email protected]c6de4d8e2013-05-17 17:51:51100
jdduke9db1b9d2014-11-15 00:49:52101 gfx::SizeF viewport_size_;
102 float edge_offsets_[EDGE_COUNT];
[email protected]afaaf902014-04-11 23:43:09103 bool initialized_;
jdduke3367dde2015-07-25 01:23:47104 bool allow_horizontal_overscroll_;
105 bool allow_vertical_overscroll_;
[email protected]c6de4d8e2013-05-17 17:51:51106
107 scoped_refptr<cc::Layer> root_layer_;
[email protected]c6de4d8e2013-05-17 17:51:51108
109 DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
110};
111
sieversa964cb742015-10-23 18:06:38112} // namespace ui
[email protected]c6de4d8e2013-05-17 17:51:51113
sieversa964cb742015-10-23 18:06:38114#endif // UI_ANDROID_OVERSCROLL_GLOW_H_