Send chat messages with Enter and use Shift+Enter for newlines
This commit is contained in:
@@ -48,8 +48,9 @@ const String chatQueuedNotice =
|
||||
///
|
||||
/// The field owns only its [TextEditingController] and focus; the durable draft
|
||||
/// text lives in the composer controller, so a live transcript rebuild never
|
||||
/// replaces the operator's caret. Enter inserts a newline and Ctrl/Cmd+Enter
|
||||
/// sends the plain message. A leading `/` opens the recorded command palette
|
||||
/// replaces the operator's caret. Enter sends, Shift+Enter inserts a newline,
|
||||
/// and Ctrl/Cmd+Enter remains a send shortcut.
|
||||
/// A leading `/` opens the recorded command palette
|
||||
/// above the field: rows are filtered by name prefix, Up/Down move the
|
||||
/// highlight, Enter or a tap completes the highlighted command, Escape closes
|
||||
/// the palette. A slash draft is never handed to the prompt path: `/new`,
|
||||
@@ -224,8 +225,6 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
KeyEventResult _onKey(FocusNode node, KeyEvent event) {
|
||||
final key = event.logicalKey;
|
||||
final commands = _paletteCommands();
|
||||
final word = chatCommandWord(widget.composer.draft);
|
||||
final slashDraft = word != null;
|
||||
if (key == LogicalKeyboardKey.escape) {
|
||||
if (commands.isEmpty && _commandNotice == null) {
|
||||
return KeyEventResult.ignored;
|
||||
@@ -253,35 +252,54 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
}
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
final sendShortcut = keyboard.isControlPressed || keyboard.isMetaPressed;
|
||||
// A plain Enter accepts the highlighted command instead of sending it. With
|
||||
// no palette showing, a slash draft is still a command, never a prompt.
|
||||
if (!sendShortcut && !keyboard.isShiftPressed) {
|
||||
if (commands.isNotEmpty) {
|
||||
if (event is! KeyDownEvent) return KeyEventResult.handled;
|
||||
_completeCommand(commands[_selectedCommand % commands.length]);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
if (slashDraft) {
|
||||
if (event is! KeyDownEvent) return KeyEventResult.handled;
|
||||
_runSlashDraft();
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
// Enter may be confirming an IME candidate rather than submitting a draft.
|
||||
if (_text.value.composing.isValid && !_text.value.composing.isCollapsed) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
if (!sendShortcut && keyboard.isShiftPressed) {
|
||||
if (event is KeyDownEvent) {
|
||||
final selection = _text.selection;
|
||||
final start = selection.isValid ? selection.start : _text.text.length;
|
||||
final end = selection.isValid ? selection.end : start;
|
||||
_text.value = TextEditingValue(
|
||||
text: _text.text.replaceRange(start, end, '\n'),
|
||||
selection: TextSelection.collapsed(offset: start + 1),
|
||||
);
|
||||
_onDraftEdited(_text.text);
|
||||
}
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
if (!sendShortcut && keyboard.isAltPressed) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
if (!sendShortcut) return KeyEventResult.ignored;
|
||||
// A held key must not post a second prompt.
|
||||
if (event is! KeyDownEvent) return KeyEventResult.handled;
|
||||
// Ctrl/Cmd+Enter is the same submit: a visible palette completes, and a
|
||||
// slash draft reports locally. Only plain text reaches the prompt path.
|
||||
_submitDraft();
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
/// Hardware Enter, the keyboard's Send action and modifier shortcuts share
|
||||
/// the same command/prompt path. An active IME candidate is never submitted.
|
||||
void _submitDraft() {
|
||||
if (_text.value.composing.isValid && !_text.value.composing.isCollapsed) {
|
||||
return;
|
||||
}
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
if (!keyboard.isControlPressed &&
|
||||
!keyboard.isMetaPressed &&
|
||||
(keyboard.isShiftPressed || keyboard.isAltPressed)) {
|
||||
return;
|
||||
}
|
||||
final commands = _paletteCommands();
|
||||
if (commands.isNotEmpty) {
|
||||
_completeCommand(commands[_selectedCommand % commands.length]);
|
||||
return KeyEventResult.handled;
|
||||
return;
|
||||
}
|
||||
if (slashDraft) {
|
||||
if (chatCommandWord(widget.composer.draft) != null) {
|
||||
_runSlashDraft();
|
||||
return KeyEventResult.handled;
|
||||
return;
|
||||
}
|
||||
unawaited(_send());
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
/// Routes a slash draft locally or through the existing Stop controller path.
|
||||
@@ -877,9 +895,13 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
minLines: lines,
|
||||
maxLines: empty ? lines : 6,
|
||||
// Keep the empty field multiline too: a single-line web editor
|
||||
// strips Shift+Enter's newline on the next text edit.
|
||||
maxLines: empty && lines > 1 ? lines : 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.newline,
|
||||
textInputAction: TextInputAction.send,
|
||||
onEditingComplete: () {},
|
||||
onSubmitted: (_) => _submitDraft(),
|
||||
onChanged: _onDraftEdited,
|
||||
);
|
||||
},
|
||||
@@ -892,7 +914,9 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
minLines: 1,
|
||||
maxLines: 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.newline,
|
||||
textInputAction: TextInputAction.send,
|
||||
onEditingComplete: () {},
|
||||
onSubmitted: (_) => _submitDraft(),
|
||||
onChanged: _onDraftEdited,
|
||||
decoration: const InputDecoration(
|
||||
hintText: chatComposerPlaceholder,
|
||||
|
||||
Reference in New Issue
Block a user