summaryrefslogtreecommitdiffstats
path: root/dom/base/VisualViewport.h
blob: 7eed2c446d49e927c7ad52f2728a2fd82a3d71db (plain)
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
/* -*- 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/. */

#ifndef mozilla_dom_VisualViewport_h
#define mozilla_dom_VisualViewport_h

#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/dom/VisualViewportBinding.h"
#include "Units.h"

class nsPresContext;

namespace mozilla {

class PresShell;

namespace dom {

/* Visual Viewport API spec:
 * https://wicg.github.io/visual-viewport/#the-visualviewport-interface */
class VisualViewport final : public mozilla::DOMEventTargetHelper {
 public:
  explicit VisualViewport(nsPIDOMWindowInner* aWindow);

  double OffsetLeft() const;
  double OffsetTop() const;
  double PageLeft() const;
  double PageTop() const;
  MOZ_CAN_RUN_SCRIPT double Width() const;
  MOZ_CAN_RUN_SCRIPT double Height() const;
  double Scale() const;
  IMPL_EVENT_HANDLER(resize)
  IMPL_EVENT_HANDLER(scroll)

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;

  void PostResizeEvent();
  void PostScrollEvent(const nsPoint& aPrevVisualOffset,
                       const nsPoint& aPrevLayoutOffset);

  // These two events are modelled after the ScrollEvent class in
  // nsGfxScrollFrame.h.
  class VisualViewportResizeEvent : public Runnable {
   public:
    NS_DECL_NSIRUNNABLE
    VisualViewportResizeEvent(VisualViewport* aViewport,
                              nsPresContext* aPresContext);
    bool HasPresContext(nsPresContext* aContext) const;
    void Revoke();

   private:
    VisualViewport* mViewport;
    WeakPtr<nsPresContext> mPresContext;
  };

  class VisualViewportScrollEvent : public Runnable {
   public:
    NS_DECL_NSIRUNNABLE
    VisualViewportScrollEvent(VisualViewport* aViewport,
                              nsPresContext* aPresContext,
                              const nsPoint& aPrevVisualOffset,
                              const nsPoint& aPrevLayoutOffset);
    bool HasPresContext(nsPresContext* aContext) const;
    void Revoke();
    nsPoint PrevVisualOffset() const { return mPrevVisualOffset; }
    nsPoint PrevLayoutOffset() const { return mPrevLayoutOffset; }

   private:
    VisualViewport* mViewport;
    WeakPtr<nsPresContext> mPresContext;
    // The VisualViewport "scroll" event is supposed to be fired only when the
    // *relative* offset between visual and layout viewport changes. The two
    // viewports are updated independently from each other, though, so the only
    // thing we can do is note the fact that one of the inputs into the relative
    // visual viewport offset changed and then check the offset again at the
    // next refresh driver tick, just before the event is going to fire.
    // Hopefully, at this point both visual and layout viewport positions have
    // been updated, so that we're able to tell whether the relative offset did
    // in fact change or not.
    const nsPoint mPrevVisualOffset;
    const nsPoint mPrevLayoutOffset;
  };

 private:
  virtual ~VisualViewport();

  MOZ_CAN_RUN_SCRIPT CSSSize VisualViewportSize() const;
  CSSPoint VisualViewportOffset() const;
  CSSPoint LayoutViewportOffset() const;
  Document* GetDocument() const;
  PresShell* GetPresShell() const;
  nsPresContext* GetPresContext() const;

  MOZ_CAN_RUN_SCRIPT void FireResizeEvent();
  MOZ_CAN_RUN_SCRIPT void FireScrollEvent();

  RefPtr<VisualViewportResizeEvent> mResizeEvent;
  RefPtr<VisualViewportScrollEvent> mScrollEvent;
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_VisualViewport_h