Skip to content

Commit 0f0a38f

Browse files
committed
chore(Key): move some event fileds to events and fix some error
1 parent c0be8b5 commit 0f0a38f

File tree

7 files changed

+195
-171
lines changed

7 files changed

+195
-171
lines changed

app/src/main/java/com/osfans/trime/ime/enums/KeyEventType.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ package com.osfans.trime.ime.enums
22

33
/** 按键事件枚举 */
44
enum class KeyEventType {
5+
56
// 长按按键展开列表时,正上方为长按对应按键,排序如上,不展示combo及之前的按键,展示extra
6-
COMPOSING, HAS_MENU, PAGING, COMBO, CLICK, SWIPE_UP, LONG_CLICK, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT,
7+
COMPOSING, HAS_MENU, PAGING, COMBO, ASCII, CLICK, SWIPE_UP, LONG_CLICK, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT, EXTRA;
8+
9+
companion object {
10+
@JvmStatic
11+
fun valueOf(ordinal: Int): KeyEventType {
12+
if (ordinal < 0 || ordinal >= values().size) {
13+
return EXTRA
14+
}
15+
return values()[ordinal]
16+
}
17+
}
718
}

app/src/main/java/com/osfans/trime/ime/enums/Keycode.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum class Keycode {
88
// 1. 数字开头的keyName添加了下划线(在init阶段已经修复),受到影响的按键有: 0-12,3D_MODE
99
// 2. 按键0恢复为UNKNOWN, VoidSymbol改为最末位的按键
1010

11-
UNKNOWN, SOFT_LEFT, SOFT_RIGHT, HOME, BACK, CALL, ENDCALL,
11+
VoidSymbol, SOFT_LEFT, SOFT_RIGHT, HOME, BACK, CALL, ENDCALL,
1212
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9,
1313
asterisk, numbersign, Up, Down, Left, Right, KP_Begin,
1414
VOLUME_UP, VOLUME_DOWN, POWER, CAMERA, Clear,
@@ -54,7 +54,7 @@ enum class Keycode {
5454
ALL_APPS, REFRESH, THUMBS_UP, THUMBS_DOWN, PROFILE_SWITCH,
5555
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
5656
exclam, quotedbl, dollar, percent, ampersand, colon, less, greater, question, asciicircum, underscore, braceleft, bar, braceright, asciitilde,
57-
VoidSymbol;
57+
;
5858

5959
companion object {
6060

@@ -96,20 +96,20 @@ enum class Keycode {
9696
@JvmStatic
9797
fun fromString(s: String): Keycode {
9898
val type = convertMap[s]
99-
return type ?: UNKNOWN
99+
return type ?: VoidSymbol
100100
}
101101

102102
@JvmStatic
103103
fun valueOf(ordinal: Int): Keycode {
104104
if (ordinal < 0 || ordinal >= values().size) {
105-
return UNKNOWN
105+
return VoidSymbol
106106
}
107107
return values()[ordinal]
108108
}
109109

110110
@JvmStatic
111111
fun keyNameOf(ordinal: Int): String {
112-
return valueOf(ordinal).toString().replaceFirst("^_", "")
112+
return valueOf(ordinal).toString().replaceFirst("^_".toRegex(), "")
113113
}
114114

115115
@JvmStatic
@@ -122,12 +122,14 @@ enum class Keycode {
122122
val sends = IntArray(2)
123123
if (TextUtils.isEmpty(s)) return sends
124124
val codes: String
125-
if (!s.contains("+")) codes = s else {
126-
val ss = s.split("\\+").toTypedArray()
125+
if (s.contains("+")) {
126+
val ss = s.split("+").toTypedArray()
127127
val n = ss.size
128128
for (i in 0 until n - 1) if (masks.containsKey(ss[i])) sends[1] =
129129
addMask(sends[1], ss[i])
130130
codes = ss[n - 1]
131+
} else {
132+
codes = s
131133
}
132134
sends[0] = fromString(codes).ordinal
133135
return sends

app/src/main/java/com/osfans/trime/ime/keyboard/Event.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Event(Keyboard keyboard, @NonNull String s) {
6464
int[] sends = Keycode.parseSend(label); // send
6565
code = sends[0];
6666
mask = sends[1];
67-
if (code >= 0) return;
67+
if (code > 0 || mask > 0) return;
6868
if (parseAction(label)) return;
6969
s = label; // key
7070
label = null;
@@ -238,7 +238,7 @@ public static int getClickCode(String s) {
238238
int keyCode = -1;
239239
if (TextUtils.isEmpty(s)) { // 空鍵
240240
keyCode = 0;
241-
} else if (Keycode.fromString(s) != Keycode.UNKNOWN) { // 字母數字
241+
} else if (Keycode.fromString(s) != Keycode.VoidSymbol) { // 字母數字
242242
keyCode = Keycode.keyCodeOf(s);
243243
} else if (Key.getSymbols().contains(s)) { // 可見符號
244244
keyCode = Key.getSymbolStart() + Key.getSymbols().indexOf(s);
@@ -262,6 +262,9 @@ public static int[] getRimeEvent(int code, int mask) {
262262
if (hasModifier(mask, KeyEvent.META_SYM_ON)) m |= Rime.META_SYM_ON;
263263
if (hasModifier(mask, KeyEvent.META_META_ON)) m |= Rime.META_META_ON;
264264
if (mask == Rime.META_RELEASE_ON) m |= Rime.META_RELEASE_ON;
265+
Timber.d(
266+
"<Event> getRimeEvent()\tcode=%d, mask=%d, name=%s\toutput key=%d, meta=%d",
267+
code, mask, Keycode.keyNameOf(code), i, m);
265268
return new int[] {i, m};
266269
}
267270

app/src/main/java/com/osfans/trime/ime/keyboard/Key.java

+49-46
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.osfans.trime.data.Config;
2929
import com.osfans.trime.ime.enums.KeyEventType;
3030
import com.osfans.trime.util.ConfigGetter;
31+
import java.util.Locale;
3132
import java.util.Map;
3233
import timber.log.Timber;
3334

@@ -62,10 +63,7 @@ public class Key {
6263
private static String symbols;
6364
private static final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
6465
private final Keyboard mKeyboard;
65-
private Event ascii;
66-
private Event composing;
67-
private Event has_menu;
68-
private Event paging;
66+
6967
private boolean send_bindings = true;
7068
private int width;
7169
private int height;
@@ -117,34 +115,29 @@ public Key(Context context, Keyboard parent, Map<String, Object> mk) {
117115
this(parent);
118116
String s;
119117
Config config = Config.get(context);
120-
for (int i = 0; i < EVENT_NUM; i++) {
121-
String[] eventTypes =
122-
new String[] {
123-
"click", "long_click", "swipe_left", "swipe_right", "swipe_up", "swipe_down", "combo"
124-
};
125-
String eventType = eventTypes[i];
126-
s = Config.getString(mk, eventType);
127-
if (!TextUtils.isEmpty(s)) events[i] = new Event(mKeyboard, s);
128-
else if (i == KeyEventType.CLICK.ordinal()) events[i] = new Event(mKeyboard, "");
129-
}
130-
s = ConfigGetter.getString(mk, "composing", "");
131-
if (!TextUtils.isEmpty(s)) composing = new Event(mKeyboard, s);
132-
s = ConfigGetter.getString(mk, "has_menu", "");
133-
if (!TextUtils.isEmpty(s)) has_menu = new Event(mKeyboard, s);
134-
s = ConfigGetter.getString(mk, "paging", "");
135-
if (!TextUtils.isEmpty(s)) paging = new Event(mKeyboard, s);
136-
if (composing != null || has_menu != null || paging != null)
137-
mKeyboard.getmComposingKeys().add(this);
138-
s = ConfigGetter.getString(mk, "ascii", "");
139-
if (!TextUtils.isEmpty(s)) ascii = new Event(mKeyboard, s);
140-
label = ConfigGetter.getString(mk, "label", "");
141-
labelSymbol = ConfigGetter.getString(mk, "label_symbol", "");
142-
hint = ConfigGetter.getString(mk, "hint", "");
143-
if (mk.containsKey("send_bindings")) {
144-
send_bindings = ConfigGetter.getBoolean(mk, "send_bindings", true);
145-
} else if (composing == null && has_menu == null && paging == null) {
146-
send_bindings = false;
118+
{
119+
boolean hasComposingKey = false;
120+
121+
for (int i = 0; i < EVENT_NUM - 1; i++) {
122+
String eventType = (KeyEventType.Companion.valueOf(i)).toString().toLowerCase(Locale.ROOT);
123+
s = ConfigGetter.getString(mk, eventType, "");
124+
if (!TextUtils.isEmpty(s)) {
125+
events[i] = new Event(mKeyboard, s);
126+
if (i < KeyEventType.COMBO.ordinal()) hasComposingKey = true;
127+
} else if (i == KeyEventType.CLICK.ordinal()) events[i] = new Event(mKeyboard, "");
128+
}
129+
if (hasComposingKey) mKeyboard.getComposingKeys().add(this);
130+
131+
label = ConfigGetter.getString(mk, "label", "");
132+
labelSymbol = ConfigGetter.getString(mk, "label_symbol", "");
133+
hint = ConfigGetter.getString(mk, "hint", "");
134+
if (mk.containsKey("send_bindings")) {
135+
send_bindings = ConfigGetter.getBoolean(mk, "send_bindings", true);
136+
} else if (!hasComposingKey) {
137+
send_bindings = false;
138+
}
147139
}
140+
148141
mKeyboard.setModiferKey(getCode(), this);
149142
key_text_size = ConfigGetter.getPixel(mk, "key_text_size", 0);
150143
symbol_text_size = ConfigGetter.getPixel(mk, "symbol_text_size", 0);
@@ -534,22 +527,26 @@ public boolean isShiftLock() {
534527
*/
535528
public boolean sendBindings(int type) {
536529
Event e = null;
537-
if (type > 0 && type <= EVENT_NUM) e = events[type];
530+
if (type != KeyEventType.CLICK.ordinal() && type >= 0 && type <= EVENT_NUM) e = events[type];
538531
if (e != null) return true;
539-
if (ascii != null && Rime.isAsciiMode()) return false;
532+
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode()) return false;
540533
if (send_bindings) {
541-
if (paging != null && Rime.isPaging()) return true;
542-
if (has_menu != null && Rime.hasMenu()) return true;
543-
if (composing != null && Rime.isComposing()) return true;
534+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging()) return true;
535+
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu()) return true;
536+
if (events[KeyEventType.COMPOSING.ordinal()] != null && Rime.isComposing()) return true;
544537
}
545538
return false;
546539
}
547540

548541
private Event getEvent() {
549-
if (ascii != null && Rime.isAsciiMode()) return ascii;
550-
if (paging != null && Rime.isPaging()) return paging;
551-
if (has_menu != null && Rime.hasMenu()) return has_menu;
552-
if (composing != null && Rime.isComposing()) return composing;
542+
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode())
543+
return events[KeyEventType.ASCII.ordinal()];
544+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging())
545+
return events[KeyEventType.PAGING.ordinal()];
546+
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu())
547+
return events[KeyEventType.HAS_MENU.ordinal()];
548+
if (events[KeyEventType.COMPOSING.ordinal()] != null && Rime.isComposing())
549+
return events[KeyEventType.COMPOSING.ordinal()];
553550
return getClick();
554551
}
555552

@@ -567,13 +564,17 @@ public boolean hasEvent(int i) {
567564

568565
public Event getEvent(int i) {
569566
Event e = null;
570-
if (i > 0 && i <= EVENT_NUM) e = events[i];
567+
if (i != KeyEventType.CLICK.ordinal() && i >= 0 && i <= EVENT_NUM) e = events[i];
571568
if (e != null) return e;
572-
if (ascii != null && Rime.isAsciiMode()) return ascii;
569+
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode())
570+
return events[KeyEventType.ASCII.ordinal()];
573571
if (send_bindings) {
574-
if (paging != null && Rime.isPaging()) return paging;
575-
if (has_menu != null && Rime.hasMenu()) return has_menu;
576-
if (composing != null && Rime.isComposing()) return composing;
572+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging())
573+
return events[KeyEventType.PAGING.ordinal()];
574+
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu())
575+
return events[KeyEventType.HAS_MENU.ordinal()];
576+
if (events[KeyEventType.COMPOSING.ordinal()] != null && Rime.isComposing())
577+
return events[KeyEventType.COMPOSING.ordinal()];
577578
}
578579
return getClick();
579580
}
@@ -588,7 +589,9 @@ public int getCode(int type) {
588589

589590
public String getLabel() {
590591
Event event = getEvent();
591-
if (!TextUtils.isEmpty(label) && event == getClick() && (ascii == null && !Rime.isAsciiMode()))
592+
if (!TextUtils.isEmpty(label)
593+
&& event == getClick()
594+
&& (events[KeyEventType.ASCII.ordinal()] == null && !Rime.isAsciiMode()))
592595
return label; // 中文狀態顯示標籤
593596
return event.getLabel();
594597
}

0 commit comments

Comments
 (0)