feat: queue a message for the running turn, and fold the composer when the IME closes

- A send while a turn runs joins it (delivery "steer") on a short press; a
  long press offers steer or queue. Queue/steer prompts wait in the server's
  inbox and show as a pending bubble at the end of the transcript, with
  改为排队/转为转向 and 撤回 (which puts the text back into the composer).
  The inbox is read on open and resume and kept current by the
  session.inbox.* events; delivery reloads the tail.
- The send key is stop only while running with an empty draft; with text it
  sends (as dsh-Go does), leaving the long press for the delivery choice.
- The composer also folds to one line when the phone's back gesture closes
  the keyboard while the field keeps focus: the page passes the keyboard
  height (a Scaffold strips the bottom inset from its body) and the composer
  treats a fall below half the peak as a dismissal.
- The Build/Plan chip now opens a bottom sheet with icons, hints and a check,
  instead of a popup menu.
- Tests: composer_box_test (focus opens, IME dismissal folds, typing reopens)
  and inbox_item_test (list items, enqueued events, delivery changes).
- 0.3.16 (316).
This commit is contained in:
blumia committed 2026-11-21 18:32:57 +08:00
1 parent ea3b1f0f5e
commit 1aa84a53e5
13 files changed
+655 -49

No files matched your search

+153 -10
View File
@@ -25,10 +25,15 @@ class ChatScreen extends StatefulWidget {
/// Opens the session drawer. Null where the list is always on screen (a
/// wide layout), so no menu button is shown there.
final VoidCallback? onOpenSessions;
/// The soft keyboard's height, from above the shell's Scaffold (which
/// removes the bottom inset from its body).
final double keyboardInset;
const ChatScreen({
super.key,
required this.app,
required this.session,
this.keyboardInset = 0,
this.onOpenSessions,
});
@@ -143,7 +148,7 @@ class _ChatScreenState extends State<ChatScreen> {
Expanded(child: _messageList()),
_permissions(),
_forms(),
_composer(),
_composer(widget.keyboardInset),
],
),
);
@@ -239,6 +244,11 @@ class _ChatScreenState extends State<ChatScreen> {
break;
}
}
// Prompts waiting in the inbox stand after the transcript, as dsh-Go's
// pending bubbles do.
for (final item in chat.inbox) {
out.add(_Row('pending', item.id, -1));
}
return out;
}
@@ -358,6 +368,8 @@ class _ChatScreenState extends State<ChatScreen> {
return _userRow(r.msgId);
case 'useractions':
return _userActions(r.msgId);
case 'pending':
return _pendingBubble(r.msgId);
case 'thinking':
return const Padding(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
@@ -397,6 +409,87 @@ class _ChatScreenState extends State<ChatScreen> {
return widget.session.title.isEmpty ? '会话' : widget.session.title;
}
/// A prompt in the server's inbox: dimmer than a sent message, with the
/// label dsh-Go uses and its actions (switch between steering and queueing,
/// or take it back into the composer).
Widget _pendingBubble(String id) {
final item = chat.inbox.where((i) => i.id == id).firstOrNull;
if (item == null) return const SizedBox.shrink();
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final steering = item.delivery == 'steer';
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Align(
alignment: Alignment.centerRight,
child: Container(
constraints: const BoxConstraints(maxWidth: 320),
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: scheme.primary.withValues(alpha: 0.55),
borderRadius: BorderRadius.circular(16),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (item.text.isNotEmpty)
Text(item.text,
style: TextStyle(color: scheme.onPrimary)),
if (item.files.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text('📎 ${item.files.length} 个附件',
style: TextStyle(
color: scheme.onPrimary, fontSize: 12.5)),
),
],
),
),
),
Padding(
padding: const EdgeInsets.only(right: 6),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(steering ? Icons.alt_route : Icons.schedule,
size: 13, color: theme.hintColor),
const SizedBox(width: 3),
Text(
!chat.running
? '未发送'
: steering
? '正在转入当前回合…'
: '已排队,本轮结束后发送',
style: TextStyle(fontSize: 12, color: theme.hintColor),
),
const SizedBox(width: 4),
TextButton(
onPressed: () => chat.changeInboxDelivery(
item.id, steering ? 'queue' : 'steer'),
child: Text(steering ? '改为排队' : '转为转向',
style: const TextStyle(fontSize: 12.5)),
),
TextButton(
onPressed: () async {
final ok = await chat.withdrawInbox(item.id);
if (!ok || !mounted) return;
_draftCtrl.text = chat.draft;
_draftCtrl.selection = TextSelection.collapsed(
offset: _draftCtrl.text.length);
_inputFocus.requestFocus();
},
child: const Text('撤回',
style: TextStyle(fontSize: 12.5)),
),
],
),
),
],
);
}
Widget _userRow(String id) {
final m = chat.byId[id]!;
final theme = Theme.of(context);
@@ -1332,10 +1425,12 @@ class _ChatScreenState extends State<ChatScreen> {
onPick: _pickSlash,
);
Widget _composer() {
Widget _composer(double keyboardInset) {
final usage = chat.usageReading();
final scheme = Theme.of(context).colorScheme;
final canSend = _draftCtrl.text.trim().isNotEmpty || _attachments.isNotEmpty;
// The key is stop only while running with nothing to send.
final showStop = chat.running && !canSend;
final slash = _slashMatches();
final slashRows = slash ?? const <SlashItem>[];
Widget body = Padding(
@@ -1356,6 +1451,7 @@ class _ChatScreenState extends State<ChatScreen> {
focusNode: _inputFocus,
hint: chat.running ? '生成中,仍可输入…' : '发消息…',
maxLines: 5,
keyboardInset: keyboardInset,
// While the palette is up, the keyboard's action key picks the
// highlighted row instead of writing a newline.
textInputAction:
@@ -1427,14 +1523,15 @@ class _ChatScreenState extends State<ChatScreen> {
),
)
: null,
// Stop only while the turn runs with an empty draft; once there is
// something to send the key sends again, as dsh-Go's does.
trailing: _roundKey(
icon: chat.running
? Icons.stop_rounded
: Icons.arrow_upward_rounded,
label: chat.running ? '停止' : '发送',
icon: showStop ? Icons.stop_rounded : Icons.arrow_upward_rounded,
label: showStop ? '停止' : '发送',
filled: true,
enabled: chat.running || canSend,
onTap: chat.running ? chat.stop : _send,
enabled: showStop || canSend,
onTap: showStop ? chat.stop : _send,
onLongPress: showStop ? null : _sendOptions,
),
),
],
@@ -1470,6 +1567,7 @@ class _ChatScreenState extends State<ChatScreen> {
required IconData icon,
required String label,
required VoidCallback onTap,
VoidCallback? onLongPress,
bool filled = false,
bool enabled = true,
bool busy = false,
@@ -1492,6 +1590,7 @@ class _ChatScreenState extends State<ChatScreen> {
child: InkWell(
customBorder: const CircleBorder(),
onTap: on ? onTap : null,
onLongPress: on ? onLongPress : null,
child: SizedBox(
width: 36,
height: 36,
@@ -1516,9 +1615,53 @@ class _ChatScreenState extends State<ChatScreen> {
onRemove: (i) => setState(() => _attachments.removeAt(i)),
);
Future<void> _send() async {
/// Long-press on send while a turn runs: join it (steer) or wait for it to
/// end (queue). Only the deliveries the server offers are listed.
Future<void> _sendOptions() async {
if (!chat.running) return _send();
final text = _draftCtrl.text.trim();
if (text.isEmpty && _attachments.isEmpty) return;
final picked = await showModalBottomSheet<String>(
context: context,
showDragHandle: true,
builder: (sheet) => SafeArea(
top: false,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(20, 4, 20, 8),
child: Text('发送方式',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700)),
),
ListTile(
leading: const Icon(Icons.alt_route),
title: const Text('转向当前回合'),
subtitle: const Text('在下一步加入,工作继续'),
onTap: () => Navigator.pop(sheet, 'steer'),
),
ListTile(
leading: const Icon(Icons.schedule),
title: const Text('排队'),
subtitle: const Text('等本轮结束后发送'),
onTap: () => Navigator.pop(sheet, 'queue'),
),
const SizedBox(height: 8),
],
),
),
);
if (picked == null || !mounted) return;
await _send(delivery: picked);
}
Future<void> _send({String? delivery}) async {
final text = _draftCtrl.text.trim();
if (text.isEmpty && _attachments.isEmpty) return;
// An ordinary send while a turn runs joins it, as dsh-Go does; the
// long-press sheet can choose to wait instead.
final effective = chat.running ? (delivery ?? 'steer') : null;
// Files ride with the prompt as data URLs.
final files = <Map<String, String>>[];
final images = <String>[];
@@ -1528,7 +1671,7 @@ class _ChatScreenState extends State<ChatScreen> {
images.add(a.path!);
}
}
final id = await chat.send(text: text, files: files);
final id = await chat.send(text: text, files: files, delivery: effective);
if (id == null) {
// The prompt failed: keep the text for a retry.
_draftCtrl.text = chat.draft;