Skip to content

Commit b746feb

Browse files
jreckAndroid (Google) Code Review
authored andcommitted
Merge "Center text selection point to avoid rounding glitches" into jb-mr1-dev
2 parents fdb6be8 + 9fcef3d commit b746feb

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

core/java/android/webkit/WebViewClassic.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5185,7 +5185,7 @@ private void showPasteWindow() {
51855185
if (cm.hasPrimaryClip()) {
51865186
Point cursorPoint = new Point(contentToViewX(mSelectCursorBase.x),
51875187
contentToViewY(mSelectCursorBase.y));
5188-
Point cursorTop = calculateCaretTop();
5188+
Point cursorTop = calculateBaseCaretTop();
51895189
cursorTop.set(contentToViewX(cursorTop.x),
51905190
contentToViewY(cursorTop.y));
51915191

@@ -5229,17 +5229,22 @@ private static float scaleAlongSegment(int x, int y, PointF a, PointF b) {
52295229
return scale;
52305230
}
52315231

5232+
private Point calculateBaseCaretTop() {
5233+
return calculateCaretTop(mSelectCursorBase, mSelectCursorBaseTextQuad);
5234+
}
5235+
5236+
private Point calculateDraggingCaretTop() {
5237+
return calculateCaretTop(mSelectDraggingCursor, mSelectDraggingTextQuad);
5238+
}
5239+
52325240
/**
52335241
* Assuming arbitrary shape of a quadralateral forming text bounds, this
52345242
* calculates the top of a caret.
52355243
*/
5236-
private Point calculateCaretTop() {
5237-
float scale = scaleAlongSegment(mSelectCursorBase.x, mSelectCursorBase.y,
5238-
mSelectCursorBaseTextQuad.p4, mSelectCursorBaseTextQuad.p3);
5239-
int x = Math.round(scaleCoordinate(scale,
5240-
mSelectCursorBaseTextQuad.p1.x, mSelectCursorBaseTextQuad.p2.x));
5241-
int y = Math.round(scaleCoordinate(scale,
5242-
mSelectCursorBaseTextQuad.p1.y, mSelectCursorBaseTextQuad.p2.y));
5244+
private static Point calculateCaretTop(Point base, QuadF quad) {
5245+
float scale = scaleAlongSegment(base.x, base.y, quad.p4, quad.p3);
5246+
int x = Math.round(scaleCoordinate(scale, quad.p1.x, quad.p2.x));
5247+
int y = Math.round(scaleCoordinate(scale, quad.p1.y, quad.p2.y));
52435248
return new Point(x, y);
52445249
}
52455250

@@ -5269,12 +5274,20 @@ private boolean setupWebkitSelect() {
52695274
return true;
52705275
}
52715276

5272-
private void updateWebkitSelection() {
5277+
private void updateWebkitSelection(boolean isSnapped) {
52735278
int handleId = (mSelectDraggingCursor == mSelectCursorBase)
52745279
? HANDLE_ID_BASE : HANDLE_ID_EXTENT;
5280+
int x = mSelectDraggingCursor.x;
5281+
int y = mSelectDraggingCursor.y;
5282+
if (isSnapped) {
5283+
// "center" the cursor in the snapping quad
5284+
Point top = calculateDraggingCaretTop();
5285+
x = Math.round((top.x + x) / 2);
5286+
y = Math.round((top.y + y) / 2);
5287+
}
52755288
mWebViewCore.removeMessages(EventHub.SELECT_TEXT);
52765289
mWebViewCore.sendMessageAtFrontOfQueue(EventHub.SELECT_TEXT,
5277-
mSelectDraggingCursor.x, mSelectDraggingCursor.y, (Integer)handleId);
5290+
x, y, (Integer)handleId);
52785291
}
52795292

52805293
private void resetCaretTimer() {
@@ -5616,7 +5629,7 @@ private void scrollEditIntoView() {
56165629
Math.max(0, mEditTextContentBounds.top - buffer),
56175630
mEditTextContentBounds.right + buffer,
56185631
mEditTextContentBounds.bottom + buffer);
5619-
Point caretTop = calculateCaretTop();
5632+
Point caretTop = calculateBaseCaretTop();
56205633
if (visibleRect.width() < mEditTextContentBounds.width()) {
56215634
// The whole edit won't fit in the width, so use the caret rect
56225635
if (mSelectCursorBase.x < caretTop.x) {
@@ -5947,10 +5960,12 @@ private void handleTouchEventCommon(MotionEvent event, int action, int x, int y)
59475960
} else {
59485961
endScrollEdit();
59495962
}
5963+
boolean snapped = false;
59505964
if (inCursorText || (mIsEditingText && !inEditBounds)) {
59515965
snapDraggingCursor();
5966+
snapped = true;
59525967
}
5953-
updateWebkitSelection();
5968+
updateWebkitSelection(snapped);
59545969
if (!inCursorText && mIsEditingText && inEditBounds) {
59555970
// Visually snap even if we have moved the handle.
59565971
snapDraggingCursor();
@@ -6277,7 +6292,7 @@ private void scrollEditWithCursor() {
62776292
int oldX = mSelectDraggingCursor.x;
62786293
int oldY = mSelectDraggingCursor.y;
62796294
mSelectDraggingCursor.set(selectionX, selectionY);
6280-
updateWebkitSelection();
6295+
updateWebkitSelection(false);
62816296
scrollEditText(scrollX, scrollY);
62826297
mSelectDraggingCursor.set(oldX, oldY);
62836298
}

0 commit comments

Comments
 (0)