Skip to content

Commit

Permalink
refactor(ime): optimize the enum classes related key event stuffs
Browse files Browse the repository at this point in the history
- Remove redundant convertMap
- Optimize their usages
  • Loading branch information
WhiredPlanck committed Jan 10, 2023
1 parent fe4c5bb commit a51a80f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
19 changes: 5 additions & 14 deletions app/src/main/java/com/osfans/trime/ime/enums/KeyCommandType.kt
Expand Up @@ -17,25 +17,16 @@
*/
package com.osfans.trime.ime.enums

import java.util.HashMap
import java.util.Locale

/*按键的特殊命令枚举(仅用于新增的liquidKeyboard)*/
enum class KeyCommandType {
NULL, LEFT, RIGHT, EXIT, DEL_LEFT, DEL_RIGHT, UNDO, REDO, ;
NULL, LEFT, RIGHT, EXIT, DEL_LEFT, DEL_RIGHT, UNDO, REDO;

companion object {
private val convertMap: HashMap<String, KeyCommandType> = hashMapOf()

init {
for (type in values()) {
convertMap[type.toString()] = type
}
}

@JvmStatic
fun fromString(code: String): KeyCommandType {
val type = convertMap[code.uppercase(Locale.getDefault())]
return type ?: NULL
return runCatching {
valueOf(code.uppercase())
}.getOrDefault(NULL)
}
}
}
10 changes: 0 additions & 10 deletions app/src/main/java/com/osfans/trime/ime/enums/KeyEventType.kt
Expand Up @@ -5,14 +5,4 @@ enum class KeyEventType {

// 长按按键展开列表时,正上方为长按对应按键,排序如上,不展示combo及之前的按键,展示extra
COMPOSING, HAS_MENU, PAGING, COMBO, ASCII, CLICK, SWIPE_UP, LONG_CLICK, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT, EXTRA;

companion object {
@JvmStatic
fun valueOf(ordinal: Int): KeyEventType {
if (ordinal < 0 || ordinal >= values().size) {
return EXTRA
}
return values()[ordinal]
}
}
}
15 changes: 9 additions & 6 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Key.java
Expand Up @@ -26,6 +26,7 @@
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.ime.enums.KeyEventType;
import com.osfans.trime.util.CollectionUtils;
import com.osfans.trime.util.ConfigGetter;
import java.text.MessageFormat;
import java.util.Arrays;
Expand Down Expand Up @@ -118,13 +119,15 @@ public Key(Keyboard parent, Map<String, Object> mk) {
{
boolean hasComposingKey = false;

for (int i = 0; i < EVENT_NUM - 1; i++) {
String eventType = (KeyEventType.Companion.valueOf(i)).toString().toLowerCase(Locale.ROOT);
s = ConfigGetter.getString(mk, eventType, "");
for (final KeyEventType type : KeyEventType.values()) {
final String typeStr = type.toString().toLowerCase(Locale.ROOT);
s = CollectionUtils.obtainString(mk, typeStr, "");
if (!TextUtils.isEmpty(s)) {
events[i] = new Event(mKeyboard, s);
if (i < KeyEventType.COMBO.ordinal()) hasComposingKey = true;
} else if (i == KeyEventType.CLICK.ordinal()) events[i] = new Event(mKeyboard, "");
events[type.ordinal()] = new Event(mKeyboard, s);
hasComposingKey = type.ordinal() < KeyEventType.COMBO.ordinal();
} else if (type == KeyEventType.CLICK) {
events[type.ordinal()] = new Event(mKeyboard, "");
}
}
if (hasComposingKey) mKeyboard.getComposingKeys().add(this);

Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabManager.java
Expand Up @@ -100,14 +100,19 @@ public void addTab(@NonNull String name, SymbolKeyboardType type, List<SimpleKey
// 处理single类型和no_key类型。前者把字符串切分为多个按键,后者把字符串转换为命令
public void addTab(String name, SymbolKeyboardType type, String string) {
if (string == null) return;

if (type == SymbolKeyboardType.SINGLE) {
addTab(name, type, SimpleKeyDao.Single(string));
} else if (type == SymbolKeyboardType.NO_KEY) {
KeyCommandType command = KeyCommandType.Companion.fromString(string);
tabTags.add(new TabTag(name, type, command));
keyboards.add(notKeyboard);
} else addTab(name, type, SimpleKeyDao.SimpleKeyboard(string));
switch (type) {
case SINGLE:
addTab(name, type, SimpleKeyDao.Single(string));
break;
case NO_KEY:
final KeyCommandType commandType = KeyCommandType.fromString(string);
tabTags.add(new TabTag(name, type, commandType));
keyboards.add(notKeyboard);
break;
default:
addTab(name, type, SimpleKeyDao.SimpleKeyboard(string));
break;
}
}

// 解析config的数据
Expand Down

0 comments on commit a51a80f

Please sign in to comment.