From 35842cf4a626ae6d57aadf62a7ace937cb4d18d0 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Tue, 9 Jun 2020 10:59:59 +0200 Subject: [PATCH] Set orientation of HandleView in show (#1477) * Place long press menu above selection Previously, the long press menu would cover the first line of the selection. * Flip selection handle at different positions depending on drag direction When the selection handle changes direction, the selection jumps to the new point of the handle. When the handle changes direction at the same place when you come from the left as from the right, that makes it impossible to select the characters which are at the position where it changes direction. With this change the handle remains pointing towards the edge further into the line when you drag it from the edge and against the center. * Set orientation of HandleView when showing it When you hold down on a word that starts or ends at the edge of the screen, the handle will appear outside of the screen. This happens because the orientation was only switched when the handle is dragged, so when it is shown it just used the same orientation as it had for the last selection. Relates to #334, but not sure if it fixes it completely. --- .../java/com/termux/view/TerminalView.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index cc3df20e..702bd2a4 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -973,12 +973,12 @@ public final class TerminalView extends View { return mContainer.isShowing(); } - private void checkChangedOrientation() { - if (!mIsDragging) { + private void checkChangedOrientation(int posX, boolean force) { + if (!mIsDragging && !force) { return; } long millis = SystemClock.currentThreadTimeMillis(); - if (millis - mLastTime < 50) { + if (millis - mLastTime < 50 && !force) { return; } mLastTime = millis; @@ -1003,10 +1003,7 @@ public final class TerminalView extends View { return; } - final int[] coords = mTempCoords; - hostView.getLocationInWindow(coords); - final int posX = coords[0] + mPointX; - if (posX < clip.left) { + if (posX - mHandleWidth < clip.left) { changeOrientation(RIGHT); } else if (posX + mHandleWidth > clip.right) { changeOrientation(LEFT); @@ -1050,13 +1047,14 @@ public final class TerminalView extends View { posY >= clip.top && posY <= clip.bottom; } - private void moveTo(int x, int y) { - mPointX = x; + private void moveTo(int x, int y, boolean forceOrientationCheck) { + float oldHotspotX = mHotspotX; + checkChangedOrientation(x, forceOrientationCheck); + mPointX = (int) (x - (isShowing() ? oldHotspotX : mHotspotX)); mPointY = y; - checkChangedOrientation(); if (isPositionVisible()) { int[] coords = null; - if (mContainer.isShowing()) { + if (isShowing()) { coords = mTempCoords; TerminalView.this.getLocationInWindow(coords); int x1 = coords[0] + mPointX; @@ -1138,10 +1136,10 @@ public final class TerminalView extends View { return mIsDragging; } - void positionAtCursor(final int cx, final int cy) { - int left = (int) (getPointX(cx) - mHotspotX); + void positionAtCursor(final int cx, final int cy, boolean forceOrientationCheck) { + int left = getPointX(cx); int bottom = getPointY(cy + 1); - moveTo(left, bottom); + moveTo(left, bottom, forceOrientationCheck); } } @@ -1162,9 +1160,8 @@ public final class TerminalView extends View { public void show() { mIsShowing = true; - updatePosition(); - mStartHandle.show(); - mEndHandle.show(); + mStartHandle.positionAtCursor(mSelX1, mSelY1, true); + mEndHandle.positionAtCursor(mSelX2 + 1, mSelY2, true); final ActionMode.Callback callback = new ActionMode.Callback() { @Override @@ -1240,7 +1237,7 @@ public final class TerminalView extends View { public void onGetContentRect(ActionMode mode, View view, Rect outRect) { int x1 = Math.round(mSelX1 * mRenderer.mFontWidth); int x2 = Math.round(mSelX2 * mRenderer.mFontWidth); - int y1 = Math.round((mSelY1 - mTopRow) * mRenderer.mFontLineSpacing); + int y1 = Math.round((mSelY1 - 1 - mTopRow) * mRenderer.mFontLineSpacing); int y2 = Math.round((mSelY2 + 1 - mTopRow) * mRenderer.mFontLineSpacing); @@ -1395,9 +1392,9 @@ public final class TerminalView extends View { return; } - mStartHandle.positionAtCursor(mSelX1, mSelY1); + mStartHandle.positionAtCursor(mSelX1, mSelY1, false); - mEndHandle.positionAtCursor(mSelX2 + 1, mSelY2); //bug + mEndHandle.positionAtCursor(mSelX2 + 1, mSelY2, false); if (mActionMode != null) { mActionMode.invalidate();