ShiftState refactored

This commit is contained in:
merkost 2023-05-20 11:36:03 +10:00
parent 763ea43c61
commit 000c907a02
4 changed files with 27 additions and 25 deletions

View file

@ -1,8 +1,10 @@
package com.simplemobiletools.keyboard.helpers
const val SHIFT_OFF = 0
const val SHIFT_ON_ONE_CHAR = 1
const val SHIFT_ON_PERMANENT = 2
enum class ShiftState {
OFF,
ON_ONE_CHAR,
ON_PERMANENT;
}
// limit the count of alternative characters that show up at long pressing a key
const val MAX_KEYS_PER_MINI_ROW = 9
@ -11,6 +13,7 @@ const val MAX_KEYS_PER_MINI_ROW = 9
const val VIBRATE_ON_KEYPRESS = "vibrate_on_keypress"
const val SHOW_POPUP_ON_KEYPRESS = "show_popup_on_keypress"
const val SHOW_KEY_BORDERS = "show_key_borders"
const val SENTENCES_CAPITALIZATION = "show_key_borders"
const val LAST_EXPORTED_CLIPS_FOLDER = "last_exported_clips_folder"
const val KEYBOARD_LANGUAGE = "keyboard_language"
const val HEIGHT_MULTIPLIER = "height_multiplier"

View file

@ -34,7 +34,7 @@ class MyKeyboard {
var mKeyboardHeightMultiplier: Float = 1F
/** Is the keyboard in the shifted state */
var mShiftState = SHIFT_OFF
var mShiftState = ShiftState.OFF
/** Total height of the keyboard, including the padding and keys */
var mHeight = 0
@ -312,12 +312,11 @@ class MyKeyboard {
mRows.add(row)
}
fun setShifted(shiftState: Int): Boolean {
fun setShifted(shiftState: ShiftState): Boolean {
if (this.mShiftState != shiftState) {
this.mShiftState = shiftState
return true
}
return false
}

View file

@ -75,9 +75,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
private fun updateShiftKeyState() {
if (keyboardMode == KEYBOARD_LETTERS) {
val editorInfo = currentInputEditorInfo
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != SHIFT_ON_PERMANENT) {
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
keyboard?.setShifted(SHIFT_ON_ONE_CHAR)
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
keyboardView?.invalidateAllKeys()
}
}
@ -96,8 +96,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
when (code) {
MyKeyboard.KEYCODE_DELETE -> {
if (keyboard!!.mShiftState == SHIFT_ON_ONE_CHAR) {
keyboard!!.mShiftState = SHIFT_OFF
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) {
keyboard!!.mShiftState = ShiftState.OFF
}
val selectedText = inputConnection.getSelectedText(0)
@ -112,10 +112,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
MyKeyboard.KEYCODE_SHIFT -> {
if (keyboardMode == KEYBOARD_LETTERS) {
when {
keyboard!!.mShiftState == SHIFT_ON_PERMANENT -> keyboard!!.mShiftState = SHIFT_OFF
System.currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!!.mShiftState = SHIFT_ON_PERMANENT
keyboard!!.mShiftState == SHIFT_ON_ONE_CHAR -> keyboard!!.mShiftState = SHIFT_OFF
keyboard!!.mShiftState == SHIFT_OFF -> keyboard!!.mShiftState = SHIFT_ON_ONE_CHAR
keyboard!!.mShiftState == ShiftState.ON_PERMANENT -> keyboard!!.mShiftState = ShiftState.OFF
System.currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!!.mShiftState = ShiftState.ON_PERMANENT
keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR -> keyboard!!.mShiftState = ShiftState.OFF
keyboard!!.mShiftState == ShiftState.OFF -> keyboard!!.mShiftState = ShiftState.ON_ONE_CHAR
}
lastShiftPressTS = System.currentTimeMillis()
@ -157,7 +157,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
}
else -> {
var codeChar = code.toChar()
if (Character.isLetter(codeChar) && keyboard!!.mShiftState > SHIFT_OFF) {
if (Character.isLetter(codeChar) && keyboard!!.mShiftState > ShiftState.OFF) {
codeChar = Character.toUpperCase(codeChar)
}
@ -173,8 +173,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
inputConnection.commitText(codeChar.toString(), 1)
}
if (keyboard!!.mShiftState == SHIFT_ON_ONE_CHAR && keyboardMode == KEYBOARD_LETTERS) {
keyboard!!.mShiftState = SHIFT_OFF
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR && keyboardMode == KEYBOARD_LETTERS) {
keyboard!!.mShiftState = ShiftState.OFF
keyboardView!!.invalidateAllKeys()
}
}
@ -191,9 +191,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
val editorInfo = currentInputEditorInfo
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != SHIFT_ON_PERMANENT) {
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
keyboard?.setShifted(SHIFT_ON_ONE_CHAR)
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
}
}

View file

@ -455,7 +455,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @param shifted whether or not to enable the state of the shift key
* @return true if the shift key state changed, false if there was no change
*/
private fun setShifted(shiftState: Int) {
private fun setShifted(shiftState: ShiftState) {
if (mKeyboard?.setShifted(shiftState) == true) {
invalidateAllKeys()
}
@ -466,7 +466,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @return true if the shift is in a pressed state, false otherwise
*/
private fun isShifted(): Boolean {
return (mKeyboard?.mShiftState ?: SHIFT_OFF) > SHIFT_OFF
return (mKeyboard?.mShiftState ?: ShiftState.OFF) > ShiftState.OFF
}
private fun setPopupOffset(x: Int, y: Int) {
@ -479,7 +479,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private fun adjustCase(label: CharSequence): CharSequence? {
var newLabel: CharSequence? = label
if (newLabel != null && newLabel.isNotEmpty() && mKeyboard!!.mShiftState > SHIFT_OFF && newLabel.length < 3 && Character.isLowerCase(newLabel[0])) {
if (newLabel != null && newLabel.isNotEmpty() && mKeyboard!!.mShiftState != ShiftState.OFF && newLabel.length < 3 && Character.isLowerCase(newLabel[0])) {
newLabel = newLabel.toString().uppercase(Locale.getDefault())
}
return newLabel
@ -602,8 +602,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} else if (key.icon != null && mKeyboard != null) {
if (code == KEYCODE_SHIFT) {
val drawableId = when (mKeyboard!!.mShiftState) {
SHIFT_OFF -> R.drawable.ic_caps_outline_vector
SHIFT_ON_ONE_CHAR -> R.drawable.ic_caps_vector
ShiftState.OFF -> R.drawable.ic_caps_outline_vector
ShiftState.ON_ONE_CHAR -> R.drawable.ic_caps_vector
else -> R.drawable.ic_caps_underlined_vector
}
key.icon = resources.getDrawable(drawableId)
@ -1129,7 +1129,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mMiniKeyboardSelectedKeyIndex = selectedKeyIndex
mMiniKeyboard!!.invalidateAllKeys()
val miniShiftStatus = if (isShifted()) SHIFT_ON_PERMANENT else SHIFT_OFF
val miniShiftStatus = if (isShifted()) ShiftState.ON_PERMANENT else ShiftState.OFF
mMiniKeyboard!!.setShifted(miniShiftStatus)
mPopupKeyboard.contentView = mMiniKeyboardContainer
mPopupKeyboard.width = mMiniKeyboardContainer!!.measuredWidth