File: overscroll_glow.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,151 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ANDROID_OVERSCROLL_GLOW_H_
#define UI_ANDROID_OVERSCROLL_GLOW_H_

#include <array>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "ui/android/edge_effect.h"
#include "ui/android/ui_android_export.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc::slim {
class Layer;
}

namespace ui {

// Provides lazy, customized EdgeEffect creation.
class UI_ANDROID_EXPORT OverscrollGlowClient {
 public:
  virtual ~OverscrollGlowClient() {}

  // Called lazily, after the initial overscrolling event.
  virtual std::unique_ptr<EdgeEffect> CreateEdgeEffect() = 0;
};

/* |OverscrollGlow| mirrors its Android counterpart, OverscrollGlow.java.
 * Conscious tradeoffs were made to align this as closely as possible with the
 * original Android Java version.
 */
class UI_ANDROID_EXPORT OverscrollGlow {
 public:
  // |client| must be valid for the duration of the effect's lifetime.
  // The effect is enabled by default, but will remain dormant until the first
  // overscroll event.
  explicit OverscrollGlow(OverscrollGlowClient* client);

  OverscrollGlow(const OverscrollGlow&) = delete;
  OverscrollGlow& operator=(const OverscrollGlow&) = delete;

  virtual ~OverscrollGlow();

  // Called when the root content layer overscrolls.
  // |accumulated_overscroll| and |overscroll_delta| are in device pixels, while
  // |velocity| is in device pixels / second.
  // |overscroll_location| is the coordinate of the causal overscrolling event.
  // Returns true if the effect still needs animation ticks.
  // This method is made virtual for mocking.
  virtual bool OnOverscrolled(base::TimeTicks current_time,
                              gfx::Vector2dF accumulated_overscroll,
                              gfx::Vector2dF overscroll_delta,
                              gfx::Vector2dF velocity,
                              gfx::Vector2dF overscroll_location);

  // Returns true if the effect still needs animation ticks, with effect layers
  // attached to |parent_layer| if necessary.
  // Note: The effect will detach itself when no further animation is required.
  bool Animate(base::TimeTicks current_time, cc::slim::Layer* parent_layer);

  // Update the effect according to the most recent display parameters,
  // Note: All dimensions are in device pixels.
  void OnFrameUpdated(const gfx::SizeF& viewport_size,
                      const gfx::SizeF& content_size,
                      const gfx::PointF& content_scroll_offset);

  // Reset the effect to its inactive state, clearing any active effects.
  void Reset();

  // Whether the effect is active, either being pulled or receding.
  bool IsActive() const;

  // The maximum alpha value (in the range [0,1]) of any animated edge layers.
  // If the effect is inactive, this will be 0.
  float GetVisibleAlpha() const;

 private:
  enum Axis { AXIS_X, AXIS_Y };
  enum { EDGE_COUNT = EdgeEffect::EDGE_COUNT };

  // Returns whether the effect has been properly initialized.
  bool InitializeIfNecessary();
  bool CheckNeedsAnimate();
  void UpdateLayerAttachment(cc::slim::Layer* parent);
  void Detach();
  void Pull(base::TimeTicks current_time,
            const gfx::Vector2dF& overscroll_delta,
            const gfx::Vector2dF& overscroll_location);
  void Absorb(base::TimeTicks current_time,
              const gfx::Vector2dF& velocity,
              bool x_overscroll_started,
              bool y_overscroll_started);
  void Release(base::TimeTicks current_time);

  EdgeEffect* GetOppositeEdge(int edge_index);

  raw_ptr<OverscrollGlowClient> client_;
  std::array<std::unique_ptr<EdgeEffect>, EDGE_COUNT> edge_effects_;

  gfx::SizeF viewport_size_;
  std::array<float, EDGE_COUNT> edge_offsets_;
  bool initialized_;
  bool allow_horizontal_overscroll_;
  bool allow_vertical_overscroll_;

  scoped_refptr<cc::slim::Layer> root_layer_;
};

}  // namespace ui

#endif  // UI_ANDROID_OVERSCROLL_GLOW_H_