mirror of
https://github.com/SimpleMobileTools/Simple-Keyboard.git
synced 2025-04-24 23:18:48 +02:00
Sentences capitalization refactored
This commit is contained in:
parent
129eea692a
commit
d14d6346a6
@ -19,30 +19,41 @@ enum class ShiftState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getShiftStateForText(context: Context, newText: CharSequence?): ShiftState {
|
fun getShiftStateForText(context: Context, newText: String?): ShiftState {
|
||||||
if (context.config.enableSentencesCapitalization.not()) return OFF
|
if (context.config.enableSentencesCapitalization.not()) {
|
||||||
|
return OFF
|
||||||
|
}
|
||||||
|
|
||||||
val twoLastSymbols = newText?.takeLast(2)
|
val twoLastSymbols = newText?.takeLast(2)
|
||||||
return when {
|
return when {
|
||||||
twoLastSymbols?.getOrNull(1) == KEYCODE_SPACE.toChar() && endOfSentenceChars.contains(twoLastSymbols.getOrNull(0)) -> {
|
shouldCapitalizeSentence(previousChar = twoLastSymbols?.getOrNull(0), currentChar = twoLastSymbols?.getOrNull(1)) -> {
|
||||||
ON_ONE_CHAR
|
ON_ONE_CHAR
|
||||||
}
|
}
|
||||||
|
else -> {
|
||||||
else -> OFF
|
OFF
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char): Boolean {
|
fun getCapitalizationOnDelete(context: Context, text: CharSequence?): ShiftState {
|
||||||
|
if (context.config.enableSentencesCapitalization.not()) {
|
||||||
|
return OFF
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (text.isNullOrEmpty() || shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))) {
|
||||||
|
ON_ONE_CHAR
|
||||||
|
} else {
|
||||||
|
OFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char?): Boolean {
|
||||||
|
if (previousChar == null || currentChar == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar)
|
return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shouldCapitalizeOnDelete(text: CharSequence?): Boolean {
|
|
||||||
if (text.isNullOrEmpty()) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,15 +95,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||||||
|
|
||||||
when (code) {
|
when (code) {
|
||||||
MyKeyboard.KEYCODE_DELETE -> {
|
MyKeyboard.KEYCODE_DELETE -> {
|
||||||
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) {
|
|
||||||
keyboard!!.setShifted(ShiftState.OFF)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.enableSentencesCapitalization) {
|
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
|
||||||
val extractedText = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
|
val extractedText = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
|
||||||
if (ShiftState.shouldCapitalizeOnDelete(text = extractedText)) {
|
keyboard!!.setShifted(ShiftState.getCapitalizationOnDelete(context = this, text = extractedText))
|
||||||
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val selectedText = inputConnection.getSelectedText(0)
|
val selectedText = inputConnection.getSelectedText(0)
|
||||||
@ -192,16 +187,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||||||
inputConnection.commitText(codeChar.toString(), 1)
|
inputConnection.commitText(codeChar.toString(), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keyboardMode == KEYBOARD_LETTERS) {
|
if (keyboardMode == KEYBOARD_LETTERS && keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
|
||||||
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) {
|
keyboard!!.setShifted(ShiftState.getShiftStateForText(this, newText = "$originalText$codeChar"))
|
||||||
keyboard!!.setShifted(ShiftState.OFF)
|
|
||||||
}
|
|
||||||
if (config.enableSentencesCapitalization && ShiftState.shouldCapitalizeSentence(
|
|
||||||
previousChar = originalText.lastOrNull(), currentChar = code.toChar()
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
|
|
||||||
}
|
|
||||||
keyboardView!!.invalidateAllKeys()
|
keyboardView!!.invalidateAllKeys()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +205,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||||||
// TODO: Change keyboardMode to enum class
|
// TODO: Change keyboardMode to enum class
|
||||||
keyboardMode = KEYBOARD_LETTERS
|
keyboardMode = KEYBOARD_LETTERS
|
||||||
val text = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0)?.text
|
val text = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0)?.text
|
||||||
val newShiftState = ShiftState.getShiftStateForText(this, text)
|
val newShiftState = ShiftState.getShiftStateForText(this, text?.toString().orEmpty())
|
||||||
|
|
||||||
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
|
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user