Skip to content

Commit b7eee6f

Browse files
committed
refactor: split out EnterKeyLabelModule from KeyboardView
1 parent 385264b commit b7eee6f

File tree

8 files changed

+105
-71
lines changed

8 files changed

+105
-71
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015 - 2024 Rime community
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package com.osfans.trime.ime.broadcast
7+
8+
import android.view.inputmethod.EditorInfo
9+
import com.osfans.trime.data.theme.Theme
10+
import com.osfans.trime.ime.dependency.InputScope
11+
import me.tatarka.inject.annotations.Inject
12+
import splitties.bitflags.hasFlag
13+
14+
@InputScope
15+
@Inject
16+
class EnterKeyLabelModule(
17+
private val broadcaster: InputBroadcaster,
18+
private val theme: Theme,
19+
) {
20+
companion object {
21+
const val DEFAULT_LABEL = ""
22+
}
23+
24+
enum class Mode {
25+
ACTION_LABEL_NEVER,
26+
ACTION_LABEL_ONLY,
27+
ACTION_LABEL_PREFERRED,
28+
CUSTOM_PREFERRED,
29+
}
30+
31+
val mode: Mode = runCatching { Mode.entries[theme.generalStyle.enterLabelMode] }.getOrDefault(Mode.ACTION_LABEL_NEVER)
32+
33+
var keyLabel: String = DEFAULT_LABEL
34+
private set
35+
36+
private var actionLabel: String = DEFAULT_LABEL
37+
38+
private fun labelFromEditorInfo(info: EditorInfo): String {
39+
if (info.imeOptions.hasFlag(EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
40+
return theme.generalStyle.enterLabel.default
41+
} else {
42+
val action = info.imeOptions and EditorInfo.IME_MASK_ACTION
43+
val actionLabel = info.actionLabel
44+
when (mode) {
45+
Mode.ACTION_LABEL_ONLY -> {
46+
return actionLabel.toString()
47+
}
48+
Mode.ACTION_LABEL_PREFERRED -> {
49+
return if (!actionLabel.isNullOrEmpty()) {
50+
actionLabel.toString()
51+
} else {
52+
theme.generalStyle.enterLabel.default
53+
}
54+
}
55+
Mode.CUSTOM_PREFERRED,
56+
Mode.ACTION_LABEL_NEVER,
57+
-> {
58+
return when (action) {
59+
EditorInfo.IME_ACTION_DONE -> theme.generalStyle.enterLabel.done
60+
EditorInfo.IME_ACTION_GO -> theme.generalStyle.enterLabel.go
61+
EditorInfo.IME_ACTION_NEXT -> theme.generalStyle.enterLabel.next
62+
EditorInfo.IME_ACTION_PREVIOUS -> theme.generalStyle.enterLabel.pre
63+
EditorInfo.IME_ACTION_SEARCH -> theme.generalStyle.enterLabel.search
64+
EditorInfo.IME_ACTION_SEND -> theme.generalStyle.enterLabel.send
65+
else -> {
66+
if (mode == Mode.ACTION_LABEL_NEVER) {
67+
theme.generalStyle.enterLabel.default
68+
} else {
69+
if (!actionLabel.isNullOrEmpty()) {
70+
actionLabel.toString()
71+
} else {
72+
theme.generalStyle.enterLabel.default
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
fun updateLabelOnEditorInfo(info: EditorInfo) {
83+
actionLabel = labelFromEditorInfo(info)
84+
if (keyLabel == actionLabel) return
85+
keyLabel = actionLabel
86+
broadcaster.onEnterKeyLabelUpdate(keyLabel)
87+
}
88+
}

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcastReceiver.kt

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ interface InputBroadcastReceiver {
2727
fun onWindowAttached(window: BoardWindow) {}
2828

2929
fun onWindowDetached(window: BoardWindow) {}
30+
31+
fun onEnterKeyLabelUpdate(label: String) {}
3032
}

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcaster.kt

+4
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ class InputBroadcaster : InputBroadcastReceiver {
6464
override fun onWindowDetached(window: BoardWindow) {
6565
receivers.forEach { it.onWindowDetached(window) }
6666
}
67+
68+
override fun onEnterKeyLabelUpdate(label: String) {
69+
receivers.forEach { it.onEnterKeyLabelUpdate(label) }
70+
}
6771
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class InputView(
102102
private val inputComponent = InputComponent::class.create(this, themedContext, theme, service, rime)
103103
private val broadcaster = inputComponent.broadcaster
104104
val commonKeyboardActionListener = inputComponent.commonKeyboardActionListener
105+
private val enterKeyLabel = inputComponent.enterKeyLabel
105106
private val windowManager = inputComponent.windowManager
106107
private val quickBar: QuickBar = inputComponent.quickBar
107108
val composition: CompositionPopupWindow = inputComponent.composition
@@ -315,10 +316,10 @@ class InputView(
315316
}
316317
}
317318
broadcaster.onStartInput(info)
319+
enterKeyLabel.updateLabelOnEditorInfo(info)
318320
if (!restarting) {
319321
windowManager.attachWindow(KeyboardWindow)
320322
}
321-
keyboardWindow.mainKeyboardView.updateEnterLabelOnEditorInfo(info)
322323
}
323324

324325
private fun handleRimeNotification(it: RimeNotification<*>) {

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

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
106106
ColorManager.OnColorChangeListener {
107107
lifecycleScope.launch(Dispatchers.Main) {
108108
recreateInputView()
109-
currentInputEditorInfo?.let { inputView?.startInput(it) }
110109
}
111110
}
112111

app/src/main/java/com/osfans/trime/ime/dependency/InputComponent.kt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.Context
88
import com.osfans.trime.daemon.RimeSession
99
import com.osfans.trime.data.theme.Theme
1010
import com.osfans.trime.ime.bar.QuickBar
11+
import com.osfans.trime.ime.broadcast.EnterKeyLabelModule
1112
import com.osfans.trime.ime.broadcast.InputBroadcaster
1213
import com.osfans.trime.ime.candidates.CompactCandidateModule
1314
import com.osfans.trime.ime.composition.CompositionPopupWindow
@@ -31,6 +32,7 @@ abstract class InputComponent(
3132
) {
3233
abstract val broadcaster: InputBroadcaster
3334
abstract val commonKeyboardActionListener: CommonKeyboardActionListener
35+
abstract val enterKeyLabel: EnterKeyLabelModule
3436
abstract val quickBar: QuickBar
3537
abstract val composition: CompositionPopupWindow
3638
abstract val windowManager: BoardWindowManager

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardView.kt

+3-69
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,18 @@ import android.view.Gravity
2121
import android.view.MotionEvent
2222
import android.view.View
2323
import android.view.ViewGroup
24-
import android.view.inputmethod.EditorInfo
2524
import android.widget.PopupWindow
2625
import com.osfans.trime.core.Rime
2726
import com.osfans.trime.data.prefs.AppPrefs
2827
import com.osfans.trime.data.theme.ColorManager
2928
import com.osfans.trime.data.theme.FontManager
3029
import com.osfans.trime.data.theme.ThemeManager
31-
import com.osfans.trime.data.theme.model.EnterLabel
3230
import com.osfans.trime.databinding.KeyboardPopupKeyboardBinding
3331
import com.osfans.trime.ime.enums.KeyEventType
3432
import com.osfans.trime.util.LeakGuardHandlerWrapper
3533
import com.osfans.trime.util.indexOfStateSet
3634
import com.osfans.trime.util.sp
3735
import com.osfans.trime.util.stateDrawableAt
38-
import splitties.bitflags.hasFlag
3936
import splitties.dimensions.dp
4037
import splitties.systemservices.layoutInflater
4138
import splitties.views.dsl.core.textView
@@ -53,20 +50,6 @@ class KeyboardView(
5350
context: Context,
5451
) : View(context),
5552
View.OnClickListener {
56-
enum class EnterLabelMode {
57-
ACTION_LABEL_NEVER,
58-
ACTION_LABEL_ONLY,
59-
ACTION_LABEL_PREFERRED,
60-
CUSTOM_PREFERRED,
61-
;
62-
63-
companion object {
64-
fun fromOrdinal(ordinal: Int) =
65-
runCatching { entries[ordinal] }
66-
.getOrDefault(ACTION_LABEL_NEVER)
67-
}
68-
}
69-
7053
private val theme get() = ThemeManager.activeTheme
7154

7255
private var mKeyboard: Keyboard? = null
@@ -237,58 +220,10 @@ class KeyboardView(
237220
private val mHeadsetRequiredToHearPasswordsAnnounced = false
238221
var showKeyHint = !Rime.getOption("_hide_key_hint")
239222
var showKeySymbol = !Rime.getOption("_hide_key_symbol")
240-
private lateinit var labelEnter: String
241-
private val mEnterLabels: EnterLabel = theme.generalStyle.enterLabel
242-
private val enterLabelMode = EnterLabelMode.fromOrdinal(theme.generalStyle.enterLabelMode)
223+
private var labelEnter: String = theme.generalStyle.enterLabel.default
243224

244-
private fun handleEnterLabel() {
245-
labelEnter = mEnterLabels.default ?: ""
246-
}
247-
248-
fun updateEnterLabelOnEditorInfo(info: EditorInfo) {
249-
if (info.imeOptions.hasFlag(EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
250-
labelEnter = mEnterLabels.default
251-
} else {
252-
val action = info.imeOptions and EditorInfo.IME_MASK_ACTION
253-
val actionLabel = info.actionLabel
254-
when (enterLabelMode) {
255-
EnterLabelMode.ACTION_LABEL_ONLY -> {
256-
labelEnter = actionLabel.toString()
257-
}
258-
EnterLabelMode.ACTION_LABEL_PREFERRED -> {
259-
labelEnter =
260-
if (!actionLabel.isNullOrEmpty()) {
261-
actionLabel.toString()
262-
} else {
263-
mEnterLabels.default
264-
}
265-
}
266-
EnterLabelMode.CUSTOM_PREFERRED,
267-
EnterLabelMode.ACTION_LABEL_NEVER,
268-
-> {
269-
labelEnter =
270-
when (action) {
271-
EditorInfo.IME_ACTION_DONE -> mEnterLabels.done
272-
EditorInfo.IME_ACTION_GO -> mEnterLabels.go
273-
EditorInfo.IME_ACTION_NEXT -> mEnterLabels.next
274-
EditorInfo.IME_ACTION_PREVIOUS -> mEnterLabels.pre
275-
EditorInfo.IME_ACTION_SEARCH -> mEnterLabels.search
276-
EditorInfo.IME_ACTION_SEND -> mEnterLabels.send
277-
else -> {
278-
if (enterLabelMode == EnterLabelMode.ACTION_LABEL_NEVER) {
279-
mEnterLabels.default
280-
} else {
281-
if (!actionLabel.isNullOrEmpty()) {
282-
actionLabel.toString()
283-
} else {
284-
mEnterLabels.default
285-
}
286-
}
287-
}
288-
}
289-
}
290-
}
291-
}
225+
fun onEnterKeyLabelUpdate(label: String) {
226+
labelEnter = label
292227
}
293228

294229
private val mHandler = MyHandler(this)
@@ -317,7 +252,6 @@ class KeyboardView(
317252

318253
init {
319254
initGestureDetector()
320-
handleEnterLabel()
321255
invalidateAllKeys()
322256
}
323257

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt

+4
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ class KeyboardWindow(
248248
mainKeyboardView.invalidateAllKeys()
249249
}
250250

251+
override fun onEnterKeyLabelUpdate(label: String) {
252+
mainKeyboardView.onEnterKeyLabelUpdate(label)
253+
}
254+
251255
override fun onAttached() {
252256
mainKeyboardView.keyboardActionListener = ListenerDecorator(commonKeyboardActionListener.listener)
253257
}

0 commit comments

Comments
 (0)