Skip to content

Commit edb39ce

Browse files
author
Michael Kolb
committed
Draw input field focus ring in WebTextView
Bug: 5628053 Change-Id: I1a3d5402e7925e71877be2c278e95f25e75746f3
1 parent de8c5ec commit edb39ce

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

core/java/android/webkit/WebTextView.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package android.webkit;
1818

1919
import android.content.Context;
20+
import android.graphics.Canvas;
2021
import android.graphics.Color;
2122
import android.graphics.Paint;
23+
import android.graphics.Paint.Style;
2224
import android.graphics.Rect;
2325
import android.graphics.drawable.ColorDrawable;
2426
import android.os.Bundle;
@@ -49,6 +51,7 @@
4951
import android.view.inputmethod.EditorInfo;
5052
import android.view.inputmethod.InputConnection;
5153
import android.view.inputmethod.InputMethodManager;
54+
import android.widget.AbsoluteLayout;
5255
import android.widget.AbsoluteLayout.LayoutParams;
5356
import android.widget.AdapterView;
5457
import android.widget.ArrayAdapter;
@@ -71,6 +74,9 @@
7174

7275
static final String LOGTAG = "webtextview";
7376

77+
private Paint mRingPaint;
78+
private int mRingInset;
79+
7480
private WebView mWebView;
7581
private boolean mSingle;
7682
private int mWidthSpec;
@@ -201,7 +207,13 @@ public void handleMessage(Message msg) {
201207
}
202208
}
203209
};
210+
float ringWidth = 4f * context.getResources().getDisplayMetrics().density;
204211
mReceiver = new MyResultReceiver(mHandler);
212+
mRingPaint = new Paint();
213+
mRingPaint.setColor(0x6633b5e5);
214+
mRingPaint.setStrokeWidth(ringWidth);
215+
mRingPaint.setStyle(Style.FILL);
216+
mRingInset = (int) ringWidth;
205217
}
206218

207219
public void setAutoFillable(int queryId) {
@@ -210,6 +222,40 @@ public void setAutoFillable(int queryId) {
210222
mQueryId = queryId;
211223
}
212224

225+
@Override
226+
protected void onDraw(Canvas canvas) {
227+
super.onDraw(canvas);
228+
if (isFocused()) {
229+
final int ib = getHeight() - mRingInset;
230+
canvas.drawRect(0, 0, getWidth(), mRingInset, mRingPaint);
231+
canvas.drawRect(0, ib, getWidth(), getHeight(), mRingPaint);
232+
canvas.drawRect(0, mRingInset, mRingInset, ib, mRingPaint);
233+
canvas.drawRect(getWidth() - mRingInset, mRingInset, getWidth(), ib, mRingPaint);
234+
}
235+
}
236+
237+
private void growOrShrink(boolean grow) {
238+
AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) getLayoutParams();
239+
if (grow) {
240+
Log.i("webtextview", "grow");
241+
lp.x -= mRingInset;
242+
lp.y -= mRingInset;
243+
lp.width += 2 * mRingInset;
244+
lp.height += 2 * mRingInset;
245+
setPadding(getPaddingLeft() + mRingInset, getPaddingTop() + mRingInset,
246+
getPaddingRight() + mRingInset, getPaddingBottom() + mRingInset);
247+
} else {
248+
Log.i("webtextview", "shrink");
249+
lp.x += mRingInset;
250+
lp.y += mRingInset;
251+
lp.width -= 2 * mRingInset;
252+
lp.height -= 2 * mRingInset;
253+
setPadding(getPaddingLeft() - mRingInset, getPaddingTop() - mRingInset,
254+
getPaddingRight() - mRingInset, getPaddingBottom() - mRingInset);
255+
}
256+
setLayoutParams(lp);
257+
}
258+
213259
@Override
214260
public boolean dispatchKeyEvent(KeyEvent event) {
215261
if (event.isSystem()) {
@@ -511,6 +557,7 @@ protected void onFocusChanged(boolean focused, int direction,
511557
} else if (!mInsideRemove) {
512558
mWebView.setActive(false);
513559
}
560+
growOrShrink(focused);
514561
mFromFocusChange = false;
515562
}
516563

core/java/android/webkit/WebView.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5658,13 +5658,13 @@ void setActive(boolean active) {
56585658
if (hasFocus()) {
56595659
// If our window regained focus, and we have focus, then begin
56605660
// drawing the cursor ring
5661-
mDrawCursorRing = true;
5661+
mDrawCursorRing = !inEditingMode();
56625662
setFocusControllerActive(true);
56635663
} else {
5664+
mDrawCursorRing = false;
56645665
if (!inEditingMode()) {
56655666
// If our window gained focus, but we do not have it, do not
56665667
// draw the cursor ring.
5667-
mDrawCursorRing = false;
56685668
setFocusControllerActive(false);
56695669
}
56705670
// We do not call recordButtons here because we assume
@@ -5739,7 +5739,7 @@ protected void onFocusChanged(boolean focused, int direction,
57395739
// When we regain focus, if we have window focus, resume drawing
57405740
// the cursor ring
57415741
if (hasWindowFocus()) {
5742-
mDrawCursorRing = true;
5742+
mDrawCursorRing = !inEditingMode();
57435743
setFocusControllerActive(true);
57445744
//} else {
57455745
// The WebView has gained focus while we do not have
@@ -5749,8 +5749,8 @@ protected void onFocusChanged(boolean focused, int direction,
57495749
} else {
57505750
// When we lost focus, unless focus went to the TextView (which is
57515751
// true if we are in editing mode), stop drawing the cursor ring.
5752+
mDrawCursorRing = false;
57525753
if (!inEditingMode()) {
5753-
mDrawCursorRing = false;
57545754
setFocusControllerActive(false);
57555755
}
57565756
mKeysPressed.clear();

0 commit comments

Comments
 (0)