Skip to content
Open
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
4 changes: 4 additions & 0 deletions lib/agent/agent_composer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class _AgentComposerState extends State<AgentComposer> {
if (event.logicalKey != LogicalKeyboardKey.enter) {
return KeyEventResult.ignored;
}
final composing = widget.controller.value.composing;
if (composing.isValid && !composing.isCollapsed) {
return KeyEventResult.ignored;
}
if (HardwareKeyboard.instance.isShiftPressed) {
_insertNewLine();
} else {
Expand Down
103 changes: 103 additions & 0 deletions test/agent_composer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,109 @@ void main() {
expect(controller.text, 'check nginx\n');
});

testWidgets('Enter during IME composition leaves the draft for the IME', (
tester,
) async {
var sends = 0;
final controller = TextEditingController();
await pumpComposer(tester, controller: controller, onSubmit: () => sends++);
await tester.tap(find.byType(TextField));
await tester.pump();

tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello',
selection: TextSelection.collapsed(offset: 5),
composing: TextRange(start: 0, end: 5),
),
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();

expect(sends, 0);
expect(controller.text, 'hello');
expect(controller.value.composing, const TextRange(start: 0, end: 5));
});

testWidgets('Shift+Enter during IME composition preserves the draft', (
tester,
) async {
var sends = 0;
final controller = TextEditingController();
await pumpComposer(tester, controller: controller, onSubmit: () => sends++);
await tester.tap(find.byType(TextField));
await tester.pump();

tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello',
selection: TextSelection.collapsed(offset: 5),
composing: TextRange(start: 0, end: 5),
),
);
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pump();

expect(sends, 0);
expect(controller.text, 'hello');
expect(controller.value.composing, const TextRange(start: 0, end: 5));
});

testWidgets('Enter sends after IME commits its text', (tester) async {
var sends = 0;
final controller = TextEditingController();
await pumpComposer(tester, controller: controller, onSubmit: () => sends++);
await tester.tap(find.byType(TextField));
await tester.pump();

tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello',
selection: TextSelection.collapsed(offset: 5),
composing: TextRange(start: 0, end: 5),
),
);
await tester.pump();
tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello',
selection: TextSelection.collapsed(offset: 5),
),
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();

expect(sends, 1);
expect(controller.text, 'hello');
});

testWidgets('Enter sends with a collapsed composing range', (tester) async {
var sends = 0;
final controller = TextEditingController();
await pumpComposer(tester, controller: controller, onSubmit: () => sends++);
await tester.tap(find.byType(TextField));
await tester.pump();

tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello',
selection: TextSelection.collapsed(offset: 5),
composing: TextRange.collapsed(5),
),
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();

expect(sends, 1);
expect(controller.text, 'hello');
});

testWidgets('a working turn queues instead of sending, and offers stop', (
tester,
) async {
Expand Down