Skip to content

Commit b1daf38

Browse files
committed
refactor: apply the new runtime option setter and getter as more as possible
feat: add new `toggleRuntimeOption` to new api interface
1 parent 61dc1ca commit b1daf38

5 files changed

Lines changed: 39 additions & 33 deletions

File tree

app/src/main/java/com/osfans/trime/core/Rime.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ class Rime : RimeApi, RimeLifecycleOwner {
109109
getRimeOption(option)
110110
}
111111

112+
override suspend fun toggleRuntimeOption(option: String): Unit =
113+
withRimeContext {
114+
setRimeOption(option, !getRimeOption(option))
115+
}
116+
112117
private fun handleRimeNotification(notif: RimeNotification<*>) {
113118
when (notif) {
114119
is RimeNotification.SchemaNotification -> schemaItemCached = notif.value
@@ -286,25 +291,11 @@ class Rime : RimeApi, RimeLifecycleOwner {
286291
}
287292
}
288293

289-
@JvmStatic
290-
fun setOption(
291-
option: String,
292-
value: Boolean,
293-
) {
294-
measureTimeMillis {
295-
setRimeOption(option, value)
296-
}.also { Timber.d("Took $it ms to set $option to $value") }
297-
}
298-
299294
@JvmStatic
300295
fun getOption(option: String): Boolean {
301296
return getRimeOption(option)
302297
}
303298

304-
fun toggleOption(option: String) {
305-
setOption(option, !getOption(option))
306-
}
307-
308299
@JvmStatic
309300
fun setCaretPos(caretPos: Int) {
310301
setRimeCaretPos(caretPos)

app/src/main/java/com/osfans/trime/core/RimeApi.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ interface RimeApi {
4141
)
4242

4343
suspend fun getRuntimeOption(option: String): Boolean
44+
45+
suspend fun toggleRuntimeOption(option: String)
4446
}

app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
180180
private fun updateRimeOption(): Boolean {
181181
try {
182182
if (shouldUpdateRimeOption) {
183-
Rime.setOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
184-
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
183+
postRimeJob {
184+
setRuntimeOption("soft_cursor", prefs.keyboard.softCursorEnabled) // 軟光標
185+
setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal) // 水平模式
186+
}
185187
shouldUpdateRimeOption = false
186188
}
187189
} catch (e: Exception) {
@@ -258,12 +260,14 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
258260

259261
fun inputSymbol(text: String) {
260262
textInputManager!!.onPress(KeyEvent.KEYCODE_UNKNOWN)
261-
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
262-
val asciiPunch = Rime.isAsciiPunch
263-
if (asciiPunch) Rime.setOption("ascii_punct", false)
264-
textInputManager!!.onText("{Escape}$text")
265-
if (asciiPunch) Rime.setOption("ascii_punct", true)
266-
self!!.selectLiquidKeyboard(-1)
263+
postRimeJob {
264+
if (Rime.isAsciiMode) setRuntimeOption("ascii_mode", false)
265+
val asciiPunch = Rime.isAsciiPunch
266+
if (asciiPunch) setRuntimeOption("ascii_punct", false)
267+
textInputManager!!.onText("{Escape}$text")
268+
if (asciiPunch) setRuntimeOption("ascii_punct", true)
269+
selectLiquidKeyboard(-1)
270+
}
267271
}
268272

269273
fun selectLiquidKeyboard(tabIndex: Int) {

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.core.content.ContextCompat
1515
import com.osfans.trime.core.Rime
1616
import com.osfans.trime.core.RimeNotification.OptionNotification
1717
import com.osfans.trime.daemon.RimeSession
18+
import com.osfans.trime.daemon.launchOnReady
1819
import com.osfans.trime.data.prefs.AppPrefs
1920
import com.osfans.trime.data.schema.SchemaManager
2021
import com.osfans.trime.data.theme.Theme
@@ -86,7 +87,9 @@ class KeyboardWindow(
8687
if (it.isLock) lastLockKeyboardId = target
8788
dispatchCapsState(it::setShifted)
8889
if (Rime.isAsciiMode != it.currentAsciiMode) {
89-
Rime.setOption("ascii_mode", it.currentAsciiMode)
90+
rime.launchOnReady { api ->
91+
api.setRuntimeOption("ascii_mode", it.currentAsciiMode)
92+
}
9093
}
9194
// TODO:为避免过量重构,这里暂时将 currentKeyboard 同步到 KeyboardSwitcher
9295
KeyboardSwitcher.currentKeyboard = it
@@ -202,13 +205,15 @@ class KeyboardWindow(
202205
}
203206
switchKeyboard(targetKeyboard)
204207
currentKeyboard?.let {
205-
if (tempAsciiMode) {
206-
if (!Rime.isAsciiMode) Rime.setOption("ascii_mode", true)
207-
} else if (theme.generalStyle.resetASCIIMode) {
208-
if (it.resetAsciiMode) {
209-
if (Rime.isAsciiMode != it.asciiMode) Rime.setOption("ascii_mode", it.asciiMode)
210-
} else {
211-
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
208+
rime.launchOnReady { api ->
209+
if (tempAsciiMode) {
210+
if (!Rime.isAsciiMode) api.setRuntimeOption("ascii_mode", true)
211+
} else if (theme.generalStyle.resetASCIIMode) {
212+
if (it.resetAsciiMode) {
213+
if (Rime.isAsciiMode != it.asciiMode) api.setRuntimeOption("ascii_mode", it.asciiMode)
214+
} else {
215+
if (Rime.isAsciiMode) api.setRuntimeOption("ascii_mode", false)
216+
}
212217
}
213218
}
214219
}

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,10 @@ class TextInputManager(
203203
)
204204
if (needSendUpRimeKey) {
205205
if (shouldUpdateRimeOption) {
206-
Rime.setOption("soft_cursors", prefs.keyboard.softCursorEnabled)
207-
Rime.setOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
206+
rime.launchOnReady {
207+
it.setRuntimeOption("soft_cursors", prefs.keyboard.softCursorEnabled)
208+
it.setRuntimeOption("_horizontal", ThemeManager.activeTheme.generalStyle.horizontal)
209+
}
208210
shouldUpdateRimeOption = false
209211
}
210212
// todo 释放按键可能不对
@@ -220,7 +222,9 @@ class TextInputManager(
220222
event ?: return
221223
when (event.code) {
222224
KeyEvent.KEYCODE_SWITCH_CHARSET -> { // Switch status
223-
Rime.toggleOption(event.getToggle())
225+
rime.launchOnReady {
226+
it.toggleRuntimeOption(event.getToggle())
227+
}
224228
trime.commitRimeText()
225229
}
226230
KeyEvent.KEYCODE_LANGUAGE_SWITCH -> { // Switch IME

0 commit comments

Comments
 (0)