55package io .flutter .plugin .editing ;
66
77import android .content .Context ;
8+ import android .text .DynamicLayout ;
89import android .text .Editable ;
10+ import android .text .Layout ;
911import android .text .Selection ;
12+ import android .text .TextPaint ;
1013import android .view .KeyEvent ;
1114import android .view .View ;
1215import android .view .inputmethod .BaseInputConnection ;
@@ -24,10 +27,12 @@ class InputConnectionAdaptor extends BaseInputConnection {
2427 private final Editable mEditable ;
2528 private int mBatchCount ;
2629 private InputMethodManager mImm ;
30+ private final Layout mLayout ;
2731
2832 private static final MethodChannel .Result logger =
2933 new ErrorLogResult ("FlutterTextInput" );
3034
35+ @ SuppressWarnings ("deprecation" )
3136 public InputConnectionAdaptor (
3237 View view ,
3338 int client ,
@@ -40,6 +45,9 @@ public InputConnectionAdaptor(
4045 this .textInputChannel = textInputChannel ;
4146 mEditable = editable ;
4247 mBatchCount = 0 ;
48+ // We create a dummy Layout with max width so that the selection
49+ // shifting acts as if all text were in one line.
50+ mLayout = new DynamicLayout (mEditable , new TextPaint (), Integer .MAX_VALUE , Layout .Alignment .ALIGN_NORMAL , 1.0f , 0.0f , false );
4351 mImm = (InputMethodManager ) view .getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
4452 }
4553
@@ -144,7 +152,8 @@ public boolean sendKeyEvent(KeyEvent event) {
144152 return true ;
145153 } else if (selStart > 0 ) {
146154 // Delete to the left of the cursor.
147- int newSel = Math .max (selStart - 1 , 0 );
155+ Selection .extendLeft (mEditable , mLayout );
156+ int newSel = Selection .getSelectionEnd (mEditable );
148157 Selection .setSelection (mEditable , newSel );
149158 mEditable .delete (newSel , selStart );
150159 updateEditingState ();
0 commit comments