Skip to content

Commit f46de22

Browse files
committed
refactor: avoid to use Rime companion directly
1 parent 5fe2f9d commit f46de22

File tree

6 files changed

+52
-79
lines changed

6 files changed

+52
-79
lines changed

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,11 @@ class Rime :
195195
private fun handleRimeMessage(it: RimeMessage<*>) {
196196
when (it) {
197197
is RimeMessage.SchemaMessage -> {
198-
statusCached = getRimeStatus()!!
198+
getRimeStatus()?.let { statusCached = it }
199199
SchemaManager.init(it.data.id)
200200
}
201201
is RimeMessage.OptionMessage -> {
202-
getRimeStatus()?.let {
203-
statusCached = it
204-
inputStatus = it // for compatibility
205-
}
202+
getRimeStatus()?.let { statusCached = it }
206203
SchemaManager.updateSwitchOptions()
207204
}
208205
is RimeMessage.DeployMessage -> {
@@ -212,11 +209,7 @@ class Rime :
212209
}
213210
is RimeMessage.ResponseMessage ->
214211
it.data.let event@{ data ->
215-
data.status.let {
216-
statusCached = it
217-
inputStatus = it // for compatibility
218-
}
219-
inputContext = data.context // for compatibility
212+
statusCached = data.status
220213
compositionCached = data.context.composition
221214
menuCached = data.context.menu
222215
rawInputCached = data.context.input
@@ -254,8 +247,6 @@ class Rime :
254247
}
255248

256249
companion object {
257-
private var inputContext: RimeProto.Context? = null
258-
private var inputStatus: RimeProto.Status? = null
259250
private val messageFlow_ =
260251
MutableSharedFlow<RimeMessage<*>>(
261252
extraBufferCapacity = 15,
@@ -268,24 +259,6 @@ class Rime :
268259
System.loadLibrary("rime_jni")
269260
}
270261

271-
@JvmStatic
272-
val isComposing get() = inputStatus?.isComposing ?: false
273-
274-
@JvmStatic
275-
val isAsciiMode get() = inputStatus?.isAsciiMode ?: true
276-
277-
@JvmStatic
278-
val currentSchemaName get() = inputStatus?.schemaName ?: ""
279-
280-
@JvmStatic
281-
fun hasMenu(): Boolean = !inputContext?.menu?.candidates.isNullOrEmpty()
282-
283-
@JvmStatic
284-
fun hasLeft(): Boolean = hasMenu() && inputContext?.menu?.pageNumber != 0
285-
286-
@JvmStatic
287-
fun showAsciiPunch(): Boolean = inputStatus?.isAsciiPunch == true || inputStatus?.isAsciiMode == true
288-
289262
@JvmStatic
290263
fun simulateKeySequence(sequence: CharSequence): Boolean {
291264
if (!sequence.first().isAsciiPrintable()) return false
@@ -313,9 +286,6 @@ class Rime :
313286
}
314287
}
315288

316-
@JvmStatic
317-
fun getOption(option: String): Boolean = getRimeOption(option)
318-
319289
// init
320290
@JvmStatic
321291
external fun startupRime(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ class CommonKeyboardActionListener(
319319
}
320320

321321
override fun onText(text: CharSequence) {
322-
if (!text.first().isAsciiPrintable() && Rime.isComposing) {
322+
val status = rime.run { statusCached }
323+
if (!text.first().isAsciiPrintable() && status.isComposing) {
323324
service.postRimeJob { commitComposition() }
324325
}
325326

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ package com.osfans.trime.ime.keyboard
77
import android.graphics.drawable.Drawable
88
import android.view.KeyEvent
99
import androidx.annotation.ColorInt
10-
import com.osfans.trime.core.Rime.Companion.hasLeft
11-
import com.osfans.trime.core.Rime.Companion.hasMenu
12-
import com.osfans.trime.core.Rime.Companion.isAsciiMode
13-
import com.osfans.trime.core.Rime.Companion.isComposing
14-
import com.osfans.trime.core.Rime.Companion.showAsciiPunch
10+
import com.osfans.trime.daemon.RimeDaemon
1511
import com.osfans.trime.data.theme.ColorManager
1612
import com.osfans.trime.data.theme.KeyActionManager
1713
import com.osfans.trime.data.theme.model.TextKeyboard
@@ -22,6 +18,8 @@ class Key(
2218
private val parent: Keyboard,
2319
private val selfConfig: TextKeyboard.TextKey? = null,
2420
) {
21+
private val rime = RimeDaemon.getFirstSessionOrNull()!!
22+
2523
val keyActions: Map<KeyBehavior, KeyAction> =
2624
buildMap {
2725
selfConfig?.behaviors?.forEach {
@@ -218,7 +216,7 @@ class Key(
218216
when (click?.shiftLock) {
219217
"long" -> false // 长按锁定
220218
"click" -> true // 点击锁定
221-
"ascii_long" -> !isAsciiMode // 英文长按锁定,中文点击锁定
219+
"ascii_long" -> !rime.run { statusCached }.isAsciiMode // 英文长按锁定,中文点击锁定
222220
else -> false
223221
}
224222

@@ -242,11 +240,14 @@ class Key(
242240
fun getAction(behavior: KeyBehavior): KeyAction? =
243241
keyActions[behavior]?.takeIf { behavior != KeyBehavior.CLICK } ?: checkKeyAction(sendBindings) ?: click
244242

245-
private fun checkKeyAction(): KeyAction? =
246-
keyActions[KeyBehavior.ASCII].takeIf { isAsciiMode }
247-
?: keyActions[KeyBehavior.PAGING]?.takeIf { hasLeft() }
248-
?: keyActions[KeyBehavior.HAS_MENU]?.takeIf { hasMenu() }
249-
?: keyActions[KeyBehavior.COMPOSING]?.takeIf { isComposing }
243+
private fun checkKeyAction(): KeyAction? {
244+
val status = rime.run { statusCached }
245+
val menu = rime.run { menuCached }
246+
return keyActions[KeyBehavior.ASCII].takeIf { status.isAsciiMode }
247+
?: keyActions[KeyBehavior.PAGING]?.takeIf { menu.pageNumber != 0 }
248+
?: keyActions[KeyBehavior.HAS_MENU]?.takeIf { menu.candidates.isNotEmpty() }
249+
?: keyActions[KeyBehavior.COMPOSING]?.takeIf { status.isComposing }
250+
}
250251

251252
private fun checkKeyAction(sendBindings: Boolean): KeyAction? = checkKeyAction().takeIf { sendBindings }
252253

@@ -259,8 +260,8 @@ class Key(
259260
when {
260261
label.isNotEmpty() &&
261262
keyAction == click &&
262-
keyActions[KeyBehavior.ASCII] == null &&
263-
!showAsciiPunch() -> label
263+
keyActions.containsKey(KeyBehavior.ASCII) &&
264+
!rime.run { statusCached }.let { it.isAsciiMode || it.isAsciiPunch } -> label
264265
else -> keyAction!!.getLabel(parent) // 中文狀態顯示標籤
265266
}
266267

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package com.osfans.trime.ime.keyboard
66

77
import android.view.KeyEvent
8-
import com.osfans.trime.core.Rime
8+
import com.osfans.trime.daemon.RimeDaemon
99
import com.osfans.trime.data.prefs.AppPrefs
1010
import com.osfans.trime.data.theme.ThemeManager
1111
import com.osfans.trime.ime.enums.Keycode
@@ -47,27 +47,32 @@ class KeyAction(
4747
private val hookShiftNum get() = AppPrefs.defaultInstance().keyboard.hookShiftNum
4848
private val hookShiftSymbol get() = AppPrefs.defaultInstance().keyboard.hookShiftSymbol
4949

50+
private val rime = RimeDaemon.getFirstSessionOrNull()!!
51+
5052
private fun adjustCase(
5153
str: String,
5254
keyboard: Keyboard,
53-
): String =
54-
if (str.length == 1 && (keyboard.isShifted || (!Rime.isAsciiMode && keyboard.isLabelUppercase))) {
55+
): String {
56+
val status = rime.run { statusCached }
57+
return if (str.length == 1 && (keyboard.isShifted || (!status.isAsciiMode && keyboard.isLabelUppercase))) {
5558
str.uppercase()
5659
} else {
5760
str
5861
}
62+
}
5963

6064
fun getLabel(keyboard: Keyboard): String {
6165
if (states.isNotEmpty() && toggle.isNotEmpty()) {
62-
return states[if (Rime.getOption(toggle)) 1 else 0]
66+
return states[if (rime.run { getRuntimeOption(toggle) }) 1 else 0]
6367
}
6468
if (keyboard.isOnlyShiftOn) {
65-
if (!hookShiftNum && !Rime.isComposing && code in KeyEvent.KEYCODE_0..KeyEvent.KEYCODE_9) {
69+
val status = rime.run { statusCached }
70+
if (!hookShiftNum && !status.isComposing && code in KeyEvent.KEYCODE_0..KeyEvent.KEYCODE_9) {
6671
return adjustCase(shiftLabel, keyboard)
6772
}
6873
if (!hookShiftSymbol &&
6974
// TODO: 判断中英模式仅能正确处理已配置映射的符号,对于未配置映射的符号,即使在中文模式下也能上屏 Shift 切换的符号。
70-
Rime.isAsciiMode &&
75+
status.isAsciiMode &&
7176
(
7277
code in KeyEvent.KEYCODE_GRAVE..KeyEvent.KEYCODE_SLASH ||
7378
code == KeyEvent.KEYCODE_COMMA ||
@@ -123,7 +128,7 @@ class KeyAction(
123128
if (label.isEmpty()) {
124129
label =
125130
when (code) {
126-
KeyEvent.KEYCODE_SPACE -> Rime.currentSchemaName
131+
KeyEvent.KEYCODE_SPACE -> rime.run { statusCached }.schemaName
127132
KeyEvent.KEYCODE_UNKNOWN -> ""
128133
else -> Keycode.getDisplayLabel(code, modifier)
129134
}
@@ -153,7 +158,7 @@ class KeyAction(
153158
} else if (label.isEmpty()) {
154159
label =
155160
when (code) {
156-
KeyEvent.KEYCODE_SPACE -> Rime.currentSchemaName
161+
KeyEvent.KEYCODE_SPACE -> rime.run { statusCached }.schemaName
157162
KeyEvent.KEYCODE_UNKNOWN -> ""
158163
else -> Keycode.getDisplayLabel(code, modifier)
159164
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import androidx.core.graphics.createBitmap
2424
import androidx.core.graphics.withClip
2525
import androidx.lifecycle.findViewTreeLifecycleOwner
2626
import androidx.lifecycle.lifecycleScope
27-
import com.osfans.trime.core.Rime
27+
import com.osfans.trime.daemon.RimeDaemon
2828
import com.osfans.trime.data.prefs.AppPrefs
2929
import com.osfans.trime.data.theme.ColorManager
3030
import com.osfans.trime.data.theme.FontManager
@@ -51,6 +51,7 @@ class KeyboardView(
5151
private val keyboard: Keyboard,
5252
private val keyPreviewChoreographer: KeyPreviewChoreographer,
5353
) : View(context) {
54+
private val rime = RimeDaemon.getFirstSessionOrNull()!!
5455
private var mCurrentKeyIndex = NOT_A_KEY
5556
private val keyTextSize = theme.generalStyle.keyTextSize
5657
private val labelTextSize =
@@ -134,8 +135,6 @@ class KeyboardView(
134135
textAlign = Paint.Align.CENTER
135136
}
136137

137-
var showKeyHint = !Rime.getOption("_hide_key_hint")
138-
var showKeySymbol = !Rime.getOption("_hide_key_symbol")
139138
private var labelEnter: String = theme.generalStyle.enterLabel.default
140139

141140
fun onEnterKeyLabelUpdate(label: String) {
@@ -541,6 +540,8 @@ class KeyboardView(
541540
// Turn off drop shadow
542541
paint.clearShadowLayer()
543542

543+
val showKeySymbol = rime.run { !getRuntimeOption("_hide_key_symbol") }
544+
val showKeyHint = rime.run { !getRuntimeOption("_hide_key_hint") }
544545
if (showKeySymbol || showKeyHint) {
545546
paint.typeface = FontManager.getTypeface("symbol_font")
546547
floatArrayOf(key.symbolTextSize, symbolTextSize)

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.view.inputmethod.EditorInfo
1111
import android.widget.FrameLayout
1212
import androidx.core.content.ContextCompat
1313
import com.osfans.trime.R
14-
import com.osfans.trime.core.Rime
1514
import com.osfans.trime.core.RimeMessage
1615
import com.osfans.trime.core.SchemaItem
1716
import com.osfans.trime.daemon.RimeSession
@@ -257,8 +256,9 @@ class KeyboardWindow(
257256
}
258257

259258
private fun dispatchCapsState(setShift: (Boolean, Boolean) -> Unit) {
259+
val status = rime.run { statusCached }
260260
// TODO: 启用自动首句大写后,点击方向键时,保持Shift锁定状态功能将无法生效
261-
if (theme.generalStyle.autoCaps && Rime.isAsciiMode && currentKeyboardView?.isCapsOn == false) {
261+
if (theme.generalStyle.autoCaps && status.isAsciiMode && currentKeyboardView?.isCapsOn == false) {
262262
setShift(false, cursorCapsMode != 0)
263263
}
264264
}
@@ -277,25 +277,20 @@ class KeyboardWindow(
277277
}
278278

279279
override fun onRimeOptionUpdated(value: RimeMessage.OptionMessage.Data) {
280-
when (val opt = value.option) {
281-
"_hide_key_hint" -> currentKeyboardView?.showKeyHint = !value.value
282-
"_hide_key_symbol" -> currentKeyboardView?.showKeySymbol = !value.value
283-
else -> {
284-
when {
285-
opt.startsWith("_keyboard_") -> {
286-
val target = opt.removePrefix("_keyboard_")
287-
if (target.isNotEmpty()) {
288-
switchKeyboard(target)
289-
}
290-
}
291-
opt.startsWith("_key_") -> {
292-
val what = opt.removePrefix("_key_")
293-
if (what.isNotEmpty() && value.value) {
294-
commonKeyboardActionListener
295-
.listener
296-
.onAction(KeyActionManager.getAction(what))
297-
}
298-
}
280+
val option = value.option
281+
when {
282+
option.startsWith("_keyboard_") -> {
283+
val target = option.removePrefix("_keyboard_")
284+
if (target.isNotEmpty()) {
285+
switchKeyboard(target)
286+
}
287+
}
288+
option.startsWith("_key_") -> {
289+
val what = option.removePrefix("_key_")
290+
if (what.isNotEmpty() && value.value) {
291+
commonKeyboardActionListener
292+
.listener
293+
.onAction(KeyActionManager.getAction(what))
299294
}
300295
}
301296
}

0 commit comments

Comments
 (0)