Skip to content

Commit

Permalink
refactor: apply the new runtime option setter and getter as more as p…
Browse files Browse the repository at this point in the history
…ossible

feat: add new `toggleRuntimeOption` to new api interface
  • Loading branch information
WhiredPlanck committed Aug 19, 2024
1 parent 61dc1ca commit b1daf38
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
19 changes: 5 additions & 14 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ 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 @@ -286,25 +291,11 @@ 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: 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 @@ -41,4 +41,6 @@ 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,8 +180,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private fun updateRimeOption(): Boolean {
try {
if (shouldUpdateRimeOption) {
Rime.setOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
postRimeJob {
setRuntimeOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
}
shouldUpdateRimeOption = false
}
} catch (e: Exception) {
Expand Down Expand Up @@ -258,12 +260,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {

fun inputSymbol(text: String) {
textInputManager!!.onPress(KeyEvent.KEYCODE_UNKNOWN)
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)
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)
}
}

fun selectLiquidKeyboard(tabIndex: Int) {
Expand Down
21 changes: 13 additions & 8 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,6 +15,7 @@ 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 @@ -86,7 +87,9 @@ class KeyboardWindow(
if (it.isLock) lastLockKeyboardId = target
dispatchCapsState(it::setShifted)
if (Rime.isAsciiMode != it.currentAsciiMode) {
Rime.setOption("ascii_mode", it.currentAsciiMode)
rime.launchOnReady { api ->
api.setRuntimeOption("ascii_mode", it.currentAsciiMode)
}
}
// TODO:为避免过量重构,这里暂时将 currentKeyboard 同步到 KeyboardSwitcher
KeyboardSwitcher.currentKeyboard = it
Expand Down Expand Up @@ -202,13 +205,15 @@ class KeyboardWindow(
}
switchKeyboard(targetKeyboard)
currentKeyboard?.let {
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)
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)
}
}
}
}
Expand Down
10 changes: 7 additions & 3 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,8 +203,10 @@ class TextInputManager(
)
if (needSendUpRimeKey) {
if (shouldUpdateRimeOption) {
Rime.setOption("soft_cursors", prefs.keyboard.softCursorEnabled)
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
rime.launchOnReady {
it.setRuntimeOption("soft_cursors", prefs.keyboard.softCursorEnabled)
it.setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
}
shouldUpdateRimeOption = false
}
// todo 释放按键可能不对
Expand All @@ -220,7 +222,9 @@ class TextInputManager(
event ?: return
when (event.code) {
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
Rime.toggleOption(event.getToggle())
rime.launchOnReady {
it.toggleRuntimeOption(event.getToggle())
}
trime.commitRimeText()
}
KeyEvent.KEYCODE_LANGUAGE_SWITCH -> { // Switch IME
Expand Down

0 comments on commit b1daf38

Please sign in to comment.