Skip to content

Commit

Permalink
refactor(api): update context in rime engine lifecycle looper
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 4, 2024
1 parent fe9c39d commit fb3903e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
16 changes: 10 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 @@ -60,6 +60,10 @@ class Rime :
lifecycleImpl.emitState(RimeLifecycle.State.READY)
}

override fun nativeScheduleTasks() {
updateContext()
}

override fun nativeFinalize() {
exitRime()
}
Expand All @@ -81,25 +85,25 @@ class Rime :
modifiers: UInt,
): Boolean =
withRimeContext {
processRimeKey(value, modifiers.toInt()).also { if (it) updateContext() }
processRimeKey(value, modifiers.toInt())
}

override suspend fun processKey(
value: KeyValue,
modifiers: KeyModifiers,
): Boolean =
withRimeContext {
processRimeKey(value.value, modifiers.toInt()).also { if (it) updateContext() }
processRimeKey(value.value, modifiers.toInt())
}

override suspend fun selectCandidate(idx: Int): Boolean =
withRimeContext {
selectRimeCandidate(idx).also { if (it) updateContext() }
selectRimeCandidate(idx)
}

override suspend fun forgetCandidate(idx: Int): Boolean =
withRimeContext {
forgetRimeCandidate(idx).also { if (it) updateContext() }
forgetRimeCandidate(idx)
}

override suspend fun availableSchemata(): Array<SchemaItem> = withRimeContext { getAvailableRimeSchemaList() }
Expand All @@ -120,12 +124,11 @@ class Rime :
schema ?: schemaItemCached
}

override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { updateContext() } }
override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition() }

override suspend fun clearComposition() =
withRimeContext {
clearRimeComposition()
updateContext()
}

override suspend fun setRuntimeOption(
Expand Down Expand Up @@ -154,6 +157,7 @@ class Rime :
is RimeNotification.SchemaNotification -> schemaItemCached = notif.value
else -> {}
}
updateContext()
}

private fun handleRimeResponse(response: RimeResponse) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class RimeDispatcher(
interface RimeLooper {
fun nativeStartup(fullCheck: Boolean)

fun nativeScheduleTasks()

fun nativeFinalize()
}

Expand Down Expand Up @@ -92,6 +94,7 @@ class RimeDispatcher(
while (isActive && isRunning.get()) {
// TODO: because we have nothing to block currently,
// here we use a channel to wait for a signal.
looper.nativeScheduleTasks()
runBlocking { channel.receive() }
while (true) {
val block = queue.poll() ?: break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package com.osfans.trime.ime.text
import android.text.InputType
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.data.prefs.AppPrefs
Expand Down Expand Up @@ -119,11 +118,9 @@ class TextInputManager(
private fun handleRimeNotification(notification: RimeNotification<*>) {
if (notification is RimeNotification.SchemaNotification) {
SchemaManager.init(notification.value.id)
Rime.updateStatus()
trime.recreateInputView()
trime.inputView?.switchBoard(InputView.Board.Main)
} else if (notification is RimeNotification.OptionNotification) {
Rime.updateContext() // 切換中英文、簡繁體時更新候選
val value = notification.value.value
when (val option = notification.value.option) {
"ascii_mode" -> {
Expand Down

0 comments on commit fb3903e

Please sign in to comment.