Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / VisualViewport.h
blob7eed2c446d49e927c7ad52f2728a2fd82a3d71db
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 #ifndef mozilla_dom_VisualViewport_h
8 #define mozilla_dom_VisualViewport_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/WeakPtr.h"
12 #include "mozilla/dom/VisualViewportBinding.h"
13 #include "Units.h"
15 class nsPresContext;
17 namespace mozilla {
19 class PresShell;
21 namespace dom {
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);
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)
39 virtual JSObject* WrapObject(JSContext* aCx,
40 JS::Handle<JSObject*> aGivenProto) override;
41 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
43 void PostResizeEvent();
44 void PostScrollEvent(const nsPoint& aPrevVisualOffset,
45 const nsPoint& aPrevLayoutOffset);
47 // These two events are modelled after the ScrollEvent class in
48 // nsGfxScrollFrame.h.
49 class VisualViewportResizeEvent : public Runnable {
50 public:
51 NS_DECL_NSIRUNNABLE
52 VisualViewportResizeEvent(VisualViewport* aViewport,
53 nsPresContext* aPresContext);
54 bool HasPresContext(nsPresContext* aContext) const;
55 void Revoke();
57 private:
58 VisualViewport* mViewport;
59 WeakPtr<nsPresContext> mPresContext;
62 class VisualViewportScrollEvent : public Runnable {
63 public:
64 NS_DECL_NSIRUNNABLE
65 VisualViewportScrollEvent(VisualViewport* aViewport,
66 nsPresContext* aPresContext,
67 const nsPoint& aPrevVisualOffset,
68 const nsPoint& aPrevLayoutOffset);
69 bool HasPresContext(nsPresContext* aContext) const;
70 void Revoke();
71 nsPoint PrevVisualOffset() const { return mPrevVisualOffset; }
72 nsPoint PrevLayoutOffset() const { return mPrevLayoutOffset; }
74 private:
75 VisualViewport* mViewport;
76 WeakPtr<nsPresContext> mPresContext;
77 // The VisualViewport "scroll" event is supposed to be fired only when the
78 // *relative* offset between visual and layout viewport changes. The two
79 // viewports are updated independently from each other, though, so the only
80 // thing we can do is note the fact that one of the inputs into the relative
81 // visual viewport offset changed and then check the offset again at the
82 // next refresh driver tick, just before the event is going to fire.
83 // Hopefully, at this point both visual and layout viewport positions have
84 // been updated, so that we're able to tell whether the relative offset did
85 // in fact change or not.
86 const nsPoint mPrevVisualOffset;
87 const nsPoint mPrevLayoutOffset;
90 private:
91 virtual ~VisualViewport();
93 MOZ_CAN_RUN_SCRIPT CSSSize VisualViewportSize() const;
94 CSSPoint VisualViewportOffset() const;
95 CSSPoint LayoutViewportOffset() const;
96 Document* GetDocument() const;
97 PresShell* GetPresShell() const;
98 nsPresContext* GetPresContext() const;
100 MOZ_CAN_RUN_SCRIPT void FireResizeEvent();
101 MOZ_CAN_RUN_SCRIPT void FireScrollEvent();
103 RefPtr<VisualViewportResizeEvent> mResizeEvent;
104 RefPtr<VisualViewportScrollEvent> mScrollEvent;
107 } // namespace dom
108 } // namespace mozilla
110 #endif // mozilla_dom_VisualViewport_h