Skip to content

Commit f910e46

Browse files
WhiredPlanckBambooin
authored andcommitted
refactor: reduce redundant operations in the input method service
1 parent 13cf60d commit f910e46

File tree

3 files changed

+19
-104
lines changed

3 files changed

+19
-104
lines changed

app/src/main/java/com/osfans/trime/ime/core/Trime.kt

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import android.os.Handler
2828
import android.os.Looper
2929
import android.os.Message
3030
import android.text.InputType
31-
import android.text.TextUtils
32-
import android.view.Gravity
3331
import android.view.KeyEvent
3432
import android.view.LayoutInflater
3533
import android.view.View
@@ -40,6 +38,7 @@ import android.view.inputmethod.CursorAnchorInfo
4038
import android.view.inputmethod.EditorInfo
4139
import android.view.inputmethod.ExtractedTextRequest
4240
import android.widget.FrameLayout
41+
import android.widget.LinearLayout
4342
import androidx.annotation.Keep
4443
import androidx.core.view.updateLayoutParams
4544
import androidx.lifecycle.lifecycleScope
@@ -72,16 +71,15 @@ import com.osfans.trime.ime.text.ScrollView
7271
import com.osfans.trime.ime.text.TextInputManager
7372
import com.osfans.trime.util.ShortcutUtils
7473
import com.osfans.trime.util.StringUtils
75-
import com.osfans.trime.util.ViewUtils
7674
import com.osfans.trime.util.WeakHashSet
7775
import com.osfans.trime.util.isNightMode
7876
import kotlinx.coroutines.Dispatchers
7977
import kotlinx.coroutines.launch
8078
import splitties.bitflags.hasFlag
8179
import splitties.dimensions.dp
8280
import splitties.systemservices.inputMethodManager
81+
import splitties.views.gravityBottom
8382
import timber.log.Timber
84-
import java.util.Objects
8583

8684
/** [輸入法][InputMethodService]主程序 */
8785

@@ -109,7 +107,7 @@ open class Trime : LifecycleInputMethodService() {
109107
private var minPopupSize = 0 // 上悬浮窗的候选词的最小词长
110108
private var minPopupCheckSize = 0 // 第一屏候选词数量少于设定值,则候选词上悬浮窗。(也就是说,第一屏存在长词)此选项大于1时,min_length等参数失效
111109
private var mCompositionPopupWindow: CompositionPopupWindow? = null
112-
private var candidateExPage = false
110+
var candidateExPage = false
113111

114112
@Keep
115113
private val onThemeChangeListener =
@@ -119,14 +117,6 @@ open class Trime : LifecycleInputMethodService() {
119117
}
120118
}
121119

122-
fun hasCandidateExPage(): Boolean {
123-
return candidateExPage
124-
}
125-
126-
fun setCandidateExPage(value: Boolean) {
127-
candidateExPage = value
128-
}
129-
130120
init {
131121
try {
132122
check(self == null) { "Trime is already initialized" }
@@ -307,11 +297,11 @@ open class Trime : LifecycleInputMethodService() {
307297
}
308298

309299
fun pasteByChar() {
310-
commitTextByChar(Objects.requireNonNull(ShortcutUtils.pasteFromClipboard(this)).toString())
300+
commitTextByChar(checkNotNull(ShortcutUtils.pasteFromClipboard(this)).toString())
311301
}
312302

313303
private fun showCompositionView(isCandidate: Boolean) {
314-
if (TextUtils.isEmpty(Rime.compositionText) && isCandidate) {
304+
if (Rime.compositionText.isEmpty() && isCandidate) {
315305
mCompositionPopupWindow!!.hideCompositionView()
316306
return
317307
}
@@ -442,7 +432,7 @@ open class Trime : LifecycleInputMethodService() {
442432
ic?.commitText("\n", 1)
443433
return
444434
}
445-
if (!TextUtils.isEmpty(editorInfo!!.actionLabel) &&
435+
if (!editorInfo!!.actionLabel.isNullOrEmpty() &&
446436
editorInfo!!.actionId != EditorInfo.IME_ACTION_UNSPECIFIED
447437
) {
448438
val ic = currentInputConnection
@@ -1038,7 +1028,6 @@ open class Trime : LifecycleInputMethodService() {
10381028
} else {
10391029
mCandidate!!.setText(0)
10401030
}
1041-
mCandidate!!.setExpectWidth(mainKeyboardView!!.width)
10421031
// 刷新候选词后,如果候选词超出屏幕宽度,滚动候选栏
10431032
mTabRoot!!.move(mCandidate!!.highlightLeft, mCandidate!!.highlightRight)
10441033
}
@@ -1113,22 +1102,18 @@ open class Trime : LifecycleInputMethodService() {
11131102
} else {
11141103
WindowManager.LayoutParams.MATCH_PARENT
11151104
}
1116-
val inputArea = w.findViewById<View>(android.R.id.inputArea)
1117-
// TODO: 需要获取到文本编辑框、完成按钮,设置其色彩和尺寸。
1118-
// if (isFullscreenMode()) {
1119-
// Timber.d("isFullscreenMode");
1120-
// /* In Fullscreen mode, when layout contains transparent color,
1121-
// * the background under input area will disturb users' typing,
1122-
// * so set the input area as light pink */
1123-
// inputArea.setBackgroundColor(parseColor("#ff660000"));
1124-
// } else {
1125-
// Timber.d("NotFullscreenMode");
1126-
// /* Otherwise, set it as light gray to avoid potential issue */
1127-
// inputArea.setBackgroundColor(parseColor("#dddddddd"));
1128-
// }
1129-
ViewUtils.updateLayoutHeightOf(inputArea, layoutHeight)
1130-
ViewUtils.updateLayoutGravityOf(inputArea, Gravity.BOTTOM)
1131-
ViewUtils.updateLayoutHeightOf(inputView!!, layoutHeight)
1105+
val inputArea = w.decorView.findViewById<FrameLayout>(android.R.id.inputArea)
1106+
inputArea.updateLayoutParams {
1107+
height = layoutHeight
1108+
if (this is FrameLayout.LayoutParams) {
1109+
this.gravity = inputArea.gravityBottom
1110+
} else if (this is LinearLayout.LayoutParams) {
1111+
this.gravity = inputArea.gravityBottom
1112+
}
1113+
}
1114+
inputView?.updateLayoutParams {
1115+
height = layoutHeight
1116+
}
11321117
}
11331118
}
11341119

app/src/main/java/com/osfans/trime/ime/text/ScrollView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void commOnTouchEvent(@NonNull MotionEvent ev) {
121121
swipeStartX = ev.getX();
122122
}
123123
} else if (swipeStartX - ev.getX() > swipeActionLimit) {
124-
if (Trime.getService().hasCandidateExPage()) {
124+
if (Trime.getService().getCandidateExPage()) {
125125
if (pageExAction != null) pageExAction.run();
126126
return;
127127
} else if (Rime.hasRight()) {

app/src/main/java/com/osfans/trime/util/ViewUtils.kt

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)