Skip to content

Commit

Permalink
refactor: handle works of KeyboardView on start/finish input in Input…
Browse files Browse the repository at this point in the history
…View

refactor: simplify parsing enter label in KeyboardView
  • Loading branch information
WhiredPlanck authored and Bambooin committed Feb 7, 2024
1 parent 0855b55 commit 13cf60d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Dialog
import android.content.res.Configuration
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.updateLayoutParams
Expand Down Expand Up @@ -213,6 +214,10 @@ class InputView(
quickBar.view.setPadding(sidePadding, 0, sidePadding, 0)
}

fun startInput(info: EditorInfo) {
keyboardWindow.oldMainInputView.mainKeyboardView.updateEnterLabelOnEditorInfo(info)
}

private fun handleRimeNotification(it: RimeNotification) {
when (it) {
is RimeNotification.OptionNotification -> {
Expand Down Expand Up @@ -265,6 +270,7 @@ class InputView(

fun finishInput() {
showingDialog?.dismiss()
keyboardWindow.oldMainInputView.mainKeyboardView.finishInput()
}

override fun onDetachedFromWindow() {
Expand Down
17 changes: 2 additions & 15 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,7 @@ open class Trime : LifecycleInputMethodService() {
bindKeyboardToInputView()
// if (!restarting) setNavBarColor();
setCandidatesViewShown(!Rime.isEmpty) // 軟鍵盤出現時顯示候選欄
if (attribute.imeOptions and EditorInfo.IME_FLAG_NO_ENTER_ACTION
== EditorInfo.IME_FLAG_NO_ENTER_ACTION
) {
mainKeyboardView!!.resetEnterLabel()
} else {
mainKeyboardView!!.setEnterLabel(
attribute.imeOptions and EditorInfo.IME_MASK_ACTION,
attribute.actionLabel,
)
}
inputView?.startInput(attribute)
when (attribute.inputType and InputType.TYPE_MASK_VARIATION) {
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_PASSWORD,
Expand Down Expand Up @@ -684,8 +675,6 @@ open class Trime : LifecycleInputMethodService() {
DraftHelper.onInputEventChanged()
}
try {
// Dismiss any pop-ups when the input-view is being finished and hidden.
mainKeyboardView!!.closing()
performEscape()
if (inputFeedbackManager != null) {
inputFeedbackManager!!.releaseSoundPool()
Expand All @@ -695,9 +684,7 @@ open class Trime : LifecycleInputMethodService() {
Timber.e(e, "Failed to show the PopupWindow.")
}
}
if (inputView != null) {
inputView!!.finishInput()
}
inputView?.finishInput()
Timber.d("OnFinishInputView")
}

Expand Down
95 changes: 44 additions & 51 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,75 +236,68 @@ class KeyboardView(context: Context?, attrs: AttributeSet?) : View(context, attr
mShowSymbol = value
}

fun resetEnterLabel() {
labelEnter = mEnterLabels!!["default"]
}

fun setOnKeyboardActionListener(listener: OnKeyboardActionListener?) {
onKeyboardActionListener = listener
}

private fun handleEnterLabel(theme: Theme) {
mEnterLabels = theme.style.getObject("enter_labels") as MutableMap<String, String?>?
if (mEnterLabels == null) {
mEnterLabels = HashMap()
}
val defaultEnterLabel: String?
if (mEnterLabels!!.containsKey("default")) {
defaultEnterLabel = mEnterLabels!!["default"]
} else {
defaultEnterLabel = "Enter"
mEnterLabels!!["default"] = defaultEnterLabel
}
?: hashMapOf()
val defaultEnterLabel = mEnterLabels!!["default"] ?: "Enter".also { mEnterLabels!!["default"] = it }
for (label in arrayOf("done", "go", "next", "none", "pre", "search", "send")) {
if (!mEnterLabels!!.containsKey(label)) {
mEnterLabels!![label] = defaultEnterLabel
}
}
}

fun setEnterLabel(
action: Int,
actionLabel: CharSequence?,
) {
// enter_label_mode 取值:
// 0不使用,1只使用actionlabel,2优先使用,3当其他方式没有获得label时才读取actionlabel
if (enterLabelMode == 1) {
labelEnter = if (!actionLabel.isNullOrEmpty()) actionLabel.toString() else mEnterLabels!!["default"]
return
}
if (enterLabelMode == 2) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
fun updateEnterLabelOnEditorInfo(info: EditorInfo) {
if (info.imeOptions and EditorInfo.IME_FLAG_NO_ENTER_ACTION
== EditorInfo.IME_FLAG_NO_ENTER_ACTION
) {
labelEnter = mEnterLabels!!["default"]
} else {
val action = info.imeOptions and EditorInfo.IME_MASK_ACTION
val actionLabel = info.actionLabel
// enter_label_mode 取值:
// 0不使用,1只使用actionlabel,2优先使用,3当其他方式没有获得label时才读取actionlabel
if (enterLabelMode == 1) {
labelEnter = if (!actionLabel.isNullOrEmpty()) actionLabel.toString() else mEnterLabels!!["default"]
return
}
}
when (action) {
EditorInfo.IME_ACTION_DONE -> labelEnter = mEnterLabels!!["done"]
EditorInfo.IME_ACTION_GO -> labelEnter = mEnterLabels!!["go"]
EditorInfo.IME_ACTION_NEXT -> labelEnter = mEnterLabels!!["next"]
EditorInfo.IME_ACTION_PREVIOUS -> labelEnter = mEnterLabels!!["pre"]
EditorInfo.IME_ACTION_SEARCH -> labelEnter = mEnterLabels!!["search"]
EditorInfo.IME_ACTION_SEND -> labelEnter = mEnterLabels!!["send"]
EditorInfo.IME_ACTION_NONE -> {
labelEnter = mEnterLabels!!["none"]
if (enterLabelMode == 3) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
return
}
if (enterLabelMode == 2) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
return
}
labelEnter = mEnterLabels!!["default"]
}
when (action) {
EditorInfo.IME_ACTION_DONE -> labelEnter = mEnterLabels!!["done"]
EditorInfo.IME_ACTION_GO -> labelEnter = mEnterLabels!!["go"]
EditorInfo.IME_ACTION_NEXT -> labelEnter = mEnterLabels!!["next"]
EditorInfo.IME_ACTION_PREVIOUS -> labelEnter = mEnterLabels!!["pre"]
EditorInfo.IME_ACTION_SEARCH -> labelEnter = mEnterLabels!!["search"]
EditorInfo.IME_ACTION_SEND -> labelEnter = mEnterLabels!!["send"]
EditorInfo.IME_ACTION_NONE -> {
labelEnter = mEnterLabels!!["none"]
if (enterLabelMode == 3) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
return
}
}
labelEnter = mEnterLabels!!["default"]
}

else -> {
if (enterLabelMode == 3) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
return
else -> {
if (enterLabelMode == 3) {
if (!actionLabel.isNullOrEmpty()) {
labelEnter = actionLabel.toString()
return
}
}
labelEnter = mEnterLabels!!["default"]
}
labelEnter = mEnterLabels!!["default"]
}
}
}
Expand Down Expand Up @@ -1709,7 +1702,7 @@ class KeyboardView(context: Context?, attrs: AttributeSet?) : View(context, attr
return true
}

fun closing() {
fun finishInput() {
if (mPreviewPopup.isShowing) {
mPreviewPopup.dismiss()
}
Expand All @@ -1728,7 +1721,7 @@ class KeyboardView(context: Context?, attrs: AttributeSet?) : View(context, attr

public override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
closing()
finishInput()
}

private fun dismissPopupKeyboard() {
Expand Down

0 comments on commit 13cf60d

Please sign in to comment.