Skip to content

Commit

Permalink
fix: editor does not scroll when mouse reaches edges during dragAndDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosemoe committed Feb 10, 2024
1 parent 78a1c65 commit 9d025be
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
30 changes: 18 additions & 12 deletions editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import android.util.Log;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.ContentInfo;
import android.view.ContextThemeWrapper;
import android.view.DragEvent;
import android.view.GestureDetector;
Expand Down Expand Up @@ -3453,21 +3452,26 @@ public void pasteText() {
if (!clipboardManager.hasPrimaryClip() || (clip = clipboardManager.getPrimaryClip()) == null) {
return;
}
var data = clip.getItemAt(0);
var text = data.getText();
if (text != null && inputConnection != null) {
inputConnection.commitText(text, 1);
if (props.formatPastedText) {
formatCodeAsync(lastInsertion.getStart(), lastInsertion.getEnd());
}
notifyIMEExternalCursorChange();
}
pasteText(ClipDataUtils.clipDataToString(clip));
} catch (Exception e) {
Log.w(LOG_TAG, "Error pasting text to editor", e);
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}

/**
* Paste external text into editor
*/
public void pasteText(@Nullable CharSequence text) {
if (text != null && inputConnection != null) {
inputConnection.commitText(text, 1);
if (props.formatPastedText) {
formatCodeAsync(lastInsertion.getStart(), lastInsertion.getEnd());
}
notifyIMEExternalCursorChange();
}
}

/**
* Copy text to clipboard.
*/
Expand Down Expand Up @@ -4629,7 +4633,7 @@ public boolean onDragEvent(DragEvent event) {
case DragEvent.ACTION_DRAG_STARTED -> {
return true;
}
case DragEvent.ACTION_DRAG_ENTERED, DragEvent.ACTION_DRAG_LOCATION -> {
case DragEvent.ACTION_DRAG_LOCATION -> {
var pos = getPointPositionOnScreen(event.getX(), event.getY());
int line = IntPair.getFirst(pos), column = IntPair.getSecond(pos);
touchHandler.draggingSelection = getText().getIndexer().getCharPosition(line, column);
Expand All @@ -4648,7 +4652,9 @@ public boolean onDragEvent(DragEvent event) {
return false;
}
touchHandler.draggingSelection = null;
text.insert(targetPos.line, targetPos.column, ClipDataUtils.clipDataToString(event.getClipData()));
setSelection(targetPos.line, targetPos.column);
pasteText(ClipDataUtils.clipDataToString(event.getClipData()));
requestFocus();
postInvalidate();
// Call super for notifying listeners
super.onDragEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import io.github.rosemoe.sora.text.Content;
import io.github.rosemoe.sora.text.Cursor;
import io.github.rosemoe.sora.util.Logger;
import io.github.rosemoe.sora.widget.component.EditorAutoCompletion;

/**
* Connection between input method and editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ protected void drawRows(Canvas canvas, float offset, LongArrayList postDrawLineN
}
} else if (cursor.getLeftLine() == line && isInside(cursor.getLeftColumn(), rowInf.startColumn, rowInf.endColumn, line)) {
float centerX = editor.measureTextRegionOffset() + layout.getCharLayoutOffset(cursor.getLeftLine(), cursor.getLeftColumn())[1] - editor.getOffsetX();
postDrawCursor.add(new DrawCursorTask(centerX, getRowBottomForBackground(row) - editor.getOffsetY(), editor.getEventHandler().shouldDrawInsertHandle() ? SelectionHandleStyle.HANDLE_TYPE_INSERT : SelectionHandleStyle.HANDLE_TYPE_UNDEFINED, editor.getInsertHandleDescriptor()));
postDrawCursor.add(new DrawCursorTask(centerX, getRowBottomForBackground(row) - editor.getOffsetY(), SelectionHandleStyle.HANDLE_TYPE_INSERT, editor.getInsertHandleDescriptor()));
}
// Draw dragging selection or selecting target
if (draggingSelection != null) {
Expand Down Expand Up @@ -2851,8 +2851,8 @@ protected void execute(Canvas canvas) {
paintGeneral.setPathEffect(null);
}
var handleType = this.handleType;
// Hide insert handle in long select mode
if (handleType == SelectionHandleStyle.HANDLE_TYPE_INSERT && editor.isInLongSelect()) {
// Hide insert handle conditionally
if (handleType == SelectionHandleStyle.HANDLE_TYPE_INSERT && (editor.isInLongSelect() || !editor.getEventHandler().shouldDrawInsertHandle())) {
handleType = SelectionHandleStyle.HANDLE_TYPE_UNDEFINED;
}
if (handleType != SelectionHandleStyle.HANDLE_TYPE_UNDEFINED && !editor.isInMouseMode() /* hide if mouse inside */) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public final class EditorTouchEventHandler implements GestureDetector.OnGestureL
*/
public EditorTouchEventHandler(@NonNull CodeEditor editor) {
this.editor = editor;
edgeFieldSize = editor.getDpUnit() * 18;
scroller = new EditorScroller(editor);
scaleMaxSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 26, Resources.getSystem().getDisplayMetrics());
scaleMinSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, Resources.getSystem().getDisplayMetrics());
Expand Down Expand Up @@ -318,9 +319,6 @@ public void dismissMagnifier() {
* @return Whether this touch event is handled by this class
*/
public boolean onTouchEvent(MotionEvent e) {
if (edgeFieldSize == 0) {
edgeFieldSize = editor.getDpUnit() * 18;
}
motionY = e.getY();
motionX = e.getX();
switch (e.getAction()) {
Expand Down Expand Up @@ -653,20 +651,23 @@ public void scrollIfReachesEdge(@Nullable MotionEvent e, float x, float y) {
y = e.getY();
}
int flag = computeEdgeFlags(x, y);
int initialDelta = (int) (8 * editor.getDpUnit());
if (flag != 0 && edgeFlags == 0) {
if (flag != 0) {
var oldFlags = edgeFlags;
edgeFlags = flag;
thumbMotionRecord = e == null ? null : MotionEvent.obtain(e);
editor.postInLifecycle(new EdgeScrollRunnable(initialDelta));
} else if (flag == 0) {
stopEdgeScroll();
if (oldFlags == 0) {
int initialDelta = (int) (8 * editor.getDpUnit());
editor.postInLifecycle(new EdgeScrollRunnable(initialDelta));
}
} else {
edgeFlags = flag;
thumbMotionRecord = e == null ? null : MotionEvent.obtain(e);
stopEdgeScroll();
}
}

private boolean isSameSign(int a, int b) {
private boolean isSameSign(float a, float b) {
if (Math.abs(a) < 1e5 || Math.abs(b) < 1e5) {
return false;
}
return (a < 0 && b < 0) || (a > 0 && b > 0);
}

Expand Down Expand Up @@ -1086,11 +1087,11 @@ private class EdgeScrollRunnable implements Runnable {
private final static int MAX_FACTOR = 32;
private final static float INCREASE_FACTOR = 1.06f;

private final int initialDelta;
private int deltaHorizontal;
private int deltaVertical;
private int lastDx, lastDy;
private int factorX, factorY;
private final float initialDelta;
private float deltaHorizontal;
private float deltaVertical;
private float lastDx, lastDy;
private float factorX, factorY;
private long postTimes;

public EdgeScrollRunnable(int initDelta) {
Expand All @@ -1100,8 +1101,8 @@ public EdgeScrollRunnable(int initDelta) {

@Override
public void run() {
int dx = (((edgeFlags & LEFT_EDGE) != 0) ? -deltaHorizontal : 0) + (((edgeFlags & RIGHT_EDGE) != 0) ? deltaHorizontal : 0);
int dy = (((edgeFlags & TOP_EDGE) != 0) ? -deltaVertical : 0) + (((edgeFlags & BOTTOM_EDGE) != 0) ? deltaVertical : 0);
float dx = (((edgeFlags & LEFT_EDGE) != 0) ? -deltaHorizontal : 0) + (((edgeFlags & RIGHT_EDGE) != 0) ? deltaHorizontal : 0);
float dy = (((edgeFlags & TOP_EDGE) != 0) ? -deltaVertical : 0) + (((edgeFlags & BOTTOM_EDGE) != 0) ? deltaVertical : 0);
if (dx > 0) {
// Check whether there is content at right
int line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import io.github.rosemoe.sora.util.IntPair
import io.github.rosemoe.sora.widget.CodeEditor
import io.github.rosemoe.sora.widget.IN_BOUND
import io.github.rosemoe.sora.widget.REGION_TEXT
import io.github.rosemoe.sora.widget.component.EditorBuiltinComponent
import io.github.rosemoe.sora.widget.resolveTouchRegion

/**
Expand Down

0 comments on commit 9d025be

Please sign in to comment.