Skip to content

Commit

Permalink
refactor: judge key up state by modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Dec 9, 2024
1 parent 19c5cab commit e45399d
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/core/KeyModifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ value class KeyModifiers(
val meta get() = has(KeyModifier.Meta)
val capsLock get() = has(KeyModifier.Lock)

val release get() = has(KeyModifier.Release)

val metaState: Int get() {
var metaState = 0
if (alt) metaState = KeyEvent.META_ALT_ON or KeyEvent.META_ALT_LEFT_ON
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,27 @@ class Rime :
override suspend fun processKey(
value: Int,
modifiers: UInt,
up: Boolean,
): Boolean =
withRimeContext {
processRimeKey(value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(KeyValue(value), KeyModifiers(modifiers), up)
keyEventCallback(KeyValue(value), KeyModifiers(modifiers))
}
}
}

override suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
up: Boolean,
): Boolean =
withRimeContext {
processRimeKey(value.value, modifiers.toInt()).also {
if (it) {
ipcResponseCallback()
} else {
keyEventCallback(value, modifiers, up)
keyEventCallback(value, modifiers)
}
}
}
Expand Down Expand Up @@ -468,9 +466,8 @@ class Rime :
private fun keyEventCallback(
value: KeyValue,
modifiers: KeyModifiers,
up: Boolean,
) {
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers, value.value, up))
handleRimeEvent(RimeEvent.EventType.Key, RimeEvent.KeyEvent.Data(value, modifiers, value.value))
}

private fun <T> handleRimeEvent(
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ interface RimeApi {
suspend fun processKey(
value: Int,
modifiers: UInt = 0u,
up: Boolean = false,
): Boolean

suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
up: Boolean = false,
): Boolean

suspend fun selectCandidate(idx: Int): Boolean
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/osfans/trime/core/RimeEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ sealed class RimeEvent<T>(
val value: KeyValue,
val modifiers: KeyModifiers,
val unicode: Int,
val up: Boolean,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.text.InputType
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.Window
Expand Down Expand Up @@ -245,7 +244,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
return
}
val eventTime = SystemClock.uptimeMillis()
if (it.up) {
if (it.modifiers.release) {
sendUpKeyEvent(eventTime, keyCode, it.modifiers.metaState)
} else {
sendDownKeyEvent(eventTime, keyCode, it.modifiers.metaState)
Expand Down Expand Up @@ -714,19 +713,18 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

private fun forwardKeyEvent(event: KeyEvent): Boolean {
val up = event.action == MotionEvent.ACTION_UP
val modifiers = KeyModifiers.fromKeyEvent(event)
val charCode = event.unicodeChar
if (charCode > 0 && charCode != '\t'.code && charCode != '\n'.code) {
postRimeJob {
processKey(charCode, modifiers.modifiers, up)
processKey(charCode, modifiers.modifiers)
}
return true
}
val keyVal = KeyValue.fromKeyEvent(event)
if (keyVal.value != RimeKeyMapping.RimeKey_VoidSymbol) {
postRimeJob {
processKey(keyVal, modifiers, up)
processKey(keyVal, modifiers)
}
return true
}
Expand Down

0 comments on commit e45399d

Please sign in to comment.