Files
palemoon27/gfx/layers/apz/util/ActiveElementManager.cpp
T
roytam1 7b202d32b4 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 895274 part.230 Rename NS_SELECT_START to eSelectStart r=smaug (776d2bc554)
- Bug 895274 part.231 Rename NS_SELECT_EVENT_START to eSelectEventFirst r=smaug (184da9374f)
- Bug 895274 part.232 Rename NS_EDITOR_INPUT to eEditorInput r=smaug (d88f5cda72)
- Bug 895274 part.233 Rename NS_EDITOR_EVENT_START to eEditorEventFirst r=smaug (82736919d5)
- Bug 895274 part.234 Rename NS_SPEAKERMANAGER_SPEAKERFORCEDCHANGE to eSpeakerForcedChange r=smaug (91da06db86)
- Bug 895274 part.235 Rename NS_SPEAKERMANAGER_EVENT_START to eSpeakerManagerEventFirst r=smaug (26e36e83dd)
- Bug 895274 part.236 Rename NS_MEDIARECORDER_EVENT_START to eMediaRecorderEventFirst r=smaug (4119ab24ab)
- Bug 895274 part.237 Rename NS_MEDIARECORDER_DATAAVAILABLE to eMediaRecorderDataAvailable r=smaug (1cb86629f3)
- Bug 895274 part.238 Rename NS_MEDIARECORDER_WARNING to eMediaRecorderWarning r=smaug (5fc925dbfe)
- Bug 895274 part.239 Rename NS_MEDIARECORDER_STOP to eMediaRecorderStop r=smaug (222663f16a)
- Bug 895274 part.240 Rename NS_TOUCH_EVENT_START to eTouchEventFirst r=smaug (88e32a70f8)
- Bug 1143618 - Change Window::OnTouch implementation to use MultiTouchInput class. r=kats (6afe05d5ad)
- Bug 895274 part.241 Rename NS_TOUCH_START to eTouchStart r=smaug (859229088b)
- Bug 1142437 - Better deal with delayed state change notifications from APZ. r=botond (b9a2fd85e0)
- Bug 1134917 - Hoist assertion into the one callsite where it is actually valid. r=botond (6946d8647a)
- Bug 895274 part.242 Rename NS_TOUCH_MOVE to eTouchMove r=smaug (ef20dc27c8)
- Bug 1171158: Convert some code to use range-based for loops in nsPresShell.cpp. r=mats (91375e2954)
- Bug 895274 part.243 Rename NS_TOUCH_END to eTouchEnd r=smaug (a1a9812c14)
- Bug 895274 part.244 Rename NS_TOUCH_CANCEL to eTouchCancel r=smaug (6d2ec155ee)
- Bug 895274 part.245 Rename NS_SHOW_EVENT to eShow r=smaug (835e74bd43)
- Bug 895274 part.246 Rename NS_MESSAGE to eMessage r=smaug (c5efac2b22)
- fix after Bug 895274 part.243 (ac85766640)
2022-05-03 12:08:49 +08:00

234 lines
6.1 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "ActiveElementManager.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/Preferences.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "mozilla/dom/Element.h"
#include "nsIDocument.h"
#include "nsStyleSet.h"
#define AEM_LOG(...)
// #define AEM_LOG(...) printf_stderr("AEM: " __VA_ARGS__)
namespace mozilla {
namespace layers {
static int32_t sActivationDelayMs = 100;
static bool sActivationDelayMsSet = false;
ActiveElementManager::ActiveElementManager()
: mCanBePan(false),
mCanBePanSet(false),
mSetActiveTask(nullptr),
mActiveElementUsesStyle(false)
{
if (!sActivationDelayMsSet) {
Preferences::AddIntVarCache(&sActivationDelayMs,
"ui.touch_activation.delay_ms",
sActivationDelayMs);
sActivationDelayMsSet = true;
}
}
ActiveElementManager::~ActiveElementManager() {}
void
ActiveElementManager::SetTargetElement(dom::EventTarget* aTarget)
{
if (mTarget) {
// Multiple fingers on screen (since HandleTouchEnd clears mTarget).
AEM_LOG("Multiple fingers on-screen, clearing target element\n");
CancelTask();
ResetActive();
ResetTouchBlockState();
return;
}
mTarget = do_QueryInterface(aTarget);
AEM_LOG("Setting target element to %p\n", mTarget.get());
TriggerElementActivation();
}
void
ActiveElementManager::HandleTouchStart(bool aCanBePan)
{
AEM_LOG("Touch start, aCanBePan: %d\n", aCanBePan);
if (mCanBePanSet) {
// Multiple fingers on screen (since HandleTouchEnd clears mCanBePanSet).
AEM_LOG("Multiple fingers on-screen, clearing touch block state\n");
CancelTask();
ResetActive();
ResetTouchBlockState();
return;
}
mCanBePan = aCanBePan;
mCanBePanSet = true;
TriggerElementActivation();
}
void
ActiveElementManager::TriggerElementActivation()
{
// Both HandleTouchStart() and SetTargetElement() call this. They can be
// called in either order. One will set mCanBePanSet, and the other, mTarget.
// We want to actually trigger the activation once both are set.
if (!(mTarget && mCanBePanSet)) {
return;
}
// If the touch cannot be a pan, make mTarget :active right away.
// Otherwise, wait a bit to see if the user will pan or not.
if (!mCanBePan) {
SetActive(mTarget);
} else {
MOZ_ASSERT(mSetActiveTask == nullptr);
mSetActiveTask = NewRunnableMethod(
this, &ActiveElementManager::SetActiveTask, mTarget);
MessageLoop::current()->PostDelayedTask(
FROM_HERE, mSetActiveTask, sActivationDelayMs);
AEM_LOG("Scheduling mSetActiveTask %p\n", mSetActiveTask);
}
}
void
ActiveElementManager::HandlePanStart()
{
AEM_LOG("Handle pan start\n");
// The user started to pan, so we don't want mTarget to be :active.
// Make it not :active, and clear any pending task to make it :active.
CancelTask();
ResetActive();
}
void
ActiveElementManager::HandleTouchEndEvent(bool aWasClick)
{
AEM_LOG("Touch end event, aWasClick: %d\n", aWasClick);
// If the touch was a click, make mTarget :active right away.
// nsEventStateManager will reset the active element when processing
// the mouse-down event generated by the click.
CancelTask();
if (aWasClick) {
SetActive(mTarget);
} else {
// We might reach here if mCanBePan was false on touch-start and
// so we set the element active right away. Now it turns out the
// action was not a click so we need to reset the active element.
ResetActive();
}
ResetTouchBlockState();
}
void
ActiveElementManager::HandleTouchEnd()
{
AEM_LOG("Touch end, clearing pan state\n");
mCanBePanSet = false;
}
bool
ActiveElementManager::ActiveElementUsesStyle() const
{
return mActiveElementUsesStyle;
}
static nsPresContext*
GetPresContextFor(nsIContent* aContent)
{
if (!aContent) {
return nullptr;
}
nsIPresShell* shell = aContent->OwnerDoc()->GetShell();
if (!shell) {
return nullptr;
}
return shell->GetPresContext();
}
static bool
ElementHasActiveStyle(dom::Element* aElement)
{
nsPresContext* pc = GetPresContextFor(aElement);
if (!pc) {
return false;
}
nsStyleSet* styleSet = pc->StyleSet();
for (dom::Element* e = aElement; e; e = e->GetParentElement()) {
if (styleSet->HasStateDependentStyle(e, NS_EVENT_STATE_ACTIVE)) {
AEM_LOG("Element %p's style is dependent on the active state\n", e);
return true;
}
}
AEM_LOG("Element %p doesn't use active styles\n", aElement);
return false;
}
void
ActiveElementManager::SetActive(dom::Element* aTarget)
{
AEM_LOG("Setting active %p\n", aTarget);
if (nsPresContext* pc = GetPresContextFor(aTarget)) {
pc->EventStateManager()->SetContentState(aTarget, NS_EVENT_STATE_ACTIVE);
mActiveElementUsesStyle = ElementHasActiveStyle(aTarget);
}
}
void
ActiveElementManager::ResetActive()
{
AEM_LOG("Resetting active from %p\n", mTarget.get());
// Clear the :active flag from mTarget by setting it on the document root.
if (mTarget) {
dom::Element* root = mTarget->OwnerDoc()->GetDocumentElement();
if (root) {
AEM_LOG("Found root %p, making active\n", root);
SetActive(root);
}
}
}
void
ActiveElementManager::ResetTouchBlockState()
{
mTarget = nullptr;
mCanBePanSet = false;
}
void
ActiveElementManager::SetActiveTask(dom::Element* aTarget)
{
AEM_LOG("mSetActiveTask %p running\n", mSetActiveTask);
// This gets called from mSetActiveTask's Run() method. The message loop
// deletes the task right after running it, so we need to null out
// mSetActiveTask to make sure we're not left with a dangling pointer.
mSetActiveTask = nullptr;
SetActive(aTarget);
}
void
ActiveElementManager::CancelTask()
{
AEM_LOG("Cancelling task %p\n", mSetActiveTask);
if (mSetActiveTask) {
mSetActiveTask->Cancel();
mSetActiveTask = nullptr;
}
}
} // namespace layers
} // namespace mozilla