Skip to content

Commit c9e9350

Browse files
committed
feat,refactor(ime): reform how to handle the return (enter) key
Depend on the editor info to decide what action to perform
1 parent 3d276c4 commit c9e9350

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import java.util.Locale;
8989
import java.util.Objects;
9090
import java.util.concurrent.CopyOnWriteArrayList;
91+
import splitties.bitflags.BitFlagsKt;
9192
import splitties.systemservices.SystemServicesKt;
9293
import timber.log.Timber;
9394

@@ -121,6 +122,8 @@ public Config getImeConfig() {
121122
public InputFeedbackManager inputFeedbackManager = null; // 效果管理器
122123
private IntentReceiver mIntentReceiver = null;
123124

125+
public EditorInfo editorInfo = null;
126+
124127
private boolean isWindowShown = false; // 键盘窗口是否已显示
125128

126129
private boolean isAutoCaps; // 句首自動大寫
@@ -606,6 +609,35 @@ public void onDestroy() {
606609
self = null;
607610
}
608611

612+
private void handleReturnKey() {
613+
if (editorInfo == null) sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
614+
if ((editorInfo.inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_NULL) {
615+
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
616+
}
617+
if (BitFlagsKt.hasFlag(editorInfo.imeOptions, EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
618+
final InputConnection ic = getCurrentInputConnection();
619+
if (ic != null) ic.commitText("\n", 1);
620+
return;
621+
}
622+
if (!TextUtils.isEmpty(editorInfo.actionLabel)
623+
&& editorInfo.actionId != EditorInfo.IME_ACTION_UNSPECIFIED) {
624+
final InputConnection ic = getCurrentInputConnection();
625+
if (ic != null) ic.performEditorAction(editorInfo.actionId);
626+
return;
627+
}
628+
final int action = editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
629+
final InputConnection ic = getCurrentInputConnection();
630+
switch (action) {
631+
case EditorInfo.IME_ACTION_UNSPECIFIED:
632+
case EditorInfo.IME_ACTION_NONE:
633+
if (ic != null) ic.commitText("\n", 1);
634+
break;
635+
default:
636+
if (ic != null) ic.performEditorAction(action);
637+
break;
638+
}
639+
}
640+
609641
@Override
610642
public void onConfigurationChanged(@NonNull Configuration newConfig) {
611643
final Configuration config = getResources().getConfiguration();
@@ -732,9 +764,16 @@ public void setShowComment(boolean show_comment) {
732764
mComposition.setShowComment(show_comment);
733765
}
734766

767+
@Override
768+
public void onStartInput(EditorInfo attribute, boolean restarting) {
769+
editorInfo = attribute;
770+
Timber.d("onStartInput: restarting=%s", restarting);
771+
}
772+
735773
@Override
736774
public void onStartInputView(EditorInfo attribute, boolean restarting) {
737-
super.onStartInputView(attribute, restarting);
775+
Timber.d("onStartInputView: restarting=%s", restarting);
776+
editorInfo = attribute;
738777
if (getPrefs().getThemeAndColor().getAutoDark()) {
739778
int nightModeFlags =
740779
getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
@@ -842,6 +881,12 @@ public void onFinishInputView(boolean finishingInput) {
842881
}
843882
}
844883

884+
@Override
885+
public void onFinishInput() {
886+
editorInfo = null;
887+
super.onFinishInput();
888+
}
889+
845890
public void bindKeyboardToInputView() {
846891
if (mainKeyboardView != null) {
847892
// Bind the selected keyboard to the input view.
@@ -1224,11 +1269,7 @@ public void launchSettings() {
12241269
private boolean performEnter(int keyCode) { // 回車
12251270
if (keyCode == KeyEvent.KEYCODE_ENTER) {
12261271
DraftHelper.INSTANCE.onInputEventChanged();
1227-
if (textInputManager.getPerformEnterAsLineBreak()) {
1228-
commitText("\n");
1229-
} else {
1230-
sendKeyChar('\n');
1231-
}
1272+
handleReturnKey();
12321273
return true;
12331274
}
12341275
return false;

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class TextInputManager private constructor() :
7575

7676
var needSendUpRimeKey: Boolean = false
7777
var shouldUpdateRimeOption: Boolean = true
78-
var performEnterAsLineBreak: Boolean = false
7978
var isComposable: Boolean = false
8079
var shouldResetAsciiMode: Boolean = false
8180

@@ -199,7 +198,6 @@ class TextInputManager private constructor() :
199198
super.onStartInputView(instance, restarting)
200199
Trime.getService().selectLiquidKeyboard(-1)
201200
isComposable = false
202-
performEnterAsLineBreak = false
203201
var tempAsciiMode = if (shouldResetAsciiMode) false else null
204202
var keyboardType =
205203
when (instance.editorInfo!!.imeOptions and EditorInfo.IME_FLAG_FORCE_ASCII) {
@@ -218,7 +216,7 @@ class TextInputManager private constructor() :
218216
InputType.TYPE_CLASS_TEXT -> {
219217
when (inputAttrsRaw and InputType.TYPE_MASK_VARIATION) {
220218
InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE -> {
221-
null.also { performEnterAsLineBreak = true }
219+
null
222220
}
223221
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
224222
InputType.TYPE_TEXT_VARIATION_PASSWORD,

0 commit comments

Comments
 (0)