Skip to content

Commit

Permalink
refactor: rewrite ascii mode switch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed May 7, 2024
1 parent 0f3644e commit 57e873a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ class Rime : RimeApi, RimeLifecycleOwner {
option: String,
value: Boolean,
) {
setRimeOption(option, value)
measureTimeMillis {
setRimeOption(option, value)
}.also { Timber.d("Took $it ms to set $option to $value") }
}

@JvmStatic
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ class Keyboard() {
/** Width of the screen available to fit the keyboard */
private val mDisplayWidth: Int

/** Keyboard mode, or zero, if none. */
private var mAsciiMode = 0
var isResetAsciiMode = false
/** Keyboard default ascii mode */
var asciiMode = false
private set
var currentAsciiMode = false
var resetAsciiMode = true
private set

var landscapeKeyboard: String? = null
private set
private var mLandscapePercent = 0
Expand Down Expand Up @@ -158,9 +161,10 @@ class Keyboard() {
val keyboardConfig = getKeyboardConfig(name)

mLabelTransform = obtainString(keyboardConfig, "label_transform", "none")
mAsciiMode = obtainInt(keyboardConfig, "ascii_mode", 1)
if (mAsciiMode == 0) asciiKeyboard = obtainString(keyboardConfig, "ascii_keyboard", "")
isResetAsciiMode = obtainBoolean(keyboardConfig, "reset_ascii_mode", false)
asciiMode = obtainInt(keyboardConfig, "ascii_mode", 1) == 1
currentAsciiMode = asciiMode
if (asciiMode) asciiKeyboard = obtainString(keyboardConfig, "ascii_keyboard", "")
resetAsciiMode = obtainBoolean(keyboardConfig, "reset_ascii_mode", true)
landscapeKeyboard = obtainString(keyboardConfig, "landscape_keyboard", "")
mLandscapePercent =
obtainInt(
Expand Down Expand Up @@ -747,8 +751,6 @@ class Keyboard() {
return IntArray(0)
}

val asciiMode: Boolean
get() = mAsciiMode != 0
val isLandscapeSplit: Boolean
get() = mLandscapePercent > 0
val isLabelUppercase: Boolean
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ class TextInputManager(
trime.performEscape()
}
isComposable = false
var tempAsciiMode = if (shouldResetAsciiMode) false else null
var forceAsciiMode = false
val keyboardType =
when (info.imeOptions and EditorInfo.IME_FLAG_FORCE_ASCII) {
EditorInfo.IME_FLAG_FORCE_ASCII -> {
tempAsciiMode = true
forceAsciiMode = true
".ascii"
}
else -> {
Expand All @@ -164,6 +164,7 @@ class TextInputManager(
InputType.TYPE_CLASS_PHONE,
InputType.TYPE_CLASS_DATETIME,
-> {
forceAsciiMode = true
"number"
}
InputType.TYPE_CLASS_TEXT -> {
Expand All @@ -182,8 +183,7 @@ class TextInputManager(
" inputAttrsRaw" + inputAttrsRaw +
"; InputType" + (inputAttrsRaw and InputType.TYPE_MASK_VARIATION),
)

tempAsciiMode = true
forceAsciiMode = true
".ascii"
}
else -> null.also { isComposable = true }
Expand All @@ -202,10 +202,19 @@ class TextInputManager(

// style/reset_ascii_mode指定了弹出键盘时是否重置ASCII状态。
// 键盘的reset_ascii_mode指定了重置时是否重置到keyboard的ascii_mode描述的状态。
if (shouldResetAsciiMode && KeyboardSwitcher.currentKeyboard.isResetAsciiMode) {
tempAsciiMode = KeyboardSwitcher.currentKeyboard.asciiMode
KeyboardSwitcher.currentKeyboard.let {
if (forceAsciiMode) {
if (!Rime.isAsciiMode) Rime.setOption("ascii_mode", true)
return@let
}
if (shouldResetAsciiMode) {
if (it.resetAsciiMode) {
if (Rime.isAsciiMode != it.asciiMode) Rime.setOption("ascii_mode", it.asciiMode)
} else {
if (Rime.isAsciiMode) Rime.setOption("ascii_mode", false)
}
}
}
tempAsciiMode?.let { Rime.setOption("ascii_mode", it) }
isComposable = isComposable && !rime.run { isEmpty() }
if (!trime.onEvaluateInputViewShown()) {
// Show candidate view when using physical keyboard
Expand All @@ -225,6 +234,7 @@ class TextInputManager(
"ascii_mode" -> {
InputFeedbackManager.ttsLanguage =
locales[if (value) 1 else 0]
KeyboardSwitcher.currentKeyboard.currentAsciiMode = value
}
"_hide_bar",
"_hide_candidate",
Expand Down Expand Up @@ -296,8 +306,10 @@ class TextInputManager(
KeyEvent.KEYCODE_EISU -> { // Switch keyboard
KeyboardSwitcher.switchKeyboard(event.select)
/** Set ascii mode according to keyboard's settings, can not place into [Rime.handleRimeNotification] */
if (shouldResetAsciiMode && KeyboardSwitcher.currentKeyboard.isResetAsciiMode) {
Rime.setOption("ascii_mode", KeyboardSwitcher.currentKeyboard.asciiMode)
KeyboardSwitcher.currentKeyboard.let {
if (Rime.isAsciiMode != it.currentAsciiMode) {
Rime.setOption("ascii_mode", it.currentAsciiMode)
}
}
trime.bindKeyboardToInputView()
trime.updateComposing()
Expand Down

0 comments on commit 57e873a

Please sign in to comment.