overscroll_glow.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef UI_ANDROID_OVERSCROLL_GLOW_H_
  5. #define UI_ANDROID_OVERSCROLL_GLOW_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/time/time.h"
  10. #include "ui/android/edge_effect.h"
  11. #include "ui/android/ui_android_export.h"
  12. #include "ui/gfx/geometry/size_f.h"
  13. #include "ui/gfx/geometry/vector2d_f.h"
  14. namespace cc {
  15. class Layer;
  16. }
  17. namespace ui {
  18. // Provides lazy, customized EdgeEffect creation.
  19. class UI_ANDROID_EXPORT OverscrollGlowClient {
  20. public:
  21. virtual ~OverscrollGlowClient() {}
  22. // Called lazily, after the initial overscrolling event.
  23. virtual std::unique_ptr<EdgeEffect> CreateEdgeEffect() = 0;
  24. };
  25. /* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
  26. * Conscious tradeoffs were made to align this as closely as possible with the
  27. * original Android Java version.
  28. */
  29. class UI_ANDROID_EXPORT OverscrollGlow {
  30. public:
  31. // |client| must be valid for the duration of the effect's lifetime.
  32. // The effect is enabled by default, but will remain dormant until the first
  33. // overscroll event.
  34. explicit OverscrollGlow(OverscrollGlowClient* client);
  35. OverscrollGlow(const OverscrollGlow&) = delete;
  36. OverscrollGlow& operator=(const OverscrollGlow&) = delete;
  37. virtual ~OverscrollGlow();
  38. // Called when the root content layer overscrolls.
  39. // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
  40. // |velocity| is in device pixels / second.
  41. // |overscroll_location| is the coordinate of the causal overscrolling event.
  42. // Returns true if the effect still needs animation ticks.
  43. // This method is made virtual for mocking.
  44. virtual bool OnOverscrolled(base::TimeTicks current_time,
  45. gfx::Vector2dF accumulated_overscroll,
  46. gfx::Vector2dF overscroll_delta,
  47. gfx::Vector2dF velocity,
  48. gfx::Vector2dF overscroll_location);
  49. // Returns true if the effect still needs animation ticks, with effect layers
  50. // attached to |parent_layer| if necessary.
  51. // Note: The effect will detach itself when no further animation is required.
  52. bool Animate(base::TimeTicks current_time, cc::Layer* parent_layer);
  53. // Update the effect according to the most recent display parameters,
  54. // Note: All dimensions are in device pixels.
  55. void OnFrameUpdated(const gfx::SizeF& viewport_size,
  56. const gfx::SizeF& content_size,
  57. const gfx::PointF& content_scroll_offset);
  58. // Reset the effect to its inactive state, clearing any active effects.
  59. void Reset();
  60. // Whether the effect is active, either being pulled or receding.
  61. bool IsActive() const;
  62. // The maximum alpha value (in the range [0,1]) of any animated edge layers.
  63. // If the effect is inactive, this will be 0.
  64. float GetVisibleAlpha() const;
  65. private:
  66. enum Axis { AXIS_X, AXIS_Y };
  67. enum { EDGE_COUNT = EdgeEffect::EDGE_COUNT };
  68. // Returns whether the effect has been properly initialized.
  69. bool InitializeIfNecessary();
  70. bool CheckNeedsAnimate();
  71. void UpdateLayerAttachment(cc::Layer* parent);
  72. void Detach();
  73. void Pull(base::TimeTicks current_time,
  74. const gfx::Vector2dF& overscroll_delta,
  75. const gfx::Vector2dF& overscroll_location);
  76. void Absorb(base::TimeTicks current_time,
  77. const gfx::Vector2dF& velocity,
  78. bool x_overscroll_started,
  79. bool y_overscroll_started);
  80. void Release(base::TimeTicks current_time);
  81. EdgeEffect* GetOppositeEdge(int edge_index);
  82. raw_ptr<OverscrollGlowClient> client_;
  83. std::unique_ptr<EdgeEffect> edge_effects_[EDGE_COUNT];
  84. gfx::SizeF viewport_size_;
  85. float edge_offsets_[EDGE_COUNT];
  86. bool initialized_;
  87. bool allow_horizontal_overscroll_;
  88. bool allow_vertical_overscroll_;
  89. scoped_refptr<cc::Layer> root_layer_;
  90. };
  91. } // namespace ui
  92. #endif // UI_ANDROID_OVERSCROLL_GLOW_H_