This source file includes following definitions.
- area
- findNonEmptyAnchorNode
- setAnchor
- computeOrigin
#include "config.h"
#include "ViewportAnchor.h"
#include "core/dom/ContainerNode.h"
#include "core/dom/Node.h"
#include "core/page/EventHandler.h"
#include "core/rendering/HitTestResult.h"
using namespace WebCore;
namespace blink {
namespace {
static const float viewportAnchorRelativeEpsilon = 0.1f;
static const int viewportToNodeMaxRelativeArea = 2;
template <typename RectType>
int area(const RectType& rect) {
return rect.width() * rect.height();
}
Node* findNonEmptyAnchorNode(const IntPoint& point, const IntRect& viewRect, EventHandler* eventHandler)
{
Node* node = eventHandler->hitTestResultAtPoint(point, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent).innerNode();
const int maxNodeArea = area(viewRect) * viewportToNodeMaxRelativeArea;
if (node && area(node->boundingBox()) > maxNodeArea) {
IntSize pointOffset = viewRect.size();
pointOffset.scale(viewportAnchorRelativeEpsilon);
node = eventHandler->hitTestResultAtPoint(point + pointOffset, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent).innerNode();
}
while (node && node->boundingBox().isEmpty())
node = node->parentNode();
return node;
}
}
ViewportAnchor::ViewportAnchor(EventHandler* eventHandler)
: m_eventHandler(eventHandler) { }
void ViewportAnchor::setAnchor(const IntRect& viewRect, const FloatSize& anchorInViewCoords)
{
m_viewRect = viewRect;
m_anchorNode.clear();
m_anchorNodeBounds = LayoutRect();
m_anchorInNodeCoords = FloatSize();
m_anchorInViewCoords = anchorInViewCoords;
if (viewRect.isEmpty())
return;
if (viewRect.location() == IntPoint::zero())
return;
FloatSize anchorOffset = viewRect.size();
anchorOffset.scale(anchorInViewCoords.width(), anchorInViewCoords.height());
const FloatPoint anchorPoint = FloatPoint(viewRect.location()) + anchorOffset;
Node* node = findNonEmptyAnchorNode(flooredIntPoint(anchorPoint), viewRect, m_eventHandler);
if (!node)
return;
m_anchorNode = node;
m_anchorNodeBounds = node->boundingBox();
m_anchorInNodeCoords = anchorPoint - m_anchorNodeBounds.location();
m_anchorInNodeCoords.scale(1.f / m_anchorNodeBounds.width(), 1.f / m_anchorNodeBounds.height());
}
IntPoint ViewportAnchor::computeOrigin(const IntSize& currentViewSize) const
{
if (!m_anchorNode || !m_anchorNode->inDocument())
return m_viewRect.location();
const LayoutRect currentNodeBounds = m_anchorNode->boundingBox();
if (m_anchorNodeBounds == currentNodeBounds)
return m_viewRect.location();
FloatSize anchorOffsetFromNode = currentNodeBounds.size();
anchorOffsetFromNode.scale(m_anchorInNodeCoords.width(), m_anchorInNodeCoords.height());
FloatPoint anchorPoint = currentNodeBounds.location() + anchorOffsetFromNode;
FloatSize anchorOffsetFromOrigin = currentViewSize;
anchorOffsetFromOrigin.scale(m_anchorInViewCoords.width(), m_anchorInViewCoords.height());
return flooredIntPoint(anchorPoint - anchorOffsetFromOrigin);
}
}