VisualViewport.h (3356B)
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/. */ 6 7 #ifndef mozilla_dom_VisualViewport_h 8 #define mozilla_dom_VisualViewport_h 9 10 #include "Units.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/WeakPtr.h" 13 #include "mozilla/dom/VisualViewportBinding.h" 14 15 class nsPresContext; 16 17 namespace mozilla { 18 19 class PresShell; 20 21 namespace dom { 22 23 /* Visual Viewport API spec: 24 * https://wicg.github.io/visual-viewport/#the-visualviewport-interface */ 25 class VisualViewport final : public mozilla::DOMEventTargetHelper { 26 public: 27 explicit VisualViewport(nsPIDOMWindowInner* aWindow); 28 29 double OffsetLeft() const; 30 double OffsetTop() const; 31 double PageLeft() const; 32 double PageTop() const; 33 MOZ_CAN_RUN_SCRIPT double Width() const; 34 MOZ_CAN_RUN_SCRIPT double Height() const; 35 double Scale() const; 36 IMPL_EVENT_HANDLER(resize) 37 IMPL_EVENT_HANDLER(scroll) 38 39 virtual JSObject* WrapObject(JSContext* aCx, 40 JS::Handle<JSObject*> aGivenProto) override; 41 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override; 42 43 void PostResizeEvent(); 44 MOZ_CAN_RUN_SCRIPT void FireResizeEvent(); 45 46 void PostScrollEvent(const nsPoint& aPrevVisualOffset, 47 const nsPoint& aPrevLayoutOffset); 48 49 class VisualViewportScrollEvent : public Runnable { 50 public: 51 NS_DECL_NSIRUNNABLE 52 VisualViewportScrollEvent(VisualViewport* aViewport, 53 nsPresContext* aPresContext, 54 const nsPoint& aPrevVisualOffset, 55 const nsPoint& aPrevLayoutOffset); 56 bool HasPresContext(nsPresContext* aContext) const; 57 void Revoke(); 58 nsPoint PrevVisualOffset() const { return mPrevVisualOffset; } 59 nsPoint PrevLayoutOffset() const { return mPrevLayoutOffset; } 60 61 private: 62 VisualViewport* mViewport; 63 WeakPtr<nsPresContext> mPresContext; 64 // The VisualViewport "scroll" event is supposed to be fired only when the 65 // *relative* offset between visual and layout viewport changes. The two 66 // viewports are updated independently from each other, though, so the only 67 // thing we can do is note the fact that one of the inputs into the relative 68 // visual viewport offset changed and then check the offset again at the 69 // next refresh driver tick, just before the event is going to fire. 70 // Hopefully, at this point both visual and layout viewport positions have 71 // been updated, so that we're able to tell whether the relative offset did 72 // in fact change or not. 73 const nsPoint mPrevVisualOffset; 74 const nsPoint mPrevLayoutOffset; 75 }; 76 77 private: 78 virtual ~VisualViewport(); 79 80 MOZ_CAN_RUN_SCRIPT CSSSize VisualViewportSize() const; 81 CSSPoint VisualViewportOffset() const; 82 CSSPoint LayoutViewportOffset() const; 83 Document* GetDocument() const; 84 PresShell* GetPresShell() const; 85 nsPresContext* GetPresContext() const; 86 87 MOZ_CAN_RUN_SCRIPT void FireScrollEvent(); 88 89 RefPtr<VisualViewportScrollEvent> mScrollEvent; 90 }; 91 92 } // namespace dom 93 } // namespace mozilla 94 95 #endif // mozilla_dom_VisualViewport_h