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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "VisualViewport.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/PresShell.h"
#include "mozilla/ToString.h"
#include "nsIScrollableFrame.h"
#include "nsIDocShell.h"
#include "nsPresContext.h"
#include "nsRefreshDriver.h"
#include "DocumentInlines.h"
static mozilla::LazyLogModule sVvpLog("visualviewport");
#define VVP_LOG(...) MOZ_LOG(sVvpLog, LogLevel::Debug, (__VA_ARGS__))
using namespace mozilla;
using namespace mozilla::dom;
VisualViewport::VisualViewport(nsPIDOMWindowInner* aWindow)
: DOMEventTargetHelper(aWindow) {}
VisualViewport::~VisualViewport() {
if (mResizeEvent) {
mResizeEvent->Revoke();
}
if (mScrollEvent) {
mScrollEvent->Revoke();
}
}
/* virtual */
JSObject* VisualViewport::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return VisualViewport_Binding::Wrap(aCx, this, aGivenProto);
}
/* virtual */
void VisualViewport::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
EventMessage msg = aVisitor.mEvent->mMessage;
aVisitor.mCanHandle = true;
EventTarget* parentTarget = nullptr;
// Only our special internal events are allowed to escape the
// Visual Viewport and be dispatched further up the DOM tree.
if (msg == eMozVisualScroll || msg == eMozVisualResize) {
if (nsPIDOMWindowInner* win = GetOwner()) {
if (Document* doc = win->GetExtantDoc()) {
parentTarget = doc;
}
}
}
aVisitor.SetParentTarget(parentTarget, false);
}
CSSSize VisualViewport::VisualViewportSize() const {
CSSSize size = CSSSize(0, 0);
// Flush layout, as that may affect the answer below (e.g. scrollbars
// may have appeared, decreasing the available viewport size).
RefPtr<const VisualViewport> kungFuDeathGrip(this);
if (Document* doc = GetDocument()) {
doc->FlushPendingNotifications(FlushType::Layout);
}
// Fetch the pres shell after the layout flush, as it might have destroyed it.
if (PresShell* presShell = GetPresShell()) {
if (presShell->IsVisualViewportSizeSet()) {
DynamicToolbarState state = presShell->GetDynamicToolbarState();
size = CSSRect::FromAppUnits(
(state == DynamicToolbarState::InTransition ||
state == DynamicToolbarState::Collapsed)
? presShell->GetVisualViewportSizeUpdatedByDynamicToolbar()
: presShell->GetVisualViewportSize());
} else {
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
if (sf) {
size = CSSRect::FromAppUnits(sf->GetScrollPortRect().Size());
}
}
}
return size;
}
double VisualViewport::Width() const {
CSSSize size = VisualViewportSize();
return size.width;
}
double VisualViewport::Height() const {
CSSSize size = VisualViewportSize();
return size.height;
}
double VisualViewport::Scale() const {
double scale = 1;
if (PresShell* presShell = GetPresShell()) {
scale = presShell->GetResolution();
}
return scale;
}
CSSPoint VisualViewport::VisualViewportOffset() const {
CSSPoint offset = CSSPoint(0, 0);
if (PresShell* presShell = GetPresShell()) {
offset = CSSPoint::FromAppUnits(presShell->GetVisualViewportOffset());
}
return offset;
}
CSSPoint VisualViewport::LayoutViewportOffset() const {
CSSPoint offset = CSSPoint(0, 0);
if (PresShell* presShell = GetPresShell()) {
offset = CSSPoint::FromAppUnits(presShell->GetLayoutViewportOffset());
}
return offset;
}
double VisualViewport::PageLeft() const { return VisualViewportOffset().X(); }
double VisualViewport::PageTop() const { return VisualViewportOffset().Y(); }
double VisualViewport::OffsetLeft() const {
return PageLeft() - LayoutViewportOffset().X();
}
double VisualViewport::OffsetTop() const {
return PageTop() - LayoutViewportOffset().Y();
}
Document* VisualViewport::GetDocument() const {
nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
if (!window) {
return nullptr;
}
nsIDocShell* docShell = window->GetDocShell();
if (!docShell) {
return nullptr;
}
return docShell->GetDocument();
}
PresShell* VisualViewport::GetPresShell() const {
RefPtr<Document> document = GetDocument();
return document ? document->GetPresShell() : nullptr;
}
nsPresContext* VisualViewport::GetPresContext() const {
RefPtr<Document> document = GetDocument();
return document ? document->GetPresContext() : nullptr;
}
/* ================= Resize event handling ================= */
void VisualViewport::PostResizeEvent() {
VVP_LOG("%p: PostResizeEvent (pre-existing: %d)\n", this, !!mResizeEvent);
nsPresContext* presContext = GetPresContext();
if (mResizeEvent && mResizeEvent->HasPresContext(presContext)) {
return;
}
if (mResizeEvent) {
// prescontext changed, so discard the old resize event and queue a new one
mResizeEvent->Revoke();
mResizeEvent = nullptr;
}
// The event constructor will register itself with the refresh driver.
if (presContext) {
mResizeEvent = new VisualViewportResizeEvent(this, presContext);
VVP_LOG("%p: PostResizeEvent, created new event\n", this);
}
}
VisualViewport::VisualViewportResizeEvent::VisualViewportResizeEvent(
VisualViewport* aViewport, nsPresContext* aPresContext)
: Runnable("VisualViewport::VisualViewportResizeEvent"),
mViewport(aViewport),
mPresContext(aPresContext) {
VVP_LOG("%p: Registering PostResize on %p %p\n", aViewport, aPresContext,
aPresContext->RefreshDriver());
aPresContext->RefreshDriver()->PostVisualViewportResizeEvent(this);
}
bool VisualViewport::VisualViewportResizeEvent::HasPresContext(
nsPresContext* aContext) const {
return mPresContext.get() == aContext;
}
void VisualViewport::VisualViewportResizeEvent::Revoke() {
mViewport = nullptr;
mPresContext = nullptr;
}
NS_IMETHODIMP
VisualViewport::VisualViewportResizeEvent::Run() {
if (mViewport) {
mViewport->FireResizeEvent();
}
return NS_OK;
}
void VisualViewport::FireResizeEvent() {
MOZ_ASSERT(mResizeEvent);
mResizeEvent->Revoke();
mResizeEvent = nullptr;
VVP_LOG("%p, FireResizeEvent, fire mozvisualresize\n", this);
WidgetEvent mozEvent(true, eMozVisualResize);
mozEvent.mFlags.mOnlySystemGroupDispatch = true;
EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent);
VVP_LOG("%p, FireResizeEvent, fire VisualViewport resize\n", this);
WidgetEvent event(true, eResize);
event.mFlags.mBubbles = false;
event.mFlags.mCancelable = false;
EventDispatcher::Dispatch(this, GetPresContext(), &event);
}
/* ================= Scroll event handling ================= */
void VisualViewport::PostScrollEvent(const nsPoint& aPrevVisualOffset,
const nsPoint& aPrevLayoutOffset) {
VVP_LOG("%p: PostScrollEvent, prevRelativeOffset=%s (pre-existing: %d)\n",
this, ToString(aPrevVisualOffset - aPrevLayoutOffset).c_str(),
!!mScrollEvent);
nsPresContext* presContext = GetPresContext();
if (mScrollEvent && mScrollEvent->HasPresContext(presContext)) {
return;
}
if (mScrollEvent) {
// prescontext changed, so discard the old scroll event and queue a new one
mScrollEvent->Revoke();
mScrollEvent = nullptr;
}
// The event constructor will register itself with the refresh driver.
if (presContext) {
mScrollEvent = new VisualViewportScrollEvent(
this, presContext, aPrevVisualOffset, aPrevLayoutOffset);
VVP_LOG("%p: PostScrollEvent, created new event\n", this);
}
}
VisualViewport::VisualViewportScrollEvent::VisualViewportScrollEvent(
VisualViewport* aViewport, nsPresContext* aPresContext,
const nsPoint& aPrevVisualOffset, const nsPoint& aPrevLayoutOffset)
: Runnable("VisualViewport::VisualViewportScrollEvent"),
mViewport(aViewport),
mPresContext(aPresContext),
mPrevVisualOffset(aPrevVisualOffset),
mPrevLayoutOffset(aPrevLayoutOffset) {
VVP_LOG("%p: Registering PostScroll on %p %p\n", aViewport, aPresContext,
aPresContext->RefreshDriver());
aPresContext->RefreshDriver()->PostVisualViewportScrollEvent(this);
}
bool VisualViewport::VisualViewportScrollEvent::HasPresContext(
nsPresContext* aContext) const {
return mPresContext.get() == aContext;
}
void VisualViewport::VisualViewportScrollEvent::Revoke() {
mViewport = nullptr;
mPresContext = nullptr;
}
NS_IMETHODIMP
VisualViewport::VisualViewportScrollEvent::Run() {
if (mViewport) {
mViewport->FireScrollEvent();
}
return NS_OK;
}
void VisualViewport::FireScrollEvent() {
MOZ_ASSERT(mScrollEvent);
nsPoint prevVisualOffset = mScrollEvent->PrevVisualOffset();
nsPoint prevLayoutOffset = mScrollEvent->PrevLayoutOffset();
mScrollEvent->Revoke();
mScrollEvent = nullptr;
if (PresShell* presShell = GetPresShell()) {
if (presShell->GetVisualViewportOffset() != prevVisualOffset) {
// The internal event will be fired whenever the visual viewport's
// *absolute* offset changed, i.e. relative to the page.
VVP_LOG("%p: FireScrollEvent, fire mozvisualscroll\n", this);
WidgetEvent mozEvent(true, eMozVisualScroll);
mozEvent.mFlags.mOnlySystemGroupDispatch = true;
EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent);
}
// Check whether the relative visual viewport offset actually changed -
// maybe both visual and layout viewport scrolled together and there was no
// change after all.
nsPoint curRelativeOffset =
presShell->GetVisualViewportOffsetRelativeToLayoutViewport();
nsPoint prevRelativeOffset = prevVisualOffset - prevLayoutOffset;
VVP_LOG(
"%p: FireScrollEvent, curRelativeOffset %s, "
"prevRelativeOffset %s\n",
this, ToString(curRelativeOffset).c_str(),
ToString(prevRelativeOffset).c_str());
if (curRelativeOffset != prevRelativeOffset) {
VVP_LOG("%p, FireScrollEvent, fire VisualViewport scroll\n", this);
WidgetGUIEvent event(true, eScroll, nullptr);
event.mFlags.mBubbles = false;
event.mFlags.mCancelable = false;
EventDispatcher::Dispatch(this, GetPresContext(), &event);
}
}
}
|