gecko-dev/dom/base/VisualViewport.h
Jan Henning 6011bbb22c Bug 1478776 - Part 6: Initial Visual Viewport event implementation. r=botond
The event rate throttling mechanism is modelled on the logic for "scroll" events
in nsGfxScrollFrame.cpp.

That is
1. When a request to fire an event is posted to the VisualViewport, we create a
   new runnable for this and register it with the RefreshDriver. If we already
   have a pending runnable, calling VisualViewport->Post...Event() becomes a
   no-op.
2. When the RefreshDriver is ready, it executes the runnable, which in turn
   fires the actual event and then cleans itself up.

To keep this patch manageable, we simply fire a scroll event every time the
stored visual viewport offset is changed. Because we are storing the absolute
offset of the viewport relative to the page, this behaviour doesn't match the
spec, which demands that scroll events are fired only when the relative offset
between visual and layout viewport changes. We'll fix this up in the next patch.

Differential Revision: https://phabricator.services.mozilla.com/D14043

--HG--
extra : moz-landing-system : lando
2018-12-20 21:35:26 +00:00

84 lines
2.3 KiB
C++

/* -*- 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/dom/VisualViewportBinding.h"
#include "Units.h"
#include "nsIPresShell.h"
namespace mozilla {
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;
double Width() const;
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 PostResizeEvent();
void PostScrollEvent();
// 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);
void Revoke() { mViewport = nullptr; }
private:
VisualViewport* mViewport;
};
class VisualViewportScrollEvent : public Runnable {
public:
NS_DECL_NSIRUNNABLE
VisualViewportScrollEvent(VisualViewport* aViewport,
nsPresContext* aPresContext);
void Revoke() { mViewport = nullptr; }
private:
VisualViewport* mViewport;
};
private:
virtual ~VisualViewport();
CSSSize VisualViewportSize() const;
CSSPoint VisualViewportOffset() const;
CSSPoint LayoutViewportOffset() const;
nsIPresShell* GetPresShell() const;
nsPresContext* GetPresContext() const;
void FireResizeEvent();
void FireScrollEvent();
RefPtr<VisualViewportResizeEvent> mResizeEvent;
RefPtr<VisualViewportScrollEvent> mScrollEvent;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_VisualViewport_h