commit 6c50119778e6e55603b0cb46620f2f51a9eda0cd
parent 9ce25678e1d05904637db5c748841f4b42c8622c
Author: Emilio Cobos Álvarez <emilio@crisal.io>
Date: Tue, 11 Nov 2025 16:41:59 +0000
Bug 1994942 - Use FrameToWidgetOffset more and remove TranslateViewToWidget(). r=tnikkel,layout-reviewers
No intended behavior change.
Differential Revision: https://phabricator.services.mozilla.com/D272109
Diffstat:
4 files changed, 32 insertions(+), 89 deletions(-)
diff --git a/layout/base/PositionedEventTargeting.cpp b/layout/base/PositionedEventTargeting.cpp
@@ -864,23 +864,18 @@ nsIFrame* FindFrameTargetedByInputEvent(
}
// Now we basically undo the operations in GetEventCoordinatesRelativeTo, to
// get back the (now-clamped) coordinates in the event's widget's space.
- nsRootPresContext* rootPresContext =
- aRootFrame.mFrame->PresContext()->GetRootPresContext();
- nsView* view = rootPresContext->PresShell()->GetRootFrame()->GetView();
- if (!view) {
- return target;
- }
+ nsPresContext* pc = aRootFrame.mFrame->PresContext();
// TODO: Consider adding an optimization similar to the one in
// GetEventCoordinatesRelativeTo, where we detect cases where
// there is no transform to apply and avoid calling
// TransformFramePointToRoot() in those cases.
point = nsLayoutUtils::TransformFramePointToRoot(ViewportType::Visual,
aRootFrame, point);
- LayoutDeviceIntPoint widgetPoint = nsLayoutUtils::TranslateViewToWidget(
- rootPresContext, view, point, ViewportType::Visual, aEvent->mWidget);
- if (widgetPoint.x != NS_UNCONSTRAINEDSIZE) {
+ if (auto widgetPoint = nsLayoutUtils::FrameToWidgetOffset(aRootFrame.mFrame,
+ aEvent->mWidget)) {
// If that succeeded, we update the point in the event
- aEvent->mRefPoint = widgetPoint;
+ aEvent->mRefPoint = LayoutDeviceIntPoint::FromAppUnitsRounded(
+ *widgetPoint + point, pc->AppUnitsPerDevPixel());
}
return target;
}
diff --git a/layout/base/PresShell.cpp b/layout/base/PresShell.cpp
@@ -10005,16 +10005,14 @@ bool PresShell::EventHandler::AdjustContextMenuKeyEvent(
aMouseEvent->mWidget = rootPC->PresShell()->GetRootWidget();
if (aMouseEvent->mWidget) {
// default the refpoint to the topleft of our document
- nsPoint frameToWidgetOffset;
if (nsIFrame* rootFrame = FrameConstructor()->GetRootFrame()) {
- nsIWidget* widget = rootFrame->GetNearestWidget(frameToWidgetOffset);
- MOZ_ASSERT(widget, "If rootPC has a widget, so should we");
- auto widgetToWidgetOffset =
- nsLayoutUtils::WidgetToWidgetOffset(widget, aMouseEvent->mWidget);
- aMouseEvent->mRefPoint =
- widgetToWidgetOffset +
- LayoutDeviceIntPoint::FromAppUnitsToNearest(
- frameToWidgetOffset, GetPresContext()->AppUnitsPerDevPixel());
+ auto frameToWidgetOffset =
+ nsLayoutUtils::FrameToWidgetOffset(rootFrame, aMouseEvent->mWidget);
+ MOZ_ASSERT(frameToWidgetOffset, "If rootPC has a widget, so should we");
+ if (frameToWidgetOffset) {
+ aMouseEvent->mRefPoint = LayoutDeviceIntPoint::FromAppUnitsToNearest(
+ *frameToWidgetOffset, GetPresContext()->AppUnitsPerDevPixel());
+ }
}
}
} else {
@@ -10147,24 +10145,22 @@ bool PresShell::EventHandler::PrepareToUseCaretPosition(
nsPresContext* presContext = GetPresContext();
+ if (!aEventWidget) {
+ return false;
+ }
// get caret position relative to the closest widget
nsRect caretCoords;
nsIFrame* caretFrame = caret->GetGeometry(&caretCoords);
if (!caretFrame) {
return false;
}
- nsPoint widgetOffset;
- nsIWidget* widget = caretFrame->GetNearestWidget(widgetOffset);
- if (!widget) {
- return false;
- }
- // and then get the caret coords relative to the event widget
+
if (aEventWidget) {
- widgetOffset += LayoutDeviceIntPoint::ToAppUnits(
- nsLayoutUtils::WidgetToWidgetOffset(widget, aEventWidget),
- presContext->AppUnitsPerDevPixel());
+ if (auto offset =
+ nsLayoutUtils::FrameToWidgetOffset(caretFrame, aEventWidget)) {
+ caretCoords.MoveBy(*offset);
+ }
}
- caretCoords.MoveBy(widgetOffset);
// caret coordinates are in app units, convert to pixels
aTargetPt.x =
@@ -10259,16 +10255,12 @@ void PresShell::EventHandler::GetCurrentItemAndPositionForElement(
frame->PresContext() == GetPresContext(),
"handling event for focused content that is not in our document?");
- nsPoint widgetOffset(0, 0);
-
- // Get the frame's origin within its closest widget
- nsIWidget* widget = frame->GetNearestWidget(widgetOffset);
-
- // And make it relative to aRootWidget
+ nsPoint widgetOffset;
if (aRootWidget) {
- widgetOffset += LayoutDeviceIntPoint::ToAppUnits(
- nsLayoutUtils::WidgetToWidgetOffset(widget, aRootWidget),
- frame->PresContext()->AppUnitsPerDevPixel());
+ if (auto offset =
+ nsLayoutUtils::FrameToWidgetOffset(frame, aRootWidget)) {
+ widgetOffset = *offset;
+ }
}
// Start context menu down and to the right from top left of frame
diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp
@@ -1485,28 +1485,18 @@ nsPoint GetEventCoordinatesRelativeTo(nsIWidget* aWidget,
rootFrame = f;
}
- nsPoint widgetToRoot = [&] {
- nsPresContext* pc = rootFrame->PresContext();
- nsPoint widgetOffset;
- nsIWidget* widget = rootFrame->GetNearestWidget(widgetOffset);
- if (NS_WARN_IF(!widget)) {
- return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
- }
- LayoutDeviceIntPoint widgetPoint =
- aPoint + nsLayoutUtils::WidgetToWidgetOffset(aWidget, widget);
- nsPoint widgetAppUnits(pc->DevPixelsToAppUnits(widgetPoint.x),
- pc->DevPixelsToAppUnits(widgetPoint.y));
- return widgetAppUnits - widgetOffset;
- }();
-
- if (widgetToRoot == nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) {
+ auto rootToWidget = nsLayoutUtils::FrameToWidgetOffset(rootFrame, aWidget);
+ if (!rootToWidget) {
return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
}
+ const int32_t rootAPD = rootFrame->PresContext()->AppUnitsPerDevPixel();
+ nsPoint widgetToRoot =
+ LayoutDeviceIntPoint::ToAppUnits(aPoint, rootAPD) - *rootToWidget;
+
// Convert from root document app units to app units of the document aFrame
// is in.
- int32_t rootAPD = rootFrame->PresContext()->AppUnitsPerDevPixel();
- int32_t localAPD = frame->PresContext()->AppUnitsPerDevPixel();
+ const int32_t localAPD = frame->PresContext()->AppUnitsPerDevPixel();
widgetToRoot = widgetToRoot.ScaleToOtherAppUnits(rootAPD, localAPD);
/* If we encountered a transform, we can't do simple arithmetic to figure
@@ -2382,27 +2372,6 @@ LayoutDeviceIntPoint nsLayoutUtils::WidgetToWidgetOffset(nsIWidget* aFrom,
return fromOffset - toOffset;
}
-LayoutDeviceIntPoint nsLayoutUtils::TranslateViewToWidget(
- nsPresContext* aPresContext, nsView* aView, nsPoint aPt,
- ViewportType aViewportType, nsIWidget* aWidget) {
- nsPoint viewOffset;
- nsIWidget* viewWidget = aView->GetNearestWidget(&viewOffset);
- if (!viewWidget) {
- return LayoutDeviceIntPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
- }
-
- nsPoint pt = (aPt + viewOffset);
- // The target coordinates are visual, so perform a layout-to-visual
- // conversion if the incoming coordinates are layout.
- if (aViewportType == ViewportType::Layout && aPresContext->GetPresShell()) {
- pt = ViewportUtils::LayoutToVisual(pt, aPresContext->GetPresShell());
- }
- LayoutDeviceIntPoint relativeToViewWidget(
- aPresContext->AppUnitsToDevPixels(pt.x),
- aPresContext->AppUnitsToDevPixels(pt.y));
- return relativeToViewWidget + WidgetToWidgetOffset(viewWidget, aWidget);
-}
-
UsedClear nsLayoutUtils::CombineClearType(UsedClear aOrigClearType,
UsedClear aNewClearType) {
UsedClear clearType = aOrigClearType;
diff --git a/layout/base/nsLayoutUtils.h b/layout/base/nsLayoutUtils.h
@@ -739,19 +739,6 @@ class nsLayoutUtils {
nsIContent** aContainer,
int32_t* aOffset);
- /**
- * Translate from view coordinates to the widget's coordinates.
- * @param aPresContext the PresContext for the view
- * @param aView the view
- * @param aPt the point relative to the view
- * @param aViewportType whether the point is in visual or layout coordinates
- * @param aWidget the widget to which returned coordinates are relative
- * @return the point in the view's coordinates
- */
- static mozilla::LayoutDeviceIntPoint TranslateViewToWidget(
- nsPresContext* aPresContext, nsView* aView, nsPoint aPt,
- ViewportType aViewportType, nsIWidget* aWidget);
-
static mozilla::LayoutDeviceIntPoint WidgetToWidgetOffset(
nsIWidget* aFromWidget, nsIWidget* aToWidget);