Skip to content

Commit

Permalink
fix: key sequence not handling commit and text preset keys and no…
Browse files Browse the repository at this point in the history
…t processed in order
  • Loading branch information
if-can authored and WhiredPlanck committed Feb 3, 2025
1 parent 88bb714 commit 2ce393c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ class CommonKeyboardActionListener(
}

override fun onAction(action: KeyAction) {
if (action.commit.isNotEmpty()) {
service.commitText(action.commit, true)
return
}
KeyboardSwitcher.currentKeyboard?.let {
if (action.getText(it).isNotEmpty()) {
onText(action.getText(it))
return
}
}
when (action.code) {
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
rime.launchOnReady { api ->
Expand Down Expand Up @@ -322,32 +332,30 @@ class CommonKeyboardActionListener(

override fun onText(text: CharSequence) {
if (!text.first().isAsciiPrintable() && Rime.isComposing) {
service.postRimeJob {
commitComposition()
}
service.postRimeJob { commitComposition() }
}

var sequence = text
while (sequence.isNotEmpty()) {
var slice: String
when {
BRACED_KEY_EVENT_WITH_ESCAPE.matches(sequence) -> {
slice = BRACED_KEY_EVENT_WITH_ESCAPE.matchEntire(sequence)?.groupValues?.get(1) ?: ""
// FIXME: rime will not handle the key sequence when
// ascii_mode is on, there may be a better solution
// for this.
if ((!Rime.simulateKeySequence(slice) || Rime.isAsciiMode) && !Rime.isComposing) {
service.commitText(slice)
}
val slice =
when {
BRACED_KEY_EVENT_WITH_ESCAPE.matches(sequence) ->
BRACED_KEY_EVENT_WITH_ESCAPE.matchEntire(sequence)?.groupValues?.get(1)
?: ""
BRACED_KEY_EVENT.matches(sequence) -> BRACED_KEY_EVENT.matchEntire(sequence)?.groupValues?.get(1) ?: ""
else -> sequence.first().toString()
}
BRACED_KEY_EVENT.matches(sequence) -> {
slice = BRACED_KEY_EVENT.matchEntire(sequence)?.groupValues?.get(1) ?: ""
onAction(KeyActionManager.getAction(slice))
}
else -> {
slice = sequence.first().toString()

service.postRimeJob {
if (slice.startsWith("{") && slice.endsWith("}")) {
onAction(KeyActionManager.getAction(slice))
} else {
if ((!Rime.simulateKeySequence(slice) || Rime.isAsciiMode) && !Rime.isComposing) {
service.commitText(slice.replace("{Escape}", ""))
}
}
}

sequence = sequence.substring(slice.length)
}
shouldReleaseKey = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class KeyboardWindow(
private val currentKeyboard: Keyboard? get() = cachedKeyboards[currentKeyboardId]?.first
private val currentKeyboardView: KeyboardView? get() = cachedKeyboards[currentKeyboardId]?.second

private val keyboardActionListener = ListenerDecorator(commonKeyboardActionListener.listener)
private val keyboardActionListener = commonKeyboardActionListener.listener

override fun onCreateView(): View {
keyboardView = context.frameLayout(R.id.keyboard_view)
Expand Down Expand Up @@ -313,25 +313,4 @@ class KeyboardWindow(
it.keyboardActionListener = null
}
}

inner class ListenerDecorator(
private val delegate: KeyboardActionListener,
) : KeyboardActionListener by delegate {
override fun onAction(action: KeyAction) {
if (action.commit.isNotEmpty()) {
// Directly commit the text and don't dispatch to Rime
service.commitText(action.commit, true)
return
}

currentKeyboard?.let {
if (action.getText(it).isNotEmpty()) {
onText(action.getText(it))
return
}
}

delegate.onAction(action)
}
}
}

0 comments on commit 2ce393c

Please sign in to comment.