Skip to content

Commit df9c966

Browse files
WhiredPlanckBambooin
authored andcommitted
refactor(text): reduce redundant code and normalize variable names
1 parent ec8f24c commit df9c966

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

app/src/main/java/com/osfans/trime/ime/text/Candidate.java

+64-60
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
import android.content.Context;
2222
import android.graphics.Canvas;
23+
import android.graphics.Color;
2324
import android.graphics.Paint;
2425
import android.graphics.Rect;
2526
import android.graphics.Typeface;
26-
import android.graphics.drawable.Drawable;
2727
import android.graphics.drawable.PaintDrawable;
2828
import android.text.TextUtils;
2929
import android.util.AttributeSet;
3030
import android.view.MotionEvent;
3131
import android.view.View;
3232
import android.view.ViewGroup.LayoutParams;
33+
34+
import androidx.annotation.NonNull;
35+
3336
import com.osfans.trime.Rime;
3437
import com.osfans.trime.setup.Config;
3538
import com.osfans.trime.util.GraphicUtils;
@@ -46,17 +49,18 @@ public interface EventListener {
4649
private static final int CANDIDATE_TOUCH_OFFSET = -12;
4750

4851
private EventListener listener;
49-
private GraphicUtils graphicUtils;
52+
private final GraphicUtils graphicUtils;
5053
private int highlightIndex;
5154
private Rime.RimeCandidate[] candidates;
52-
private int num_candidates;
53-
private int start_num = 0;
55+
private int numCandidates;
56+
private int startNum = 0;
5457

55-
private Drawable candidateHighlight, candidateSeparator;
58+
private PaintDrawable candidateHighlight;
59+
private final Paint separatorPaint;
5660
private final Paint candidatePaint;
5761
private final Paint symbolPaint;
5862
private final Paint commentPaint;
59-
private Typeface candidateTypeface, symbolTypeface, commentTypeface, hanBTypeface, latinTypeface;
63+
private Typeface candidateFont, symbolFont, commentFont;
6064
private int candidateTextColor, hilitedCandidateTextColor;
6165
private int commentTextColor, hilitedCommentTextColor;
6266
private int candidateViewHeight, commentHeight, candidateSpacing, candidatePadding;
@@ -67,8 +71,8 @@ public interface EventListener {
6771
public void reset(Context context) {
6872
Config config = Config.get(context);
6973
candidateHighlight = new PaintDrawable(config.getColor("hilited_candidate_back_color"));
70-
((PaintDrawable) candidateHighlight).setCornerRadius(config.getFloat("layout/round_corner"));
71-
candidateSeparator = new PaintDrawable(config.getColor("candidate_separator_color"));
74+
candidateHighlight.setCornerRadius(config.getFloat("layout/round_corner"));
75+
separatorPaint.setColor(config.getColor("candidate_separator_color"));
7276
candidateSpacing = config.getPixel("candidate_spacing");
7377
candidatePadding = config.getPixel("candidate_padding");
7478

@@ -82,18 +86,16 @@ public void reset(Context context) {
8286
candidateViewHeight = config.getPixel("candidate_view_height");
8387
commentHeight = config.getPixel("comment_height");
8488

85-
candidateTypeface = config.getFont("candidate_font");
86-
latinTypeface = config.getFont("latin_font");
87-
hanBTypeface = config.getFont("hanb_font");
88-
commentTypeface = config.getFont("comment_font");
89-
symbolTypeface = config.getFont("symbol_font");
89+
candidateFont = config.getFont("candidate_font");
90+
commentFont = config.getFont("comment_font");
91+
symbolFont = config.getFont("symbol_font");
9092

9193
candidatePaint.setTextSize(candidate_text_size);
92-
candidatePaint.setTypeface(candidateTypeface);
94+
candidatePaint.setTypeface(candidateFont);
9395
symbolPaint.setTextSize(candidate_text_size);
94-
symbolPaint.setTypeface(symbolTypeface);
96+
symbolPaint.setTypeface(symbolFont);
9597
commentPaint.setTextSize(comment_text_size);
96-
commentPaint.setTypeface(commentTypeface);
98+
commentPaint.setTypeface(commentFont);
9799

98100
isCommentOnTop = config.getBoolean("comment_on_top");
99101
candidateUseCursor = config.getBoolean("candidate_use_cursor");
@@ -116,6 +118,9 @@ public Candidate(Context context, AttributeSet attrs) {
116118
commentPaint.setAntiAlias(true);
117119
commentPaint.setStrokeWidth(0);
118120

121+
separatorPaint = new Paint();
122+
separatorPaint.setColor(Color.BLACK);
123+
119124
graphicUtils = new GraphicUtils(context);
120125

121126
reset(context);
@@ -137,10 +142,10 @@ public void setCandidateListener(EventListener listener) {
137142
* @param start 候選的起始編號
138143
*/
139144
public void setText(int start) {
140-
start_num = start;
145+
startNum = start;
141146
removeHighlight();
142147
updateCandidateWidth();
143-
if (getCandNum() > 0) {
148+
if (updateCandidates() > 0) {
144149
invalidate();
145150
}
146151
}
@@ -155,7 +160,7 @@ public void setText(int start) {
155160
private boolean pickHighlighted(int index) {
156161
if ((highlightIndex != -1) && (listener != null)) {
157162
if (index == -1) index = highlightIndex;
158-
if (index >= 0) index += start_num;
163+
if (index >= 0) index += startNum;
159164
listener.onCandidatePressed(index);
160165
return true;
161166
}
@@ -217,47 +222,49 @@ private void drawCandidates(Canvas canvas) {
217222
if (shouldShowComment && !isCommentOnTop) commentY += candidateRect[0].bottom - commentHeight;
218223

219224
int i = 0;
220-
while (i < num_candidates) {
225+
while (i < numCandidates) {
221226
// Calculate a position where the text could be centered in the rectangle.
222227
candidateX = candidateRect[i].centerX();
223228
if (shouldShowComment) {
224229
final String comment = getComment(i);
225230
if (!TextUtils.isEmpty(comment)) {
226-
commentWidth = graphicUtils.measureText(commentPaint, comment, commentTypeface);
231+
commentWidth = graphicUtils.measureText(commentPaint, comment, commentFont);
227232
if (isCommentOnTop) {
228233
commentX = candidateRect[i].centerX();
229234
} else {
230235
candidateX -= commentWidth / 2;
231236
commentX = candidateRect[i].right - commentWidth / 2;
232237
}
233238
commentPaint.setColor(isHighlighted(i) ? hilitedCommentTextColor : commentTextColor);
234-
graphicUtils.drawText(canvas, comment, commentX, commentY, commentPaint, commentTypeface);
239+
graphicUtils.drawText(canvas, comment, commentX, commentY, commentPaint, commentFont);
235240
}
236241
}
237242
candidatePaint.setColor(isHighlighted(i) ? hilitedCandidateTextColor : candidateTextColor);
238-
graphicUtils.drawText(canvas, getCandidate(i), candidateX, candidateY, candidatePaint, candidateTypeface);
243+
graphicUtils.drawText(canvas, getCandidate(i), candidateX, candidateY, candidatePaint, candidateFont);
239244
// Draw the separator at the right edge of each candidate.
240-
candidateSeparator.setBounds(
241-
candidateRect[i].right - candidateSeparator.getIntrinsicWidth(),
242-
candidateRect[i].top,
243-
candidateRect[i].right + candidateSpacing,
244-
candidateRect[i].bottom);
245-
candidateSeparator.draw(canvas);
245+
canvas.drawRect(
246+
candidateRect[i].right - candidateSpacing,
247+
candidateRect[i].top,
248+
candidateRect[i].right + candidateSpacing,
249+
candidateRect[i].bottom,
250+
separatorPaint
251+
);
246252
i++;
247253
}
248254
for (int j = -4; j >= -5; j--) { // -4: left, -5: right
249255
candidate = getCandidate(j);
250256
if (candidate == null) continue;
251257
symbolPaint.setColor(isHighlighted(i) ? hilitedCommentTextColor : commentTextColor);
252258
candidateX =
253-
candidateRect[i].centerX() - graphicUtils.measureText(symbolPaint, candidate, symbolTypeface) / 2;
259+
candidateRect[i].centerX() - graphicUtils.measureText(symbolPaint, candidate, symbolFont) / 2;
254260
canvas.drawText(candidate, candidateX, candidateY, symbolPaint);
255-
candidateSeparator.setBounds(
256-
candidateRect[i].right - candidateSeparator.getIntrinsicWidth(),
257-
candidateRect[i].top,
258-
candidateRect[i].right + candidateSpacing,
259-
candidateRect[i].bottom);
260-
candidateSeparator.draw(canvas);
261+
canvas.drawRect(
262+
candidateRect[i].right - candidateSpacing,
263+
candidateRect[i].top,
264+
candidateRect[i].right + candidateSpacing,
265+
candidateRect[i].bottom,
266+
separatorPaint
267+
);
261268
i++;
262269
}
263270
}
@@ -274,18 +281,16 @@ protected void onDraw(Canvas canvas) {
274281
}
275282

276283
private void updateCandidateWidth() {
277-
final int top = 0;
278-
final int bottom = getHeight();
279284
int i;
280285
int x = 0;
281286
if (Rime.hasLeft()) x += getCandidateWidth(-4) + candidateSpacing;
282-
getCandNum();
283-
for (i = 0; i < num_candidates; i++) {
284-
candidateRect[i] = new Rect(x, top, x += getCandidateWidth(i), bottom);
287+
updateCandidates();
288+
for (i = 0; i < numCandidates; i++) {
289+
candidateRect[i] = new Rect(x, 0, x += getCandidateWidth(i), getHeight());
285290
x += candidateSpacing;
286291
}
287-
if (Rime.hasLeft()) candidateRect[i++] = new Rect(0, top, (int) getCandidateWidth(-4), bottom);
288-
if (Rime.hasRight()) candidateRect[i++] = new Rect(x, top, x += getCandidateWidth(-5), bottom);
292+
if (Rime.hasLeft()) candidateRect[i++] = new Rect(0, 0, (int) getCandidateWidth(-4), getHeight());
293+
if (Rime.hasRight()) candidateRect[i++] = new Rect(x, 0, x += getCandidateWidth(-5), getHeight());
289294
LayoutParams params = getLayoutParams();
290295
params.width = x;
291296
params.height = candidateViewHeight;
@@ -305,12 +310,11 @@ public boolean performClick() {
305310
}
306311

307312
@Override
308-
public boolean onTouchEvent(MotionEvent me) {
309-
int action = me.getAction();
313+
public boolean onTouchEvent(@NonNull MotionEvent me) {
310314
int x = (int) me.getX();
311315
int y = (int) me.getY();
312316

313-
switch (action) {
317+
switch (me.getActionMasked()) {
314318
case MotionEvent.ACTION_DOWN:
315319
case MotionEvent.ACTION_MOVE:
316320
updateHighlight(x, y);
@@ -337,13 +341,13 @@ private int getCandidateIndex(int x, int y) {
337341
Rect r = new Rect();
338342

339343
int j = 0;
340-
for (int i = 0; i < num_candidates; i++) {
344+
for (int i = 0; i < numCandidates; i++) {
341345
// Enlarge the rectangle to be more responsive to user clicks.
342346
r.set(candidateRect[j++]);
343347
r.inset(0, CANDIDATE_TOUCH_OFFSET);
344348
if (r.contains(x, y)) {
345349
// Returns -1 if there is no candidate in the hitting rectangle.
346-
return (i < num_candidates) ? i : -1;
350+
return (i < numCandidates) ? i : -1;
347351
}
348352
}
349353

@@ -366,42 +370,42 @@ private int getCandidateIndex(int x, int y) {
366370
return -1;
367371
}
368372

369-
private int getCandNum() {
373+
private int updateCandidates() {
370374
candidates = Rime.getCandidates();
371-
highlightIndex = Rime.getCandHighlightIndex() - start_num;
372-
num_candidates = candidates == null ? 0 : candidates.length - start_num;
373-
return num_candidates;
375+
highlightIndex = Rime.getCandHighlightIndex() - startNum;
376+
numCandidates = candidates == null ? 0 : candidates.length - startNum;
377+
return numCandidates;
374378
}
375379

376380
private String getCandidate(int i) {
377381
String s = null;
378-
if (candidates != null && i >= 0) s = candidates[i + start_num].text;
382+
if (candidates != null && i >= 0) s = candidates[i + startNum].text;
379383
else if (i == -4 && Rime.hasLeft()) s = "◀";
380384
else if (i == -5 && Rime.hasRight()) s = "▶";
381385
return s;
382386
}
383387

384388
private String getComment(int i) {
385389
String s = null;
386-
if (candidates != null && i >= 0) s = candidates[i + start_num].comment;
390+
if (candidates != null && i >= 0) s = candidates[i + startNum].comment;
387391
return s;
388392
}
389393

390394
private float getCandidateWidth(int i) {
391395
String s = getCandidate(i);
392396
// float n = (s == null ? 0 : s.codePointCount(0, s.length()));
393-
float x = 2 * candidatePadding;
394-
if (s != null) x += graphicUtils.measureText(candidatePaint, s, candidateTypeface);
397+
float candidateWidth = 2 * candidatePadding;
398+
if (s != null) candidateWidth += graphicUtils.measureText(candidatePaint, s, candidateFont);
395399
if (i >= 0 && shouldShowComment) {
396400
String comment = getComment(i);
397401
if (comment != null) {
398-
float x2 = graphicUtils.measureText(commentPaint, comment, commentTypeface);
402+
float commentWidth = graphicUtils.measureText(commentPaint, comment, commentFont);
399403
if (isCommentOnTop) {
400-
if (x2 > x) x = x2;
404+
if (commentWidth > candidateWidth) candidateWidth = commentWidth;
401405
} // 提示在上方
402-
else x += x2; // 提示在右方
406+
else candidateWidth += commentWidth; // 提示在右方
403407
}
404408
}
405-
return x;
409+
return candidateWidth;
406410
}
407411
}

0 commit comments

Comments
 (0)