mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Keyboard can get out of sync with flutter
We need to tell the InputMethodManager to restartInput whenever we change our editing state or else it might persist state from earlier in the editing session. Fixes https://github.com/flutter/flutter/issues/2431
This commit is contained in:
parent
98f53e1e5a
commit
77e7311ced
@ -21,24 +21,24 @@ import org.chromium.mojom.editing.SubmitAction;
|
||||
* An adaptor between InputConnection and KeyboardClient.
|
||||
*/
|
||||
public class InputConnectionAdaptor extends BaseInputConnection {
|
||||
private KeyboardViewState mState;
|
||||
private EditingState mEditingState;
|
||||
private KeyboardClient mClient;
|
||||
private EditingState mOutgoingState;
|
||||
|
||||
public InputConnectionAdaptor(View view, KeyboardViewState state) {
|
||||
public InputConnectionAdaptor(View view, KeyboardClient client) {
|
||||
super(view, true);
|
||||
assert state != null;
|
||||
mState = state;
|
||||
mEditingState = new EditingState();
|
||||
assert client != null;
|
||||
mClient = client;
|
||||
mOutgoingState = new EditingState();
|
||||
}
|
||||
|
||||
private void updateEditingState() {
|
||||
Editable content = getEditable();
|
||||
mEditingState.text = content.toString();
|
||||
mEditingState.selectionBase = Selection.getSelectionStart(content);
|
||||
mEditingState.selectionExtent = Selection.getSelectionEnd(content);
|
||||
mEditingState.composingBase = BaseInputConnection.getComposingSpanStart(content);
|
||||
mEditingState.composingExtent = BaseInputConnection.getComposingSpanEnd(content);
|
||||
mState.getClient().updateEditingState(mEditingState);
|
||||
mOutgoingState.text = content.toString();
|
||||
mOutgoingState.selectionBase = Selection.getSelectionStart(content);
|
||||
mOutgoingState.selectionExtent = Selection.getSelectionEnd(content);
|
||||
mOutgoingState.composingBase = BaseInputConnection.getComposingSpanStart(content);
|
||||
mOutgoingState.composingExtent = BaseInputConnection.getComposingSpanEnd(content);
|
||||
mClient.updateEditingState(mOutgoingState);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -86,7 +86,7 @@ public class InputConnectionAdaptor extends BaseInputConnection {
|
||||
|
||||
@Override
|
||||
public boolean performEditorAction(int actionCode) {
|
||||
mState.getClient().submit(SubmitAction.DONE);
|
||||
mClient.submit(SubmitAction.DONE);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,20 +27,17 @@ public class KeyboardViewState {
|
||||
private View mView;
|
||||
private KeyboardClient mClient;
|
||||
private KeyboardConfiguration mConfiguration;
|
||||
private InputConnectionAdaptor mInputConnection;
|
||||
private EditingState mIncomingState;
|
||||
|
||||
public KeyboardViewState(View view) {
|
||||
mView = view;
|
||||
mClient = null;
|
||||
}
|
||||
|
||||
public KeyboardClient getClient() {
|
||||
return mClient;
|
||||
}
|
||||
|
||||
public void setClient(KeyboardClient client, KeyboardConfiguration configuration) {
|
||||
if (mClient != null)
|
||||
mClient.close();
|
||||
mIncomingState = null;
|
||||
mClient = client;
|
||||
mConfiguration = configuration;
|
||||
InputMethodManager imm =
|
||||
@ -49,14 +46,10 @@ public class KeyboardViewState {
|
||||
}
|
||||
|
||||
public void setEditingState(EditingState state) {
|
||||
Editable content = mInputConnection.getEditable();
|
||||
content.replace(0, content.length(), state.text);
|
||||
mInputConnection.setSelection(state.selectionBase, state.selectionExtent);
|
||||
mInputConnection.setComposingRegion(state.composingBase, state.composingExtent);
|
||||
mIncomingState = state;
|
||||
InputMethodManager imm =
|
||||
(InputMethodManager) mView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.updateSelection(mView, state.selectionBase, state.selectionExtent,
|
||||
state.composingBase, state.composingExtent);
|
||||
imm.restartInput(mView);
|
||||
}
|
||||
|
||||
private static int inputTypeFromKeyboardType(int keyboardType) {
|
||||
@ -75,8 +68,15 @@ public class KeyboardViewState {
|
||||
outAttrs.inputType = inputTypeFromKeyboardType(mConfiguration.type);
|
||||
outAttrs.actionLabel = mConfiguration.actionLabel;
|
||||
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
|
||||
mInputConnection = new InputConnectionAdaptor(mView, this);
|
||||
return mInputConnection;
|
||||
InputConnectionAdaptor connection = new InputConnectionAdaptor(mView, mClient);
|
||||
if (mIncomingState != null) {
|
||||
connection.getEditable().append(mIncomingState.text);
|
||||
connection.setSelection(mIncomingState.selectionBase,
|
||||
mIncomingState.selectionExtent);
|
||||
connection.setComposingRegion(mIncomingState.composingBase,
|
||||
mIncomingState.composingExtent);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
public View getView() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user