Skip to content

Commit

Permalink
Revert "refactor: apply the new runtime option setter and getter as m…
Browse files Browse the repository at this point in the history
…ore as possible"

This reverts commit b1daf38, which may cause the input dispatch timed out.
  • Loading branch information
WhiredPlanck committed Aug 24, 2024
1 parent db08b6d commit 5159f57
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
19 changes: 14 additions & 5 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ class Rime : RimeApi, RimeLifecycleOwner {
getRimeOption(option)
}

override suspend fun toggleRuntimeOption(option: String): Unit =
withRimeContext {
setRimeOption(option, !getRimeOption(option))
}

private fun handleRimeNotification(notif: RimeNotification<*>) {
when (notif) {
is RimeNotification.SchemaNotification -> schemaItemCached = notif.value
Expand Down Expand Up @@ -297,11 +292,25 @@ class Rime : RimeApi, RimeLifecycleOwner {
}
}

@JvmStatic
fun setOption(
option: String,
value: Boolean,
) {
measureTimeMillis {
setRimeOption(option, value)
}.also { Timber.d("Took $it ms to set $option to $value") }
}

@JvmStatic
fun getOption(option: String): Boolean {
return getRimeOption(option)
}

fun toggleOption(option: String) {
setOption(option, !getOption(option))
}

@JvmStatic
fun setCaretPos(caretPos: Int) {
setRimeCaretPos(caretPos)
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 @@ -41,6 +41,4 @@ interface RimeApi {
)

suspend fun getRuntimeOption(option: String): Boolean

suspend fun toggleRuntimeOption(option: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private fun updateRimeOption(): Boolean {
try {
if (shouldUpdateRimeOption) {
postRimeJob {
setRuntimeOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
}
Rime.setOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
shouldUpdateRimeOption = false
}
} catch (e: Exception) {
Expand Down Expand Up @@ -260,14 +258,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {

fun inputSymbol(text: String) {
textInputManager!!.onPress(KeyEvent.KEYCODE_UNKNOWN)
postRimeJob {
if (Rime.isAsciiMode) setRuntimeOption("ascii_mode", false)
val asciiPunch = Rime.isAsciiPunch
if (asciiPunch) setRuntimeOption("ascii_punct", false)
textInputManager!!.onText("{Escape}$text")
if (asciiPunch) setRuntimeOption("ascii_punct", true)
selectLiquidKeyboard(-1)
}
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
val asciiPunch = Rime.isAsciiPunch
if (asciiPunch) Rime.setOption("ascii_punct", false)
textInputManager!!.onText("{Escape}$text")
if (asciiPunch) Rime.setOption("ascii_punct", true)
self!!.selectLiquidKeyboard(-1)
}

fun selectLiquidKeyboard(tabIndex: Int) {
Expand Down
21 changes: 8 additions & 13 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.core.content.ContextCompat
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification.OptionNotification
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.schema.SchemaManager
import com.osfans.trime.data.theme.Theme
Expand Down Expand Up @@ -87,9 +86,7 @@ class KeyboardWindow(
if (it.isLock) lastLockKeyboardId = target
dispatchCapsState(it::setShifted)
if (Rime.isAsciiMode != it.currentAsciiMode) {
rime.launchOnReady { api ->
api.setRuntimeOption("ascii_mode", it.currentAsciiMode)
}
Rime.setOption("ascii_mode", it.currentAsciiMode)
}
// TODO:为避免过量重构,这里暂时将 currentKeyboard 同步到 KeyboardSwitcher
KeyboardSwitcher.currentKeyboard = it
Expand Down Expand Up @@ -205,15 +202,13 @@ class KeyboardWindow(
}
switchKeyboard(targetKeyboard)
currentKeyboard?.let {
rime.launchOnReady { api ->
if (tempAsciiMode) {
if (!Rime.isAsciiMode) api.setRuntimeOption("ascii_mode", true)
} else if (theme.generalStyle.resetASCIIMode) {
if (it.resetAsciiMode) {
if (Rime.isAsciiMode != it.asciiMode) api.setRuntimeOption("ascii_mode", it.asciiMode)
} else {
if (Rime.isAsciiMode) api.setRuntimeOption("ascii_mode", false)
}
if (tempAsciiMode) {
if (!Rime.isAsciiMode) Rime.setOption("ascii_mode", true)
} else if (theme.generalStyle.resetASCIIMode) {
if (it.resetAsciiMode) {
if (Rime.isAsciiMode != it.asciiMode) Rime.setOption("ascii_mode", it.asciiMode)
} else {
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ class TextInputManager(
)
if (needSendUpRimeKey) {
if (shouldUpdateRimeOption) {
rime.launchOnReady {
it.setRuntimeOption("soft_cursors", prefs.keyboard.softCursorEnabled)
it.setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
}
Rime.setOption("soft_cursors", prefs.keyboard.softCursorEnabled)
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
shouldUpdateRimeOption = false
}
// todo 释放按键可能不对
Expand All @@ -222,9 +220,7 @@ class TextInputManager(
event ?: return
when (event.code) {
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
rime.launchOnReady {
it.toggleRuntimeOption(event.getToggle())
}
Rime.toggleOption(event.getToggle())
trime.commitRimeText()
}
KeyEvent.KEYCODE_LANGUAGE_SWITCH -> { // Switch IME
Expand Down

0 comments on commit 5159f57

Please sign in to comment.