Bug 1357365 - part 1: Rename `HTMLEditor::OnMouseDown()`, `HTMLEditor::OnMouseUp()` and `HTMLEditor::OnMouseMove()` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 13 Jan 2021 01:54:17 +0000
changeset 562855 ef37ec496436307f52c5f5ab56d59b49c62d2bf4
parent 562854 32f63c6fa98e635a2f5fcd7de08d56cac06c793f
child 562856 8728fb04a7efd0eb07698511d255d99c39b9b36d
push id38100
push userrmaries@mozilla.com
push dateWed, 13 Jan 2021 10:02:40 +0000
treeherdermozilla-central@8dd1cdbc4076 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1357365
milestone86.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1357365 - part 1: Rename `HTMLEditor::OnMouseDown()`, `HTMLEditor::OnMouseUp()` and `HTMLEditor::OnMouseMove()` r=m_kato They were designed for object resizer and grabber to move absolutely positioned element. Therefore, they should have better name to explain what they do. Then, we can create event listener methods for generic cases. Differential Revision: https://phabricator.services.mozilla.com/D100999
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorEventListener.cpp
editor/libeditor/HTMLEditorEventListener.h
editor/libeditor/HTMLEditorObjectResizer.cpp
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -305,38 +305,46 @@ class HTMLEditor final : public TextEdit
    */
   enum class SelectAllOfCurrentList { Yes, No };
   [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult MakeOrChangeListAsAction(
       nsAtom& aListElementTagName, const nsAString& aBulletType,
       SelectAllOfCurrentList aSelectAllOfCurrentList,
       nsIPrincipal* aPrincipal = nullptr);
 
   /**
-   * event callback when a mouse button is pressed
-   * @param aX      [IN] horizontal position of the pointer
-   * @param aY      [IN] vertical position of the pointer
-   * @param aTarget [IN] the element triggering the event
-   * @param aMouseEvent [IN] the event
-   */
-  MOZ_CAN_RUN_SCRIPT nsresult OnMouseDown(int32_t aX, int32_t aY,
-                                          Element* aTarget,
-                                          dom::Event* aMouseEvent);
-
-  /**
-   * event callback when a mouse button is released
-   * @param aX      [IN] horizontal position of the pointer
-   * @param aY      [IN] vertical position of the pointer
-   */
-  MOZ_CAN_RUN_SCRIPT nsresult OnMouseUp(int32_t aX, int32_t aY);
-
-  /**
-   * event callback when the mouse pointer is moved
-   * @param aMouseEvent [IN] the event
-   */
-  MOZ_CAN_RUN_SCRIPT nsresult OnMouseMove(dom::MouseEvent* aMouseEvent);
+   * If aTargetElement is a resizer, start to drag the resizer.  Otherwise, if
+   * aTargetElement is the grabber, start to handle drag gester on it.
+   *
+   * @param aMouseDownEvent     A `mousedown` event fired on aTargetElement.
+   * @param aEventTargetElement The target element being pressed.  This must
+   *                            be same as explicit original event target of
+   *                            aMouseDownEvent.
+   */
+  MOZ_CAN_RUN_SCRIPT nsresult StartToDragResizerOrHandleDragGestureOnGrabber(
+      dom::MouseEvent& aMouseDownEvent, Element& aEventTargetElement);
+
+  /**
+   * If the editor is handling dragging a resizer, handling drag gesture on
+   * the grabber or dragging the grabber, this finalize it.  Otherwise,
+   * does nothing.
+   *
+   * @param aClientPoint    The final point of the drag.
+   */
+  MOZ_CAN_RUN_SCRIPT nsresult
+  StopDraggingResizerOrGrabberAt(const CSSIntPoint& aClientPoint);
+
+  /**
+   * If the editor is handling dragging a resizer, handling drag gesture to
+   * start dragging the grabber or dragging the grabber, this method updates
+   * it's position.
+   *
+   * @param aClientPoint    The new point of the drag.
+   */
+  MOZ_CAN_RUN_SCRIPT nsresult
+  UpdateResizerOrGrabberPositionTo(const CSSIntPoint& aClientPoint);
 
   /**
    * IsCSSEnabled() returns true if this editor treats styles with style
    * attribute of HTML elements.  Otherwise, if this editor treats all styles
    * with "font style elements" like <b>, <i>, etc, and <blockquote> to indent,
    * align attribute to align contents, returns false.
    */
   bool IsCSSEnabled() const {
--- a/editor/libeditor/HTMLEditorEventListener.cpp
+++ b/editor/libeditor/HTMLEditorEventListener.cpp
@@ -78,19 +78,22 @@ NS_IMETHODIMP HTMLEditorEventListener::H
 
       RefPtr<MouseEvent> mouseMoveEvent = aEvent->AsMouseEvent();
       if (NS_WARN_IF(!aEvent->WidgetEventPtr())) {
         return NS_ERROR_FAILURE;
       }
 
       RefPtr<HTMLEditor> htmlEditor = mEditorBase->AsHTMLEditor();
       MOZ_ASSERT(htmlEditor);
-      DebugOnly<nsresult> rvIgnored = htmlEditor->OnMouseMove(mouseMoveEvent);
-      NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
-                           "HTMLEditor::OnMouseMove() failed, but ignored");
+      DebugOnly<nsresult> rvIgnored =
+          htmlEditor->UpdateResizerOrGrabberPositionTo(CSSIntPoint(
+              mouseMoveEvent->ClientX(), mouseMoveEvent->ClientY()));
+      NS_WARNING_ASSERTION(
+          NS_SUCCEEDED(rvIgnored),
+          "HTMLEditor::UpdateResizerOrGrabberPositionTo() failed, but ignored");
       return NS_OK;
     }
     case eResize: {
       if (DetachedFromEditor()) {
         return NS_OK;
       }
 
       RefPtr<HTMLEditor> htmlEditor = mEditorBase->AsHTMLEditor();
@@ -238,21 +241,21 @@ nsresult HTMLEditorEventListener::MouseU
 
   if (NS_WARN_IF(!aMouseEvent->GetTarget())) {
     return NS_ERROR_FAILURE;
   }
   // FYI: The event target must be an element node for conforming to the
   //      UI Events, but it may not be not an element node if it occurs
   //      on native anonymous node like a resizer.
 
-  int32_t clientX = aMouseEvent->ClientX();
-  int32_t clientY = aMouseEvent->ClientY();
-  DebugOnly<nsresult> rvIgnored = htmlEditor->OnMouseUp(clientX, clientY);
-  NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
-                       "HTMLEditor::OnMouseUp() failed, but ignored");
+  DebugOnly<nsresult> rvIgnored = htmlEditor->StopDraggingResizerOrGrabberAt(
+      CSSIntPoint(aMouseEvent->ClientX(), aMouseEvent->ClientY()));
+  NS_WARNING_ASSERTION(
+      NS_SUCCEEDED(rvIgnored),
+      "HTMLEditor::StopDraggingResizerOrGrabberAt() failed, but ignored");
 
   nsresult rv = EditorEventListener::MouseUp(aMouseEvent);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                        "EditorEventListener::MouseUp() failed");
   return rv;
 }
 
 static bool IsAcceptableMouseEvent(const HTMLEditor& aHTMLEditor,
@@ -260,28 +263,16 @@ static bool IsAcceptableMouseEvent(const
   // Contenteditable should disregard mousedowns outside it.
   // IsAcceptableInputEvent() checks it for a mouse event.
   WidgetMouseEvent* mousedownEvent =
       aMouseEvent->WidgetEventPtr()->AsMouseEvent();
   MOZ_ASSERT(mousedownEvent);
   return aHTMLEditor.IsAcceptableInputEvent(mousedownEvent);
 }
 
-void HTMLEditorEventListener::MaybeDisplayResizers(HTMLEditor& aHTMLEditor,
-                                                   Element& aElement,
-                                                   MouseEvent& aMouseEvent) {
-  // if the target element is an image, we have to display resizers
-  int32_t clientX = aMouseEvent.ClientX();
-  int32_t clientY = aMouseEvent.ClientY();
-  DebugOnly<nsresult> rvIgnored =
-      aHTMLEditor.OnMouseDown(clientX, clientY, &aElement, &aMouseEvent);
-  NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
-                       "HTMLEditor::OnMouseDown() failed, but ignored");
-}
-
 nsresult HTMLEditorEventListener::HandlePrimaryMouseButtonDown(
     HTMLEditor& aHTMLEditor, MouseEvent& aMouseEvent) {
   RefPtr<EventTarget> eventTarget = aMouseEvent.GetExplicitOriginalTarget();
   if (NS_WARN_IF(!eventTarget)) {
     return NS_ERROR_FAILURE;
   }
   nsCOMPtr<nsIContent> eventTargetContent = do_QueryInterface(eventTarget);
   if (!eventTargetContent) {
@@ -289,18 +280,24 @@ nsresult HTMLEditorEventListener::Handle
   }
 
   RefPtr<Element> toSelect;
   bool isElement = eventTargetContent->IsElement();
   int32_t clickCount = aMouseEvent.Detail();
   switch (clickCount) {
     case 1:
       if (isElement) {
-        RefPtr<Element> element = eventTargetContent->AsElement();
-        MaybeDisplayResizers(aHTMLEditor, *element, aMouseEvent);
+        OwningNonNull<Element> element(*eventTargetContent->AsElement());
+        DebugOnly<nsresult> rvIgnored =
+            aHTMLEditor.StartToDragResizerOrHandleDragGestureOnGrabber(
+                aMouseEvent, element);
+        NS_WARNING_ASSERTION(
+            NS_SUCCEEDED(rvIgnored),
+            "HTMLEditor::StartToDragResizerOrHandleDragGestureOnGrabber() "
+            "failed, but ignored");
       }
       break;
     case 2:
       if (isElement) {
         toSelect = eventTargetContent->AsElement();
       }
       break;
     case 3:
--- a/editor/libeditor/HTMLEditorEventListener.h
+++ b/editor/libeditor/HTMLEditorEventListener.h
@@ -79,19 +79,16 @@ class HTMLEditorEventListener final : pu
   MOZ_CAN_RUN_SCRIPT virtual nsresult MouseUp(
       dom::MouseEvent* aMouseEvent) override;
   MOZ_CAN_RUN_SCRIPT virtual nsresult MouseClick(
       WidgetMouseEvent* aMouseClickEvent) override;
 
   nsresult ListenToMouseMoveEventForResizersOrGrabber(bool aListen,
                                                       bool aForGrabber);
 
-  MOZ_CAN_RUN_SCRIPT void MaybeDisplayResizers(HTMLEditor& aHTMLEditor,
-                                               dom::Element& aElement,
-                                               dom::MouseEvent& aMouseEvent);
   MOZ_CAN_RUN_SCRIPT nsresult HandlePrimaryMouseButtonDown(
       HTMLEditor& aHTMLEditor, dom::MouseEvent& aMouseEvent);
   MOZ_CAN_RUN_SCRIPT nsresult HandleSecondaryMouseButtonDown(
       HTMLEditor& aHTMLEditor, dom::MouseEvent& aMouseEvent);
 
   bool mListeningToMouseMoveEventForResizers;
   bool mListeningToMouseMoveEventForGrabber;
   bool mListeningToResizeEvent;
--- a/editor/libeditor/HTMLEditorObjectResizer.cpp
+++ b/editor/libeditor/HTMLEditorObjectResizer.cpp
@@ -736,63 +736,65 @@ nsresult HTMLEditor::StartResizing(Eleme
   nsresult rv = static_cast<HTMLEditorEventListener*>(mEventListener.get())
                     ->ListenToMouseMoveEventForResizers(true);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                        "HTMLEditorEventListener::"
                        "ListenToMouseMoveEventForResizers(true) failed");
   return rv;
 }
 
-nsresult HTMLEditor::OnMouseDown(int32_t aClientX, int32_t aClientY,
-                                 Element* aTarget, Event* aEvent) {
-  if (NS_WARN_IF(!aTarget)) {
-    return NS_ERROR_INVALID_ARG;
-  }
+nsresult HTMLEditor::StartToDragResizerOrHandleDragGestureOnGrabber(
+    MouseEvent& aMouseDownEvent, Element& aEventTargetElement) {
+  MOZ_ASSERT(aMouseDownEvent.GetExplicitOriginalTarget() ==
+             &aEventTargetElement);
+  MOZ_ASSERT(!aMouseDownEvent.DefaultPrevented());
+  MOZ_ASSERT(aMouseDownEvent.WidgetEventPtr()->mMessage == eMouseDown);
 
   nsAutoString anonclass;
-  aTarget->GetAttr(nsGkAtoms::_moz_anonclass, anonclass);
+  aEventTargetElement.GetAttr(nsGkAtoms::_moz_anonclass, anonclass);
 
   if (anonclass.EqualsLiteral("mozResizer")) {
     AutoEditActionDataSetter editActionData(*this,
                                             EditAction::eResizingElement);
     if (NS_WARN_IF(!editActionData.CanHandle())) {
       return NS_ERROR_NOT_INITIALIZED;
     }
 
     // If we have an anonymous element and that element is a resizer,
     // let's start resizing!
-    aEvent->PreventDefault();
-    mOriginalX = aClientX;
-    mOriginalY = aClientY;
-    nsresult rv = StartResizing(*aTarget);
+    aMouseDownEvent.PreventDefault();
+    mOriginalX = aMouseDownEvent.ClientX();
+    mOriginalY = aMouseDownEvent.ClientY();
+    nsresult rv = StartResizing(aEventTargetElement);
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                          "HTMLEditor::StartResizing() failed");
     return EditorBase::ToGenericNSResult(rv);
   }
 
   if (anonclass.EqualsLiteral("mozGrabber")) {
     AutoEditActionDataSetter editActionData(*this, EditAction::eMovingElement);
     if (NS_WARN_IF(!editActionData.CanHandle())) {
       return NS_ERROR_NOT_INITIALIZED;
     }
 
     // If we have an anonymous element and that element is a grabber,
     // let's start moving the element!
-    mOriginalX = aClientX;
-    mOriginalY = aClientY;
+    mOriginalX = aMouseDownEvent.ClientX();
+    mOriginalY = aMouseDownEvent.ClientY();
     nsresult rv = GrabberClicked();
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                          "HTMLEditor::GrabberClicked() failed");
     return EditorBase::ToGenericNSResult(rv);
   }
 
   return NS_OK;
 }
 
-nsresult HTMLEditor::OnMouseUp(int32_t aClientX, int32_t aClientY) {
+nsresult HTMLEditor::StopDraggingResizerOrGrabberAt(
+    const CSSIntPoint& aClientPoint) {
   if (mIsResizing) {
     AutoEditActionDataSetter editActionData(*this, EditAction::eResizeElement);
     if (NS_WARN_IF(!editActionData.CanHandle())) {
       return NS_ERROR_NOT_INITIALIZED;
     }
 
     // we are resizing and release the mouse button, so let's
     // end the resizing process
@@ -802,17 +804,17 @@ nsresult HTMLEditor::OnMouseUp(int32_t a
     nsresult rv = editActionData.MaybeDispatchBeforeInputEvent();
     if (NS_FAILED(rv)) {
       NS_WARNING_ASSERTION(
           rv == NS_ERROR_EDITOR_ACTION_CANCELED,
           "EditorBase::MaybeDispatchBeforeInputEvent(), failed");
       return EditorBase::ToGenericNSResult(rv);
     }
 
-    rv = SetFinalSizeWithTransaction(aClientX, aClientY);
+    rv = SetFinalSizeWithTransaction(aClientPoint.x, aClientPoint.y);
     if (rv == NS_ERROR_EDITOR_DESTROYED) {
       NS_WARNING(
           "HTMLEditor::SetFinalSizeWithTransaction() destroyed the editor");
       return NS_ERROR_EDITOR_DESTROYED;
     }
     NS_WARNING_ASSERTION(
         NS_SUCCEEDED(rv),
         "HTMLEditor::SetFinalSizeWithTransaction() failed, but ignored");
@@ -829,17 +831,17 @@ nsresult HTMLEditor::OnMouseUp(int32_t a
 
     if (mIsMoving) {
       DebugOnly<nsresult> rvIgnored = mPositioningShadow->SetAttr(
           kNameSpaceID_None, nsGkAtoms::_class, u"hidden"_ns, true);
       NS_WARNING_ASSERTION(
           NS_SUCCEEDED(rvIgnored),
           "Element::SetAttr(nsGkAtoms::_class, hidden) failed");
       if (rv != NS_ERROR_EDITOR_ACTION_CANCELED) {
-        SetFinalPosition(aClientX, aClientY);
+        SetFinalPosition(aClientPoint.x, aClientPoint.y);
       }
     }
     if (mGrabberClicked) {
       DebugOnly<nsresult> rvIgnored = EndMoving();
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
                            "HTMLEditor::EndMoving() failed");
     }
     return EditorBase::ToGenericNSResult(rv);
@@ -1077,35 +1079,33 @@ int32_t HTMLEditor::GetNewResizingWidth(
 
 int32_t HTMLEditor::GetNewResizingHeight(int32_t aX, int32_t aY) {
   int32_t resized = mResizedObjectHeight +
                     GetNewResizingIncrement(aX, aY, ResizeAt::eHeight) *
                         mHeightIncrementFactor;
   return std::max(resized, 1);
 }
 
-nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
-  MOZ_ASSERT(aMouseEvent);
-
+nsresult HTMLEditor::UpdateResizerOrGrabberPositionTo(
+    const CSSIntPoint& aClientPoint) {
   if (mIsResizing) {
     AutoEditActionDataSetter editActionData(*this,
                                             EditAction::eResizingElement);
     if (NS_WARN_IF(!editActionData.CanHandle())) {
       return NS_ERROR_NOT_INITIALIZED;
     }
 
     // we are resizing and the mouse pointer's position has changed
     // we have to resdisplay the shadow
-    int32_t clientX = aMouseEvent->ClientX();
-    int32_t clientY = aMouseEvent->ClientY();
-
-    int32_t newX = GetNewResizingX(clientX, clientY);
-    int32_t newY = GetNewResizingY(clientX, clientY);
-    int32_t newWidth = GetNewResizingWidth(clientX, clientY);
-    int32_t newHeight = GetNewResizingHeight(clientX, clientY);
+    const int32_t newX = GetNewResizingX(aClientPoint.x, aClientPoint.y);
+    const int32_t newY = GetNewResizingY(aClientPoint.x, aClientPoint.y);
+    const int32_t newWidth =
+        GetNewResizingWidth(aClientPoint.x, aClientPoint.y);
+    const int32_t newHeight =
+        GetNewResizingHeight(aClientPoint.x, aClientPoint.y);
 
     if (RefPtr<nsStyledElement> resizingShadowStyledElement =
             nsStyledElement::FromNodeOrNull(mResizingShadow.get())) {
       nsresult rv;
       rv = mCSSEditUtils->SetCSSPropertyPixelsWithoutTransaction(
           *resizingShadowStyledElement, *nsGkAtoms::left, newX);
       if (rv == NS_ERROR_EDITOR_DESTROYED) {
         NS_WARNING(
@@ -1162,39 +1162,34 @@ nsresult HTMLEditor::OnMouseMove(MouseEv
   }
 
   AutoEditActionDataSetter editActionData(*this, EditAction::eMovingElement);
   if (NS_WARN_IF(!editActionData.CanHandle())) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   if (mGrabberClicked) {
-    int32_t clientX = aMouseEvent->ClientX();
-    int32_t clientY = aMouseEvent->ClientY();
-
     int32_t xThreshold =
         LookAndFeel::GetInt(LookAndFeel::IntID::DragThresholdX, 1);
     int32_t yThreshold =
         LookAndFeel::GetInt(LookAndFeel::IntID::DragThresholdY, 1);
 
-    if (DeprecatedAbs(clientX - mOriginalX) * 2 >= xThreshold ||
-        DeprecatedAbs(clientY - mOriginalY) * 2 >= yThreshold) {
+    if (DeprecatedAbs(aClientPoint.x - mOriginalX) * 2 >= xThreshold ||
+        DeprecatedAbs(aClientPoint.y - mOriginalY) * 2 >= yThreshold) {
       mGrabberClicked = false;
       DebugOnly<nsresult> rvIgnored = StartMoving();
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
                            "HTMLEditor::StartMoving() failed, but ignored");
     }
   }
   if (mIsMoving) {
-    int32_t clientX = aMouseEvent->ClientX();
-    int32_t clientY = aMouseEvent->ClientY();
+    int32_t newX = mPositionedObjectX + aClientPoint.x - mOriginalX;
+    int32_t newY = mPositionedObjectY + aClientPoint.y - mOriginalY;
 
-    int32_t newX = mPositionedObjectX + clientX - mOriginalX;
-    int32_t newY = mPositionedObjectY + clientY - mOriginalY;
-
+    // Maybe align newX and newY to the grid.
     SnapToGrid(newX, newY);
 
     if (RefPtr<nsStyledElement> positioningShadowStyledElement =
             nsStyledElement::FromNodeOrNull(mPositioningShadow.get())) {
       nsresult rv;
       rv = mCSSEditUtils->SetCSSPropertyPixelsWithoutTransaction(
           *positioningShadowStyledElement, *nsGkAtoms::left, newX);
       if (rv == NS_ERROR_EDITOR_DESTROYED) {