MouseRelatedEvent.cpp [plain text]
#include "config.h"
#include "MouseRelatedEvent.h"
#include "Document.h"
#include "Frame.h"
#include "FrameView.h"
#include "LayoutPoint.h"
#include "RenderLayer.h"
#include "RenderObject.h"
namespace WebCore {
MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, bool canBubble, bool cancelable, double timestamp, DOMWindow* DOMWindow,
int detail, const IntPoint& screenLocation, const IntPoint& windowLocation,
#if ENABLE(POINTER_LOCK)
const IntPoint& movementDelta,
#endif
bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool isSimulated)
: UIEventWithKeyState(eventType, canBubble, cancelable, timestamp, DOMWindow, detail, ctrlKey, altKey, shiftKey, metaKey, false, false)
, m_screenLocation(screenLocation)
#if ENABLE(POINTER_LOCK)
, m_movementDelta(movementDelta)
#endif
, m_isSimulated(isSimulated)
{
init(isSimulated, windowLocation);
}
MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, const MouseRelatedEventInit& initializer, IsTrusted isTrusted)
: UIEventWithKeyState(eventType, initializer, isTrusted)
, m_screenLocation(IntPoint(initializer.screenX, initializer.screenY))
#if ENABLE(POINTER_LOCK)
, m_movementDelta(IntPoint(0, 0))
#endif
{
init(false, IntPoint(0, 0));
}
void MouseRelatedEvent::init(bool isSimulated, const IntPoint& windowLocation)
{
if (!isSimulated) {
if (auto* frameView = frameViewFromDOMWindow(view())) {
FloatPoint absolutePoint = frameView->windowToContents(windowLocation);
FloatPoint documentPoint = frameView->absoluteToDocumentPoint(absolutePoint);
m_pageLocation = flooredLayoutPoint(documentPoint);
m_clientLocation = pagePointToClientPoint(m_pageLocation, frameView);
}
}
initCoordinates();
}
void MouseRelatedEvent::initCoordinates()
{
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
computePageLocation();
m_hasCachedRelativePosition = false;
}
FrameView* MouseRelatedEvent::frameViewFromDOMWindow(DOMWindow* window)
{
auto* frame = window ? window->frame() : nullptr;
if (!frame)
return nullptr;
return frame->view();
}
LayoutPoint MouseRelatedEvent::pagePointToClientPoint(LayoutPoint pagePoint, FrameView* frameView)
{
if (!frameView)
return pagePoint;
return flooredLayoutPoint(frameView->documentToClientPoint(pagePoint));
}
LayoutPoint MouseRelatedEvent::pagePointToAbsolutePoint(LayoutPoint pagePoint, FrameView* frameView)
{
if (!frameView)
return pagePoint;
return pagePoint.scaled(frameView->documentToAbsoluteScaleFactor());
}
void MouseRelatedEvent::initCoordinates(const LayoutPoint& clientLocation)
{
FloatSize documentToClientOffset;
if (auto* frameView = frameViewFromDOMWindow(view()))
documentToClientOffset = frameView->documentToClientOffset();
m_clientLocation = clientLocation;
m_pageLocation = clientLocation - LayoutSize(documentToClientOffset);
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
computePageLocation();
m_hasCachedRelativePosition = false;
}
float MouseRelatedEvent::documentToAbsoluteScaleFactor() const
{
if (auto* frameView = frameViewFromDOMWindow(view()))
return frameView->documentToAbsoluteScaleFactor();
return 1;
}
void MouseRelatedEvent::computePageLocation()
{
m_absoluteLocation = pagePointToAbsolutePoint(m_pageLocation, frameViewFromDOMWindow(view()));
}
void MouseRelatedEvent::receivedTarget()
{
m_hasCachedRelativePosition = false;
}
void MouseRelatedEvent::computeRelativePosition()
{
Node* targetNode = target() ? target()->toNode() : nullptr;
if (!targetNode)
return;
m_layerLocation = m_pageLocation;
m_offsetLocation = m_pageLocation;
targetNode->document().updateLayoutIgnorePendingStylesheets();
if (RenderObject* r = targetNode->renderer()) {
m_offsetLocation = LayoutPoint(r->absoluteToLocal(absoluteLocation(), UseTransforms));
float scaleFactor = 1 / documentToAbsoluteScaleFactor();
if (scaleFactor != 1.0f)
m_offsetLocation.scale(scaleFactor);
}
Node* n = targetNode;
while (n && !n->renderer())
n = n->parentNode();
RenderLayer* layer;
if (n && (layer = n->renderer()->enclosingLayer())) {
for (; layer; layer = layer->parent()) {
m_layerLocation -= toLayoutSize(layer->location());
}
}
m_hasCachedRelativePosition = true;
}
FloatPoint MouseRelatedEvent::locationInRootViewCoordinates() const
{
if (auto* frameView = frameViewFromDOMWindow(view()))
return frameView->contentsToRootView(roundedIntPoint(m_absoluteLocation));
return m_absoluteLocation;
}
int MouseRelatedEvent::layerX()
{
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return m_layerLocation.x();
}
int MouseRelatedEvent::layerY()
{
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return m_layerLocation.y();
}
int MouseRelatedEvent::offsetX()
{
if (isSimulated())
return 0;
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return roundToInt(m_offsetLocation.x());
}
int MouseRelatedEvent::offsetY()
{
if (isSimulated())
return 0;
if (!m_hasCachedRelativePosition)
computeRelativePosition();
return roundToInt(m_offsetLocation.y());
}
int MouseRelatedEvent::pageX() const
{
return m_pageLocation.x();
}
int MouseRelatedEvent::pageY() const
{
return m_pageLocation.y();
}
const LayoutPoint& MouseRelatedEvent::pageLocation() const
{
return m_pageLocation;
}
int MouseRelatedEvent::x() const
{
return m_clientLocation.x();
}
int MouseRelatedEvent::y() const
{
return m_clientLocation.y();
}
}