[Model] Mouse interaction for <model> is flipped in the y-axis
https://bugs.webkit.org/show_bug.cgi?id=235363
<rdar://problem/87772557>

Reviewed by Dean Jackson.

Source/WebCore:

The mouse coordinates we should send up to the ARQL APIs are in the coordinates
of the <model> element with the y-axis flipped.

* Modules/model-element/HTMLModelElement.cpp:
(WebCore::HTMLModelElement::flippedLocationInElementForMouseEvent):
(WebCore::HTMLModelElement::dragDidStart):
(WebCore::HTMLModelElement::dragDidChange):
(WebCore::HTMLModelElement::dragDidEnd):
* Modules/model-element/HTMLModelElement.h:

Source/WebKit:

The mouse coordinates consumed by the ARQL APIs are in the coordinates
of the <model> element with the y-axis flipped, so let's label them
as such.

* UIProcess/Cocoa/ModelElementControllerCocoa.mm:
(WebKit::ModelElementController::handleMouseDownForModelElement):
(WebKit::ModelElementController::handleMouseMoveForModelElement):
(WebKit::ModelElementController::handleMouseUpForModelElement):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::handleMouseDownForModelElement):
(WebKit::WebPageProxy::handleMouseMoveForModelElement):
(WebKit::WebPageProxy::handleMouseUpForModelElement):
* UIProcess/WebPageProxy.messages.in:
* WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm:
(WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseDown):
(WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseMove):
(WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseUp):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@288610 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index d06f44e..b53ec20 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,21 @@
+2022-01-26  Antoine Quint  <graouts@webkit.org>
+
+        [Model] Mouse interaction for <model> is flipped in the y-axis
+        https://bugs.webkit.org/show_bug.cgi?id=235363
+        <rdar://problem/87772557>
+
+        Reviewed by Dean Jackson.
+
+        The mouse coordinates we should send up to the ARQL APIs are in the coordinates
+        of the <model> element with the y-axis flipped.
+
+        * Modules/model-element/HTMLModelElement.cpp:
+        (WebCore::HTMLModelElement::flippedLocationInElementForMouseEvent):
+        (WebCore::HTMLModelElement::dragDidStart):
+        (WebCore::HTMLModelElement::dragDidChange):
+        (WebCore::HTMLModelElement::dragDidEnd):
+        * Modules/model-element/HTMLModelElement.h:
+
 2022-01-25  Chris Dumez  <cdumez@apple.com>
 
         REGRESSION (iOS 15.2): Loading gets stuck after back-navigation involving COOP header
diff --git a/Source/WebCore/Modules/model-element/HTMLModelElement.cpp b/Source/WebCore/Modules/model-element/HTMLModelElement.cpp
index 24fd3cb..9fea18c 100644
--- a/Source/WebCore/Modules/model-element/HTMLModelElement.cpp
+++ b/Source/WebCore/Modules/model-element/HTMLModelElement.cpp
@@ -344,6 +344,14 @@
         dragDidEnd(mouseEvent);
 }
 
+LayoutPoint HTMLModelElement::flippedLocationInElementForMouseEvent(MouseEvent& event)
+{
+    LayoutUnit flippedY { event.offsetY() };
+    if (auto* renderModel = dynamicDowncast<RenderModel>(renderer()))
+        flippedY = renderModel->paddingBoxHeight() - flippedY;
+    return { LayoutUnit(event.offsetX()), flippedY };
+}
+
 void HTMLModelElement::dragDidStart(MouseEvent& event)
 {
     ASSERT(!m_isDragging);
@@ -357,7 +365,7 @@
     m_isDragging = true;
 
     if (m_modelPlayer)
-        m_modelPlayer->handleMouseDown(event.pageLocation(), event.timeStamp());
+        m_modelPlayer->handleMouseDown(flippedLocationInElementForMouseEvent(event), event.timeStamp());
 }
 
 void HTMLModelElement::dragDidChange(MouseEvent& event)
@@ -367,7 +375,7 @@
     event.setDefaultHandled();
 
     if (m_modelPlayer)
-        m_modelPlayer->handleMouseMove(event.pageLocation(), event.timeStamp());
+        m_modelPlayer->handleMouseMove(flippedLocationInElementForMouseEvent(event), event.timeStamp());
 }
 
 void HTMLModelElement::dragDidEnd(MouseEvent& event)
@@ -383,7 +391,7 @@
     m_isDragging = false;
 
     if (m_modelPlayer)
-        m_modelPlayer->handleMouseUp(event.pageLocation(), event.timeStamp());
+        m_modelPlayer->handleMouseUp(flippedLocationInElementForMouseEvent(event), event.timeStamp());
 }
 
 // MARK: - Camera support.
diff --git a/Source/WebCore/Modules/model-element/HTMLModelElement.h b/Source/WebCore/Modules/model-element/HTMLModelElement.h
index 3dcac28..57ddf26 100644
--- a/Source/WebCore/Modules/model-element/HTMLModelElement.h
+++ b/Source/WebCore/Modules/model-element/HTMLModelElement.h
@@ -137,6 +137,8 @@
     void dragDidChange(MouseEvent&);
     void dragDidEnd(MouseEvent&);
 
+    LayoutPoint flippedLocationInElementForMouseEvent(MouseEvent&);
+
     void setAnimationIsPlaying(bool, DOMPromiseDeferred<void>&&);
 
     URL m_sourceURL;
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index c64876e..95fc4e4 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,29 @@
+2022-01-26  Antoine Quint  <graouts@webkit.org>
+
+        [Model] Mouse interaction for <model> is flipped in the y-axis
+        https://bugs.webkit.org/show_bug.cgi?id=235363
+        <rdar://problem/87772557>
+
+        Reviewed by Dean Jackson.
+
+        The mouse coordinates consumed by the ARQL APIs are in the coordinates
+        of the <model> element with the y-axis flipped, so let's label them
+        as such.
+
+        * UIProcess/Cocoa/ModelElementControllerCocoa.mm:
+        (WebKit::ModelElementController::handleMouseDownForModelElement):
+        (WebKit::ModelElementController::handleMouseMoveForModelElement):
+        (WebKit::ModelElementController::handleMouseUpForModelElement):
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::handleMouseDownForModelElement):
+        (WebKit::WebPageProxy::handleMouseMoveForModelElement):
+        (WebKit::WebPageProxy::handleMouseUpForModelElement):
+        * UIProcess/WebPageProxy.messages.in:
+        * WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm:
+        (WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseDown):
+        (WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseMove):
+        (WebKit::ARKitInlinePreviewModelPlayerMac::handleMouseUp):
+
 2022-01-26  Alexander Mikhaylenko  <alexm@gnome.org>
 
         [GTK] REGRESSION: Touch scrolling is broken
diff --git a/Source/WebKit/UIProcess/Cocoa/ModelElementControllerCocoa.mm b/Source/WebKit/UIProcess/Cocoa/ModelElementControllerCocoa.mm
index 53418b9..ce2b16a 100644
--- a/Source/WebKit/UIProcess/Cocoa/ModelElementControllerCocoa.mm
+++ b/Source/WebKit/UIProcess/Cocoa/ModelElementControllerCocoa.mm
@@ -215,22 +215,22 @@
     return m_inlinePreviews.get(uuid);
 }
 
-void ModelElementController::handleMouseDownForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ModelElementController::handleMouseDownForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto preview = previewForUUID(uuid))
-        [preview mouseDownAtLocation:CGPointMake(locationInPageCoordinates.x().toFloat(), locationInPageCoordinates.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
+        [preview mouseDownAtLocation:CGPointMake(flippedLocationInElement.x().toFloat(), flippedLocationInElement.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
 }
 
-void ModelElementController::handleMouseMoveForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ModelElementController::handleMouseMoveForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto preview = previewForUUID(uuid))
-        [preview mouseDraggedAtLocation:CGPointMake(locationInPageCoordinates.x().toFloat(), locationInPageCoordinates.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
+        [preview mouseDraggedAtLocation:CGPointMake(flippedLocationInElement.x().toFloat(), flippedLocationInElement.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
 }
 
-void ModelElementController::handleMouseUpForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ModelElementController::handleMouseUpForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto preview = previewForUUID(uuid))
-        [preview mouseUpAtLocation:CGPointMake(locationInPageCoordinates.x().toFloat(), locationInPageCoordinates.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
+        [preview mouseUpAtLocation:CGPointMake(flippedLocationInElement.x().toFloat(), flippedLocationInElement.y().toFloat()) timestamp:timestamp.secondsSinceEpoch().value()];
 }
 
 #endif
diff --git a/Source/WebKit/UIProcess/WebPageProxy.cpp b/Source/WebKit/UIProcess/WebPageProxy.cpp
index 5aa3c07..f240ff4 100644
--- a/Source/WebKit/UIProcess/WebPageProxy.cpp
+++ b/Source/WebKit/UIProcess/WebPageProxy.cpp
@@ -11011,19 +11011,19 @@
     modelElementController()->modelElementDidCreatePreview(url, uuid, size, WTFMove(completionHandler));
 }
 
-void WebPageProxy::handleMouseDownForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void WebPageProxy::handleMouseDownForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
-    modelElementController()->handleMouseDownForModelElement(uuid, locationInPageCoordinates, timestamp);
+    modelElementController()->handleMouseDownForModelElement(uuid, flippedLocationInElement, timestamp);
 }
 
-void WebPageProxy::handleMouseMoveForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void WebPageProxy::handleMouseMoveForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
-    modelElementController()->handleMouseMoveForModelElement(uuid, locationInPageCoordinates, timestamp);
+    modelElementController()->handleMouseMoveForModelElement(uuid, flippedLocationInElement, timestamp);
 }
 
-void WebPageProxy::handleMouseUpForModelElement(const String& uuid, const WebCore::LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void WebPageProxy::handleMouseUpForModelElement(const String& uuid, const WebCore::LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
-    modelElementController()->handleMouseUpForModelElement(uuid, locationInPageCoordinates, timestamp);
+    modelElementController()->handleMouseUpForModelElement(uuid, flippedLocationInElement, timestamp);
 }
 #endif
 
diff --git a/Source/WebKit/UIProcess/WebPageProxy.messages.in b/Source/WebKit/UIProcess/WebPageProxy.messages.in
index b1703f6..a866e536 100644
--- a/Source/WebKit/UIProcess/WebPageProxy.messages.in
+++ b/Source/WebKit/UIProcess/WebPageProxy.messages.in
@@ -588,9 +588,9 @@
 #endif
 #if ENABLE(ARKIT_INLINE_PREVIEW_MAC)
     ModelElementDidCreatePreview(URL url, String uuid, WebCore::FloatSize size) -> (Expected<std::pair<String, uint32_t>, WebCore::ResourceError> result) Async
-    HandleMouseDownForModelElement(String uuid, WebCore::LayoutPoint locationInPageCoordinates, MonotonicTime timestamp)
-    HandleMouseMoveForModelElement(String uuid, WebCore::LayoutPoint locationInPageCoordinates, MonotonicTime timestamp)
-    HandleMouseUpForModelElement(String uuid, WebCore::LayoutPoint locationInPageCoordinates, MonotonicTime timestamp)
+    HandleMouseDownForModelElement(String uuid, WebCore::LayoutPoint flippedLocationInElement, MonotonicTime timestamp)
+    HandleMouseMoveForModelElement(String uuid, WebCore::LayoutPoint flippedLocationInElement, MonotonicTime timestamp)
+    HandleMouseUpForModelElement(String uuid, WebCore::LayoutPoint flippedLocationInElement, MonotonicTime timestamp)
 #endif
 #if ENABLE(ARKIT_INLINE_PREVIEW)
     ModelElementGetCamera(struct WebKit::ModelIdentifier modelIdentifier) -> (Expected<WebCore::HTMLModelElementCamera, WebCore::ResourceError> result) Async
diff --git a/Source/WebKit/WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm b/Source/WebKit/WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm
index a784ec3..5266b14 100644
--- a/Source/WebKit/WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm
+++ b/Source/WebKit/WebProcess/Model/mac/ARKitInlinePreviewModelPlayerMac.mm
@@ -185,22 +185,22 @@
     return false;
 }
 
-void ARKitInlinePreviewModelPlayerMac::handleMouseDown(const LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ARKitInlinePreviewModelPlayerMac::handleMouseDown(const LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto* page = this->page())
-        page->send(Messages::WebPageProxy::HandleMouseDownForModelElement([m_inlinePreview uuid].UUIDString, locationInPageCoordinates, timestamp));
+        page->send(Messages::WebPageProxy::HandleMouseDownForModelElement([m_inlinePreview uuid].UUIDString, flippedLocationInElement, timestamp));
 }
 
-void ARKitInlinePreviewModelPlayerMac::handleMouseMove(const LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ARKitInlinePreviewModelPlayerMac::handleMouseMove(const LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto* page = this->page())
-        page->send(Messages::WebPageProxy::HandleMouseMoveForModelElement([m_inlinePreview uuid].UUIDString, locationInPageCoordinates, timestamp));
+        page->send(Messages::WebPageProxy::HandleMouseMoveForModelElement([m_inlinePreview uuid].UUIDString, flippedLocationInElement, timestamp));
 }
 
-void ARKitInlinePreviewModelPlayerMac::handleMouseUp(const LayoutPoint& locationInPageCoordinates, MonotonicTime timestamp)
+void ARKitInlinePreviewModelPlayerMac::handleMouseUp(const LayoutPoint& flippedLocationInElement, MonotonicTime timestamp)
 {
     if (auto* page = this->page())
-        page->send(Messages::WebPageProxy::HandleMouseUpForModelElement([m_inlinePreview uuid].UUIDString, locationInPageCoordinates, timestamp));
+        page->send(Messages::WebPageProxy::HandleMouseUpForModelElement([m_inlinePreview uuid].UUIDString, flippedLocationInElement, timestamp));
 }
 
 }