From 2df3ae5dc231ab32db177276cdd64f3b19392928 Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Fri, 21 Jun 2024 22:02:34 +0800 Subject: [PATCH] refactor: add `Char.isAsciiPrintable` function --- app/src/main/java/com/osfans/trime/core/Rime.kt | 9 ++------- .../java/com/osfans/trime/ime/text/TextInputManager.kt | 4 ++-- app/src/main/java/com/osfans/trime/util/Char.kt | 4 ++++ 3 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/osfans/trime/util/Char.kt diff --git a/app/src/main/java/com/osfans/trime/core/Rime.kt b/app/src/main/java/com/osfans/trime/core/Rime.kt index 36d4620458..73fe73bd04 100644 --- a/app/src/main/java/com/osfans/trime/core/Rime.kt +++ b/app/src/main/java/com/osfans/trime/core/Rime.kt @@ -9,6 +9,7 @@ import com.osfans.trime.data.opencc.OpenCCDictManager import com.osfans.trime.data.prefs.AppPrefs import com.osfans.trime.data.schema.SchemaManager import com.osfans.trime.util.appContext +import com.osfans.trime.util.isAsciiPrintable import com.osfans.trime.util.isStorageAvailable import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow @@ -214,15 +215,9 @@ class Rime : RimeApi, RimeLifecycleOwner { } } - private fun isValidText(text: CharSequence?): Boolean { - if (text.isNullOrEmpty()) return false - val ch = text.toString().codePointAt(0) - return ch in 0x20..0x7f - } - @JvmStatic fun simulateKeySequence(sequence: CharSequence): Boolean { - if (!isValidText(sequence)) return false + if (!sequence.first().isAsciiPrintable()) return false Timber.d("simulateKeySequence: $sequence") return simulateRimeKeySequence( sequence.toString().replace("{}", "{braceleft}{braceright}"), diff --git a/app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt b/app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt index a421ab3f0f..c78bad32ac 100644 --- a/app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt +++ b/app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt @@ -35,7 +35,7 @@ import com.osfans.trime.ui.main.settings.ColorPickerDialog import com.osfans.trime.ui.main.settings.KeySoundEffectPickerDialog import com.osfans.trime.ui.main.settings.ThemePickerDialog import com.osfans.trime.util.ShortcutUtils -import com.osfans.trime.util.startsWithAsciiChar +import com.osfans.trime.util.isAsciiPrintable import kotlinx.coroutines.Job import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -461,7 +461,7 @@ class TextInputManager( override fun onText(text: CharSequence?) { text ?: return - if (!text.startsWithAsciiChar() && Rime.isComposing) { + if (!text.first().isAsciiPrintable() && Rime.isComposing) { Rime.commitComposition() trime.commitRimeText() } diff --git a/app/src/main/java/com/osfans/trime/util/Char.kt b/app/src/main/java/com/osfans/trime/util/Char.kt new file mode 100644 index 0000000000..ece88f2c1e --- /dev/null +++ b/app/src/main/java/com/osfans/trime/util/Char.kt @@ -0,0 +1,4 @@ +package com.osfans.trime.util + +@Suppress("NOTHING_TO_INLINE") +inline fun Char.isAsciiPrintable(): Boolean = code in 32 until 127