Bug 780847. Built-in support for mouse/touch events targeting nodes within a certain radius via heuristics. r=mats,smaug

--HG--
extra : rebase_source : 13a350bb6c993e5a23e54657cbe9f22c287036ad
This commit is contained in:
Robert O'Callahan 2012-08-20 12:02:42 +12:00
Родитель 99e948a368
Коммит 1b7000f104
7 изменённых файлов: 517 добавлений и 10 удалений

Просмотреть файл

@ -88,6 +88,7 @@ CPPSRCS = \
nsStyleChangeList.cpp \
nsStyleSheetService.cpp \
PaintTracker.cpp \
PositionedEventTargeting.cpp \
StackArena.cpp \
$(NULL)

Просмотреть файл

@ -0,0 +1,274 @@
/* 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 "PositionedEventTargeting.h"
#include "mozilla/Preferences.h"
#include "nsGUIEvent.h"
#include "nsLayoutUtils.h"
#include "nsGkAtoms.h"
#include "nsEventListenerManager.h"
#include "nsPrintfCString.h"
#include "mozilla/dom/Element.h"
namespace mozilla {
/*
* The basic goal of FindFrameTargetedByInputEvent() is to find a good
* target element that can respond to mouse events. Both mouse events and touch
* events are targeted at this element. Note that even for touch events, we
* check responsiveness to mouse events. We assume Web authors
* designing for touch events will take their own steps to account for
* inaccurate touch events.
*
* IsElementClickable() encapsulates the heuristic that determines whether an
* element is expected to respond to mouse events. An element is deemed
* "clickable" if it has registered listeners for "click", "mousedown" or
* "mouseup", or is on a whitelist of element tags (<a>, <button>, <input>,
* <select>, <textarea>, <label>), or has role="button", or is a link, or
* is a suitable XUL element.
* Any descendant (in the same document) of a clickable element is also
* deemed clickable since events will propagate to the clickable element from its
* descendant.
*
* If the element directly under the event position is clickable (or
* event radii are disabled), we always use that element. Otherwise we collect
* all frames intersecting a rectangle around the event position (taking CSS
* transforms into account) and choose the best candidate in GetClosest().
* Only IsElementClickable() candidates are considered; if none are found,
* then we revert to targeting the element under the event position.
* We ignore candidates outside the document subtree rooted by the
* document of the element directly under the event position. This ensures that
* event listeners in ancestor documents don't make it completely impossible
* to target a non-clickable element in a child document.
*
* When both a frame and its ancestor are in the candidate list, we ignore
* the ancestor. Otherwise a large ancestor element with a mouse event listener
* and some descendant elements that need to be individually targetable would
* disable intelligent targeting of those descendants within its bounds.
*
* GetClosest() computes the transformed axis-aligned bounds of each
* candidate frame, then computes the Manhattan distance from the event point
* to the bounds rect (which can be zero). The frame with the
* shortest distance is chosen. For visited links we multiply the distance
* by a specified constant weight; this can be used to make visited links
* more or less likely to be targeted than non-visited links.
*/
struct EventRadiusPrefs
{
uint32_t mVisitedWeight; // in percent, i.e. default is 100
uint32_t mSideRadii[4]; // TRBL order, in millimetres
bool mEnabled;
bool mRegistered;
};
static EventRadiusPrefs sMouseEventRadiusPrefs;
static EventRadiusPrefs sTouchEventRadiusPrefs;
static const EventRadiusPrefs*
GetPrefsFor(uint8_t aEventStructType)
{
EventRadiusPrefs* prefs = nullptr;
const char* prefBranch = nullptr;
if (aEventStructType == NS_MOZTOUCH_EVENT ||
aEventStructType == NS_TOUCH_EVENT) {
prefBranch = "touch";
prefs = &sTouchEventRadiusPrefs;
} else if (aEventStructType == NS_MOUSE_EVENT) {
// Mostly for testing purposes
prefBranch = "mouse";
prefs = &sMouseEventRadiusPrefs;
} else {
return nullptr;
}
if (!prefs->mRegistered) {
prefs->mRegistered = true;
nsPrintfCString enabledPref("ui.%s.radius.enabled", prefBranch);
Preferences::AddBoolVarCache(&prefs->mEnabled, enabledPref.get(), false);
nsPrintfCString visitedWeightPref("ui.%s.radius.visitedWeight", prefBranch);
Preferences::AddUintVarCache(&prefs->mVisitedWeight, visitedWeightPref.get(), 100);
static const char prefNames[4][9] =
{ "topmm", "rightmm", "bottommm", "leftmm" };
for (int32_t i = 0; i < 4; ++i) {
nsPrintfCString radiusPref("ui.%s.radius.%s", prefBranch, prefNames[i]);
Preferences::AddUintVarCache(&prefs->mSideRadii[i], radiusPref.get(), 0);
}
}
return prefs;
}
static bool
HasMouseListener(nsIContent* aContent)
{
nsEventListenerManager* elm = aContent->GetListenerManager(false);
if (!elm) {
return false;
}
return elm->HasListenersFor(nsGkAtoms::onclick) ||
elm->HasListenersFor(nsGkAtoms::onmousedown) ||
elm->HasListenersFor(nsGkAtoms::onmouseup);
}
static bool
IsElementClickable(nsIFrame* aFrame)
{
// Input events propagate up the content tree so we'll follow the content
// ancestors to look for elements accepting the click.
for (nsIContent* content = aFrame->GetContent(); content;
content = content->GetFlattenedTreeParent()) {
if (HasMouseListener(content)) {
return true;
}
if (content->IsHTML()) {
nsIAtom* tag = content->Tag();
if (tag == nsGkAtoms::button ||
tag == nsGkAtoms::input ||
tag == nsGkAtoms::select ||
tag == nsGkAtoms::textarea ||
tag == nsGkAtoms::label) {
return true;
}
} else if (content->IsXUL()) {
nsIAtom* tag = content->Tag();
// See nsCSSFrameConstructor::FindXULTagData. This code is not
// really intended to be used with XUL, though.
if (tag == nsGkAtoms::button ||
tag == nsGkAtoms::checkbox ||
tag == nsGkAtoms::radio ||
tag == nsGkAtoms::autorepeatbutton ||
tag == nsGkAtoms::menu ||
tag == nsGkAtoms::menubutton ||
tag == nsGkAtoms::menuitem ||
tag == nsGkAtoms::menulist ||
tag == nsGkAtoms::scrollbarbutton ||
tag == nsGkAtoms::resizer) {
return true;
}
}
if (content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::role,
nsGkAtoms::button, eIgnoreCase)) {
return true;
}
nsCOMPtr<nsIURI> linkURI;
if (content->IsLink(getter_AddRefs(linkURI))) {
return true;
}
}
return false;
}
static nscoord
AppUnitsFromMM(nsIFrame* aFrame, uint32_t aMM, bool aVertical)
{
nsPresContext* pc = aFrame->PresContext();
nsIPresShell* presShell = pc->PresShell();
float result = float(aMM) *
(pc->DeviceContext()->AppUnitsPerPhysicalInch() / MM_PER_INCH_FLOAT) *
(aVertical ? presShell->GetYResolution() : presShell->GetXResolution());
return NSToCoordRound(result);
}
static nsRect
GetTargetRect(nsIFrame* aRootFrame, const nsPoint& aPointRelativeToRootFrame,
const EventRadiusPrefs* aPrefs)
{
nsMargin m(AppUnitsFromMM(aRootFrame, aPrefs->mSideRadii[3], false),
AppUnitsFromMM(aRootFrame, aPrefs->mSideRadii[0], true),
AppUnitsFromMM(aRootFrame, aPrefs->mSideRadii[1], false),
AppUnitsFromMM(aRootFrame, aPrefs->mSideRadii[2], true));
nsRect r(aPointRelativeToRootFrame, nsSize(0,0));
r.Inflate(m);
return r;
}
static float
ComputeDistanceFromRect(const nsPoint& aPoint, const nsRect& aRect)
{
nscoord dx = NS_MAX(0, NS_MAX(aRect.x - aPoint.x, aPoint.x - aRect.XMost()));
nscoord dy = NS_MAX(0, NS_MAX(aRect.y - aPoint.y, aPoint.y - aRect.YMost()));
return float(NS_hypot(dx, dy));
}
static nsIFrame*
GetClosest(nsIFrame* aRoot, const nsPoint& aPointRelativeToRootFrame,
const EventRadiusPrefs* aPrefs, nsIFrame* aRestrictToDescendants,
nsTArray<nsIFrame*>& aCandidates)
{
nsIFrame* bestTarget = nullptr;
// Lower is better; distance is in appunits
float bestDistance = 1e6f;
for (uint32_t i = 0; i < aCandidates.Length(); ++i) {
nsIFrame* f = aCandidates[i];
if (!IsElementClickable(f)) {
continue;
}
// If our current closest frame is a descendant of 'f', skip 'f' (prefer
// the nested frame).
if (bestTarget && nsLayoutUtils::IsProperAncestorFrameCrossDoc(f, bestTarget, aRoot)) {
continue;
}
if (!nsLayoutUtils::IsAncestorFrameCrossDoc(aRestrictToDescendants, f, aRoot)) {
continue;
}
nsRect borderBox = nsLayoutUtils::TransformFrameRectToAncestor(f,
nsRect(nsPoint(0, 0), f->GetSize()), aRoot);
// distance is in appunits
float distance = ComputeDistanceFromRect(aPointRelativeToRootFrame, borderBox);
nsIContent* content = f->GetContent();
if (content && content->IsElement() &&
content->AsElement()->State().HasState(nsEventStates(NS_EVENT_STATE_VISITED))) {
distance *= aPrefs->mVisitedWeight / 100.0f;
}
if (distance < bestDistance) {
bestDistance = distance;
bestTarget = f;
}
}
return bestTarget;
}
nsIFrame*
FindFrameTargetedByInputEvent(uint8_t aEventStructType,
nsIFrame* aRootFrame,
const nsPoint& aPointRelativeToRootFrame,
uint32_t aFlags)
{
bool ignoreRootScrollFrame = (aFlags & INPUT_IGNORE_ROOT_SCROLL_FRAME) != 0;
nsIFrame* target =
nsLayoutUtils::GetFrameForPoint(aRootFrame, aPointRelativeToRootFrame,
false, ignoreRootScrollFrame);
const EventRadiusPrefs* prefs = GetPrefsFor(aEventStructType);
if (!prefs || !prefs->mEnabled || (target && IsElementClickable(target))) {
return target;
}
nsRect targetRect = GetTargetRect(aRootFrame, aPointRelativeToRootFrame, prefs);
nsAutoTArray<nsIFrame*,8> candidates;
nsresult rv = nsLayoutUtils::GetFramesForArea(aRootFrame, targetRect, candidates,
false, ignoreRootScrollFrame);
if (NS_FAILED(rv)) {
return target;
}
// If the exact target is non-null, only consider candidate targets in the same
// document as the exact target. Otherwise, if an ancestor document has
// a mouse event handler for example, targets that are !IsElementClickable can
// never be targeted --- something nsSubDocumentFrame in an ancestor document
// would be targeted instead.
nsIFrame* restrictToDescendants = target ?
target->PresContext()->PresShell()->GetRootFrame() : aRootFrame;
nsIFrame* closestClickable =
GetClosest(aRootFrame, aPointRelativeToRootFrame, prefs,
restrictToDescendants, candidates);
return closestClickable ? closestClickable : target;
}
}

Просмотреть файл

@ -0,0 +1,31 @@
/* 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_PositionedEventTargeting_h
#define mozilla_PositionedEventTargeting_h
#include "nsPoint.h"
class nsGUIEvent;
class nsIFrame;
namespace mozilla {
enum {
INPUT_IGNORE_ROOT_SCROLL_FRAME = 0x01
};
/**
* Finds the target frame for a pointer event given the event type and location.
* This can look for frames within a rectangle surrounding the actual location
* that are suitable targets, to account for inaccurate pointing devices.
*/
nsIFrame*
FindFrameTargetedByInputEvent(uint8_t aEventStructType,
nsIFrame* aRootFrame,
const nsPoint& aPointRelativeToRootFrame,
uint32_t aFlags = 0);
}
#endif /* mozilla_PositionedEventTargeting_h */

Просмотреть файл

@ -99,6 +99,7 @@
#ifdef MOZ_REFLOW_PERF
#include "nsFontMetrics.h"
#endif
#include "PositionedEventTargeting.h"
#include "nsIReflowCallback.h"
@ -5891,16 +5892,15 @@ PresShell::HandleEvent(nsIFrame *aFrame,
} else {
eventPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, frame);
}
{
bool ignoreRootScrollFrame = false;
if (aEvent->eventStructType == NS_MOUSE_EVENT) {
ignoreRootScrollFrame = static_cast<nsMouseEvent*>(aEvent)->ignoreRootScrollFrame;
}
nsIFrame* target = nsLayoutUtils::GetFrameForPoint(frame, eventPoint,
false, ignoreRootScrollFrame);
if (target) {
frame = target;
}
uint32_t flags = 0;
if (aEvent->eventStructType == NS_MOUSE_EVENT &&
static_cast<nsMouseEvent*>(aEvent)->ignoreRootScrollFrame) {
flags |= INPUT_IGNORE_ROOT_SCROLL_FRAME;
}
nsIFrame* target =
FindFrameTargetedByInputEvent(aEvent->eventStructType, frame, eventPoint, flags);
if (target) {
frame = target;
}
}

Просмотреть файл

@ -131,6 +131,7 @@ MOCHITEST_FILES = \
test_bug667512.html \
test_bug677878.html \
test_bug696020.html \
test_event_target_radius.html \
test_mozPaintCount.html \
test_scroll_selection_into_view.html \
test_bug583889.html \

Просмотреть файл

@ -0,0 +1,177 @@
<!DOCTYPE HTML>
<html id="html" style="height:100%">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=780847
-->
<head>
<title>Test radii for mouse events</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
.target { position:absolute; left:100px; top:100px; width:100px; height:100px; background:blue; }
</style>
</head>
<body id="body" onload="setTimeout(runTest, 0)" style="margin:0; width:100%; height:100%">
<p id="display"></p>
<div id="content">
<div id="ruler" style="position:absolute; left:0; top:0; width:1mozmm; height:0;"></div>
<div class="target" id="t" onmousedown="x=1"></div>
<div class="target" id="t2" hidden></div>
<input class="target" id="t3_1" hidden></input>
<a href="#" class="target" id="t3_2" hidden></a>
<label class="target" id="t3_3" hidden></label>
<button class="target" id="t3_4" hidden></button>
<select class="target" id="t3_5" hidden></select>
<textarea class="target" id="t3_6" hidden></textarea>
<div role="button" class="target" id="t3_7" hidden></div>
<img class="target" id="t3_8" hidden></img>
<div class="target" style="transform:translate(-80px,0);" id="t4" onmousedown="x=1" hidden></div>
<div class="target" style="left:0; z-index:1" id="t5_left" onmousedown="x=1" hidden></div>
<div class="target" style="left:106px;" id="t5_right" onmousedown="x=1" hidden></div>
<div class="target" style="left:0; top:210px;" id="t5_below" onmousedown="x=1" hidden></div>
<div class="target" id="t6" onmousedown="x=1" hidden>
<div id="t6_inner" style="position:absolute; left:-20px; top:20px; width:60px; height:60px; background:yellow;"></div>
</div>
</div>
<pre id="test">
<script type="application/javascript">
var prefBase = "ui.mouse.radius.";
SpecialPowers.setBoolPref(prefBase + "enabled", true);
SpecialPowers.setIntPref(prefBase + "leftmm", 12);
SpecialPowers.setIntPref(prefBase + "topmm", 8);
SpecialPowers.setIntPref(prefBase + "rightmm", 4);
SpecialPowers.setIntPref(prefBase + "bottommm", 4);
SpecialPowers.setIntPref(prefBase + "visitedWeight", 50);
SimpleTest.waitForExplicitFinish();
var mm = document.getElementById("ruler").getBoundingClientRect().width;
ok(4*mm >= 10, "WARNING: mm " + mm + " too small in this configuration. Test results will be bogus");
function endTest() {
SpecialPowers.clearUserPref(prefBase + "enabled");
SpecialPowers.clearUserPref(prefBase + "leftmmm");
SpecialPowers.clearUserPref(prefBase + "topmm");
SpecialPowers.clearUserPref(prefBase + "rightmm");
SpecialPowers.clearUserPref(prefBase + "bottommm");
SpecialPowers.clearUserPref(prefBase + "visitedWeight");
SimpleTest.finish();
}
var eventTarget;
window.onmousedown = function(event) { eventTarget = event.target; };
function testMouseClick(idPosition, dx, dy, idTarget, msg) {
eventTarget = null;
synthesizeMouse(document.getElementById(idPosition), dx, dy, {});
try {
is(eventTarget.id, idTarget,
"checking '" + idPosition + "' offset " + dx + "," + dy + " [" + msg + "]");
} catch (ex) {
ok(false, "checking '" + idPosition + "' offset " + dx + "," + dy + " [" + msg + "]; got " + eventTarget);
}
}
function setShowing(id, show) {
var e = document.getElementById(id);
e.hidden = !show;
}
function runTest() {
// Test basic functionality: clicks sufficiently close to the element
// should be allowed to hit the element. We test points just inside and
// just outside the edges we set up in the prefs.
testMouseClick("t", 100 + 13*mm, 10, "body", "basic functionality");
testMouseClick("t", 100 + 11*mm, 10, "t", "basic functionality");
testMouseClick("t", 10, 100 + 9*mm, "body", "basic functionality");
testMouseClick("t", 10, 100 + 7*mm, "t", "basic functionality");
testMouseClick("t", -5*mm, 10, "body", "basic functionality");
testMouseClick("t", -3*mm, 10, "t", "basic functionality");
testMouseClick("t", 10, -5*mm, "body", "basic functionality");
testMouseClick("t", 10, -3*mm, "t", "basic functionality");
setShowing("t", false);
// Now test the criteria we use to determine which elements are hittable
// this way.
setShowing("t2", true);
var t2 = document.getElementById("t2");
// Unadorned DIVs are not click radius targets
testMouseClick("t2", 100 + 11*mm, 10, "body", "unadorned DIV");
// DIVs with the right event handlers are click radius targets
t2.onmousedown = function() {};
testMouseClick("t2", 100 + 11*mm, 10, "t2", "DIV with onmousedown");
t2.onmousedown = null;
testMouseClick("t2", 100 + 11*mm, 10, "body", "DIV with onmousedown removed");
t2.onmouseup = function() {};
testMouseClick("t2", 100 + 11*mm, 10, "t2", "DIV with onmouseup");
t2.onmouseup = null;
t2.onclick = function() {};
testMouseClick("t2", 100 + 11*mm, 10, "t2", "DIV with onclick");
t2.onclick = null;
// Keypresses don't make click radius targets
t2.onkeypress = function() {};
testMouseClick("t2", 100 + 11*mm, 10, "body", "DIV with onkeypress");
t2.onkeypress = null;
setShowing("t2", false);
// Now check that certain elements are click radius targets and others are not
for (var i = 1; i <= 8; ++i) {
var id = "t3_" + i;
var shouldHit = i <= 7;
setShowing(id, true);
testMouseClick(id, 100 + 11*mm, 10, shouldHit ? id : "body",
"<" + document.getElementById(id).tagName + "> element");
setShowing(id, false);
}
// Check that our targeting computations take into account the effects of
// CSS transforms
setShowing("t4", true);
testMouseClick("t4", -1, 10, "t4", "translated DIV");
setShowing("t4", false);
// Test the prioritization of multiple targets based on distance to
// the target.
setShowing("t5_left", true);
setShowing("t5_right", true);
setShowing("t5_below", true);
testMouseClick("t5_left", 102, 10, "t5_left", "closest DIV is left");
testMouseClick("t5_left", 102.5, 10, "t5_left",
"closest DIV to midpoint is left because of its higher z-index");
testMouseClick("t5_left", 104, 10, "t5_right", "closest DIV is right");
testMouseClick("t5_left", 10, 104, "t5_left", "closest DIV is left");
testMouseClick("t5_left", 10, 105, "t5_left",
"closest DIV to midpoint is left because of its higher z-index");
testMouseClick("t5_left", 10, 106, "t5_below", "closest DIV is below");
setShowing("t5_left", false);
setShowing("t5_right", false);
setShowing("t5_below", false);
// Test behavior of nested elements.
// The following behaviors are questionable and may need to be changed.
setShowing("t6", true);
testMouseClick("t6_inner", -1, 10, "t6_inner",
"inner element is clickable because its parent is, even when it sticks outside parent");
testMouseClick("t6_inner", 19, -1, "t6_inner",
"when outside both inner and parent, but in range of both, the inner is selected");
testMouseClick("t6_inner", 25, -1, "t6",
"clicking in clickable parent close to inner activates parent, not inner");
setShowing("t6", false);
// Not yet tested:
// -- visited link weight
// -- "Closest" using Euclidean distance
endTest();
}
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -1788,6 +1788,29 @@ pref("font.size.inflation.lineThreshold", 400);
*/
pref("font.size.inflation.mappingIntercept", 1);
/*
* When enabled, the touch.radius and mouse.radius prefs allow events to be dispatched
* to nearby elements that are sensitive to the event. See PositionedEventTargeting.cpp.
* The 'mm' prefs define a rectangle around the nominal event target point within which
* we will search for suitable elements. 'visitedWeight' is a percentage weight;
* a value > 100 makes a visited link be treated as further away from the event target
* than it really is, while a value < 100 makes a visited link be treated as closer
* to the event target than it really is.
*/
pref("ui.touch.radius.enabled", false);
pref("ui.touch.radius.leftmm", 8);
pref("ui.touch.radius.topmm", 12);
pref("ui.touch.radius.rightmm", 8);
pref("ui.touch.radius.bottommm", 4);
pref("ui.touch.radius.visitedWeight", 120);
pref("ui.mouse.radius.enabled", false);
pref("ui.mouse.radius.leftmm", 8);
pref("ui.mouse.radius.topmm", 12);
pref("ui.mouse.radius.rightmm", 8);
pref("ui.mouse.radius.bottommm", 4);
pref("ui.mouse.radius.visitedWeight", 120);
#ifdef XP_WIN
pref("font.name.serif.ar", "Times New Roman");