Skip to content

Commit

Permalink
Only trigger image edit drag after user moves at least 3 pixels.
Browse files Browse the repository at this point in the history
  • Loading branch information
clark-signal committed Apr 6, 2023
1 parent 9d4e13c commit 16f1fbf
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.TypedArrayUtils;
import androidx.core.view.GestureDetectorCompat;

import org.signal.imageeditor.R;
Expand Down Expand Up @@ -51,6 +49,9 @@ public final class ImageEditorView extends FrameLayout {

private static final int DEFAULT_BLACKOUT_COLOR = 0xFF000000;

/** Maximum distance squared a user can move the pointer before we consider a drag starting */
private static final int MAX_MOVE_SQUARED_BEFORE_DRAG = 10;

private HiddenEditText editText;

@NonNull
Expand Down Expand Up @@ -94,6 +95,9 @@ public final class ImageEditorView extends FrameLayout {
@Nullable
private EditSession editSession;
private boolean moreThanOnePointerUsedInSession;
private PointF touchDownStart;

private boolean inDrag;

public ImageEditorView(Context context) {
super(context);
Expand Down Expand Up @@ -268,13 +272,14 @@ public boolean onTouchEvent(MotionEvent event) {
PointF point = getPoint(event);
EditorElement selected = model.findElementAtPoint(point, viewMatrix, inverse);

inDrag = false;
moreThanOnePointerUsedInSession = false;
touchDownStart = point;
model.pushUndoPoint();
editSession = startEdit(inverse, point, selected);

if (editSession != null) {
checkTrashIntersect(point);
notifyDragStart(editSession.getSelected());
}

if (tapListener != null && allowTaps()) {
Expand Down Expand Up @@ -303,7 +308,11 @@ public boolean onTouchEvent(MotionEvent event) {
}
model.moving(editSession.getSelected());
invalidate();
notifyDragMove(editSession.getSelected(), checkTrashIntersect(getPoint(event)));
if (inDrag) {
notifyDragMove(editSession.getSelected(), checkTrashIntersect(getPoint(event)));
} else if (pointerCount == 1) {
checkDragStart(event);
}
return true;
}
break;
Expand Down Expand Up @@ -353,7 +362,10 @@ public boolean onTouchEvent(MotionEvent event) {
checkTrashIntersect(point) &&
model.findElementAtPoint(point, viewMatrix, new Matrix()) == editSession.getSelected();

notifyDragEnd(editSession.getSelected(), hittingTrash);
if (inDrag) {
notifyDragEnd(editSession.getSelected(), hittingTrash);
inDrag = false;
}

editSession = null;
model.postEdit(moreThanOnePointerUsedInSession);
Expand Down Expand Up @@ -387,6 +399,20 @@ private boolean checkTrashIntersect(@NonNull PointF point) {
}
}

private void checkDragStart(MotionEvent moveEvent) {
if (inDrag || editSession == null) {
return;
}
float dX = touchDownStart.x - moveEvent.getX();
float dY = touchDownStart.y - moveEvent.getY();

float distSquared = dX * dX + dY * dY;
if (distSquared > MAX_MOVE_SQUARED_BEFORE_DRAG) {
inDrag = true;
notifyDragStart(editSession.getSelected());
}
}

private void notifyDragStart(@Nullable EditorElement editorElement) {
if (dragListener != null) {
dragListener.onDragStarted(editorElement);
Expand Down

0 comments on commit 16f1fbf

Please sign in to comment.