Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers

import android.text.TextUtils
import android.view.inputmethod.InputConnection
import be.scri.helpers.PreferencesHelper.getIsWordByWordDeletionEnabled
import be.scri.services.GeneralKeyboardIME
import be.scri.services.GeneralKeyboardIME.Companion.MAX_TEXT_LENGTH

/**
* Handles backspace/delete events for the [GeneralKeyboardIME].
* Handles backspace/delete events for the [be.scri.services.GeneralKeyboardIME].
* Encapsulates logic for single character deletion, word-by-word deletion,
* command bar deletion, and repeating delete state.
*
* @property ime The [GeneralKeyboardIME] instance this handler is associated with.
* @property ime The [be.scri.services.GeneralKeyboardIME] instance this handler is associated with.
*/
class BackspaceHandler(
private val ime: GeneralKeyboardIME,
Expand Down Expand Up @@ -46,7 +42,10 @@ class BackspaceHandler(
} else {
val inputConnection = ime.currentInputConnection ?: return
if (TextUtils.isEmpty(inputConnection.getSelectedText(0))) {
val isWordByWordEnabled = getIsWordByWordDeletionEnabled(ime.applicationContext, ime.language)
val isWordByWordEnabled = PreferencesHelper.getIsWordByWordDeletionEnabled(
ime.applicationContext,
ime.language
)
// Only use word-by-word deletion on long press when the feature is enabled.
if (isWordByWordEnabled && isLongPress) {
deleteWordByWord(inputConnection)
Expand Down Expand Up @@ -104,7 +103,7 @@ class BackspaceHandler(
* @param inputConnection The current input connection.
*/
private fun deleteWordByWord(inputConnection: InputConnection) {
val textBeforeCursor = inputConnection.getTextBeforeCursor(MAX_TEXT_LENGTH, 0)?.toString() ?: ""
val textBeforeCursor = inputConnection.getTextBeforeCursor(GeneralKeyboardIME.Companion.MAX_TEXT_LENGTH, 0)?.toString() ?: ""

if (textBeforeCursor.isEmpty()) {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package be.scri.helpers

import android.view.inputmethod.InputConnection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers

import android.content.Context
import android.util.Log
import android.view.inputmethod.InputConnection
import be.scri.services.GeneralKeyboardIME
import be.scri.services.GeneralKeyboardIME.ScribeState

/**
* Handles key events for the [GeneralKeyboardIME].
* Handles key events for the [be.scri.services.GeneralKeyboardIME].
* This class processes raw key codes, determines the appropriate action based on the
* current keyboard state and the key pressed, and delegates to specific handlers
* or directly interacts with the [GeneralKeyboardIME] instance.
* or directly interacts with the [be.scri.services.GeneralKeyboardIME] instance.
*
* @property ime The [GeneralKeyboardIME] instance this handler is associated with.
* @property ime The [be.scri.services.GeneralKeyboardIME] instance this handler is associated with.
*/
@Suppress("TooManyFunctions")
class KeyHandler(
Expand Down Expand Up @@ -174,7 +171,7 @@ class KeyHandler(
* @param inputConnection The active input connection.
*/
private fun commitTab(inputConnection: InputConnection) {
inputConnection.commitText("\t", GeneralKeyboardIME.COMMIT_TEXT_CURSOR_POSITION)
inputConnection.commitText("\t", GeneralKeyboardIME.Companion.COMMIT_TEXT_CURSOR_POSITION)
}

/**
Expand Down Expand Up @@ -203,7 +200,7 @@ class KeyHandler(
private fun handleDeleteKey() {
ime.handleDelete(ime.isDeleteRepeating()) // pass the actual repeating status

if (ime.currentState == ScribeState.IDLE) {
if (ime.currentState == GeneralKeyboardIME.ScribeState.IDLE) {
val currentWord = ime.getLastWordBeforeCursor()
autocompletionHandler.processAutocomplete(currentWord)
suggestionHandler.processEmojiSuggestions(currentWord)
Expand All @@ -218,7 +215,7 @@ class KeyHandler(
ime.currentInputConnection?.commitText(currencySymbol, 1)

// Process emoji suggestions if in idle state.
if (ime.currentState == ScribeState.IDLE) {
if (ime.currentState == GeneralKeyboardIME.ScribeState.IDLE) {
suggestionHandler.processEmojiSuggestions(ime.getLastWordBeforeCursor())
}
}
Expand Down Expand Up @@ -257,10 +254,10 @@ class KeyHandler(
private fun handleNavigationKey(code: Int) {
val isRight = code == KeyboardBase.KEYCODE_RIGHT_ARROW
ime.currentInputConnection?.let { ic ->
val currentPos = ic.getTextBeforeCursor(GeneralKeyboardIME.MAX_TEXT_LENGTH, 0)?.length ?: 0
val currentPos = ic.getTextBeforeCursor(GeneralKeyboardIME.Companion.MAX_TEXT_LENGTH, 0)?.length ?: 0
val newPos =
if (isRight) {
val textAfter = ic.getTextAfterCursor(GeneralKeyboardIME.MAX_TEXT_LENGTH, 0)?.toString() ?: ""
val textAfter = ic.getTextAfterCursor(GeneralKeyboardIME.Companion.MAX_TEXT_LENGTH, 0)?.toString() ?: ""
(currentPos + 1).coerceAtMost(currentPos + textAfter.length)
} else {
(currentPos - 1).coerceAtLeast(0)
Expand Down Expand Up @@ -348,16 +345,16 @@ class KeyHandler(
private fun handleDefaultKey(code: Int) {
val isCommandBarActive =
when (ime.currentState) {
ScribeState.TRANSLATE,
ScribeState.CONJUGATE,
ScribeState.PLURAL,
GeneralKeyboardIME.ScribeState.TRANSLATE,
GeneralKeyboardIME.ScribeState.CONJUGATE,
GeneralKeyboardIME.ScribeState.PLURAL,
-> true // use command bar for actual commands
else -> false // use main input field for IDLE and SELECT_COMMAND
}

ime.handleElseCondition(code, ime.keyboardMode, isCommandBarActive)

if (ime.currentState == ScribeState.IDLE) {
if (ime.currentState == GeneralKeyboardIME.ScribeState.IDLE) {
val currentWord = ime.getLastWordBeforeCursor()
autocompletionHandler.processAutocomplete(currentWord)
suggestionHandler.processEmojiSuggestions(currentWord)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers

import android.annotation.SuppressLint
Expand All @@ -13,7 +11,6 @@ import android.util.Log
import android.util.TypedValue
import android.util.Xml
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
import androidx.annotation.XmlRes
import be.scri.R
import be.scri.services.GeneralKeyboardIME
Expand Down Expand Up @@ -53,7 +50,7 @@ class KeyboardBase {
private var mDisplayWidth = 0

/** What icon should we show at Enter key */
var mEnterKeyType = IME_ACTION_NONE
var mEnterKeyType = EditorInfo.IME_ACTION_NONE

/** Keyboard rows */
private val mRows = ArrayList<Row?>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers

import be.scri.services.GeneralKeyboardIME
import be.scri.services.GeneralKeyboardIME.ScribeState

/**
* Processes key events specifically related to the space key.
* This includes handling "period on double tap" logic, committing spaces
* in normal input mode or command bar mode, and interacting with suggestions.
*
* @property ime The [GeneralKeyboardIME] instance this processor is associated with.
* @property ime The [be.scri.services.GeneralKeyboardIME] instance this processor is associated with.
* @property suggestionHandler The [SuggestionHandler] to manage suggestions.
*/
class SpaceKeyProcessor(
Expand All @@ -28,8 +25,8 @@ class SpaceKeyProcessor(
*/
fun processKeycodeSpace(currentWasLastKeySpace: Boolean): Boolean {
val isCommandBar =
ime.currentState != ScribeState.IDLE &&
ime.currentState != ScribeState.SELECT_COMMAND
ime.currentState != GeneralKeyboardIME.ScribeState.IDLE &&
ime.currentState != GeneralKeyboardIME.ScribeState.SELECT_COMMAND

return if (isCommandBar) {
handleSpaceInCommandBar()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers

import android.os.Handler
import android.os.Looper
import be.scri.services.GeneralKeyboardIME
import be.scri.services.GeneralKeyboardIME.ScribeState

/**
* Handles auto-suggestions such as noun gender, plurality, case, and emojis.
*
* @property ime The [GeneralKeyboardIME] instance this handler is associated with.
* @property ime The [be.scri.services.GeneralKeyboardIME] instance this handler is associated with.
*/
class SuggestionHandler(
private val ime: GeneralKeyboardIME,
Expand Down Expand Up @@ -39,7 +36,7 @@ class SuggestionHandler(

linguisticSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
if (ime.currentState != GeneralKeyboardIME.ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
Expand Down Expand Up @@ -80,7 +77,7 @@ class SuggestionHandler(

wordSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
if (ime.currentState != GeneralKeyboardIME.ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
Expand Down Expand Up @@ -119,7 +116,7 @@ class SuggestionHandler(

emojiSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
if (ime.currentState != GeneralKeyboardIME.ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
Expand Down Expand Up @@ -175,7 +172,7 @@ class SuggestionHandler(

ime.disableAutoSuggest()

if (ime.currentState != ScribeState.SELECT_COMMAND) {
if (ime.currentState != GeneralKeyboardIME.ScribeState.SELECT_COMMAND) {
ime.updateButtonVisibility(false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import be.scri.helpers.SHIFT_ON_PERMANENT
import be.scri.helpers.SuggestionHandler
import be.scri.helpers.data.AutocompletionDataManager
import be.scri.helpers.english.ENInterfaceVariables.ALREADY_PLURAL_MSG
import be.scri.helpers.ui.KeyboardUIManager
import be.scri.ui.KeyboardUIManager
import be.scri.views.KeyboardView
import java.util.Locale

Expand Down Expand Up @@ -1418,7 +1418,7 @@ abstract class GeneralKeyboardIME(
backgroundRes: Int,
) {
button.text = text
button.setTextColor(ContextCompat.getColor(applicationContext, be.scri.R.color.white))
button.setTextColor(ContextCompat.getColor(applicationContext, R.color.white))
button.isClickable = false
button.setOnClickListener(null)

Expand All @@ -1430,7 +1430,7 @@ abstract class GeneralKeyboardIME(
if (contentDrawable is LayerDrawable) {
val shapeDrawable =
contentDrawable.findDrawableByLayerId(
be.scri.R.id.button_background_shape,
R.id.button_background_shape,
) as? GradientDrawable

shapeDrawable?.setColor(
Expand Down Expand Up @@ -1472,7 +1472,7 @@ abstract class GeneralKeyboardIME(
it,
leftSuggestion.first,
leftSuggestion.second,
be.scri.R.drawable.gender_suggestion_button_left_background,
R.drawable.gender_suggestion_button_left_background,
)
}

Expand All @@ -1481,7 +1481,7 @@ abstract class GeneralKeyboardIME(
it,
rightSuggestion.first,
rightSuggestion.second,
be.scri.R.drawable.gender_suggestion_button_right_background,
R.drawable.gender_suggestion_button_right_background,
)
}
}
Expand Down
Loading
Loading