1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "VisualViewport.h"
9 #include "mozilla/EventDispatcher.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/ToString.h"
12 #include "nsIScrollableFrame.h"
13 #include "nsIDocShell.h"
14 #include "nsPresContext.h"
15 #include "nsRefreshDriver.h"
16 #include "DocumentInlines.h"
19 // #define VVP_LOG(...) printf_stderr("VVP: " __VA_ARGS__)
21 using namespace mozilla
;
22 using namespace mozilla::dom
;
24 VisualViewport::VisualViewport(nsPIDOMWindowInner
* aWindow
)
25 : DOMEventTargetHelper(aWindow
) {}
27 VisualViewport::~VisualViewport() {
29 mResizeEvent
->Revoke();
33 mScrollEvent
->Revoke();
38 JSObject
* VisualViewport::WrapObject(JSContext
* aCx
,
39 JS::Handle
<JSObject
*> aGivenProto
) {
40 return VisualViewport_Binding::Wrap(aCx
, this, aGivenProto
);
44 void VisualViewport::GetEventTargetParent(EventChainPreVisitor
& aVisitor
) {
45 EventMessage msg
= aVisitor
.mEvent
->mMessage
;
47 aVisitor
.mCanHandle
= true;
48 EventTarget
* parentTarget
= nullptr;
49 // Only our special internal events are allowed to escape the
50 // Visual Viewport and be dispatched further up the DOM tree.
51 if (msg
== eMozVisualScroll
|| msg
== eMozVisualResize
) {
52 if (nsPIDOMWindowInner
* win
= GetOwner()) {
53 if (Document
* doc
= win
->GetExtantDoc()) {
58 aVisitor
.SetParentTarget(parentTarget
, false);
61 CSSSize
VisualViewport::VisualViewportSize() const {
62 CSSSize size
= CSSSize(0, 0);
64 // Flush layout, as that may affect the answer below (e.g. scrollbars
65 // may have appeared, decreasing the available viewport size).
66 RefPtr
<const VisualViewport
> kungFuDeathGrip(this);
67 if (Document
* doc
= GetDocument()) {
68 doc
->FlushPendingNotifications(FlushType::Layout
);
71 // Fetch the pres shell after the layout flush, as it might have destroyed it.
72 if (PresShell
* presShell
= GetPresShell()) {
73 if (presShell
->IsVisualViewportSizeSet()) {
74 size
= CSSRect::FromAppUnits(presShell
->GetVisualViewportSize());
76 nsIScrollableFrame
* sf
= presShell
->GetRootScrollFrameAsScrollable();
78 size
= CSSRect::FromAppUnits(sf
->GetScrollPortRect().Size());
85 double VisualViewport::Width() const {
86 CSSSize size
= VisualViewportSize();
90 double VisualViewport::Height() const {
91 CSSSize size
= VisualViewportSize();
95 double VisualViewport::Scale() const {
97 if (PresShell
* presShell
= GetPresShell()) {
98 scale
= presShell
->GetResolution();
103 CSSPoint
VisualViewport::VisualViewportOffset() const {
104 CSSPoint offset
= CSSPoint(0, 0);
106 if (PresShell
* presShell
= GetPresShell()) {
107 offset
= CSSPoint::FromAppUnits(presShell
->GetVisualViewportOffset());
112 CSSPoint
VisualViewport::LayoutViewportOffset() const {
113 CSSPoint offset
= CSSPoint(0, 0);
115 if (PresShell
* presShell
= GetPresShell()) {
116 offset
= CSSPoint::FromAppUnits(presShell
->GetLayoutViewportOffset());
121 double VisualViewport::PageLeft() const { return VisualViewportOffset().X(); }
123 double VisualViewport::PageTop() const { return VisualViewportOffset().Y(); }
125 double VisualViewport::OffsetLeft() const {
126 return PageLeft() - LayoutViewportOffset().X();
129 double VisualViewport::OffsetTop() const {
130 return PageTop() - LayoutViewportOffset().Y();
133 Document
* VisualViewport::GetDocument() const {
134 nsCOMPtr
<nsPIDOMWindowInner
> window
= GetOwner();
139 nsIDocShell
* docShell
= window
->GetDocShell();
144 return docShell
->GetDocument();
147 PresShell
* VisualViewport::GetPresShell() const {
148 RefPtr
<Document
> document
= GetDocument();
149 return document
? document
->GetPresShell() : nullptr;
152 nsPresContext
* VisualViewport::GetPresContext() const {
153 RefPtr
<Document
> document
= GetDocument();
154 return document
? document
->GetPresContext() : nullptr;
157 /* ================= Resize event handling ================= */
159 void VisualViewport::PostResizeEvent() {
160 VVP_LOG("%p: PostResizeEvent (pre-existing: %d)\n", this, !!mResizeEvent
);
161 nsPresContext
* presContext
= GetPresContext();
162 if (mResizeEvent
&& mResizeEvent
->HasPresContext(presContext
)) {
166 // prescontext changed, so discard the old resize event and queue a new one
167 mResizeEvent
->Revoke();
168 mResizeEvent
= nullptr;
171 // The event constructor will register itself with the refresh driver.
173 mResizeEvent
= new VisualViewportResizeEvent(this, presContext
);
174 VVP_LOG("%p: PostResizeEvent, created new event\n", this);
178 VisualViewport::VisualViewportResizeEvent::VisualViewportResizeEvent(
179 VisualViewport
* aViewport
, nsPresContext
* aPresContext
)
180 : Runnable("VisualViewport::VisualViewportResizeEvent"),
181 mViewport(aViewport
),
182 mPresContext(aPresContext
) {
183 VVP_LOG("%p: Registering PostResize on %p %p\n", aViewport
, aPresContext
,
184 aPresContext
->RefreshDriver());
185 aPresContext
->RefreshDriver()->PostVisualViewportResizeEvent(this);
188 bool VisualViewport::VisualViewportResizeEvent::HasPresContext(
189 nsPresContext
* aContext
) const {
190 return mPresContext
.get() == aContext
;
193 void VisualViewport::VisualViewportResizeEvent::Revoke() {
195 mPresContext
= nullptr;
199 VisualViewport::VisualViewportResizeEvent::Run() {
201 mViewport
->FireResizeEvent();
206 void VisualViewport::FireResizeEvent() {
207 MOZ_ASSERT(mResizeEvent
);
208 mResizeEvent
->Revoke();
209 mResizeEvent
= nullptr;
211 VVP_LOG("%p, FireResizeEvent, fire mozvisualresize\n", this);
212 WidgetEvent
mozEvent(true, eMozVisualResize
);
213 mozEvent
.mFlags
.mOnlySystemGroupDispatch
= true;
214 EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent
);
216 VVP_LOG("%p, FireResizeEvent, fire VisualViewport resize\n", this);
217 WidgetEvent
event(true, eResize
);
218 event
.mFlags
.mBubbles
= false;
219 event
.mFlags
.mCancelable
= false;
220 EventDispatcher::Dispatch(this, GetPresContext(), &event
);
223 /* ================= Scroll event handling ================= */
225 void VisualViewport::PostScrollEvent(const nsPoint
& aPrevVisualOffset
,
226 const nsPoint
& aPrevLayoutOffset
) {
227 VVP_LOG("%p: PostScrollEvent, prevRelativeOffset=%s (pre-existing: %d)\n",
228 this, ToString(aPrevVisualOffset
- aPrevLayoutOffset
).c_str(),
230 nsPresContext
* presContext
= GetPresContext();
231 if (mScrollEvent
&& mScrollEvent
->HasPresContext(presContext
)) {
236 // prescontext changed, so discard the old scroll event and queue a new one
237 mScrollEvent
->Revoke();
238 mScrollEvent
= nullptr;
241 // The event constructor will register itself with the refresh driver.
243 mScrollEvent
= new VisualViewportScrollEvent(
244 this, presContext
, aPrevVisualOffset
, aPrevLayoutOffset
);
245 VVP_LOG("%p: PostScrollEvent, created new event\n", this);
249 VisualViewport::VisualViewportScrollEvent::VisualViewportScrollEvent(
250 VisualViewport
* aViewport
, nsPresContext
* aPresContext
,
251 const nsPoint
& aPrevVisualOffset
, const nsPoint
& aPrevLayoutOffset
)
252 : Runnable("VisualViewport::VisualViewportScrollEvent"),
253 mViewport(aViewport
),
254 mPresContext(aPresContext
),
255 mPrevVisualOffset(aPrevVisualOffset
),
256 mPrevLayoutOffset(aPrevLayoutOffset
) {
257 VVP_LOG("%p: Registering PostScroll on %p %p\n", aViewport
, aPresContext
,
258 aPresContext
->RefreshDriver());
259 aPresContext
->RefreshDriver()->PostVisualViewportScrollEvent(this);
262 bool VisualViewport::VisualViewportScrollEvent::HasPresContext(
263 nsPresContext
* aContext
) const {
264 return mPresContext
.get() == aContext
;
267 void VisualViewport::VisualViewportScrollEvent::Revoke() {
269 mPresContext
= nullptr;
273 VisualViewport::VisualViewportScrollEvent::Run() {
275 mViewport
->FireScrollEvent();
280 void VisualViewport::FireScrollEvent() {
281 MOZ_ASSERT(mScrollEvent
);
282 nsPoint prevVisualOffset
= mScrollEvent
->PrevVisualOffset();
283 nsPoint prevLayoutOffset
= mScrollEvent
->PrevLayoutOffset();
284 mScrollEvent
->Revoke();
285 mScrollEvent
= nullptr;
287 if (PresShell
* presShell
= GetPresShell()) {
288 if (presShell
->GetVisualViewportOffset() != prevVisualOffset
) {
289 // The internal event will be fired whenever the visual viewport's
290 // *absolute* offset changed, i.e. relative to the page.
291 VVP_LOG("%p: FireScrollEvent, fire mozvisualscroll\n", this);
292 WidgetEvent
mozEvent(true, eMozVisualScroll
);
293 mozEvent
.mFlags
.mOnlySystemGroupDispatch
= true;
294 EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent
);
297 // Check whether the relative visual viewport offset actually changed -
298 // maybe both visual and layout viewport scrolled together and there was no
300 nsPoint curRelativeOffset
=
301 presShell
->GetVisualViewportOffsetRelativeToLayoutViewport();
302 nsPoint prevRelativeOffset
= prevVisualOffset
- prevLayoutOffset
;
304 "%p: FireScrollEvent, curRelativeOffset %s, "
305 "prevRelativeOffset %s\n",
306 this, ToString(curRelativeOffset
).c_str(),
307 ToString(prevRelativeOffset
).c_str());
308 if (curRelativeOffset
!= prevRelativeOffset
) {
309 VVP_LOG("%p, FireScrollEvent, fire VisualViewport scroll\n", this);
310 WidgetGUIEvent
event(true, eScroll
, nullptr);
311 event
.mFlags
.mBubbles
= false;
312 event
.mFlags
.mCancelable
= false;
313 EventDispatcher::Dispatch(this, GetPresContext(), &event
);