Skip to content

Commit

Permalink
feat(api): initial implementation of emitting rime response via share…
Browse files Browse the repository at this point in the history
…d flow
  • Loading branch information
WhiredPlanck committed Aug 30, 2024
1 parent 5e1e9bc commit c007982
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Rime : RimeApi, RimeLifecycleOwner {
override val lifecycle get() = lifecycleImpl

override val notificationFlow = notificationFlow_.asSharedFlow()
override val responseFlow = responseFlow_.asSharedFlow()
override val stateFlow get() = lifecycle.currentStateFlow

override val isReady: Boolean
Expand Down Expand Up @@ -171,6 +172,12 @@ class Rime : RimeApi, RimeLifecycleOwner {
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)

private val responseFlow_ =
MutableSharedFlow<RimeResponse>(
extraBufferCapacity = 15,
onBufferOverflow = BufferOverflow.DROP_LATEST,
)

private val notificationHandlers = ArrayList<(RimeNotification<*>) -> Unit>()

init {
Expand All @@ -190,6 +197,7 @@ class Rime : RimeApi, RimeLifecycleOwner {
inputContext = getRimeContext()
}.also { Timber.d("Took $it ms to get context") }
updateStatus()
requestRimeResponse()
}

/*
Expand Down Expand Up @@ -504,5 +512,11 @@ class Rime : RimeApi, RimeLifecycleOwner {
private fun unregisterRimeNotificationHandler(handler: (RimeNotification<*>) -> Unit) {
notificationHandlers.remove(handler)
}

fun requestRimeResponse() {
val response = RimeResponse(getRimeCommit(), getRimeContext(), getRimeStatus())
Timber.d("Got Rime response: $response")
responseFlow_.tryEmit(response)
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.SharedFlow
interface RimeApi {
val notificationFlow: SharedFlow<RimeNotification<*>>

val responseFlow: SharedFlow<RimeResponse>

val stateFlow: SharedFlow<RimeLifecycle.State>

val isReady: Boolean
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.osfans.trime.core

data class RimeResponse(
val commit: RimeProto.Commit?,
val context: RimeProto.Context?,
val status: RimeProto.Status?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.osfans.trime.BuildConfig
import com.osfans.trime.R
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeApi
import com.osfans.trime.core.RimeResponse
import com.osfans.trime.daemon.RimeDaemon
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.data.db.DraftHelper
Expand Down Expand Up @@ -65,6 +66,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import splitties.bitflags.hasFlag
Expand Down Expand Up @@ -223,6 +225,11 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
lifecycleScope.launch {
jobs.consumeEach { it.join() }
}
lifecycleScope.launch {
rime.run { responseFlow }.collect {
handleRimeResponse(it)
}
}
super.onCreate()
// MUST WRAP all code within Service onCreate() in try..catch to prevent any crash loops
try {
Expand Down Expand Up @@ -256,6 +263,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
}

private fun handleRimeResponse(response: RimeResponse) {
val (commit, _, _) = response
if (commit != null && !commit.text.isNullOrEmpty()) {
commitCharSequence(commit.text)
}
updateComposing()
}

fun inputSymbol(text: String) {
textInputManager!!.onPress(KeyEvent.KEYCODE_UNKNOWN)
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
Expand Down Expand Up @@ -567,17 +582,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
return true
}

/**
* Commits the text got from Rime.
*/
fun commitRimeText(): Boolean {
val commit = Rime.getRimeCommit()
commit?.text?.let { commitCharSequence(it) }
Timber.d("commitRimeText: updateComposing")
updateComposing()
return commit != null
}

/**
* Commit the current composing text together with the new text
*
Expand Down Expand Up @@ -779,9 +783,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private fun onRimeKey(event: IntArray): Boolean {
updateRimeOption()
// todo 改为异步处理按键事件、刷新UI
val ret = Rime.processKey(event[0], event[1])
commitRimeText()
return ret
return Rime.processKey(event[0], event[1])
}

private fun composeEvent(event: KeyEvent): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ class LiquidKeyboard(
when (currentBoardType) {
SymbolBoardType.CANDIDATE -> {
service.lifecycleScope.launch {
rime.runOnReady {
if (selectCandidate(position)) {
service.commitRimeText()
}
}
rime.runOnReady { selectCandidate(position) }
}
if (Rime.isComposing) {
service.lifecycleScope.launch {
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class TextInputManager(
// todo 释放按键可能不对
val event = Event.getRimeEvent(keyEventCode, Rime.META_RELEASE_ON)
Rime.processKey(event[0], event[1])
trime.commitRimeText()
}
Timber.d("\t<TrimeInput>\tonRelease() finish")
}
Expand All @@ -221,7 +220,9 @@ class TextInputManager(
when (event.code) {
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
Rime.toggleOption(event.getToggle())
trime.commitRimeText()
trime.lifecycleScope.launch {
rime.runOnReady { commitComposition() }
}
}
KeyEvent.KEYCODE_LANGUAGE_SWITCH -> { // Switch IME
when {
Expand Down Expand Up @@ -373,7 +374,6 @@ class TextInputManager(
if (!text.first().isAsciiPrintable() && Rime.isComposing) {
trime.postRimeJob {
commitComposition()
trime.commitRimeText()
}
}
var textToParse = text
Expand All @@ -385,10 +385,10 @@ class TextInputManager(
escapeTagMatcher.matches() -> {
target = escapeTagMatcher.group(1) ?: ""
Rime.simulateKeySequence(target)
if (!trime.commitRimeText() && !Rime.isComposing) {
trime.commitCharSequence(target)
}
trime.updateComposing()
// if (!trime.commitRimeText() && !Rime.isComposing) {
// trime.commitCharSequence(target)
// }
// trime.updateComposing()
}
propertyGroupMatcher.matches() -> {
target = propertyGroupMatcher.group(1) ?: ""
Expand All @@ -414,9 +414,7 @@ class TextInputManager(
if (prefs.keyboard.hookCandidateCommit) {
// todo 找到切换高亮候选词的API,并把此处改为模拟移动候选后发送空格
// 如果使用了lua处理候选上屏,模拟数字键、空格键是非常有必要的
trime.commitRimeText()
} else {
trime.commitRimeText()
}
}
} else if (index == 9) {
Expand Down

0 comments on commit c007982

Please sign in to comment.