This source file includes following definitions.
- boundingBoxForEventNodes
- scoreTouchTarget
- findGoodTouchTargets
#include "config.h"
#include "core/page/TouchDisambiguation.h"
#include <algorithm>
#include <cmath>
#include "HTMLNames.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/NodeTraversal.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLHtmlElement.h"
#include "core/page/EventHandler.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/RenderBlock.h"
using namespace std;
namespace WebCore {
static IntRect boundingBoxForEventNodes(Node* eventNode)
{
if (!eventNode->document().view())
return IntRect();
IntRect result;
Node* node = eventNode;
while (node) {
if (node != eventNode && node->willRespondToMouseClickEvents()) {
node = NodeTraversal::nextSkippingChildren(*node, eventNode);
continue;
}
result.unite(node->pixelSnappedBoundingBox());
node = NodeTraversal::next(*node, eventNode);
}
return eventNode->document().view()->contentsToWindow(result);
}
static float scoreTouchTarget(IntPoint touchPoint, int padding, IntRect boundingBox)
{
if (boundingBox.isEmpty())
return 0;
float reciprocalPadding = 1.f / padding;
float score = 1;
IntSize distance = boundingBox.differenceToPoint(touchPoint);
score *= max((padding - abs(distance.width())) * reciprocalPadding, 0.f);
score *= max((padding - abs(distance.height())) * reciprocalPadding, 0.f);
return score;
}
struct TouchTargetData {
IntRect windowBoundingBox;
float score;
};
void findGoodTouchTargets(const IntRect& touchBox, LocalFrame* mainFrame, Vector<IntRect>& goodTargets, Vector<Node*>& highlightNodes)
{
goodTargets.clear();
int touchPointPadding = ceil(max(touchBox.width(), touchBox.height()) * 0.5);
IntPoint touchPoint = touchBox.center();
IntPoint contentsPoint = mainFrame->view()->windowToContents(touchPoint);
HitTestResult result = mainFrame->eventHandler().hitTestResultAtPoint(contentsPoint, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent, IntSize(touchPointPadding, touchPointPadding));
const ListHashSet<RefPtr<Node> >& hitResults = result.rectBasedTestResult();
HashSet<Node*> blackList;
for (ListHashSet<RefPtr<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) {
RenderObject* renderer = it->get()->renderer();
if (!renderer || !it->get()->willRespondToMouseClickEvents())
continue;
for (RenderBlock* container = renderer->containingBlock(); container; container = container->containingBlock()) {
Node* containerNode = container->node();
if (!containerNode)
continue;
if (!blackList.add(containerNode).isNewEntry)
break;
}
}
HashMap<Node*, TouchTargetData> touchTargets;
float bestScore = 0;
for (ListHashSet<RefPtr<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) {
for (Node* node = it->get(); node; node = node->parentNode()) {
if (blackList.contains(node))
continue;
if (node->isDocumentNode() || isHTMLHtmlElement(*node) || isHTMLBodyElement(*node))
break;
if (node->willRespondToMouseClickEvents()) {
TouchTargetData& targetData = touchTargets.add(node, TouchTargetData()).storedValue->value;
targetData.windowBoundingBox = boundingBoxForEventNodes(node);
targetData.score = scoreTouchTarget(touchPoint, touchPointPadding, targetData.windowBoundingBox);
bestScore = max(bestScore, targetData.score);
break;
}
}
}
for (HashMap<Node*, TouchTargetData>::iterator it = touchTargets.begin(); it != touchTargets.end(); ++it) {
if (it->value.score < bestScore * 0.5)
continue;
goodTargets.append(it->value.windowBoundingBox);
highlightNodes.append(it->key);
}
}
}