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
This commit is contained in:
Masayuki Nakano 2021-01-13 01:54:17 +00:00
Родитель 3cd22c1f8e
Коммит 0028b2127b
4 изменённых файлов: 72 добавлений и 75 удалений

Просмотреть файл

@ -310,28 +310,36 @@ class HTMLEditor final : public TextEditor,
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
* 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 OnMouseDown(int32_t aX, int32_t aY,
Element* aTarget,
dom::Event* aMouseEvent);
MOZ_CAN_RUN_SCRIPT nsresult StartToDragResizerOrHandleDragGestureOnGrabber(
dom::MouseEvent& aMouseDownEvent, Element& aEventTargetElement);
/**
* event callback when a mouse button is released
* @param aX [IN] horizontal position of the pointer
* @param aY [IN] vertical position of the pointer
* 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 OnMouseUp(int32_t aX, int32_t aY);
MOZ_CAN_RUN_SCRIPT nsresult
StopDraggingResizerOrGrabberAt(const CSSIntPoint& aClientPoint);
/**
* event callback when the mouse pointer is moved
* @param aMouseEvent [IN] the event
* 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 OnMouseMove(dom::MouseEvent* aMouseEvent);
MOZ_CAN_RUN_SCRIPT nsresult
UpdateResizerOrGrabberPositionTo(const CSSIntPoint& aClientPoint);
/**
* IsCSSEnabled() returns true if this editor treats styles with style

Просмотреть файл

@ -83,9 +83,12 @@ NS_IMETHODIMP HTMLEditorEventListener::HandleEvent(Event* aEvent) {
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: {
@ -243,11 +246,11 @@ nsresult HTMLEditorEventListener::MouseUp(MouseEvent* aMouseEvent) {
// 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),
@ -265,18 +268,6 @@ static bool IsAcceptableMouseEvent(const HTMLEditor& aHTMLEditor,
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();
@ -294,8 +285,14 @@ nsresult HTMLEditorEventListener::HandlePrimaryMouseButtonDown(
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:

Просмотреть файл

@ -84,9 +84,6 @@ class HTMLEditorEventListener final : public EditorEventListener {
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(

Просмотреть файл

@ -741,14 +741,15 @@ nsresult HTMLEditor::StartResizing(Element& aHandleElement) {
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,
@ -759,10 +760,10 @@ nsresult HTMLEditor::OnMouseDown(int32_t aClientX, int32_t aClientY,
// 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);
@ -776,8 +777,8 @@ nsresult HTMLEditor::OnMouseDown(int32_t aClientX, int32_t aClientY,
// 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");
@ -787,7 +788,8 @@ nsresult HTMLEditor::OnMouseDown(int32_t aClientX, int32_t aClientY,
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())) {
@ -807,7 +809,7 @@ nsresult HTMLEditor::OnMouseUp(int32_t aClientX, int32_t aClientY) {
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");
@ -834,7 +836,7 @@ nsresult HTMLEditor::OnMouseUp(int32_t aClientX, int32_t aClientY) {
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) {
@ -1082,9 +1084,8 @@ int32_t HTMLEditor::GetNewResizingHeight(int32_t aX, int32_t aY) {
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);
@ -1094,13 +1095,12 @@ nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
// 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())) {
@ -1167,16 +1167,13 @@ nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
}
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),
@ -1184,12 +1181,10 @@ nsresult HTMLEditor::OnMouseMove(MouseEvent* aMouseEvent) {
}
}
if (mIsMoving) {
int32_t clientX = aMouseEvent->ClientX();
int32_t clientY = aMouseEvent->ClientY();
int32_t newX = mPositionedObjectX + clientX - mOriginalX;
int32_t newY = mPositionedObjectY + clientY - mOriginalY;
int32_t newX = mPositionedObjectX + aClientPoint.x - mOriginalX;
int32_t newY = mPositionedObjectY + aClientPoint.y - mOriginalY;
// Maybe align newX and newY to the grid.
SnapToGrid(newX, newY);
if (RefPtr<nsStyledElement> positioningShadowStyledElement =