Skip to content

Commit 98a85c2

Browse files
Gilles DebunneAndroid (Google) Code Review
authored andcommitted
Merge "Invalidated bounds tightened in TextView" into ics-mr1
2 parents 58ad16c + 8615ac9 commit 98a85c2

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

core/java/android/widget/SpellChecker.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ public void onGetSuggestions(SuggestionsInfo[] results) {
274274
((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
275275

276276
SpellCheckSpan spellCheckSpan = mSpellCheckSpans[j];
277+
277278
if (!isInDictionary && looksLikeTypo) {
278279
createMisspelledSuggestionSpan(editable, suggestionsInfo, spellCheckSpan);
279280
}
281+
280282
editable.removeSpan(spellCheckSpan);
281283
break;
282284
}
@@ -292,20 +294,21 @@ public void onGetSuggestions(SuggestionsInfo[] results) {
292294
}
293295
}
294296

295-
private void createMisspelledSuggestionSpan(Editable editable,
296-
SuggestionsInfo suggestionsInfo, SpellCheckSpan spellCheckSpan) {
297+
private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
298+
SpellCheckSpan spellCheckSpan) {
297299
final int start = editable.getSpanStart(spellCheckSpan);
298300
final int end = editable.getSpanEnd(spellCheckSpan);
299-
if (start < 0 || end < 0) return; // span was removed in the meantime
301+
if (start < 0 || end <= start) return; // span was removed in the meantime
300302

301303
// Other suggestion spans may exist on that region, with identical suggestions, filter
302-
// them out to avoid duplicates. First, filter suggestion spans on that exact region.
304+
// them out to avoid duplicates.
303305
SuggestionSpan[] suggestionSpans = editable.getSpans(start, end, SuggestionSpan.class);
304306
final int length = suggestionSpans.length;
305307
for (int i = 0; i < length; i++) {
306308
final int spanStart = editable.getSpanStart(suggestionSpans[i]);
307309
final int spanEnd = editable.getSpanEnd(suggestionSpans[i]);
308310
if (spanStart != start || spanEnd != end) {
311+
// Nulled (to avoid new array allocation) if not on that exact same region
309312
suggestionSpans[i] = null;
310313
}
311314
}
@@ -353,8 +356,7 @@ private void createMisspelledSuggestionSpan(Editable editable,
353356
SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
354357
editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
355358

356-
// TODO limit to the word rectangle region
357-
mTextView.invalidate();
359+
mTextView.invalidateRegion(start, end);
358360
}
359361

360362
private class SpellParser {

core/java/android/widget/TextView.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4323,52 +4323,61 @@ private void invalidateCursor() {
43234323
}
43244324

43254325
private void invalidateCursor(int a, int b, int c) {
4326+
if (a >= 0 || b >= 0 || c >= 0) {
4327+
int start = Math.min(Math.min(a, b), c);
4328+
int end = Math.max(Math.max(a, b), c);
4329+
invalidateRegion(start, end);
4330+
}
4331+
}
4332+
4333+
/**
4334+
* Invalidates the region of text enclosed between the start and end text offsets.
4335+
*
4336+
* @hide
4337+
*/
4338+
void invalidateRegion(int start, int end) {
43264339
if (mLayout == null) {
43274340
invalidate();
43284341
} else {
4329-
if (a >= 0 || b >= 0 || c >= 0) {
4330-
int first = Math.min(Math.min(a, b), c);
4331-
int last = Math.max(Math.max(a, b), c);
4332-
4333-
int line = mLayout.getLineForOffset(first);
4334-
int top = mLayout.getLineTop(line);
4342+
int lineStart = mLayout.getLineForOffset(start);
4343+
int top = mLayout.getLineTop(lineStart);
43354344

43364345
// This is ridiculous, but the descent from the line above
43374346
// can hang down into the line we really want to redraw,
43384347
// so we have to invalidate part of the line above to make
43394348
// sure everything that needs to be redrawn really is.
43404349
// (But not the whole line above, because that would cause
43414350
// the same problem with the descenders on the line above it!)
4342-
if (line > 0) {
4343-
top -= mLayout.getLineDescent(line - 1);
4351+
if (lineStart > 0) {
4352+
top -= mLayout.getLineDescent(lineStart - 1);
43444353
}
43454354

4346-
int line2;
4355+
int lineEnd;
43474356

4348-
if (first == last)
4349-
line2 = line;
4357+
if (start == end)
4358+
lineEnd = lineStart;
43504359
else
4351-
line2 = mLayout.getLineForOffset(last);
4360+
lineEnd = mLayout.getLineForOffset(end);
43524361

4353-
int bottom = mLayout.getLineTop(line2 + 1);
4362+
int bottom = mLayout.getLineBottom(lineEnd);
43544363

4355-
final int horizontalPadding = getCompoundPaddingLeft();
4364+
final int compoundPaddingLeft = getCompoundPaddingLeft();
43564365
final int verticalPadding = getExtendedPaddingTop() + getVerticalOffset(true);
4357-
4358-
// If used, the cursor drawables can have an arbitrary dimension that can go beyond
4359-
// the invalidated lines specified above.
4360-
for (int i = 0; i < mCursorCount; i++) {
4361-
Rect bounds = mCursorDrawable[i].getBounds();
4362-
top = Math.min(top, bounds.top);
4363-
bottom = Math.max(bottom, bounds.bottom);
4364-
// Horizontal bounds are already full width, no need to update
4366+
4367+
int left, right;
4368+
if (lineStart == lineEnd) {
4369+
left = (int) mLayout.getPrimaryHorizontal(start);
4370+
right = (int) (mLayout.getPrimaryHorizontal(end) + 1.0);
4371+
left += compoundPaddingLeft;
4372+
right += compoundPaddingLeft;
4373+
} else {
4374+
// Rectangle bounding box when the region spans several lines
4375+
left = compoundPaddingLeft;
4376+
right = getWidth() - getCompoundPaddingRight();
43654377
}
43664378

4367-
invalidate(horizontalPadding + mScrollX, top + verticalPadding,
4368-
horizontalPadding + mScrollX + getWidth() -
4369-
getCompoundPaddingLeft() - getCompoundPaddingRight(),
4370-
bottom + verticalPadding);
4371-
}
4379+
invalidate(mScrollX + left, verticalPadding + top,
4380+
mScrollX + right, verticalPadding + bottom);
43724381
}
43734382
}
43744383

@@ -5901,10 +5910,10 @@ public void draw(Canvas canvas, int cursorOffsetVertical) {
59015910
if (cursorOffsetVertical != 0) {
59025911
canvas.translate(0, -cursorOffsetVertical);
59035912
}
5904-
invalidate(true);
5913+
invalidate(true); // TODO invalidate cursor region only
59055914
} else {
59065915
stopAnimation();
5907-
invalidate(false);
5916+
invalidate(false); // TODO invalidate cursor region only
59085917
}
59095918
}
59105919

@@ -7724,10 +7733,8 @@ void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd
77247733
onSelectionChanged(newSelStart, newSelEnd);
77257734
}
77267735
}
7727-
7728-
if (what instanceof UpdateAppearance || what instanceof ParagraphStyle
7729-
|| (what instanceof SuggestionSpan && (((SuggestionSpan)what).getFlags()
7730-
& SuggestionSpan.FLAG_AUTO_CORRECTION) != 0)) {
7736+
7737+
if (what instanceof UpdateAppearance || what instanceof ParagraphStyle) {
77317738
if (ims == null || ims.mBatchEditNesting == 0) {
77327739
invalidate();
77337740
mHighlightPathBogus = true;

0 commit comments

Comments
 (0)