Skip to content

Commit

Permalink
Format files with google-java-format -a; no functional changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansche committed Sep 4, 2016
1 parent 243e15e commit 2c05af8
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public class CandidateView extends View {
private int mTouchX = OUT_OF_BOUNDS;
private Drawable mSelectionHighlight;
private boolean mTypedWordValid;

private Rect mBgPadding;

private static final int MAX_SUGGESTIONS = 32;
private static final int SCROLL_PIXELS = 20;

private int[] mWordWidth = new int[MAX_SUGGESTIONS];
private int[] mWordX = new int[MAX_SUGGESTIONS];

private static final int X_GAP = 10;

private static final List<String> EMPTY_LIST = new ArrayList<String>();

private int mColorNormal;
Expand All @@ -59,9 +59,9 @@ public class CandidateView extends View {
private Paint mPaint;
private boolean mScrolled;
private int mTargetScrollX;

private int mTotalWidth;

private GestureDetector mGestureDetector;

/**
Expand All @@ -71,63 +71,69 @@ public class CandidateView extends View {
*/
public CandidateView(Context context) {
super(context);
mSelectionHighlight = context.getResources().getDrawable(
android.R.drawable.list_selector_background);
mSelectionHighlight.setState(new int[] {
android.R.attr.state_enabled,
android.R.attr.state_focused,
android.R.attr.state_window_focused,
android.R.attr.state_pressed
});
mSelectionHighlight =
context.getResources().getDrawable(android.R.drawable.list_selector_background);
mSelectionHighlight.setState(
new int[] {
android.R.attr.state_enabled,
android.R.attr.state_focused,
android.R.attr.state_window_focused,
android.R.attr.state_pressed
});

Resources r = context.getResources();

setBackgroundColor(r.getColor(R.color.candidate_background));

mColorNormal = r.getColor(R.color.candidate_normal);
mColorRecommended = r.getColor(R.color.candidate_recommended);
mColorOther = r.getColor(R.color.candidate_other);
mVerticalPadding = r.getDimensionPixelSize(R.dimen.candidate_vertical_padding);

mPaint = new Paint();
mPaint.setColor(mColorNormal);
mPaint.setAntiAlias(true);
mPaint.setTextSize(r.getDimensionPixelSize(R.dimen.candidate_font_height));
mPaint.setStrokeWidth(0);

mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
mScrolled = true;
int sx = getScrollX();
sx += distanceX;
if (sx < 0) {
sx = 0;
}
if (sx + getWidth() > mTotalWidth) {
sx -= distanceX;
}
mTargetScrollX = sx;
scrollTo(sx, getScrollY());
invalidate();
return true;
}
});

mGestureDetector =
new GestureDetector(
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(
MotionEvent e1,
MotionEvent e2,
float distanceX,
float distanceY) {
mScrolled = true;
int sx = getScrollX();
sx += distanceX;
if (sx < 0) {
sx = 0;
}
if (sx + getWidth() > mTotalWidth) {
sx -= distanceX;
}
mTargetScrollX = sx;
scrollTo(sx, getScrollY());
invalidate();
return true;
}
});
setHorizontalFadingEdgeEnabled(true);
setWillNotDraw(false);
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
}

/**
* A connection back to the service to communicate with the text field
* @param listener
*/
public void setService(SoftKeyboard listener) {
mService = listener;
}

@Override
public int computeHorizontalScrollRange() {
return mTotalWidth;
Expand All @@ -136,17 +142,16 @@ public int computeHorizontalScrollRange() {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredWidth = resolveSize(50, widthMeasureSpec);

// Get the desired height of the icon menu view (last row of items does
// not have a divider below)
Rect padding = new Rect();
mSelectionHighlight.getPadding(padding);
final int desiredHeight = ((int)mPaint.getTextSize()) + mVerticalPadding
+ padding.top + padding.bottom;
final int desiredHeight =
((int) mPaint.getTextSize()) + mVerticalPadding + padding.top + padding.bottom;

// Maximum possible width and desired height
setMeasuredDimension(measuredWidth,
resolveSize(desiredHeight, heightMeasureSpec));
setMeasuredDimension(measuredWidth, resolveSize(desiredHeight, heightMeasureSpec));
}

/**
Expand All @@ -160,15 +165,15 @@ protected void onDraw(Canvas canvas) {
}
mTotalWidth = 0;
if (mSuggestions == null) return;

if (mBgPadding == null) {
mBgPadding = new Rect(0, 0, 0, 0);
if (getBackground() != null) {
getBackground().getPadding(mBgPadding);
}
}
int x = 0;
final int count = mSuggestions.size();
final int count = mSuggestions.size();
final int height = getHeight();
final Rect bgPadding = mBgPadding;
final Paint paint = mPaint;
Expand Down Expand Up @@ -204,9 +209,13 @@ protected void onDraw(Canvas canvas) {
paint.setColor(mColorOther);
}
canvas.drawText(suggestion, x + X_GAP, y, paint);
paint.setColor(mColorOther);
canvas.drawLine(x + wordWidth + 0.5f, bgPadding.top,
x + wordWidth + 0.5f, height + 1, paint);
paint.setColor(mColorOther);
canvas.drawLine(
x + wordWidth + 0.5f,
bgPadding.top,
x + wordWidth + 0.5f,
height + 1,
paint);
paint.setFakeBoldText(false);
}
x += wordWidth;
Expand All @@ -216,7 +225,7 @@ protected void onDraw(Canvas canvas) {
scrollToTarget();
}
}

private void scrollToTarget() {
int sx = getScrollX();
if (mTargetScrollX > sx) {
Expand All @@ -235,9 +244,9 @@ private void scrollToTarget() {
scrollTo(sx, getScrollY());
invalidate();
}
public void setSuggestions(List<String> suggestions, boolean completions,
boolean typedWordValid) {

public void setSuggestions(
List<String> suggestions, boolean completions, boolean typedWordValid) {
clear();
if (suggestions != null) {
mSuggestions = new ArrayList<String>(suggestions);
Expand All @@ -257,7 +266,7 @@ public void clear() {
mSelectedIndex = -1;
invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent me) {

Expand All @@ -271,36 +280,36 @@ public boolean onTouchEvent(MotionEvent me) {
mTouchX = x;

switch (action) {
case MotionEvent.ACTION_DOWN:
mScrolled = false;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
if (y <= 0) {
// Fling up!?
if (mSelectedIndex >= 0) {
mService.pickSuggestionManually(mSelectedIndex);
mSelectedIndex = -1;
case MotionEvent.ACTION_DOWN:
mScrolled = false;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
if (y <= 0) {
// Fling up!?
if (mSelectedIndex >= 0) {
mService.pickSuggestionManually(mSelectedIndex);
mSelectedIndex = -1;
}
}
}
invalidate();
break;
case MotionEvent.ACTION_UP:
if (!mScrolled) {
if (mSelectedIndex >= 0) {
mService.pickSuggestionManually(mSelectedIndex);
invalidate();
break;
case MotionEvent.ACTION_UP:
if (!mScrolled) {
if (mSelectedIndex >= 0) {
mService.pickSuggestionManually(mSelectedIndex);
}
}
}
mSelectedIndex = -1;
removeHighlight();
requestLayout();
break;
mSelectedIndex = -1;
removeHighlight();
requestLayout();
break;
}
return true;
}

/**
* For flick through from keyboard, call this method with the x coordinate of the flick
* For flick through from keyboard, call this method with the x coordinate of the flick
* gesture.
* @param x
*/
Expand Down
Loading

0 comments on commit 2c05af8

Please sign in to comment.