Skip to content

Commit

Permalink
feat: floating window for liquid keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan authored and Bambooin committed Aug 18, 2022
1 parent 963998f commit 33c3487
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
7 changes: 6 additions & 1 deletion app/src/main/assets/rime/trime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ style:
- {start: "", move: 'ㄓ ', end: ""}
- {start: "", composition: "%s", end: "", letter_spacing: 0} #letter_spacing需要>=Android5.0。TODO: 不爲0時,會導致不換行的問題
- {start: "\n", label: "%s.", candidate: "%s", comment: " %s", end: "", sep: " "}
liquid_keyboard_window: #液态键盘模式下显示的悬浮窗口組件
- {start: "", click: "space", label: " 空格 "}
- {start: "", click: "BackSpace", label: " 删除 "}
- {start: "", click: "Return", label: " 回车 "}
- {start: "", click: "liquid_keyboard_exit", label: " 返回 "}
key_font: symbol.ttf #鍵盤字型
key_height: 44 #鍵高
key_long_text_size: 14 #長標籤字號
Expand Down Expand Up @@ -918,7 +923,7 @@ preset_keys:
Keyboard_default: {label: 返回, send: Eisu_toggle, select: .default}
Keyboard_switch: {label: 鍵盤, send: Eisu_toggle, select: .next}
liquid_keyboard_switch: { label: 更多, send: function, command: liquid_keyboard, option: "特殊" }
liquid_keyboard_switch2: { toggle: _liquid_keyboard, send: Mode_switch, states: [ 更多, 更多 ] }
liquid_keyboard_exit: {label: 返回, send: function, command: liquid_keyboard, option: "-1"} #退出liquidkeyboard
liquid_keyboard_emoji: { label: 🙂, send: function, command: liquid_keyboard, option: "emoji" }
liquid_keyboard_clipboard: { label: 剪贴, send: function, command: liquid_keyboard, option: "剪贴" }
# trime設定
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public void selectLiquidKeyboard(final int tabIndex) {

// 按键需要通过tab name来打开liquidKeyboard的指定tab
public void selectLiquidKeyboard(@NonNull String name) {
if (name.matches("\\d+")) selectLiquidKeyboard(Integer.parseInt(name));
if (name.matches("-?\\d+")) selectLiquidKeyboard(Integer.parseInt(name));
else if (name.matches("[A-Z]+")) selectLiquidKeyboard(SymbolKeyboardType.valueOf(name));
else selectLiquidKeyboard(TabManager.getTagIndex(name));
}
Expand Down Expand Up @@ -498,8 +498,8 @@ private void hideCompositionView() {
}
}

private void showCompositionView() {
if (TextUtils.isEmpty(Rime.getCompositionText())) {
private void showCompositionView(boolean isCandidate) {
if (TextUtils.isEmpty(Rime.getCompositionText()) && isCandidate) {
hideCompositionView();
return;
}
Expand Down Expand Up @@ -693,7 +693,7 @@ public void onUpdateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) {
cursorAnchorInfo.getMatrix().mapRect(mPopupRectF);
}
if (mCandidateRoot != null) {
showCompositionView();
showCompositionView(true);
}
}

Expand Down Expand Up @@ -1216,14 +1216,16 @@ public int updateComposing() {
Timber.d("updateComposing() SymbolKeyboardType=%s", symbolKeyboardType.toString());
if (symbolKeyboardType != SymbolKeyboardType.NO_KEY
&& symbolKeyboardType != SymbolKeyboardType.CANDIDATE) {
mComposition.getRootView().setVisibility(View.GONE);
mComposition.setWindow();
showCompositionView(false);
return 0;
} else {
mComposition.getRootView().setVisibility(View.VISIBLE);

startNum = mComposition.setWindow(minPopupSize, minPopupCheckSize, Integer.MAX_VALUE);
mCandidate.setText(startNum);
// if isCursorUpdated, showCompositionView will be called in onUpdateCursorAnchorInfo
// otherwise we need to call it here
if (!isCursorUpdated) showCompositionView();
if (!isCursorUpdated) showCompositionView(true);
}
} else {
mCandidate.setText(0);
Expand All @@ -1239,8 +1241,6 @@ public int updateComposing() {
if (symbolKeyboardType == SymbolKeyboardType.CANDIDATE) {
if (isComposing()) {
liquidKeyboard.updateCandidates();
} else {
selectLiquidKeyboard(-1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ private void onBufferDraw() {
int[] drawableState = key.getCurrentDrawableState();
keyBackground = key.getBackColorForState(drawableState);
if (keyBackground == null) {
Timber.i("onBufferDraw() keyBackground==null, key=%s", key.getLabel());
Timber.d("onBufferDraw() keyBackground==null, key=%s", key.getLabel());
try {
final int index = (int) findStateDrawableIndex.invoke(mKeyBackColor, drawableState);
keyBackground = (Drawable) getStateDrawable.invoke(mKeyBackColor, index);
Expand Down
32 changes: 30 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/text/Composition.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.ime.keyboard.Event;
import com.osfans.trime.util.ConfigGetter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import timber.log.Timber;
Expand All @@ -63,7 +64,7 @@ public class Composition extends AppCompatTextView {
private int max_entries = Candidate.getMaxCandidateCount();
private boolean candidate_use_cursor, show_comment;
private int highlightIndex;
private List<Map<String, Object>> windows_comps;
private List<Map<String, Object>> windows_comps, liquid_keyboard_window_comp;
private SpannableStringBuilder ss;
private final int span = 0;
private String movable;
Expand Down Expand Up @@ -210,7 +211,14 @@ public void setShowComment(boolean value) {

public void reset(Context context) {
final Config config = Config.get(context);
windows_comps = (List<Map<String, Object>>) config.getValue("window");

Object o = config.getValue("window");
if (o == null) windows_comps = new ArrayList<>();
else windows_comps = (List<Map<String, Object>>) o;
o = config.getValue("liquid_keyboard_window");
if (o == null) liquid_keyboard_window_comp = new ArrayList<>();
else liquid_keyboard_window_comp = (List<Map<String, Object>>) o;

if (config.hasKey("layout/max_entries")) max_entries = config.getInt("layout/max_entries");
candidate_use_cursor = config.getBoolean("candidate_use_cursor");
text_size = config.getPixel("text_size");
Expand Down Expand Up @@ -564,4 +572,24 @@ else if (m.containsKey("candidate")) {
setMovementMethod(LinkMovementMethod.getInstance());
return start_num;
}

/** 设置悬浮窗, 用于liquidKeyboard的悬浮窗工具栏 */
public void setWindow() {
if (getVisibility() != View.VISIBLE) return;
if (liquid_keyboard_window_comp.isEmpty()) {
this.setVisibility(GONE);
return;
}

ss = new SpannableStringBuilder();

for (Map<String, ?> m : liquid_keyboard_window_comp) {
if (m.containsKey("composition")) appendComposition(m);
else if (m.containsKey("click")) appendButton(m);
}
setSingleLine(!ss.toString().contains("\n"));

setText(ss);
setMovementMethod(LinkMovementMethod.getInstance());
}
}

0 comments on commit 33c3487

Please sign in to comment.