Skip to content

Commit

Permalink
multi: a little optimization to several classes
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Aug 11, 2021
1 parent 6f77ec1 commit c839f66
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 156 deletions.
22 changes: 13 additions & 9 deletions app/src/main/java/com/osfans/trime/Candidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -50,14 +51,16 @@ public interface CandidateListener {
private int start_num = 0;

private Drawable candidateHighlight, candidateSeparator;
private Paint paintCandidate, paintSymbol, paintComment;
private final Paint paintCandidate;
private final Paint paintSymbol;
private final Paint paintComment;
private Typeface tfCandidate, tfSymbol, tfComment, tfHanB, tfLatin;
private int candidate_text_color, hilited_candidate_text_color;
private int comment_text_color, hilited_comment_text_color;
private int candidate_view_height, comment_height, candidate_spacing, candidate_padding;
private boolean show_comment = true, comment_on_top, candidate_use_cursor;

private Rect candidateRect[] = new Rect[MAX_CANDIDATE_COUNT + 2];
private final Rect[] candidateRect = new Rect[MAX_CANDIDATE_COUNT + 2];

public void reset(Context context) {
Config config = Config.get(context);
Expand Down Expand Up @@ -144,6 +147,7 @@ public void setText(int start) {
* @param index 候選項序號(從0開始),{@code -1}表示選擇當前高亮候選項
* @return 是否成功選字
*/
@SuppressWarnings("UnusedReturnValue")
private boolean pickHighlighted(int index) {
if ((highlightIndex != -1) && (listener != null)) {
if (index == -1) index = highlightIndex;
Expand Down Expand Up @@ -214,24 +218,24 @@ private void drawText(
private void drawCandidates(Canvas canvas) {
if (candidates == null) return;

float x = 0;
float y = 0;
float x;
float y;
int i = 0;
float comment_x, comment_y;
float comment_width;
String candidate, comment;

y = candidateRect[0].centerY() - (paintCandidate.ascent() + paintCandidate.descent()) / 2;
if (show_comment && comment_on_top) y += comment_height / 2;
comment_y = comment_height / 2 - (paintComment.ascent() + paintComment.descent()) / 2;
if (show_comment && comment_on_top) y += comment_height / 2f;
comment_y = comment_height / 2f - (paintComment.ascent() + paintComment.descent()) / 2;
if (show_comment && !comment_on_top) comment_y += candidateRect[0].bottom - comment_height;

while (i < num_candidates) {
// Calculate a position where the text could be centered in the rectangle.
x = candidateRect[i].centerX();
if (show_comment) {
comment = getComment(i);
if (!Function.isEmpty(comment)) {
if (!TextUtils.isEmpty(comment)) {
comment_width = measureText(comment, paintComment, tfComment);
if (comment_on_top) {
comment_x = candidateRect[i].centerX();
Expand Down Expand Up @@ -285,7 +289,7 @@ protected void onDraw(Canvas canvas) {
private void updateCandidateWidth() {
final int top = 0;
final int bottom = getHeight();
int i = 0;
int i;
int x = 0;
if (Rime.hasLeft()) x += getCandidateWidth(-4) + candidate_spacing;
getCandNum();
Expand Down Expand Up @@ -422,7 +426,7 @@ private float measureText(String s, Paint paint, Typeface font) {

private float getCandidateWidth(int i) {
String s = getCandidate(i);
float n = (s == null ? 0 : s.codePointCount(0, s.length()));
//float n = (s == null ? 0 : s.codePointCount(0, s.length()));
float x = 2 * candidate_padding;
if (s != null) x += measureText(s, paintCandidate, tfCandidate);
if (i >= 0 && show_comment) {
Expand Down
110 changes: 58 additions & 52 deletions app/src/main/java/com/osfans/trime/Composition.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.osfans.trime;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
Expand All @@ -27,6 +28,7 @@
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
Expand All @@ -38,6 +40,8 @@
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.osfans.trime.ime.core.Trime;
Expand All @@ -54,22 +58,22 @@ public class Composition extends AppCompatTextView {
private int back_color, hilited_back_color, hilited_candidate_back_color;
private Integer key_back_color;
private Typeface tfText, tfLabel, tfCandidate, tfComment;
private int composition_pos[] = new int[2];
private final int[] composition_pos = new int[2];
private int max_length, sticky_lines;
private int max_entries = Candidate.getMaxCandidateCount();
private boolean candidate_use_cursor, show_comment;
private int highlightIndex;
private List<Map<String, Object>> components;
private SpannableStringBuilder ss;
private int span = 0;
private final int span = 0;
private String movable;
private int move_pos[] = new int[2];
private final int[] move_pos = new int[2];
private boolean first_move = true;
private float mDx, mDy;
private int mCurrentX, mCurrentY;
private int candidate_num;
private boolean all_phrases;
private View mInputRoot;
//private View mInputRoot;

private class CompositionSpan extends UnderlineSpan {
public CompositionSpan() {
Expand Down Expand Up @@ -139,8 +143,8 @@ public void updateDrawState(TextPaint ds) {
}

@TargetApi(21)
public class LetterSpacingSpan extends UnderlineSpan {
private float letterSpacing;
public static class LetterSpacingSpan extends UnderlineSpan {
private final float letterSpacing;

/** @param letterSpacing 字符間距 */
public LetterSpacingSpan(float letterSpacing) {
Expand All @@ -158,8 +162,9 @@ public Composition(Context context, AttributeSet attrs) {
reset(context);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
public boolean onTouchEvent(@NonNull MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int n = getOffsetForPosition(event.getX(), event.getY());
Expand All @@ -178,7 +183,7 @@ public boolean onTouchEvent(MotionEvent event) {
if (action == MotionEvent.ACTION_DOWN) {
if (first_move || movable.contentEquals("once")) {
first_move = false;
int location[] = Trime.getLocationOnScreen(this);
int[] location = Trime.getLocationOnScreen(this);
mCurrentX = location[0];
mCurrentY = location[1];
}
Expand All @@ -200,7 +205,7 @@ public void setShowComment(boolean value) {
}

public void reset(Context context) {
Config config = Config.get(context);
final Config config = Config.get(context);
components = (List<Map<String, Object>>) config.getValue("window");
if (config.hasKey("layout/max_entries")) max_entries = config.getInt("layout/max_entries");
candidate_use_cursor = config.getBoolean("candidate_use_cursor");
Expand Down Expand Up @@ -255,7 +260,7 @@ public void reset(Context context) {
tfComment = config.getFont("comment_font");
}

private Object getAlign(Map m) {
private Object getAlign(Map<?, ?> m) {
Layout.Alignment i = Layout.Alignment.ALIGN_NORMAL;
if (m.containsKey("align")) {
String align = Config.getString(m, "align");
Expand All @@ -276,12 +281,13 @@ private Object getAlign(Map m) {
return new AlignmentSpan.Standard(i);
}

private void appendComposition(Map m) {
Rime.RimeComposition r = Rime.getComposition();
String s = r.getText();
private void appendComposition(Map<?, ?> m) {
final Rime.RimeComposition r = Rime.getComposition();
assert r != null;
final String s = r.getText();
int start, end;
String sep = Config.getString(m, "start");
if (!Function.isEmpty(sep)) {
if (!TextUtils.isEmpty(sep)) {
start = ss.length();
ss.append(sep);
end = ss.length();
Expand All @@ -304,48 +310,48 @@ private void appendComposition(Map m) {
ss.setSpan(new ForegroundColorSpan(hilited_text_color), start, end, span);
ss.setSpan(new BackgroundColorSpan(hilited_back_color), start, end, span);
sep = Config.getString(m, "end");
if (!Function.isEmpty(sep)) ss.append(sep);
if (!TextUtils.isEmpty(sep)) ss.append(sep);
}

/**
* 计算悬浮窗显示候选词后,候选栏从第几个候选词开始展示
* 注意当all_phrases==true时,悬浮窗显示的候选词数量和候选栏从第几个开始,是不一致的
* 注意当 all_phrases==true 时,悬浮窗显示的候选词数量和候选栏从第几个开始,是不一致的
* @param min_length 候选词长度大于设定,才会显示到悬浮窗中
* @param min_check 检查至少多少个候选词。当首选词长度不足时,继续检查后方候选词
* @return
* @return j
*/
private int calc_start_num(int min_length, int min_check) {
Rime.RimeCandidate[] candidates = Rime.getCandidates();
private int calcStartNum(int min_length, int min_check) {
final Rime.RimeCandidate[] candidates = Rime.getCandidates();
if (candidates == null) return 0;

int j=min_check>max_entries?max_entries-1:min_check-1;
if(j>=candidates.length)
j=candidates.length-1;
for(;j>=0;j--){
String cand = candidates[j].text;
if(cand.length()>=min_length)
int j = min_check > max_entries ? (max_entries - 1) : (min_check - 1);
if(j >= candidates.length)
j = candidates.length - 1;
for(; j >= 0; j--) {
final String cand = candidates[j].text;
if (cand.length() >= min_length)
break;
}

if(j<0)
j=0;
if( j < 0)
j = 0;

for(;j<max_entries && j<candidates.length;j++){
String cand = candidates[j].text;
if(cand.length()<min_length){
for(; j < max_entries && j < candidates.length; j++){
final String cand = candidates[j].text;
if (cand.length() < min_length) {
return j;
}
}
return j;
}


// 生成悬浮窗内的文本
private void appendCandidates(Map m, int length,int end_num) {
/** 生成悬浮窗内的文本 */
private void appendCandidates(Map<?, ?> m, int length,int end_num) {
Log.d("Composition","appendCandidates() length="+length);
int start, end;

Rime.RimeCandidate[] candidates = Rime.getCandidates();
final Rime.RimeCandidate[] candidates = Rime.getCandidates();
if (candidates == null) return;
String sep = Config.getString(m, "start");
highlightIndex = candidate_use_cursor ? Rime.getCandHighlightIndex() : -1;
Expand All @@ -360,7 +366,7 @@ private void appendCandidates(Map m, int length,int end_num) {
candidate_num = 0;
for (Rime.RimeCandidate o : candidates) {
String cand = o.text;
if (Function.isEmpty(cand)) cand = "";
if (TextUtils.isEmpty(cand)) cand = "";
i++;
if (candidate_num >= max_entries)
break;
Expand All @@ -372,7 +378,7 @@ private void appendCandidates(Map m, int length,int end_num) {
continue;
}
cand = String.format(candidate_format, cand);
String line_sep;
final String line_sep;
if (candidate_num == 0) {
line_sep = sep;
} else if ((sticky_lines > 0 && sticky_lines >= i)
Expand All @@ -382,14 +388,14 @@ private void appendCandidates(Map m, int length,int end_num) {
} else {
line_sep = line;
}
if (!Function.isEmpty(line_sep)) {
if (!TextUtils.isEmpty(line_sep)) {
start = ss.length();
ss.append(line_sep);
end = ss.length();
ss.setSpan(getAlign(m), start, end, span);
}
if (!Function.isEmpty(label_format) && labels != null) {
String label = String.format(label_format, labels[i]);
if (!TextUtils.isEmpty(label_format) && labels != null) {
final String label = String.format(label_format, labels[i]);
start = ss.length();
ss.append(label);
end = ss.length();
Expand Down Expand Up @@ -422,7 +428,7 @@ private void appendCandidates(Map m, int length,int end_num) {
span);
ss.setSpan(new AbsoluteSizeSpan(candidate_text_size), start, end, span);
String comment = o.comment;
if (show_comment && !Function.isEmpty(comment_format) && !Function.isEmpty(comment)) {
if (show_comment && !TextUtils.isEmpty(comment_format) && !TextUtils.isEmpty(comment)) {
comment = String.format(comment_format, comment);
start = ss.length();
ss.append(comment);
Expand All @@ -444,23 +450,23 @@ private void appendCandidates(Map m, int length,int end_num) {
candidate_num++;
}
sep = Config.getString(m, "end");
if (!Function.isEmpty(sep)) ss.append(sep);
if (!TextUtils.isEmpty(sep)) ss.append(sep);
}

private void appendButton(Map m) {
private void appendButton(@NonNull Map<?, ?> m) {
if (m.containsKey("when")) {
String when = Config.getString(m, "when");
final String when = Config.getString(m, "when");
if (when.contentEquals("paging") && !Rime.isPaging()) return;
if (when.contentEquals("has_menu") && !Rime.hasMenu()) return;
}
String label;
Event e = new Event(Config.getString(m, "click"));
final String label;
final Event e = new Event(Config.getString(m, "click"));
if (m.containsKey("label")) label = Config.getString(m, "label");
else label = e.getLabel();
int start, end;
String sep = null;
if (m.containsKey("start")) sep = Config.getString(m, "start");
if (!Function.isEmpty(sep)) {
if (!TextUtils.isEmpty(sep)) {
start = ss.length();
ss.append(sep);
end = ss.length();
Expand All @@ -473,14 +479,14 @@ private void appendButton(Map m) {
ss.setSpan(new EventSpan(e), start, end, span);
ss.setSpan(new AbsoluteSizeSpan(key_text_size), start, end, span);
sep = Config.getString(m, "end");
if (!Function.isEmpty(sep)) ss.append(sep);
if (!TextUtils.isEmpty(sep)) ss.append(sep);
}

private void appendMove(Map m) {
private void appendMove(Map<?, ?> m) {
String s = Config.getString(m, "move");
int start, end;
String sep = Config.getString(m, "start");
if (!Function.isEmpty(sep)) {
if (!TextUtils.isEmpty(sep)) {
start = ss.length();
ss.append(sep);
end = ss.length();
Expand All @@ -495,22 +501,22 @@ private void appendMove(Map m) {
ss.setSpan(new AbsoluteSizeSpan(key_text_size), start, end, span);
ss.setSpan(new ForegroundColorSpan(key_text_color), start, end, span);
sep = Config.getString(m, "end");
if (!Function.isEmpty(sep)) ss.append(sep);
if (!TextUtils.isEmpty(sep)) ss.append(sep);
}

public int setWindow(int length,int min_check) {
if (getVisibility() != View.VISIBLE) return 0;
Rime.RimeComposition r = Rime.getComposition();
if (r == null) return 0;
String s = r.getText();
if (Function.isEmpty(s)) return 0;
if (TextUtils.isEmpty(s)) return 0;
setSingleLine(true); //設置單行
ss = new SpannableStringBuilder();
int start_num = 0;
for (Map<String, Object> m : components) {
for (Map<?, ?> m : components) {
if (m.containsKey("composition")) appendComposition(m);
else if (m.containsKey("candidate")) {
start_num = calc_start_num(length,min_check);
start_num = calcStartNum(length,min_check);
Log.d("setWindow()","start_num="+start_num+" min_length="+length+" min_check="+min_check);
appendCandidates(m, length,start_num);
}
Expand Down
Loading

0 comments on commit c839f66

Please sign in to comment.