Only trigger image edit drag after user moves at least 3 pixels.

This commit is contained in:
Clark
2023-04-06 11:52:06 -04:00
committed by GitHub
parent 9d4e13cd08
commit 16f1fbf583
@@ -16,8 +16,6 @@ import android.widget.FrameLayout;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.TypedArrayUtils;
import androidx.core.view.GestureDetectorCompat; import androidx.core.view.GestureDetectorCompat;
import org.signal.imageeditor.R; import org.signal.imageeditor.R;
@@ -51,6 +49,9 @@ public final class ImageEditorView extends FrameLayout {
private static final int DEFAULT_BLACKOUT_COLOR = 0xFF000000; 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; private HiddenEditText editText;
@NonNull @NonNull
@@ -94,6 +95,9 @@ public final class ImageEditorView extends FrameLayout {
@Nullable @Nullable
private EditSession editSession; private EditSession editSession;
private boolean moreThanOnePointerUsedInSession; private boolean moreThanOnePointerUsedInSession;
private PointF touchDownStart;
private boolean inDrag;
public ImageEditorView(Context context) { public ImageEditorView(Context context) {
super(context); super(context);
@@ -268,13 +272,14 @@ public final class ImageEditorView extends FrameLayout {
PointF point = getPoint(event); PointF point = getPoint(event);
EditorElement selected = model.findElementAtPoint(point, viewMatrix, inverse); EditorElement selected = model.findElementAtPoint(point, viewMatrix, inverse);
inDrag = false;
moreThanOnePointerUsedInSession = false; moreThanOnePointerUsedInSession = false;
touchDownStart = point;
model.pushUndoPoint(); model.pushUndoPoint();
editSession = startEdit(inverse, point, selected); editSession = startEdit(inverse, point, selected);
if (editSession != null) { if (editSession != null) {
checkTrashIntersect(point); checkTrashIntersect(point);
notifyDragStart(editSession.getSelected());
} }
if (tapListener != null && allowTaps()) { if (tapListener != null && allowTaps()) {
@@ -303,7 +308,11 @@ public final class ImageEditorView extends FrameLayout {
} }
model.moving(editSession.getSelected()); model.moving(editSession.getSelected());
invalidate(); invalidate();
notifyDragMove(editSession.getSelected(), checkTrashIntersect(getPoint(event))); if (inDrag) {
notifyDragMove(editSession.getSelected(), checkTrashIntersect(getPoint(event)));
} else if (pointerCount == 1) {
checkDragStart(event);
}
return true; return true;
} }
break; break;
@@ -353,7 +362,10 @@ public final class ImageEditorView extends FrameLayout {
checkTrashIntersect(point) && checkTrashIntersect(point) &&
model.findElementAtPoint(point, viewMatrix, new Matrix()) == editSession.getSelected(); model.findElementAtPoint(point, viewMatrix, new Matrix()) == editSession.getSelected();
notifyDragEnd(editSession.getSelected(), hittingTrash); if (inDrag) {
notifyDragEnd(editSession.getSelected(), hittingTrash);
inDrag = false;
}
editSession = null; editSession = null;
model.postEdit(moreThanOnePointerUsedInSession); model.postEdit(moreThanOnePointerUsedInSession);
@@ -387,6 +399,20 @@ public final class ImageEditorView extends FrameLayout {
} }
} }
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) { private void notifyDragStart(@Nullable EditorElement editorElement) {
if (dragListener != null) { if (dragListener != null) {
dragListener.onDragStarted(editorElement); dragListener.onDragStarted(editorElement);