Skip to content

Commit bd12ff0

Browse files
lzlv312ENOA-ANSUL-EDEN
authored andcommitted
fix(keyboard): resolve incorrect behavior of reset_ascii_mode and ascii_mode
Fix the issue where reset_ascii_mode and ascii_mode commands don't behave as configured. The problem was caused by improper handling of ASCII mode state transitions in the keyboard window management. Now these commands correctly respect the configured settings and properly switch between ASCII and non-ASCII input modes as expected by users. Co-authored-by: ENOA-ANSUL-EDEN <i@enoa.me>
1 parent 44b4454 commit bd12ff0

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class Keyboard(
120120
/** Keyboard default ascii mode */
121121
val asciiMode = selfConfig?.asciiMode ?: false
122122
val resetAsciiMode = selfConfig?.resetAsciiMode ?: true
123+
var lastAsciiMode: Boolean = asciiMode
123124

124125
val landscapeKeyboard: String? = selfConfig?.landscapeKeyboard
125126
private val preferredSplitPercent by AppPrefs.defaultInstance().keyboard.splitSpacePercent

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class KeyboardWindow(
8787

8888
override fun onCreateView(): View {
8989
keyboardView = context.frameLayout(R.id.keyboard_view)
90-
attachKeyboard(evalKeyboard(rime.run { statusCached }.schemaId))
90+
attachKeyboard(evalKeyboard(".default"))
9191
return keyboardView
9292
}
9393

@@ -97,6 +97,7 @@ class KeyboardWindow(
9797
keyboardView.removeView(it)
9898
it.keyboardActionListener = null
9999
}
100+
currentKeyboard?.lastAsciiMode = rime.run { statusCached }.isAsciiMode
100101
}
101102

102103
private fun selectKeyboardConfig(name: String): TextKeyboard? {
@@ -111,26 +112,33 @@ class KeyboardWindow(
111112
private fun attachKeyboard(target: String) {
112113
currentKeyboardId = target
113114
lastKeyboardId = target
114-
val newConfig = selectKeyboardConfig(target)
115-
val newKeyboard =
116-
(currentKeyboard ?: Keyboard(theme, newConfig)).also {
117-
runBlocking {
118-
_currentKeyboardHeight.emit(it.keyboardHeight)
119-
}
120-
if (it.isLock) lastLockKeyboardId = target
121-
dispatchCapsState(it::setShifted)
122-
val isAsciiMode = rime.run { statusCached }.isAsciiMode
123-
if (isAsciiMode != it.asciiMode) {
124-
service.postRimeJob { setRuntimeOption("ascii_mode", it.asciiMode) }
125-
}
126-
// TODO:为避免过量重构,这里暂时将 currentKeyboard 同步到 KeyboardSwitcher
127-
KeyboardSwitcher.currentKeyboard = it
128-
}
129-
val newView =
130-
currentKeyboardView ?: KeyboardView(context, theme, newKeyboard, popupComponent, service).also {
131-
cachedKeyboards[target] = newKeyboard to it
115+
116+
val config = selectKeyboardConfig(target)
117+
val keyboard = currentKeyboard ?: Keyboard(theme, config)
118+
val view = currentKeyboardView ?: KeyboardView(context, theme, keyboard, popupComponent, service)
119+
120+
if (currentKeyboard == null) {
121+
cachedKeyboards[target] = keyboard to view
122+
keyboard.lastAsciiMode = keyboard.asciiMode
123+
}
124+
125+
keyboard.also {
126+
runBlocking { _currentKeyboardHeight.emit(it.keyboardHeight) }
127+
if (it.isLock) lastLockKeyboardId = target
128+
dispatchCapsState(it::setShifted)
129+
130+
val currentMode = rime.run { statusCached }.isAsciiMode
131+
val targetMode = if (it.resetAsciiMode) it.asciiMode else it.lastAsciiMode
132+
133+
if (currentMode != targetMode) {
134+
service.postRimeJob { setRuntimeOption("ascii_mode", targetMode) }
132135
}
133-
newView.let {
136+
137+
// TODO:为避免过量重构,这里暂时将 currentKeyboard 同步到 KeyboardSwitcher
138+
KeyboardSwitcher.currentKeyboard = it
139+
}
140+
141+
view.let {
134142
it.keyboardActionListener = keyboardActionListener
135143
keyboardView.apply { add(it, lParams(matchParent, matchParent)) }
136144
}
@@ -241,14 +249,9 @@ class KeyboardWindow(
241249
service.postRimeJob { setRuntimeOption("ascii_mode", true) }
242250
}
243251
} else if (theme.generalStyle.resetASCIIMode) {
244-
if (it.resetAsciiMode) {
245-
if (isAsciiMode != it.asciiMode) {
246-
service.postRimeJob { setRuntimeOption("ascii_mode", it.asciiMode) }
247-
}
248-
} else {
249-
if (isAsciiMode) {
250-
service.postRimeJob { setRuntimeOption("ascii_mode", false) }
251-
}
252+
val targetMode = if (it.resetAsciiMode) it.asciiMode else it.lastAsciiMode
253+
if (isAsciiMode != targetMode) {
254+
service.postRimeJob { setRuntimeOption("ascii_mode", targetMode) }
252255
}
253256
}
254257
}
@@ -279,7 +282,7 @@ class KeyboardWindow(
279282
}
280283

281284
override fun onRimeSchemaUpdated(schema: SchemaItem) {
282-
switchKeyboard(schema.id)
285+
switchKeyboard(".default")
283286
}
284287

285288
override fun onRimeOptionUpdated(value: RimeMessage.OptionMessage.Data) {

0 commit comments

Comments
 (0)