summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mihai Popa <popam@google.com> 2018-03-07 14:52:05 +0000
committer Mihai Popa <popam@google.com> 2018-03-09 16:04:31 +0000
commit27e4dfbc2c2237d4526405b2f8aa739a9a3d1bbf (patch)
tree80197a3c2ce4f2c841fa0b7541a4787d3b4e111f
parent6d7cbf5e7bf47e0e35201db83a36dd4ba0106e41 (diff)
[Magnifier-32] Do not magnify outside selection
Currently, for selection, if we move one handle towards the other, the moving handle will stop when the selection becomes 1 character long. However, the magnifier would continue to follow the finger after this, no longer being helpful for the selection. This CL fixes this, by replicating what happens when the magnifier reaches the end of the line also for the case when it reaches the other handle. Bug: 72314536 Test: manual testing (both English and Arabic) Test: atest FrameworksCoreTests:android.widget.TextViewActivityTest Test: atest CtsWidgetTestCases:android.widget.cts.TextViewTest Change-Id: I5bde622421c7fb8ecce0ea00f0d8d2af7aa72cf4 (cherry picked from commit 2d6b40b82151aec9ff13478cd5fb58f4ab3fcb7a) Merged-In: I5bde622421c7fb8ecce0ea00f0d8d2af7aa72cf4
-rw-r--r--core/java/android/widget/Editor.java45
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)