Skip to content

Commit

Permalink
perf: split keyboard from the event
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Feb 6, 2024
1 parent de9a181 commit 0855b55
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
49 changes: 23 additions & 26 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import timber.log.Timber
import java.util.Locale

/** [按鍵][Key]的各種事件(單擊、長按、滑動等) */
class Event(keyboard: Keyboard?, var s: String) {
// private String TAG = "Event";
private val mKeyboard: Keyboard?

class Event(var s: String) {
var code = 0
var mask = 0
private var text: String = ""
Expand Down Expand Up @@ -72,55 +69,58 @@ class Event(keyboard: Keyboard?, var s: String) {
return result
}

// TODO 进一步解耦,在Event中去除mKeyboard
private fun adjustCase(s: String): String {
private fun adjustCase(
s: String,
kb: Keyboard?,
): String {
var str = s
if (str.isEmpty()) return ""
if (str.length == 1 && mKeyboard != null && mKeyboard.needUpCase()) {
if (str.length == 1 && kb != null && kb.needUpCase()) {
str = str.uppercase(Locale.getDefault())
} else if (str.length == 1 && mKeyboard != null && !Rime.isAsciiMode &&
mKeyboard.isLabelUppercase
} else if (str.length == 1 && kb != null && !Rime.isAsciiMode &&
kb.isLabelUppercase
) {
str = str.uppercase(Locale.getDefault())
}
return str
}

fun getLabel(): String {
fun getLabel(kb: Keyboard?): String {
val state = states?.get(if (Rime.getOption(toggle)) 1 else 0)
if (state != null) return state
if (mKeyboard == null) return adjustCase(label)
if (mKeyboard.isOnlyShiftOn) {
if (kb == null) return adjustCase(label, null)
if (kb.isOnlyShiftOn) {
if (code >= KeyEvent.KEYCODE_0 && code <= KeyEvent.KEYCODE_9 &&
!AppPrefs.defaultInstance().keyboard.hookShiftNum
) {
return adjustCase(shiftLabel)
return adjustCase(shiftLabel, kb)
}
if (code >= KeyEvent.KEYCODE_GRAVE &&
code <= KeyEvent.KEYCODE_SLASH ||
code == KeyEvent.KEYCODE_COMMA ||
code == KeyEvent.KEYCODE_PERIOD
) {
if (!AppPrefs.defaultInstance().keyboard.hookShiftSymbol) return adjustCase(shiftLabel)
if (!AppPrefs.defaultInstance().keyboard.hookShiftSymbol) return adjustCase(shiftLabel, kb)
}
} else if (mKeyboard.modifer or mask and KeyEvent.META_SHIFT_ON != 0) {
return adjustCase(shiftLabel)
} else if (kb.modifer or mask and KeyEvent.META_SHIFT_ON != 0) {
return adjustCase(shiftLabel, kb)
}
return adjustCase(label)
return adjustCase(label, kb)
}

fun getText(): String {
if (text.isNotEmpty()) return adjustCase(text)
if (mKeyboard != null && mKeyboard.needUpCase() && mask == 0 &&
fun getText(kb: Keyboard?): String {
if (text.isNotEmpty()) return adjustCase(text, kb)
if (kb != null && kb.needUpCase() && mask == 0 &&
code >= KeyEvent.KEYCODE_A && code <= KeyEvent.KEYCODE_Z
) {
return adjustCase(label)
return adjustCase(label, kb)
}
return ""
}

val previewText: String
get() = preview.ifEmpty { getLabel() }
fun getPreviewText(kb: Keyboard?): String {
return preview.ifEmpty { getLabel(kb) }
}

fun getToggle(): String {
return toggle.ifEmpty { "ascii_mode" }
Expand All @@ -147,10 +147,7 @@ class Event(keyboard: Keyboard?, var s: String) {
return c == KeyEvent.KEYCODE_ALT_LEFT || c == KeyEvent.KEYCODE_ALT_RIGHT
}

constructor(s: String) : this(null, s)

init {
mKeyboard = keyboard
initHelper()
}

Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ class Key(private val mKeyboard: Keyboard) {
val typeStr = type.toString().lowercase()
s = obtainString(mk, typeStr, "")
if (s.isNotEmpty()) {
events[type.ordinal] = Event(mKeyboard, s)
events[type.ordinal] = Event(s)
if (type.ordinal < KeyEventType.COMBO.ordinal) hasComposingKey = true
} else if (type == KeyEventType.CLICK) {
events[type.ordinal] = Event(mKeyboard, "")
events[type.ordinal] = Event("")
}
}
if (hasComposingKey) mKeyboard.composingKeys.add(this)
Expand Down Expand Up @@ -450,23 +450,23 @@ class Key(private val mKeyboard: Keyboard) {
) {
label
} else {
event!!.getLabel() // 中文狀態顯示標籤
event!!.getLabel(mKeyboard) // 中文狀態顯示標籤
}
}

fun getPreviewText(type: Int): String {
return if (type == KeyEventType.CLICK.ordinal) {
event!!.previewText
event!!.getPreviewText(mKeyboard)
} else {
getEvent(type)!!.previewText
getEvent(type)!!.getPreviewText(mKeyboard)
}
}

val symbolLabel: String?
get() {
if (labelSymbol!!.isEmpty()) {
val longClick = longClick
if (longClick != null) return longClick.getLabel()
if (longClick != null) return longClick.getLabel(mKeyboard)
}
return labelSymbol
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Keyboard() {
key.width = keyWidth
key.height = keyHeight
key.gap = horizontalGap
key.events[0] = Event(this, element.toString())
key.events[0] = Event(element.toString())
column++
x += key.width + key.gap
mKeys.add(key)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/text/Composition.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.osfans.trime.data.theme.ThemeManager;
import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.ime.keyboard.Event;
import com.osfans.trime.ime.keyboard.KeyboardSwitcher;
import com.osfans.trime.util.CollectionUtils;
import com.osfans.trime.util.DimensionsKt;
import java.util.ArrayList;
Expand Down Expand Up @@ -491,7 +492,7 @@ private void appendButton(@NonNull Map<String, Object> m) {
final String label;
final Event e = new Event(CollectionUtils.obtainString(m, "click", ""));
if (m.containsKey("label")) label = CollectionUtils.obtainString(m, "label", "");
else label = e.getLabel();
else label = e.getLabel(KeyboardSwitcher.currentKeyboard);
int start, end;
String sep = null;
if (m.containsKey("start")) sep = CollectionUtils.obtainString(m, "start", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ class TextInputManager private constructor() :
activeEditorInstance.commitText(event.commit, true)
return
}
if (event.getText().isNotEmpty()) {
onText(event.getText())
if (event.getText(KeyboardSwitcher.currentKeyboard).isNotEmpty()) {
onText(event.getText(KeyboardSwitcher.currentKeyboard))
return
}
when (event.code) {
Expand Down

0 comments on commit 0855b55

Please sign in to comment.