Bug 1756323 - Cleanup mouse event coord code. r=jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D139234
This commit is contained in:
Emilio Cobos Álvarez 2022-02-21 11:53:02 +00:00
parent 6e24f3bf7d
commit 957e1363f5
6 changed files with 68 additions and 109 deletions

View file

@ -133,16 +133,9 @@ RootAccessibleWrap::HandleEvent(Event* aDOMEvent) {
return NS_OK; return NS_OK;
} }
MouseEvent* mouseEvent = aDOMEvent->AsMouseEvent(); if (MouseEvent* mouseEvent = aDOMEvent->AsMouseEvent()) {
if (mouseEvent) { LayoutDeviceIntPoint point = mouseEvent->ScreenPointLayoutDevicePix();
nsPresContext* pc = PresContext(); ExploreByTouch(point.x, point.y);
int32_t x =
pc->CSSPixelsToDevPixels(mouseEvent->ScreenX(CallerType::System));
int32_t y =
pc->CSSPixelsToDevPixels(mouseEvent->ScreenY(CallerType::System));
ExploreByTouch(x, y);
} }
return NS_OK; return NS_OK;

View file

@ -63,8 +63,8 @@ void MouseEvent::InitMouseEvent(const nsAString& aType, bool aCanBubble,
mouseEventBase->mButton = aButton; mouseEventBase->mButton = aButton;
mouseEventBase->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, mouseEventBase->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey,
aMetaKey); aMetaKey);
mClientPoint.x = aClientX; mDefaultClientPoint.x = aClientX;
mClientPoint.y = aClientY; mDefaultClientPoint.y = aClientY;
mouseEventBase->mRefPoint.x = aScreenX; mouseEventBase->mRefPoint.x = aScreenX;
mouseEventBase->mRefPoint.y = aScreenY; mouseEventBase->mRefPoint.y = aScreenY;
@ -223,123 +223,77 @@ void MouseEvent::GetRegion(nsAString& aRegion) {
} }
} }
int32_t MouseEvent::ScreenX(CallerType aCallerType) { CSSIntPoint MouseEvent::ScreenPoint(CallerType aCallerType) const {
if (mEvent->mFlags.mIsPositionless) { if (mEvent->mFlags.mIsPositionless) {
return 0; return {};
} }
if (nsContentUtils::ResistFingerprinting(aCallerType)) { if (nsContentUtils::ResistFingerprinting(aCallerType)) {
// Sanitize to something sort of like client cooords, but not quite // Sanitize to something sort of like client cooords, but not quite
// (defaulting to (0,0) instead of our pre-specified client coords). // (defaulting to (0,0) instead of our pre-specified client coords).
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint, return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
CSSIntPoint(0, 0)) CSSIntPoint(0, 0));
.x;
} }
return Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint).x; return Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint);
} }
int32_t MouseEvent::ScreenY(CallerType aCallerType) { LayoutDeviceIntPoint MouseEvent::ScreenPointLayoutDevicePix() const {
if (mEvent->mFlags.mIsPositionless) { const CSSIntPoint point = ScreenPoint(CallerType::System);
return 0; auto scale = mPresContext ? mPresContext->CSSToDevPixelScale()
: CSSToLayoutDeviceScale();
return LayoutDeviceIntPoint::Round(point * scale);
} }
if (nsContentUtils::ResistFingerprinting(aCallerType)) { DesktopIntPoint MouseEvent::ScreenPointDesktopPix() const {
// Sanitize to something sort of like client cooords, but not quite const CSSIntPoint point = ScreenPoint(CallerType::System);
// (defaulting to (0,0) instead of our pre-specified client coords). auto scale =
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint, mPresContext
CSSIntPoint(0, 0)) ? mPresContext->CSSToDevPixelScale() /
.y; mPresContext->DeviceContext()->GetDesktopToDeviceScale()
} : CSSToDesktopScale();
return DesktopIntPoint::Round(point * scale);
return Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint).y;
} }
already_AddRefed<nsIScreen> MouseEvent::GetScreen() { already_AddRefed<nsIScreen> MouseEvent::GetScreen() {
if (mEvent->mFlags.mIsPositionless) {
return nullptr;
}
if (!mPresContext) {
return nullptr;
}
nsCOMPtr<nsIScreenManager> screenMgr = nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1"); do_GetService("@mozilla.org/gfx/screenmanager;1");
if (!screenMgr) { if (!screenMgr) {
return nullptr; return nullptr;
} }
const CSSIntPoint point = return screenMgr->ScreenForRect(
Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint); DesktopIntRect(ScreenPointDesktopPix(), DesktopIntSize(1, 1)));
DesktopPoint desktopPoint =
point * mPresContext->CSSToDevPixelScale() /
mPresContext->DeviceContext()->GetDesktopToDeviceScale();
return screenMgr->ScreenForRect(DesktopIntRect(
DesktopIntPoint::Round(desktopPoint), DesktopIntSize(1, 1)));
} }
int32_t MouseEvent::PageX() const { CSSIntPoint MouseEvent::PagePoint() const {
if (mEvent->mFlags.mIsPositionless) { if (mEvent->mFlags.mIsPositionless) {
return 0; return {};
} }
if (mPrivateDataDuplicated) { if (mPrivateDataDuplicated) {
return mPagePoint.x; return mPagePoint;
} }
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint, return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint) mDefaultClientPoint);
.x;
} }
int32_t MouseEvent::PageY() const { CSSIntPoint MouseEvent::ClientPoint() const {
if (mEvent->mFlags.mIsPositionless) { if (mEvent->mFlags.mIsPositionless) {
return 0; return {};
}
if (mPrivateDataDuplicated) {
return mPagePoint.y;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
}
int32_t MouseEvent::ClientX() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
} }
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint, return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint) mDefaultClientPoint);
.x;
} }
int32_t MouseEvent::ClientY() { CSSIntPoint MouseEvent::OffsetPoint() const {
if (mEvent->mFlags.mIsPositionless) { if (mEvent->mFlags.mIsPositionless) {
return 0; return {};
} }
return Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
}
int32_t MouseEvent::OffsetX() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetOffsetCoords(mPresContext, mEvent, mEvent->mRefPoint, return Event::GetOffsetCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint) mDefaultClientPoint);
.x;
}
int32_t MouseEvent::OffsetY() {
if (mEvent->mFlags.mIsPositionless) {
return 0;
}
return Event::GetOffsetCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint)
.y;
} }
bool MouseEvent::AltKey() { return mEvent->AsInputEvent()->IsAlt(); } bool MouseEvent::AltKey() { return mEvent->AsInputEvent()->IsAlt(); }

View file

@ -12,8 +12,7 @@
#include "mozilla/dom/MouseEventBinding.h" #include "mozilla/dom/MouseEventBinding.h"
#include "mozilla/EventForwards.h" #include "mozilla/EventForwards.h"
namespace mozilla { namespace mozilla::dom {
namespace dom {
class MouseEvent : public UIEvent { class MouseEvent : public UIEvent {
public: public:
@ -34,15 +33,31 @@ class MouseEvent : public UIEvent {
return Button() + 1; return Button() + 1;
} }
int32_t ScreenX(CallerType aCallerType);
int32_t ScreenY(CallerType aCallerType);
already_AddRefed<nsIScreen> GetScreen(); already_AddRefed<nsIScreen> GetScreen();
int32_t PageX() const;
int32_t PageY() const; // In CSS coords.
int32_t ClientX(); CSSIntPoint ScreenPoint(CallerType) const;
int32_t ClientY(); int32_t ScreenX(CallerType aCallerType) const {
int32_t OffsetX(); return ScreenPoint(aCallerType).x;
int32_t OffsetY(); }
int32_t ScreenY(CallerType aCallerType) const {
return ScreenPoint(aCallerType).y;
}
LayoutDeviceIntPoint ScreenPointLayoutDevicePix() const;
DesktopIntPoint ScreenPointDesktopPix() const;
CSSIntPoint PagePoint() const;
int32_t PageX() const { return PagePoint().x; }
int32_t PageY() const { return PagePoint().y; }
CSSIntPoint ClientPoint() const;
int32_t ClientX() const { return ClientPoint().x; }
int32_t ClientY() const { return ClientPoint().y; }
CSSIntPoint OffsetPoint() const;
int32_t OffsetX() const { return OffsetPoint().x; }
int32_t OffsetY() const { return OffsetPoint().y; }
bool CtrlKey(); bool CtrlKey();
bool ShiftKey(); bool ShiftKey();
bool AltKey(); bool AltKey();
@ -91,8 +106,7 @@ class MouseEvent : public UIEvent {
const nsAString& aModifiersList); const nsAString& aModifiersList);
}; };
} // namespace dom } // namespace mozilla::dom
} // namespace mozilla
already_AddRefed<mozilla::dom::MouseEvent> NS_NewDOMMouseEvent( already_AddRefed<mozilla::dom::MouseEvent> NS_NewDOMMouseEvent(
mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext, mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext,

View file

@ -29,7 +29,7 @@ UIEvent::UIEvent(EventTarget* aOwner, nsPresContext* aPresContext,
WidgetGUIEvent* aEvent) WidgetGUIEvent* aEvent)
: Event(aOwner, aPresContext, : Event(aOwner, aPresContext,
aEvent ? aEvent : new InternalUIEvent(false, eVoidEvent, nullptr)), aEvent ? aEvent : new InternalUIEvent(false, eVoidEvent, nullptr)),
mClientPoint(0, 0), mDefaultClientPoint(0, 0),
mLayerPoint(0, 0), mLayerPoint(0, 0),
mPagePoint(0, 0), mPagePoint(0, 0),
mMovementPoint(0, 0), mMovementPoint(0, 0),
@ -189,12 +189,12 @@ nsIntPoint UIEvent::GetLayerPoint() const {
} }
void UIEvent::DuplicatePrivateData() { void UIEvent::DuplicatePrivateData() {
mClientPoint = Event::GetClientCoords(mPresContext, mEvent, mEvent->mRefPoint, mDefaultClientPoint = Event::GetClientCoords(
mClientPoint); mPresContext, mEvent, mEvent->mRefPoint, mDefaultClientPoint);
mMovementPoint = GetMovementPoint(); mMovementPoint = GetMovementPoint();
mLayerPoint = GetLayerPoint(); mLayerPoint = GetLayerPoint();
mPagePoint = Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint, mPagePoint = Event::GetPageCoords(mPresContext, mEvent, mEvent->mRefPoint,
mClientPoint); mDefaultClientPoint);
// GetScreenPoint converts mEvent->mRefPoint to right coordinates. // GetScreenPoint converts mEvent->mRefPoint to right coordinates.
CSSIntPoint screenPoint = CSSIntPoint screenPoint =
Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint); Event::GetScreenCoords(mPresContext, mEvent, mEvent->mRefPoint);

View file

@ -102,7 +102,7 @@ class UIEvent : public Event {
nsCOMPtr<nsPIDOMWindowOuter> mView; nsCOMPtr<nsPIDOMWindowOuter> mView;
int32_t mDetail; int32_t mDetail;
CSSIntPoint mClientPoint; CSSIntPoint mDefaultClientPoint;
// Screenpoint is mEvent->mRefPoint. // Screenpoint is mEvent->mRefPoint.
nsIntPoint mLayerPoint; nsIntPoint mLayerPoint;
CSSIntPoint mPagePoint; CSSIntPoint mPagePoint;

View file

@ -336,10 +336,8 @@ nsresult nsXULPopupListener::LaunchPopup(MouseEvent* aEvent) {
pm->ShowPopup(mPopupContent, mElement, u""_ns, 0, 0, false, true, false, pm->ShowPopup(mPopupContent, mElement, u""_ns, 0, 0, false, true, false,
aEvent); aEvent);
} else { } else {
int32_t xPos = aEvent->ScreenX(CallerType::System); CSSIntPoint pos = aEvent->ScreenPoint(CallerType::System);
int32_t yPos = aEvent->ScreenY(CallerType::System); pm->ShowPopupAtScreen(mPopupContent, pos.x, pos.y, mIsContext, aEvent);
pm->ShowPopupAtScreen(mPopupContent, xPos, yPos, mIsContext, aEvent);
} }
return NS_OK; return NS_OK;