diff options
| author | 2018-03-12 11:46:06 +0000 | |
|---|---|---|
| committer | 2018-03-12 11:46:06 +0000 | |
| commit | 8a06a9f7dd52a9e7ab1ae8370993e75d0c16633f (patch) | |
| tree | e0367f6b407c7f7313bc354b96287f9640091982 | |
| parent | 4b306e5e23b2554b61329e8e0f41bb2dfa97d4a5 (diff) | |
| parent | 27e4dfbc2c2237d4526405b2f8aa739a9a3d1bbf (diff) | |
Merge "[Magnifier-32] Do not magnify outside selection" into pi-dev
| -rw-r--r-- | core/java/android/widget/Editor.java | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 27dd39b3224f..b1410f17c0ed 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -4660,16 +4660,23 @@ public class Editor { final int trigger = getMagnifierHandleTrigger(); final int offset; + final int otherHandleOffset; switch (trigger) { - case MagnifierHandleTrigger.INSERTION: // Fall through. + case MagnifierHandleTrigger.INSERTION: + offset = mTextView.getSelectionStart(); + otherHandleOffset = -1; + break; case MagnifierHandleTrigger.SELECTION_START: offset = mTextView.getSelectionStart(); + otherHandleOffset = mTextView.getSelectionEnd(); break; case MagnifierHandleTrigger.SELECTION_END: offset = mTextView.getSelectionEnd(); + otherHandleOffset = mTextView.getSelectionStart(); break; default: offset = -1; + otherHandleOffset = -1; break; } @@ -4679,22 +4686,38 @@ public class Editor { final Layout layout = mTextView.getLayout(); final int lineNumber = layout.getLineForOffset(offset); - - // Horizontally move the magnifier smoothly but clamp inside the current line. + // Compute whether the selection handles are currently on the same line, and, + // in this particular case, whether the selected text is right to left. + final boolean sameLineSelection = otherHandleOffset != -1 + && lineNumber == layout.getLineForOffset(otherHandleOffset); + final boolean rtl = sameLineSelection + && (offset < otherHandleOffset) + != (getHorizontal(mTextView.getLayout(), offset) + < getHorizontal(mTextView.getLayout(), otherHandleOffset)); + + // Horizontally move the magnifier smoothly, clamp inside the current line / selection. final int[] textViewLocationOnScreen = new int[2]; mTextView.getLocationOnScreen(textViewLocationOnScreen); final float touchXInView = event.getRawX() - textViewLocationOnScreen[0]; - final float lineLeft = mTextView.getLayout().getLineLeft(lineNumber) - + mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); - final float lineRight = mTextView.getLayout().getLineRight(lineNumber) - + mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); + float leftBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); + float rightBound = mTextView.getTotalPaddingLeft() - mTextView.getScrollX(); + if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_END) ^ rtl)) { + leftBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); + } else { + leftBound += mTextView.getLayout().getLineLeft(lineNumber); + } + if (sameLineSelection && ((trigger == MagnifierHandleTrigger.SELECTION_START) ^ rtl)) { + rightBound += getHorizontal(mTextView.getLayout(), otherHandleOffset); + } else { + rightBound += mTextView.getLayout().getLineRight(lineNumber); + } final float contentWidth = Math.round(mMagnifier.getWidth() / mMagnifier.getZoom()); - if (touchXInView < lineLeft - contentWidth / 2 - || touchXInView > lineRight + contentWidth / 2) { - // The touch is too out of the bounds of the current line, so hide the magnifier. + if (touchXInView < leftBound - contentWidth / 2 + || touchXInView > rightBound + contentWidth / 2) { + // The touch is too far from the current line / selection, so hide the magnifier. return false; } - showPosInView.x = Math.max(lineLeft, Math.min(lineRight, touchXInView)); + showPosInView.x = Math.max(leftBound, Math.min(rightBound, touchXInView)); // Vertically snap to middle of current line. showPosInView.y = (mTextView.getLayout().getLineTop(lineNumber) |