Skip to content

Commit

Permalink
refactor: add Char.isAsciiPrintable function
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jun 22, 2024
1 parent 576d382 commit 2df3ae5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
9 changes: 2 additions & 7 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/osfans/trime/util/Char.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.osfans.trime.util

@Suppress("NOTHING_TO_INLINE")
inline fun Char.isAsciiPrintable(): Boolean = code in 32 until 127

0 comments on commit 2df3ae5

Please sign in to comment.