Bug 1110039 - Part 2.4 - Add AccessibleCaretEventHub. r=roc

See AccessibleCaretEventHub.h for the class description.

Both TouchCaret and SelectionCarets have their event handling mechanism,
which lead to a lot of code duplication. Now AccessibleCaretEventHub
serves as the single entry point for all events and callbacks.

We also encountered performance issues in SelectionCarets because many
unnecessary events might be dispatched to Gaia driven by the selection
changed events. SelectionCarets did not have clear internal states to
avoid this. To solve it, AccessibleCaretEventHub implements state
classes, and rely on the current states to call the CopyPasteManager's
handler only when it's needed.

For example, when dragging a caret, we do not interest in
NotifySelectionChanged() for updating the carets. Since we've known a
caret is being dragging, we can call UpdateCarets() directly. Hence
DragCaretState does not override OnSelectionChanged().
This commit is contained in:
Ting-Yu Lin 2015-05-03 23:37:00 +02:00
parent 5749b23267
commit 27e0c9e194
4 changed files with 1078 additions and 0 deletions

View File

@ -0,0 +1,814 @@
/* -*- 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/. */
#include "AccessibleCaretEventHub.h"
#include "AccessibleCaretLogger.h"
#include "AccessibleCaretManager.h"
#include "gfxPrefs.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/TextEvents.h"
#include "mozilla/TouchEvents.h"
#include "nsDocShell.h"
#include "nsFocusManager.h"
#include "nsFrameSelection.h"
#include "nsITimer.h"
#include "nsPresContext.h"
namespace mozilla {
#ifdef PR_LOGGING
#undef AC_LOG
#define AC_LOG(message, ...) \
AC_LOG_BASE("AccessibleCaretEventHub (%p): " message, this, ##__VA_ARGS__);
#undef AC_LOGV
#define AC_LOGV(message, ...) \
AC_LOGV_BASE("AccessibleCaretEventHub (%p): " message, this, ##__VA_ARGS__);
#endif // #ifdef PR_LOGGING
NS_IMPL_ISUPPORTS(AccessibleCaretEventHub, nsIReflowObserver, nsIScrollObserver,
nsISelectionListener, nsISupportsWeakReference);
// -----------------------------------------------------------------------------
// NoActionState
//
class AccessibleCaretEventHub::NoActionState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(NoActionState)
virtual nsEventStatus OnPress(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint,
int32_t aTouchId) override
{
nsEventStatus rv = nsEventStatus_eIgnore;
if (NS_SUCCEEDED(aContext->mManager->PressCaret(aPoint))) {
aContext->SetState(aContext->PressCaretState());
rv = nsEventStatus_eConsumeNoDefault;
} else {
aContext->SetState(aContext->PressNoCaretState());
}
aContext->mPressPoint = aPoint;
aContext->mActiveTouchId = aTouchId;
return rv;
}
virtual void OnScrollStart(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnScrollStart();
aContext->SetState(aContext->ScrollState());
}
virtual void OnScrolling(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnScrolling();
}
virtual void OnScrollPositionChanged(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnScrollPositionChanged();
}
virtual void OnSelectionChanged(AccessibleCaretEventHub* aContext,
nsIDOMDocument* aDoc, nsISelection* aSel,
int16_t aReason) override
{
aContext->mManager->OnSelectionChanged(aDoc, aSel, aReason);
}
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) override
{
aContext->mManager->OnBlur();
}
virtual void OnReflow(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnReflow();
}
virtual void Enter(AccessibleCaretEventHub* aContext) override
{
aContext->mPressPoint = nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
aContext->mActiveTouchId = kInvalidTouchId;
}
};
// -----------------------------------------------------------------------------
// PressCaretState: Always consume the event since we've pressed on the caret.
//
class AccessibleCaretEventHub::PressCaretState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(PressCaretState)
virtual nsEventStatus OnMove(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
if (aContext->MoveDistanceIsLarge(aPoint)) {
if (NS_SUCCEEDED(aContext->mManager->DragCaret(aPoint))) {
aContext->SetState(aContext->DragCaretState());
}
}
return nsEventStatus_eConsumeNoDefault;
}
virtual nsEventStatus OnRelease(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->ReleaseCaret();
aContext->mManager->TapCaret(aContext->mPressPoint);
aContext->SetState(aContext->NoActionState());
return nsEventStatus_eConsumeNoDefault;
}
virtual nsEventStatus OnLongTap(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
return nsEventStatus_eConsumeNoDefault;
}
};
// -----------------------------------------------------------------------------
// DragCaretState: Always consume the event since we've pressed on the caret.
//
class AccessibleCaretEventHub::DragCaretState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(DragCaretState)
virtual nsEventStatus OnMove(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
aContext->mManager->DragCaret(aPoint);
return nsEventStatus_eConsumeNoDefault;
}
virtual nsEventStatus OnRelease(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->ReleaseCaret();
aContext->SetState(aContext->NoActionState());
return nsEventStatus_eConsumeNoDefault;
}
};
// -----------------------------------------------------------------------------
// PressNoCaretState
//
class AccessibleCaretEventHub::PressNoCaretState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(PressNoCaretState)
virtual nsEventStatus OnMove(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
if (aContext->MoveDistanceIsLarge(aPoint)) {
aContext->SetState(aContext->NoActionState());
}
return nsEventStatus_eIgnore;
}
virtual nsEventStatus OnRelease(AccessibleCaretEventHub* aContext) override
{
aContext->SetState(aContext->NoActionState());
return nsEventStatus_eIgnore;
}
virtual nsEventStatus OnLongTap(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
aContext->SetState(aContext->LongTapState());
return aContext->GetState()->OnLongTap(aContext, aPoint);
}
virtual void OnScrollStart(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnScrollStart();
aContext->SetState(aContext->ScrollState());
}
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) override
{
aContext->mManager->OnBlur();
if (aIsLeavingDocument) {
aContext->SetState(aContext->NoActionState());
}
}
virtual void OnSelectionChanged(AccessibleCaretEventHub* aContext,
nsIDOMDocument* aDoc, nsISelection* aSel,
int16_t aReason) override
{
aContext->mManager->OnSelectionChanged(aDoc, aSel, aReason);
}
virtual void OnReflow(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnReflow();
}
virtual void Enter(AccessibleCaretEventHub* aContext) override
{
aContext->LaunchLongTapInjector();
}
virtual void Leave(AccessibleCaretEventHub* aContext) override
{
aContext->CancelLongTapInjector();
}
};
// -----------------------------------------------------------------------------
// ScrollState
//
class AccessibleCaretEventHub::ScrollState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(ScrollState)
virtual void OnScrollEnd(AccessibleCaretEventHub* aContext) override
{
aContext->SetState(aContext->PostScrollState());
}
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) override
{
aContext->mManager->OnBlur();
if (aIsLeavingDocument) {
aContext->SetState(aContext->NoActionState());
}
}
};
// -----------------------------------------------------------------------------
// PostScrollState: In this state, we are waiting for another APZ start, press
// event, or momentum wheel scroll.
//
class AccessibleCaretEventHub::PostScrollState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(PostScrollState)
virtual nsEventStatus OnPress(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint,
int32_t aTouchId) override
{
aContext->mManager->OnScrollEnd();
aContext->SetState(aContext->NoActionState());
return aContext->GetState()->OnPress(aContext, aPoint, aTouchId);
}
virtual void OnScrollStart(AccessibleCaretEventHub* aContext) override
{
aContext->SetState(aContext->ScrollState());
}
virtual void OnScrollEnd(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnScrollEnd();
aContext->SetState(aContext->NoActionState());
}
virtual void OnScrolling(AccessibleCaretEventHub* aContext) override
{
// Momentum scroll by wheel event.
aContext->LaunchScrollEndInjector();
}
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) override
{
aContext->mManager->OnBlur();
if (aIsLeavingDocument) {
aContext->SetState(aContext->NoActionState());
}
}
virtual void Enter(AccessibleCaretEventHub* aContext) override
{
// Launch the injector to leave PostScrollState.
aContext->LaunchScrollEndInjector();
}
virtual void Leave(AccessibleCaretEventHub* aContext) override
{
aContext->CancelScrollEndInjector();
}
};
// -----------------------------------------------------------------------------
// LongTapState
//
class AccessibleCaretEventHub::LongTapState
: public AccessibleCaretEventHub::State
{
public:
NS_IMPL_STATE_UTILITIES(LongTapState)
virtual nsEventStatus OnLongTap(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) override
{
nsEventStatus rv = nsEventStatus_eIgnore;
if (NS_SUCCEEDED(aContext->mManager->SelectWordOrShortcut(aPoint))) {
rv = nsEventStatus_eConsumeNoDefault;
}
aContext->SetState(aContext->NoActionState());
return rv;
}
virtual void OnReflow(AccessibleCaretEventHub* aContext) override
{
aContext->mManager->OnReflow();
}
};
// -----------------------------------------------------------------------------
// Implementation of AccessibleCaretEventHub methods
//
AccessibleCaretEventHub::State*
AccessibleCaretEventHub::GetState() const
{
return mState;
}
void
AccessibleCaretEventHub::SetState(State* aState)
{
MOZ_ASSERT(aState);
AC_LOG("%s -> %s", mState->Name(), aState->Name());
mState->Leave(this);
mState = aState;
mState->Enter(this);
}
NS_IMPL_STATE_CLASS_GETTER(NoActionState)
NS_IMPL_STATE_CLASS_GETTER(PressCaretState)
NS_IMPL_STATE_CLASS_GETTER(DragCaretState)
NS_IMPL_STATE_CLASS_GETTER(PressNoCaretState)
NS_IMPL_STATE_CLASS_GETTER(ScrollState)
NS_IMPL_STATE_CLASS_GETTER(PostScrollState)
NS_IMPL_STATE_CLASS_GETTER(LongTapState)
AccessibleCaretEventHub::AccessibleCaretEventHub()
{
}
AccessibleCaretEventHub::~AccessibleCaretEventHub()
{
}
void
AccessibleCaretEventHub::Init(nsIPresShell* aPresShell)
{
if (mInitialized || !aPresShell || !aPresShell->GetCanvasFrame() ||
!aPresShell->GetCanvasFrame()->GetCustomContentContainer()) {
return;
}
// Without nsAutoScriptBlocker, the script might be run after constructing
// mFirstCaret in AccessibleCaretManager's constructor, which might destructs
// the whole frame tree. Therefore we'll fail to construct mSecondCaret
// because we cannot get root frame or canvas frame from mPresShell to inject
// anonymous content. To avoid that, we protect Init() by nsAutoScriptBlocker.
// To reproduce, run "./mach crashtest layout/base/crashtests/897852.html"
// without the following scriptBlocker.
nsAutoScriptBlocker scriptBlocker;
mPresShell = aPresShell;
nsPresContext* presContext = mPresShell->GetPresContext();
MOZ_ASSERT(presContext, "PresContext should be given in PresShell::Init()");
nsIDocShell* docShell = presContext->GetDocShell();
if (!docShell) {
return;
}
#if defined(MOZ_WIDGET_GONK)
mUseAsyncPanZoom = gfxPrefs::AsyncPanZoomEnabled();
#endif
docShell->AddWeakReflowObserver(this);
docShell->AddWeakScrollObserver(this);
mDocShell = static_cast<nsDocShell*>(docShell);
mLongTapInjectorTimer = do_CreateInstance("@mozilla.org/timer;1");
mScrollEndInjectorTimer = do_CreateInstance("@mozilla.org/timer;1");
mManager = MakeUnique<AccessibleCaretManager>(mPresShell);
mInitialized = true;
}
void
AccessibleCaretEventHub::Terminate()
{
if (!mInitialized) {
return;
}
nsRefPtr<nsDocShell> docShell(mDocShell.get());
if (docShell) {
docShell->RemoveWeakReflowObserver(this);
docShell->RemoveWeakScrollObserver(this);
}
if (mLongTapInjectorTimer) {
mLongTapInjectorTimer->Cancel();
}
if (mScrollEndInjectorTimer) {
mScrollEndInjectorTimer->Cancel();
}
mManager = nullptr;
mPresShell = nullptr;
mInitialized = false;
}
nsEventStatus
AccessibleCaretEventHub::HandleEvent(WidgetEvent* aEvent)
{
nsEventStatus status = nsEventStatus_eIgnore;
if (!mInitialized) {
return status;
}
switch (aEvent->mClass) {
case eMouseEventClass:
status = HandleMouseEvent(aEvent->AsMouseEvent());
break;
case eWheelEventClass:
status = HandleWheelEvent(aEvent->AsWheelEvent());
break;
case eTouchEventClass:
status = HandleTouchEvent(aEvent->AsTouchEvent());
break;
case eKeyboardEventClass:
status = HandleKeyboardEvent(aEvent->AsKeyboardEvent());
break;
default:
break;
}
return status;
}
nsEventStatus
AccessibleCaretEventHub::HandleMouseEvent(WidgetMouseEvent* aEvent)
{
nsEventStatus rv = nsEventStatus_eIgnore;
if (aEvent->button != WidgetMouseEvent::eLeftButton) {
return rv;
}
int32_t id = (mActiveTouchId == kInvalidTouchId ?
kDefaultTouchId : mActiveTouchId);
nsPoint point = GetMouseEventPosition(aEvent);
switch (aEvent->message) {
case NS_MOUSE_BUTTON_DOWN:
AC_LOGV("Before NS_MOUSE_BUTTON_DOWN, state: %s", mState->Name());
rv = mState->OnPress(this, point, id);
AC_LOGV("After NS_MOUSE_BUTTON_DOWN, state: %s, consume: %d",
mState->Name(), rv);
break;
case NS_MOUSE_MOVE:
AC_LOGV("Before NS_MOUSE_MOVE, state: %s", mState->Name());
rv = mState->OnMove(this, point);
AC_LOGV("After NS_MOUSE_MOVE, state: %s, consume: %d", mState->Name(), rv);
break;
case NS_MOUSE_BUTTON_UP:
AC_LOGV("Before NS_MOUSE_BUTTON_UP, state: %s", mState->Name());
rv = mState->OnRelease(this);
AC_LOGV("After NS_MOUSE_BUTTON_UP, state: %s, consume: %d", mState->Name(),
rv);
break;
case NS_MOUSE_MOZLONGTAP:
AC_LOGV("Before NS_MOUSE_MOZLONGTAP, state: %s", mState->Name());
rv = mState->OnLongTap(this, point);
AC_LOGV("After NS_MOUSE_MOZLONGTAP, state: %s, consume: %d", mState->Name(),
rv);
break;
default:
break;
}
return rv;
}
nsEventStatus
AccessibleCaretEventHub::HandleWheelEvent(WidgetWheelEvent* aEvent)
{
switch (aEvent->message) {
case NS_WHEEL_WHEEL:
AC_LOGV("NS_WHEEL_WHEEL, isMomentum %d, state: %s", aEvent->isMomentum,
mState->Name());
mState->OnScrolling(this);
break;
case NS_WHEEL_START:
AC_LOGV("NS_WHEEL_START, state: %s", mState->Name());
mState->OnScrollStart(this);
break;
case NS_WHEEL_STOP:
AC_LOGV("NS_WHEEL_STOP, state: %s", mState->Name());
mState->OnScrollEnd(this);
break;
default:
break;
}
// Always ignore this event since we only want to know scroll start and scroll
// end, not to consume it.
return nsEventStatus_eIgnore;
}
nsEventStatus
AccessibleCaretEventHub::HandleTouchEvent(WidgetTouchEvent* aEvent)
{
nsEventStatus rv = nsEventStatus_eIgnore;
int32_t id = (mActiveTouchId == kInvalidTouchId ?
aEvent->touches[0]->Identifier() : mActiveTouchId);
nsPoint point = GetTouchEventPosition(aEvent, id);
switch (aEvent->message) {
case NS_TOUCH_START:
AC_LOGV("Before NS_TOUCH_START, state: %s", mState->Name());
rv = mState->OnPress(this, point, id);
AC_LOGV("After NS_TOUCH_START, state: %s, consume: %d", mState->Name(), rv);
break;
case NS_TOUCH_MOVE:
AC_LOGV("Before NS_TOUCH_MOVE, state: %s", mState->Name());
rv = mState->OnMove(this, point);
AC_LOGV("After NS_TOUCH_MOVE, state: %s, consume: %d", mState->Name(), rv);
break;
case NS_TOUCH_END:
AC_LOGV("Before NS_TOUCH_END, state: %s", mState->Name());
rv = mState->OnRelease(this);
AC_LOGV("After NS_TOUCH_END, state: %s, consume: %d", mState->Name(), rv);
break;
case NS_TOUCH_CANCEL:
AC_LOGV("Before NS_TOUCH_CANCEL, state: %s", mState->Name());
rv = mState->OnRelease(this);
AC_LOGV("After NS_TOUCH_CANCEL, state: %s, consume: %d", mState->Name(),
rv);
break;
default:
break;
}
return rv;
}
nsEventStatus
AccessibleCaretEventHub::HandleKeyboardEvent(WidgetKeyboardEvent* aEvent)
{
switch (aEvent->message) {
case NS_KEY_UP:
case NS_KEY_DOWN:
case NS_KEY_PRESS:
mManager->OnKeyboardEvent();
break;
default:
break;
}
return nsEventStatus_eIgnore;
}
bool
AccessibleCaretEventHub::MoveDistanceIsLarge(const nsPoint& aPoint) const
{
nsPoint delta = aPoint - mPressPoint;
return NS_hypot(delta.x, delta.y) >
nsPresContext::AppUnitsPerCSSPixel() * kMoveStartToleranceInPixel;
}
void
AccessibleCaretEventHub::LaunchLongTapInjector()
{
if (mUseAsyncPanZoom) {
return;
}
if (!mLongTapInjectorTimer) {
return;
}
int32_t longTapDelay = gfxPrefs::UiClickHoldContextMenusDelay();
mLongTapInjectorTimer->InitWithFuncCallback(FireLongTap, this, longTapDelay,
nsITimer::TYPE_ONE_SHOT);
}
void
AccessibleCaretEventHub::CancelLongTapInjector()
{
if (mUseAsyncPanZoom) {
return;
}
if (!mLongTapInjectorTimer) {
return;
}
mLongTapInjectorTimer->Cancel();
}
/* static */ void
AccessibleCaretEventHub::FireLongTap(nsITimer* aTimer,
void* aAccessibleCaretEventHub)
{
auto self = static_cast<AccessibleCaretEventHub*>(aAccessibleCaretEventHub);
self->mState->OnLongTap(self, self->mPressPoint);
}
NS_IMETHODIMP
AccessibleCaretEventHub::Reflow(DOMHighResTimeStamp aStart,
DOMHighResTimeStamp aEnd)
{
if (!mInitialized) {
return NS_OK;
}
AC_LOG("%s, state: %s", __FUNCTION__, mState->Name());
mState->OnReflow(this);
return NS_OK;
}
NS_IMETHODIMP
AccessibleCaretEventHub::ReflowInterruptible(DOMHighResTimeStamp aStart,
DOMHighResTimeStamp aEnd)
{
if (!mInitialized) {
return NS_OK;
}
return Reflow(aStart, aEnd);
}
void
AccessibleCaretEventHub::AsyncPanZoomStarted()
{
if (!mInitialized) {
return;
}
AC_LOG("%s, state: %s", __FUNCTION__, mState->Name());
mState->OnScrollStart(this);
}
void
AccessibleCaretEventHub::AsyncPanZoomStopped()
{
if (!mInitialized) {
return;
}
AC_LOG("%s, state: %s", __FUNCTION__, mState->Name());
mState->OnScrollEnd(this);
}
void
AccessibleCaretEventHub::ScrollPositionChanged()
{
if (!mInitialized) {
return;
}
AC_LOG("%s, state: %s", __FUNCTION__, mState->Name());
mState->OnScrollPositionChanged(this);
}
void
AccessibleCaretEventHub::LaunchScrollEndInjector()
{
if (!mScrollEndInjectorTimer) {
return;
}
mScrollEndInjectorTimer->InitWithFuncCallback(
FireScrollEnd, this, kScrollEndTimerDelay, nsITimer::TYPE_ONE_SHOT);
}
void
AccessibleCaretEventHub::CancelScrollEndInjector()
{
if (!mScrollEndInjectorTimer) {
return;
}
mScrollEndInjectorTimer->Cancel();
}
/* static */ void
AccessibleCaretEventHub::FireScrollEnd(nsITimer* aTimer,
void* aAccessibleCaretEventHub)
{
auto self = static_cast<AccessibleCaretEventHub*>(aAccessibleCaretEventHub);
self->mState->OnScrollEnd(self);
}
nsresult
AccessibleCaretEventHub::NotifySelectionChanged(nsIDOMDocument* aDoc,
nsISelection* aSel,
int16_t aReason)
{
if (!mInitialized) {
return NS_OK;
}
AC_LOG("%s, state: %s, reason: %d", __FUNCTION__, mState->Name(), aReason);
mState->OnSelectionChanged(this, aDoc, aSel, aReason);
return NS_OK;
}
void
AccessibleCaretEventHub::NotifyBlur(bool aIsLeavingDocument)
{
if (!mInitialized) {
return;
}
AC_LOG("%s, state: %s", __FUNCTION__, mState->Name());
mState->OnBlur(this, aIsLeavingDocument);
}
nsPoint
AccessibleCaretEventHub::GetTouchEventPosition(WidgetTouchEvent* aEvent,
int32_t aIdentifier) const
{
for (dom::Touch* touch : aEvent->touches) {
if (touch->Identifier() == aIdentifier) {
LayoutDeviceIntPoint touchIntPoint = touch->mRefPoint;
// Get event coordinate relative to root frame.
nsIFrame* rootFrame = mPresShell->GetRootFrame();
return nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, touchIntPoint,
rootFrame);
}
}
return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
}
nsPoint
AccessibleCaretEventHub::GetMouseEventPosition(WidgetMouseEvent* aEvent) const
{
LayoutDeviceIntPoint mouseIntPoint = aEvent->AsGUIEvent()->refPoint;
// Get event coordinate relative to root frame.
nsIFrame* rootFrame = mPresShell->GetRootFrame();
return nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, mouseIntPoint,
rootFrame);
}
} // namespace mozilla

View File

@ -0,0 +1,227 @@
/* -*- 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 AccessibleCaretEventHub_h
#define AccessibleCaretEventHub_h
#include "mozilla/EventForwards.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WeakPtr.h"
#include "nsCOMPtr.h"
#include "nsIFrame.h"
#include "nsIReflowObserver.h"
#include "nsIScrollObserver.h"
#include "nsISelectionListener.h"
#include "nsPoint.h"
#include "nsRefPtr.h"
#include "nsWeakReference.h"
class nsDocShell;
class nsIPresShell;
class nsITimer;
namespace mozilla {
class AccessibleCaretManager;
class WidgetKeyboardEvent;
class WidgetMouseEvent;
class WidgetTouchEvent;
class WidgetWheelEvent;
// -----------------------------------------------------------------------------
// Each PresShell holds a shared pointer to an AccessibleCaretEventHub; each
// AccessibleCaretEventHub holds a unique pointer to an AccessibleCaretManager.
// Thus, there's one AccessibleCaretManager per PresShell.
//
// AccessibleCaretEventHub implements a state pattern. It receives events from
// PresShell and callbacks by observers and listeners, and then relays them to
// the current concrete state which calls necessary event-handling methods in
// AccessibleCaretManager.
//
// We separate AccessibleCaretEventHub from AccessibleCaretManager to make the
// state transitions in AccessibleCaretEventHub testable. We put (nearly) all
// the operations involving PresShell, Selection, and AccessibleCaret
// manipulation in AccessibleCaretManager so that we can mock methods in
// AccessibleCaretManager in gtest. We test the correctness of the state
// transitions by giving events, callbacks, and the return values by mocked
// methods of AccessibleCaretEventHub. See TestAccessibleCaretEventHub.cpp.
//
// Besides dealing with real events, AccessibleCaretEventHub also synthesizes
// fake events such as scroll-end or long-tap providing APZ is not in use.
//
// State transition diagram:
// http://hg.mozilla.org/mozilla-central/file/default/layout/base/doc/AccessibleCaretEventHubStates.png
// Source code of the diagram:
// http://hg.mozilla.org/mozilla-central/file/default/layout/base/doc/AccessibleCaretEventHubStates.dot
//
class AccessibleCaretEventHub : public nsIReflowObserver,
public nsIScrollObserver,
public nsISelectionListener,
public nsSupportsWeakReference
{
public:
explicit AccessibleCaretEventHub();
virtual void Init(nsIPresShell* aPresShell);
virtual void Terminate();
nsEventStatus HandleEvent(WidgetEvent* aEvent);
// Call this function to notify the blur event happened.
void NotifyBlur(bool aIsLeavingDocument);
NS_DECL_ISUPPORTS
NS_DECL_NSIREFLOWOBSERVER
NS_DECL_NSISELECTIONLISTENER
// Override nsIScrollObserver methods.
virtual void ScrollPositionChanged() override;
virtual void AsyncPanZoomStarted() override;
virtual void AsyncPanZoomStopped() override;
// Base state
class State;
State* GetState() const;
protected:
virtual ~AccessibleCaretEventHub();
#define NS_DECL_STATE_CLASS_GETTER(aClassName) \
class aClassName; \
static State* aClassName();
#define NS_IMPL_STATE_CLASS_GETTER(aClassName) \
AccessibleCaretEventHub::State* AccessibleCaretEventHub::aClassName() \
{ \
return AccessibleCaretEventHub::aClassName::Singleton(); \
}
// Concrete state getters
NS_DECL_STATE_CLASS_GETTER(NoActionState)
NS_DECL_STATE_CLASS_GETTER(PressCaretState)
NS_DECL_STATE_CLASS_GETTER(DragCaretState)
NS_DECL_STATE_CLASS_GETTER(PressNoCaretState)
NS_DECL_STATE_CLASS_GETTER(ScrollState)
NS_DECL_STATE_CLASS_GETTER(PostScrollState)
NS_DECL_STATE_CLASS_GETTER(LongTapState)
void SetState(State* aState);
nsEventStatus HandleMouseEvent(WidgetMouseEvent* aEvent);
nsEventStatus HandleWheelEvent(WidgetWheelEvent* aEvent);
nsEventStatus HandleTouchEvent(WidgetTouchEvent* aEvent);
nsEventStatus HandleKeyboardEvent(WidgetKeyboardEvent* aEvent);
virtual nsPoint GetTouchEventPosition(WidgetTouchEvent* aEvent,
int32_t aIdentifier) const;
virtual nsPoint GetMouseEventPosition(WidgetMouseEvent* aEvent) const;
bool MoveDistanceIsLarge(const nsPoint& aPoint) const;
void LaunchLongTapInjector();
void CancelLongTapInjector();
static void FireLongTap(nsITimer* aTimer, void* aAccessibleCaretEventHub);
void LaunchScrollEndInjector();
void CancelScrollEndInjector();
static void FireScrollEnd(nsITimer* aTimer, void* aAccessibleCaretEventHub);
// Member variables
bool mInitialized = false;
// True if async-pan-zoom should be used.
bool mUseAsyncPanZoom = false;
State* mState = NoActionState();
// Will be set to nullptr in Terminate().
nsIPresShell* MOZ_NON_OWNING_REF mPresShell = nullptr;
UniquePtr<AccessibleCaretManager> mManager;
WeakPtr<nsDocShell> mDocShell;
// Use this timer for injecting a long tap event when APZ is disabled. If APZ
// is enabled, it will send long tap event to us.
nsCOMPtr<nsITimer> mLongTapInjectorTimer;
// Use this timer for injecting a simulated scroll end.
nsCOMPtr<nsITimer> mScrollEndInjectorTimer;
// Last mouse button down event or touch start event point.
nsPoint mPressPoint{ NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE };
// For filter multitouch event
int32_t mActiveTouchId = kInvalidTouchId;
static const int32_t kScrollEndTimerDelay = 300;
static const int32_t kMoveStartToleranceInPixel = 5;
static const int32_t kInvalidTouchId = -1;
static const int32_t kDefaultTouchId = 0; // For mouse event
};
// -----------------------------------------------------------------------------
// The base class for concrete states. A concrete state should inherit from this
// class, and override the methods to handle the events or callbacks. A concrete
// state is also responsible for transforming itself to the next concrete state.
//
class AccessibleCaretEventHub::State
{
public:
#define NS_IMPL_STATE_UTILITIES(aClassName) \
virtual const char* Name() const { return #aClassName; } \
static aClassName* Singleton() \
{ \
static aClassName singleton; \
return &singleton; \
}
virtual const char* Name() const { return ""; }
virtual nsEventStatus OnPress(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint, int32_t aTouchId)
{
return nsEventStatus_eIgnore;
}
virtual nsEventStatus OnMove(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint)
{
return nsEventStatus_eIgnore;
}
virtual nsEventStatus OnRelease(AccessibleCaretEventHub* aContext)
{
return nsEventStatus_eIgnore;
}
virtual nsEventStatus OnLongTap(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint)
{
return nsEventStatus_eIgnore;
}
virtual void OnScrollStart(AccessibleCaretEventHub* aContext) {}
virtual void OnScrollEnd(AccessibleCaretEventHub* aContext) {}
virtual void OnScrolling(AccessibleCaretEventHub* aContext) {}
virtual void OnScrollPositionChanged(AccessibleCaretEventHub* aContext) {}
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) {}
virtual void OnSelectionChanged(AccessibleCaretEventHub* aContext,
nsIDOMDocument* aDoc, nsISelection* aSel,
int16_t aReason) {}
virtual void OnReflow(AccessibleCaretEventHub* aContext) {}
virtual void Enter(AccessibleCaretEventHub* aContext) {}
virtual void Leave(AccessibleCaretEventHub* aContext) {}
protected:
explicit State() = default;
virtual ~State() = default;
State(const State&) = delete;
void operator=(const State&) = delete;
};
} // namespace mozilla
#endif // AccessibleCaretEventHub_h

View File

@ -0,0 +1,37 @@
// Steps to generate AccessibleCaretEventHubStates.png
// 1. Install Graphviz
// 2. dot -T png -o AccessibleCaretEventHubStates.png AccessibleCaretEventHubStates.dot
digraph event_hub_states {
node [style=filled];
edge [color="gray30", fontcolor="gray20", fontsize=12]
NoAction [label="NoAction\n(Initial)"color="#96FF2F"];
NoAction -> PressCaret [label="Press & on a caret"];
NoAction -> PressNoCaret [label="Press & not on a caret"];
NoAction -> Scroll [label="Scroll start"];
PressCaret [color="#84D8FF"];
PressCaret -> DragCaret [label="Move & distance is large"];
PressCaret -> NoAction [label="Release (synthesizing a tap)"];
DragCaret [color="#84D8FF"];
DragCaret -> DragCaret [label="Move"];
DragCaret -> NoAction [label="Release"];
PressNoCaret [color="#E8C516"];
PressNoCaret -> NoAction [label="Move & distance is large or\nRelease or\nBlur"];
PressNoCaret -> LongTap [label="Long tap"];
PressNoCaret -> Scroll [label="Scroll start", constraint=false];
LongTap [color="#E8C516"]
LongTap -> NoAction;
Scroll [color="#FF9022"]
Scroll -> PostScroll [label="Scroll end"];
Scroll -> NoAction [label="Blur"];
PostScroll [color="#FF9022"]
PostScroll -> Scroll [label="Scroll start"];
PostScroll -> NoAction [label="Blur or\nWait 300ms"];
PostScroll -> NoAction [label="Press (forward to NoAction)", constraint=false];
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB