20
20
21
21
import android .content .Context ;
22
22
import android .graphics .Canvas ;
23
+ import android .graphics .Color ;
23
24
import android .graphics .Paint ;
24
25
import android .graphics .Rect ;
25
26
import android .graphics .Typeface ;
26
- import android .graphics .drawable .Drawable ;
27
27
import android .graphics .drawable .PaintDrawable ;
28
28
import android .text .TextUtils ;
29
29
import android .util .AttributeSet ;
30
30
import android .view .MotionEvent ;
31
31
import android .view .View ;
32
32
import android .view .ViewGroup .LayoutParams ;
33
+
34
+ import androidx .annotation .NonNull ;
35
+
33
36
import com .osfans .trime .Rime ;
34
37
import com .osfans .trime .setup .Config ;
35
38
import com .osfans .trime .util .GraphicUtils ;
@@ -46,17 +49,18 @@ public interface EventListener {
46
49
private static final int CANDIDATE_TOUCH_OFFSET = -12 ;
47
50
48
51
private EventListener listener ;
49
- private GraphicUtils graphicUtils ;
52
+ private final GraphicUtils graphicUtils ;
50
53
private int highlightIndex ;
51
54
private Rime .RimeCandidate [] candidates ;
52
- private int num_candidates ;
53
- private int start_num = 0 ;
55
+ private int numCandidates ;
56
+ private int startNum = 0 ;
54
57
55
- private Drawable candidateHighlight , candidateSeparator ;
58
+ private PaintDrawable candidateHighlight ;
59
+ private final Paint separatorPaint ;
56
60
private final Paint candidatePaint ;
57
61
private final Paint symbolPaint ;
58
62
private final Paint commentPaint ;
59
- private Typeface candidateTypeface , symbolTypeface , commentTypeface , hanBTypeface , latinTypeface ;
63
+ private Typeface candidateFont , symbolFont , commentFont ;
60
64
private int candidateTextColor , hilitedCandidateTextColor ;
61
65
private int commentTextColor , hilitedCommentTextColor ;
62
66
private int candidateViewHeight , commentHeight , candidateSpacing , candidatePadding ;
@@ -67,8 +71,8 @@ public interface EventListener {
67
71
public void reset (Context context ) {
68
72
Config config = Config .get (context );
69
73
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" ));
72
76
candidateSpacing = config .getPixel ("candidate_spacing" );
73
77
candidatePadding = config .getPixel ("candidate_padding" );
74
78
@@ -82,18 +86,16 @@ public void reset(Context context) {
82
86
candidateViewHeight = config .getPixel ("candidate_view_height" );
83
87
commentHeight = config .getPixel ("comment_height" );
84
88
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" );
90
92
91
93
candidatePaint .setTextSize (candidate_text_size );
92
- candidatePaint .setTypeface (candidateTypeface );
94
+ candidatePaint .setTypeface (candidateFont );
93
95
symbolPaint .setTextSize (candidate_text_size );
94
- symbolPaint .setTypeface (symbolTypeface );
96
+ symbolPaint .setTypeface (symbolFont );
95
97
commentPaint .setTextSize (comment_text_size );
96
- commentPaint .setTypeface (commentTypeface );
98
+ commentPaint .setTypeface (commentFont );
97
99
98
100
isCommentOnTop = config .getBoolean ("comment_on_top" );
99
101
candidateUseCursor = config .getBoolean ("candidate_use_cursor" );
@@ -116,6 +118,9 @@ public Candidate(Context context, AttributeSet attrs) {
116
118
commentPaint .setAntiAlias (true );
117
119
commentPaint .setStrokeWidth (0 );
118
120
121
+ separatorPaint = new Paint ();
122
+ separatorPaint .setColor (Color .BLACK );
123
+
119
124
graphicUtils = new GraphicUtils (context );
120
125
121
126
reset (context );
@@ -137,10 +142,10 @@ public void setCandidateListener(EventListener listener) {
137
142
* @param start 候選的起始編號
138
143
*/
139
144
public void setText (int start ) {
140
- start_num = start ;
145
+ startNum = start ;
141
146
removeHighlight ();
142
147
updateCandidateWidth ();
143
- if (getCandNum () > 0 ) {
148
+ if (updateCandidates () > 0 ) {
144
149
invalidate ();
145
150
}
146
151
}
@@ -155,7 +160,7 @@ public void setText(int start) {
155
160
private boolean pickHighlighted (int index ) {
156
161
if ((highlightIndex != -1 ) && (listener != null )) {
157
162
if (index == -1 ) index = highlightIndex ;
158
- if (index >= 0 ) index += start_num ;
163
+ if (index >= 0 ) index += startNum ;
159
164
listener .onCandidatePressed (index );
160
165
return true ;
161
166
}
@@ -217,47 +222,49 @@ private void drawCandidates(Canvas canvas) {
217
222
if (shouldShowComment && !isCommentOnTop ) commentY += candidateRect [0 ].bottom - commentHeight ;
218
223
219
224
int i = 0 ;
220
- while (i < num_candidates ) {
225
+ while (i < numCandidates ) {
221
226
// Calculate a position where the text could be centered in the rectangle.
222
227
candidateX = candidateRect [i ].centerX ();
223
228
if (shouldShowComment ) {
224
229
final String comment = getComment (i );
225
230
if (!TextUtils .isEmpty (comment )) {
226
- commentWidth = graphicUtils .measureText (commentPaint , comment , commentTypeface );
231
+ commentWidth = graphicUtils .measureText (commentPaint , comment , commentFont );
227
232
if (isCommentOnTop ) {
228
233
commentX = candidateRect [i ].centerX ();
229
234
} else {
230
235
candidateX -= commentWidth / 2 ;
231
236
commentX = candidateRect [i ].right - commentWidth / 2 ;
232
237
}
233
238
commentPaint .setColor (isHighlighted (i ) ? hilitedCommentTextColor : commentTextColor );
234
- graphicUtils .drawText (canvas , comment , commentX , commentY , commentPaint , commentTypeface );
239
+ graphicUtils .drawText (canvas , comment , commentX , commentY , commentPaint , commentFont );
235
240
}
236
241
}
237
242
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 );
239
244
// 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
+ );
246
252
i ++;
247
253
}
248
254
for (int j = -4 ; j >= -5 ; j --) { // -4: left, -5: right
249
255
candidate = getCandidate (j );
250
256
if (candidate == null ) continue ;
251
257
symbolPaint .setColor (isHighlighted (i ) ? hilitedCommentTextColor : commentTextColor );
252
258
candidateX =
253
- candidateRect [i ].centerX () - graphicUtils .measureText (symbolPaint , candidate , symbolTypeface ) / 2 ;
259
+ candidateRect [i ].centerX () - graphicUtils .measureText (symbolPaint , candidate , symbolFont ) / 2 ;
254
260
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
+ );
261
268
i ++;
262
269
}
263
270
}
@@ -274,18 +281,16 @@ protected void onDraw(Canvas canvas) {
274
281
}
275
282
276
283
private void updateCandidateWidth () {
277
- final int top = 0 ;
278
- final int bottom = getHeight ();
279
284
int i ;
280
285
int x = 0 ;
281
286
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 () );
285
290
x += candidateSpacing ;
286
291
}
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 () );
289
294
LayoutParams params = getLayoutParams ();
290
295
params .width = x ;
291
296
params .height = candidateViewHeight ;
@@ -305,12 +310,11 @@ public boolean performClick() {
305
310
}
306
311
307
312
@ Override
308
- public boolean onTouchEvent (MotionEvent me ) {
309
- int action = me .getAction ();
313
+ public boolean onTouchEvent (@ NonNull MotionEvent me ) {
310
314
int x = (int ) me .getX ();
311
315
int y = (int ) me .getY ();
312
316
313
- switch (action ) {
317
+ switch (me . getActionMasked () ) {
314
318
case MotionEvent .ACTION_DOWN :
315
319
case MotionEvent .ACTION_MOVE :
316
320
updateHighlight (x , y );
@@ -337,13 +341,13 @@ private int getCandidateIndex(int x, int y) {
337
341
Rect r = new Rect ();
338
342
339
343
int j = 0 ;
340
- for (int i = 0 ; i < num_candidates ; i ++) {
344
+ for (int i = 0 ; i < numCandidates ; i ++) {
341
345
// Enlarge the rectangle to be more responsive to user clicks.
342
346
r .set (candidateRect [j ++]);
343
347
r .inset (0 , CANDIDATE_TOUCH_OFFSET );
344
348
if (r .contains (x , y )) {
345
349
// Returns -1 if there is no candidate in the hitting rectangle.
346
- return (i < num_candidates ) ? i : -1 ;
350
+ return (i < numCandidates ) ? i : -1 ;
347
351
}
348
352
}
349
353
@@ -366,42 +370,42 @@ private int getCandidateIndex(int x, int y) {
366
370
return -1 ;
367
371
}
368
372
369
- private int getCandNum () {
373
+ private int updateCandidates () {
370
374
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 ;
374
378
}
375
379
376
380
private String getCandidate (int i ) {
377
381
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 ;
379
383
else if (i == -4 && Rime .hasLeft ()) s = "◀" ;
380
384
else if (i == -5 && Rime .hasRight ()) s = "▶" ;
381
385
return s ;
382
386
}
383
387
384
388
private String getComment (int i ) {
385
389
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 ;
387
391
return s ;
388
392
}
389
393
390
394
private float getCandidateWidth (int i ) {
391
395
String s = getCandidate (i );
392
396
// 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 );
395
399
if (i >= 0 && shouldShowComment ) {
396
400
String comment = getComment (i );
397
401
if (comment != null ) {
398
- float x2 = graphicUtils .measureText (commentPaint , comment , commentTypeface );
402
+ float commentWidth = graphicUtils .measureText (commentPaint , comment , commentFont );
399
403
if (isCommentOnTop ) {
400
- if (x2 > x ) x = x2 ;
404
+ if (commentWidth > candidateWidth ) candidateWidth = commentWidth ;
401
405
} // 提示在上方
402
- else x += x2 ; // 提示在右方
406
+ else candidateWidth += commentWidth ; // 提示在右方
403
407
}
404
408
}
405
- return x ;
409
+ return candidateWidth ;
406
410
}
407
411
}
0 commit comments