forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoverscroll_refresh.h
132 lines (104 loc) · 4.51 KB
/
overscroll_refresh.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2014 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_REFRESH_H_
#define UI_ANDROID_OVERSCROLL_REFRESH_H_
#include "base/memory/raw_ptr.h"
#include "ui/android/ui_android_export.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/vector2d_f.h"
// A Java counterpart will be generated for this enum.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.ui
enum class OverscrollAction {
kNone = 0,
kPullToRefresh = 1,
kHistoryNavigation = 2,
kPullFromBottomEdge = 3
};
namespace cc {
struct OverscrollBehavior;
}
namespace gfx {
class PointF;
}
namespace ui {
class OverscrollRefreshHandler;
// Simple pull-to-refresh styled effect. Listens to scroll events, conditionally
// activating when:
// 1) The scroll begins when the page's root layer 1) has no vertical scroll
// offset and 2) lacks the overflow-y:hidden property.
// 2) The page doesn't consume the initial scroll events.
// 3) The initial scroll direction is upward.
// The actuall pull response, animation and action are delegated to the
// provided refresh handler.
class UI_ANDROID_EXPORT OverscrollRefresh {
public:
// The default distance in dp from a side of the device to start a navigation
// from.
// LINT.IfChange
static constexpr int kDefaultNavigationEdgeWidth = 24;
// LINT.ThenChange(//ui/android/java/src/org/chromium/ui/OverscrollRefreshHandler.java:kDefaultNavigationEdgeWidth)
OverscrollRefresh(OverscrollRefreshHandler* handler, float edge_width);
OverscrollRefresh(const OverscrollRefresh&) = delete;
OverscrollRefresh& operator=(const OverscrollRefresh&) = delete;
virtual ~OverscrollRefresh();
// Scroll event stream listening methods.
void OnScrollBegin(const gfx::PointF& pos);
// Returns whether the refresh was activated.
void OnScrollEnd(const gfx::Vector2dF& velocity);
// Scroll ack listener. The effect will only be activated if |can_navigate|
// is true which happens when the scroll update is not consumed and the
// overscroll_behavior on y axis is 'auto'.
// This method is made virtual for mocking.
virtual void OnOverscrolled(const cc::OverscrollBehavior& behavior,
gfx::Vector2dF accumulated_overscroll);
// Disables scroll consumption if the activation shouldn't have happened.
void MaybeDisableScrollConsumption(const gfx::Vector2dF& scroll_delta);
// Returns true if the effect has consumed the |scroll_delta|.
bool WillHandleScrollUpdate(const gfx::Vector2dF& scroll_delta);
// Release the effect (if active), preventing any associated refresh action.
void ReleaseWithoutActivation();
// Notify the effect of the latest scroll offset and overflow properties.
// The effect will be disabled when the offset is non-zero or overflow is
// hidden. Note: All dimensions are in device pixels.
void OnFrameUpdated(const gfx::SizeF& viewport_size,
const gfx::PointF& content_scroll_offset,
const gfx::SizeF& content_size,
bool root_overflow_y_hidden);
// Reset the effect to its inactive state, immediately detaching and
// disabling any active effects.
// This method is made virtual for mocking.
virtual void Reset();
// Returns true if the refresh effect is either being manipulated or animated.
// This method is made virtual for mocking.
virtual bool IsActive() const;
// Returns true if the effect is waiting for an unconsumed scroll to start.
// This method is made virtual for mocking.
virtual bool IsAwaitingScrollUpdateAck() const;
protected:
// This constructor is for mocking only.
OverscrollRefresh();
private:
void Release(bool allow_refresh);
bool scrolled_to_top_;
bool scrolled_to_bottom_;
// True if the content y offset was zero before scroll began. Overscroll
// should not be triggered for the scroll that started from non-zero offset.
bool top_at_scroll_start_;
// True if the scroll is from the bottom of the screen. Overscroll
// should not be triggered for the scroll that started from non-zero offset.
bool bottom_at_scroll_start_;
bool overflow_y_hidden_;
enum class ScrollConsumptionState {
kDisabled,
kAwaitingScrollUpdateAck,
kEnabled,
} scroll_consumption_state_;
float viewport_width_;
float scroll_begin_x_;
float scroll_begin_y_;
const float edge_width_; // in px
const raw_ptr<OverscrollRefreshHandler, DanglingUntriaged> handler_;
};
} // namespace ui
#endif // UI_ANDROID_OVERSCROLL_REFRESH_H_