Skip to content

Commit

Permalink
refactor: move RimeResponse into RimeEvent as IpcResponse(Event)
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 17, 2024
1 parent 19005e6 commit 82c4aa0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 64 deletions.
52 changes: 27 additions & 25 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Rime :

lifecycleImpl.emitState(RimeLifecycle.State.READY)

requestRimeResponse()
ipcResponseCallback()
SchemaManager.init(getCurrentRimeSchema())
}

Expand All @@ -81,7 +81,7 @@ class Rime :
withRimeContext {
processRimeKey(value, modifiers.toInt()).also {
if (it) {
requestRimeResponse()
ipcResponseCallback()
} else {
keyEventCallback(KeyValue(value), KeyModifiers(modifiers))
}
Expand All @@ -95,7 +95,7 @@ class Rime :
withRimeContext {
processRimeKey(value.value, modifiers.toInt()).also {
if (it) {
requestRimeResponse()
ipcResponseCallback()
} else {
keyEventCallback(value, modifiers)
}
Expand All @@ -104,12 +104,12 @@ class Rime :

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

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

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

override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { if (it) requestRimeResponse() } }
override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { if (it) ipcResponseCallback() } }

override suspend fun clearComposition() =
withRimeContext {
clearRimeComposition()
requestRimeResponse()
ipcResponseCallback()
}

override suspend fun setRuntimeOption(
Expand Down Expand Up @@ -177,19 +177,20 @@ class Rime :
"start" -> OpenCCDictManager.buildOpenCCDict()
}
}
is RimeResponse -> {
it.status?.let {
val status = InputStatus.fromStatus(it)
inputStatusCached = status
inputStatus = it // for compatibility

val item = SchemaItem.fromStatus(it)
if (item != schemaItemCached) {
schemaItemCached = item
is RimeEvent.IpcResponseEvent ->
it.data.let event@{ data ->
data.status?.let {
val status = InputStatus.fromStatus(it)
inputStatusCached = status
inputStatus = it // for compatibility

val item = SchemaItem.fromStatus(it)
if (item != schemaItemCached) {
schemaItemCached = item
}
}
data.context?.let { inputContext = it } // for compatibility
}
it.context?.let { inputContext = it } // for compatibility
}
else -> {}
}
}
Expand Down Expand Up @@ -299,7 +300,7 @@ class Rime :
return processRimeKey(keycode, mask).also {
Timber.d("processKey ${if (it) "success" else "failed"}")
if (it) {
requestRimeResponse()
ipcResponseCallback()
} else {
keyEventCallback(KeyValue(keycode), KeyModifiers.of(mask))
}
Expand All @@ -314,7 +315,7 @@ class Rime :
sequence.toString().replace("{}", "{braceleft}{braceright}"),
).also {
Timber.d("simulateKeySequence ${if (it) "success" else "failed"}")
if (it) requestRimeResponse()
if (it) ipcResponseCallback()
}
}

Expand All @@ -334,7 +335,7 @@ class Rime :
@JvmStatic
fun setCaretPos(caretPos: Int) {
setRimeCaretPos(caretPos)
requestRimeResponse()
ipcResponseCallback()
}

// init
Expand Down Expand Up @@ -498,10 +499,11 @@ class Rime :
callbackFlow_.tryEmit(notification)
}

fun requestRimeResponse() {
val response = RimeResponse(getRimeCommit(), getRimeContext(), getRimeStatus())
Timber.d("Got Rime response: $response")
callbackFlow_.tryEmit(response)
private fun ipcResponseCallback() {
handleRimeEvent(
RimeEvent.EventType.IpcResponse,
RimeEvent.IpcResponseEvent.Data(getRimeCommit(), getRimeContext(), getRimeStatus()),
)
}

private fun keyEventCallback(
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ sealed class RimeEvent<T>(
) : RimeCallback {
abstract val eventType: EventType

data class IpcResponseEvent(
override val data: Data,
) : RimeEvent<IpcResponseEvent.Data>(data) {
override val eventType = EventType.IpcResponse

data class Data(
val commit: RimeProto.Commit?,
val context: RimeProto.Context?,
val status: RimeProto.Status?,
)
}

data class KeyEvent(
override val data: Data,
) : RimeEvent<KeyEvent.Data>(data) {
Expand All @@ -22,6 +34,7 @@ sealed class RimeEvent<T>(
}

enum class EventType {
IpcResponse,
Key,
}

Expand All @@ -30,6 +43,9 @@ sealed class RimeEvent<T>(
type: EventType,
data: T,
) = when (type) {
EventType.IpcResponse -> {
IpcResponseEvent(data as IpcResponseEvent.Data)
}
EventType.Key ->
KeyEvent(data as KeyEvent.Data)
}
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/com/osfans/trime/core/RimeResponse.kt

This file was deleted.

37 changes: 19 additions & 18 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.core.CandidateItem
import com.osfans.trime.core.RimeCallback
import com.osfans.trime.core.RimeEvent
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.core.RimeResponse
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ColorManager
Expand Down Expand Up @@ -334,25 +334,26 @@ class InputView(
}
}
}
is RimeResponse -> {
val ctx = it.context
if (ctx != null) {
broadcaster.onInputContextUpdate(ctx)
val candidates = ctx.menu.candidates.map { CandidateItem(it.comment ?: "", it.text) }
val isLastPage = ctx.menu.isLastPage
val previous = ctx.menu.run { pageSize * pageNumber }
val highlightedIdx = ctx.menu.highlightedCandidateIndex
if (composition.isPopupWindowEnabled) {
val sticky = composition.composition.update(ctx)
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx, sticky)
} else {
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx)
}
if (candidates.isEmpty()) {
compactCandidate.refreshUnrolled()
is RimeEvent.IpcResponseEvent ->
it.data.let event@{
val ctx = it.context
if (ctx != null) {
broadcaster.onInputContextUpdate(ctx)
val candidates = ctx.menu.candidates.map { CandidateItem(it.comment ?: "", it.text) }
val isLastPage = ctx.menu.isLastPage
val previous = ctx.menu.run { pageSize * pageNumber }
val highlightedIdx = ctx.menu.highlightedCandidateIndex
if (composition.isPopupWindowEnabled) {
val sticky = composition.composition.update(ctx)
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx, sticky)
} else {
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx)
}
if (candidates.isEmpty()) {
compactCandidate.refreshUnrolled()
}
}
}
}
else -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import com.osfans.trime.core.RimeEvent
import com.osfans.trime.core.RimeKeyMapping
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.core.RimeProto
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 @@ -285,16 +284,17 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
}
}
is RimeResponse -> {
val (commit, ctx) = it
if (commit?.text?.isNotEmpty() == true) {
commitText(commit.text)
}
if (ctx != null) {
updateComposingText(ctx)
is RimeEvent.IpcResponseEvent ->
it.data.let event@{
val (commit, ctx) = it
if (commit?.text?.isNotEmpty() == true) {
commitText(commit.text)
}
if (ctx != null) {
updateComposingText(ctx)
}
updateComposing()
}
updateComposing()
}
is RimeEvent.KeyEvent ->
it.data.let event@{
val keyCode = it.value.keyCode
Expand Down

0 comments on commit 82c4aa0

Please sign in to comment.