diff --git a/app/lib/features/chat/conversation/chat_no_reveal.dart b/app/lib/features/chat/conversation/chat_no_reveal.dart new file mode 100644 index 00000000..e0800e55 --- /dev/null +++ b/app/lib/features/chat/conversation/chat_no_reveal.dart @@ -0,0 +1,27 @@ +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; + +/// Keeps a text field inside the transcript from scrolling the transcript to +/// "reveal" its caret on every keystroke. +/// +/// An editable field asks every scrollable above it to show its caret after +/// each edit. The transcript's split, bottom-following viewport answers that +/// with a jump and then follows its newest row back, so a question card's +/// answer box made the whole conversation shake while typing. The card is on +/// screen while the operator types in it, so the request stops here. +class ChatNoReveal extends SingleChildRenderObjectWidget { + const ChatNoReveal({super.key, super.child}); + + @override + RenderObject createRenderObject(BuildContext context) => _RenderNoReveal(); +} + +class _RenderNoReveal extends RenderProxyBox { + @override + void showOnScreen({ + RenderObject? descendant, + Rect? rect, + Duration duration = Duration.zero, + Curve curve = Curves.ease, + }) {} +} diff --git a/app/lib/features/chat/conversation/chat_question_card.dart b/app/lib/features/chat/conversation/chat_question_card.dart index 067a4cdc..4d039e5d 100644 --- a/app/lib/features/chat/conversation/chat_question_card.dart +++ b/app/lib/features/chat/conversation/chat_question_card.dart @@ -173,6 +173,7 @@ class _ChatQuestionCardState extends State { answers: _answers, controllers: _controllers, onChanged: _changed, + onSubmit: _submit, ) else ...chatQuestionRecordedAnswers(context: context, form: widget.form), diff --git a/app/lib/features/chat/conversation/chat_question_card_fields.dart b/app/lib/features/chat/conversation/chat_question_card_fields.dart index 9201c367..9ed4c3ff 100644 --- a/app/lib/features/chat/conversation/chat_question_card_fields.dart +++ b/app/lib/features/chat/conversation/chat_question_card_fields.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../../../core/api_gateway.dart'; import 'chat_card_styles.dart'; import 'chat_question_card_model.dart'; +import 'chat_composer_bar.dart' show chatEnterSends; +import 'chat_no_reveal.dart'; import 'chat_question_option_controls.dart'; /// A free-text input is part of a field when the form allows an answer outside @@ -18,6 +21,7 @@ List chatQuestionFieldWidgets({ required Map answers, required Map controllers, required void Function(String key, Object? value) onChanged, + VoidCallback? onSubmit, }) { final widgets = []; for (final field in fields) { @@ -32,6 +36,7 @@ List chatQuestionFieldWidgets({ answers: answers, controllers: controllers, onChanged: onChanged, + onSubmit: onSubmit, ), ); } @@ -45,6 +50,7 @@ Widget _field({ required Map answers, required Map controllers, required void Function(String key, Object? value) onChanged, + VoidCallback? onSubmit, }) { final title = '${field['title'] ?? ''}'.trim(); final description = '${field['description'] ?? ''}'.trim(); @@ -86,6 +92,7 @@ Widget _field({ keyName: keyName, controller: controllers[keyName]!, hasOptions: chatQuestionOptions(field).isNotEmpty, + onSubmit: onSubmit, onChanged: (value) => onChanged( keyName, chatQuestionTextValue(field, value, answers), @@ -105,6 +112,7 @@ Widget _input({ required TextEditingController controller, required bool hasOptions, required ValueChanged onChanged, + VoidCallback? onSubmit, }) { final placeholder = '${field['placeholder'] ?? ''}'.trim().isNotEmpty ? '${field['placeholder']}'.trim() @@ -123,15 +131,36 @@ Widget _input({ : TextInputType.text; // The visual label sits above the input; the same words must also reach the // input's own semantics node, as the web's `aria-label` does. + // A free-text answer behaves like the chat composer: it grows to a few + // lines, Enter sends (on a desktop or web keyboard) and Shift+Enter breaks + // the line. A secret or typed (number, email, ...) answer stays one line. + final multiline = keyboard == TextInputType.text && !secret; final input = TextField( key: ValueKey('chat-question-input-$keyName'), controller: controller, obscureText: secret, - keyboardType: keyboard, + keyboardType: multiline ? TextInputType.multiline : keyboard, + minLines: 1, + maxLines: multiline ? 6 : 1, + textInputAction: multiline && !chatEnterSends() + ? TextInputAction.newline + : TextInputAction.done, decoration: InputDecoration(hintText: placeholder), onChanged: onChanged, + onSubmitted: onSubmit == null ? null : (_) => onSubmit(), + ); + return Semantics( + label: label, + child: ChatNoReveal( + child: Focus( + canRequestFocus: false, + skipTraversal: true, + onKeyEvent: (_, event) => + _enterKey(event, controller, multiline, onChanged, onSubmit), + child: input, + ), + ), ); - return Semantics(label: label, child: input); } Widget _external(BuildContext context, JsonMap field) { @@ -146,3 +175,49 @@ Widget _external(BuildContext context, JsonMap field) { } return SelectableText(raw); } + +/// Enter sends the answer and Shift+Enter breaks the line, as in the chat +/// composer. An open IME candidate keeps its Enter, and a phone keyboard's +/// Return stays the field's own newline. +KeyEventResult _enterKey( + KeyEvent event, + TextEditingController controller, + bool multiline, + ValueChanged onChanged, + VoidCallback? onSubmit, +) { + final key = event.logicalKey; + if (key != LogicalKeyboardKey.enter && + key != LogicalKeyboardKey.numpadEnter) { + return KeyEventResult.ignored; + } + final value = controller.value; + if (value.composing.isValid && !value.composing.isCollapsed) { + return KeyEventResult.ignored; + } + if (!chatEnterSends()) return KeyEventResult.ignored; + final keyboard = HardwareKeyboard.instance; + if (keyboard.isShiftPressed) { + if (!multiline) return KeyEventResult.handled; + if (event is KeyDownEvent || event is KeyRepeatEvent) { + final selection = value.selection; + final start = selection.isValid ? selection.start : value.text.length; + final end = selection.isValid ? selection.end : start; + final text = value.text.replaceRange(start, end, '\n'); + controller.value = TextEditingValue( + text: text, + selection: TextSelection.collapsed(offset: start + 1), + ); + onChanged(text); + } + return KeyEventResult.handled; + } + if (keyboard.isAltPressed || + keyboard.isControlPressed || + keyboard.isMetaPressed) { + return KeyEventResult.ignored; + } + // A held key must not send twice. + if (event is KeyDownEvent) onSubmit?.call(); + return KeyEventResult.handled; +} diff --git a/app/test/features/chat/conversation/chat_question_card_test.dart b/app/test/features/chat/conversation/chat_question_card_test.dart index 1a2e9fa4..0d3a075a 100644 --- a/app/test/features/chat/conversation/chat_question_card_test.dart +++ b/app/test/features/chat/conversation/chat_question_card_test.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_question_card.dart'; @@ -297,6 +298,53 @@ void main() { }, ); + testWidgets( + 'Enter sends a typed answer; Shift+Enter breaks the line', + (tester) async { + final sent = >[]; + await tester.pumpWidget( + scrollHost( + ChatQuestionCard( + form: { + 'id': 'q', + 'kind': 'question', + 'actions': ['accept', 'decline'], + 'fields': [ + { + 'key': 'q0', + 'title': 'Which one?', + 'type': 'string', + 'required': true, + 'custom': true, + 'options': [ + {'value': 'A', 'label': 'A'}, + ], + }, + ], + }, + onSubmit: (answers, action) async => sent.add(answers), + ), + ), + ); + final box = find.byKey(const ValueKey('chat-question-input-q0')); + await tester.tap(box); + await tester.enterText(box, 'first line'); + await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); + await tester.pump(); + expect(tester.widget(box).controller!.text, 'first line\n'); + expect(sent, isEmpty); + await tester.enterText(box, 'first line\nsecond'); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pumpAndSettle(); + expect(sent, [ + {'q0': 'first line\nsecond'}, + ]); + }, + variant: TargetPlatformVariant.only(TargetPlatform.linux), + ); + testWidgets('dismissing a question sends the reply to the composer', ( tester, ) async { diff --git a/app/tool/perf/chat_question_fixture.dart b/app/tool/perf/chat_question_fixture.dart index edb1faaa..3940ce9d 100644 --- a/app/tool/perf/chat_question_fixture.dart +++ b/app/tool/perf/chat_question_fixture.dart @@ -37,12 +37,15 @@ void chatQuestionStream(String sessionId, void Function(JsonMap frame) emit) { 'session_id': sessionId, 'kind': 'question', 'title': 'Database', + // Claude questions take a custom answer and can be dismissed. + if (_mode == 'claude') 'actions': ['accept', 'decline'], 'fields': [ { 'key': 'q0', 'title': 'Which database should the release use?', 'type': 'string', 'required': true, + if (_mode == 'claude') 'custom': true, 'options': [ {'value': 'Postgres', 'label': 'Postgres'}, {'value': 'SQLite', 'label': 'SQLite'},