Skip to content

Commit 775e4ee

Browse files
refactor: add targeted key appearance update mechanism
1 parent 75d6ace commit 775e4ee

7 files changed

Lines changed: 38 additions & 16 deletions

File tree

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcastReceiver.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import com.osfans.trime.ime.window.BoardWindow
1616
interface InputBroadcastReceiver {
1717
fun onStartInput(info: EditorInfo) {}
1818

19-
fun onSelectionUpdate(
20-
start: Int,
21-
end: Int,
22-
) {}
19+
fun onSelectionUpdate(start: Int, end: Int) {}
2320

2421
fun onRimeSchemaUpdated(schema: SchemaItem) {}
2522

@@ -31,6 +28,8 @@ interface InputBroadcastReceiver {
3128

3229
fun onCandidateMenuUpdate(data: MenuProto) {}
3330

31+
fun onKeyAppearanceUpdate(composing: Boolean, menu: Boolean) {}
32+
3433
fun onInputStatusUpdate(value: StatusProto) {}
3534

3635
fun onWindowAttached(window: BoardWindow) {}

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcaster.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class InputBroadcaster : InputBroadcastReceiver {
6666
receivers.forEach { it.onCandidateMenuUpdate(data) }
6767
}
6868

69+
override fun onKeyAppearanceUpdate(composing: Boolean, menu: Boolean) {
70+
receivers.forEach { it.onKeyAppearanceUpdate(composing, menu) }
71+
}
72+
6973
override fun onInputStatusUpdate(value: StatusProto) {
7074
receivers.forEach { it.onInputStatusUpdate(value) }
7175
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ class InputView(
117117
return dp(value)
118118
}
119119

120+
private var lastAppearanceState = Triple(false, false, false)
121+
122+
private fun broadcastKeyAppearanceUpdate() {
123+
val composing = rime.run { statusCached.isComposing }
124+
val hasMenu = rime.run { hasMenu }
125+
val paging = rime.run { paging }
126+
val current = Triple(composing, hasMenu, paging)
127+
if (current != lastAppearanceState) {
128+
lastAppearanceState = current
129+
broadcaster.onKeyAppearanceUpdate(current.first, current.second)
130+
}
131+
}
132+
120133
private val keyboardBottomPaddingPx: Int
121134
get() {
122135
val value =
@@ -317,6 +330,7 @@ class InputView(
317330
}
318331
else -> {}
319332
}
333+
broadcastKeyAppearanceUpdate()
320334
}
321335

322336
fun updateSelection(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Key(
2020
) {
2121
private val rime get() = RimeDaemon.getFirstSessionOrNull()!!
2222

23+
var index: Int = -1
24+
2325
val keyActions: Map<KeyBehavior, KeyAction> =
2426
buildMap {
2527
selfConfig?.behaviors?.forEach {
@@ -110,9 +112,9 @@ class Key(
110112

111113
init {
112114
if (selfConfig != null) {
113-
val hasComposingKey = selfConfig.behaviors.keys.any { it < KeyBehavior.COMBO }
114-
if (hasComposingKey) parent.composingKeys.add(this)
115-
sendBindings = selfConfig.sendBindings || hasComposingKey
115+
val hasStateDependentBehavior = selfConfig.behaviors.keys.any { it < KeyBehavior.COMBO }
116+
if (hasStateDependentBehavior) parent.appearanceStateKeys.add(this)
117+
sendBindings = selfConfig.sendBindings || hasStateDependentBehavior
116118
} else {
117119
sendBindings = true
118120
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Keyboard(
8383

8484
/** List of keys in this keyboard */
8585
private val mKeys = mutableListOf<Key>()
86-
val composingKeys = mutableListOf<Key>()
86+
val appearanceStateKeys = mutableListOf<Key>()
8787
var modifier = 0
8888
private set
8989

@@ -322,7 +322,8 @@ class Keyboard(
322322

323323
height = yPos + currentRowHeight
324324

325-
for (key in mKeys) {
325+
mKeys.forEachIndexed { index, key ->
326+
key.index = index
326327
if (key.column == 0) key.edgeFlags = key.edgeFlags or EDGE_LEFT
327328
if (key.row == 0) key.edgeFlags = key.edgeFlags or EDGE_TOP
328329
if (key.row == row) key.edgeFlags = key.edgeFlags or EDGE_BOTTOM

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class KeyboardView(
8888
children.forEach { it.invalidate() }
8989
}
9090

91+
fun invalidateKeyByIndex(index: Int) {
92+
children.find { it.id == index }?.invalidate()
93+
}
94+
9195
val isCapsOn: Boolean
9296
get() = keyboard.mShiftKey?.isOn == true
9397

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class KeyboardWindow :
7575
private var currentKeyboardId = ""
7676
private var lastKeyboardId = ""
7777
private var lastLockKeyboardId = ""
78-
private var lastComposing = true
7978
private val cachedKeyboards = mutableMapOf<String, Pair<Keyboard, KeyboardView>>()
8079
private val currentKeyboard: Keyboard? get() = cachedKeyboards[currentKeyboardId]?.first
8180
private val currentKeyboardView: KeyboardView? get() = cachedKeyboards[currentKeyboardId]?.second
@@ -266,12 +265,11 @@ class KeyboardWindow :
266265
}
267266
}
268267

269-
override fun onCompositionUpdate(data: CompositionProto) {
270-
val status = rime.run { statusCached }
271-
val isComposing = status.isComposing
272-
if (!status.isAsciiMode && lastComposing != isComposing) {
273-
lastComposing = isComposing
274-
currentKeyboardView?.invalidateAllKeys()
268+
override fun onKeyAppearanceUpdate(composing: Boolean, menu: Boolean) {
269+
if (!rime.run { statusCached }.isAsciiMode) {
270+
currentKeyboard?.appearanceStateKeys?.forEach { key ->
271+
currentKeyboardView?.invalidateKeyByIndex(key.index)
272+
}
275273
}
276274
}
277275

0 commit comments

Comments
 (0)