Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send empty composing range to engine if invalid #304

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/fleather/lib/src/widgets/editor_input_client_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mixin RawEditorStateTextInputClientMixin on EditorState
// with the last known remote value.
// It is important to prevent excessive remote updates as it can cause
// race conditions.
final actualValue = value.copyWith(
var actualValue = value.copyWith(
composing: _lastKnownRemoteTextEditingValue?.composing,
);

Expand All @@ -107,6 +107,17 @@ mixin RawEditorStateTextInputClientMixin on EditorState
performSpellCheck(value.text);
}

// This is to prevent sending an editing state with invalid composing range
// to engine.
// TODO: Maybe it's better to also include composing range in controller?
// Actions like [_DeleteTextAction] are using controller's editing value
// to update remote value.
// See https://github.com/fleather-editor/fleather/issues/259#issuecomment-2032404450
if (actualValue.composing != TextRange.empty &&
!actualValue.isComposingRangeValid) {
actualValue = actualValue.copyWith(composing: TextRange.empty);
}

_lastKnownRemoteTextEditingValue = actualValue;
_textInputConnection!.setEditingState(actualValue);
}
Expand Down
49 changes: 49 additions & 0 deletions packages/fleather/test/widgets/editor_input_client_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,55 @@ import 'package:flutter_test/flutter_test.dart';
import '../testing.dart';

void main() {
group('send text editing state to TextInputConnection', () {
final composingRanges = <TextRange>[];

void bind(WidgetTester tester) {
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.textInput, (MethodCall methodCall) async {
if (methodCall.method == 'TextInput.setEditingState') {
final Map<String, dynamic> args =
methodCall.arguments as Map<String, dynamic>;
composingRanges.add(TextRange(
start: args['composingBase'], end: args['composingExtent']));
}
return null;
});
}

setUp(() => composingRanges.clear());

testWidgets(
'sends empty composing range if composing range becomes invalid',
(tester) async {
bind(tester);
final document = ParchmentDocument.fromJson([
{'insert': 'some text\n'}
]);
final editor = EditorSandBox(tester: tester, document: document);
await editor.pump();
await editor.tap();
tester.binding.scheduleWarmUpFrame();
final editorState =
tester.state(find.byType(RawEditor)) as RawEditorState;
editorState.updateEditingValueWithDeltas([
TextEditingDeltaNonTextUpdate(
oldText: editorState.textEditingValue.text,
selection: const TextSelection.collapsed(offset: 9),
composing: const TextRange(start: 5, end: 9),
)
]);
await tester.pumpAndSettle();
editor.controller.replaceText(4, 5, '',
selection: const TextSelection.collapsed(offset: 4));
await tester.pumpAndSettle(throttleDuration);
expect(
composingRanges.fold(
true, (v, e) => v && (e == TextRange.empty || e.isValid)),
isTrue);
});
});

group('sets style to TextInputConnection', () {
final log = <TextInputConnectionStyle>[];

Expand Down
Loading