Files
pipelines/app/lib/features/chat/shell/chat_shell_dismiss.dart
T
alan bac21efc5b Dismiss finished shell command cards with Undo
Finished command cards get a Dismiss button and the chat's More menu a
Dismiss all finished commands entry. Dismissal applies to every client of
the chat; a SnackBar (Material) or transient notice (Cupertino) offers Undo
for 5 s. Running commands cannot be dismissed until they end.
2026-11-09 21:40:20 +00:00

167 lines
5.0 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/segmented_bar_overflow.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'chat_shell_runs_controller.dart';
const String chatShellDismissLabel = 'Dismiss';
const String chatShellDismissAllLabel = 'Dismiss all finished commands';
const String chatShellUndoLabel = 'Undo';
/// How long the Undo notice stays. The server keeps a dismissed run
/// restorable for longer (protocol ChatShellRunUndoWindow, 30 s).
const Duration chatShellUndoDuration = Duration(seconds: 5);
/// The card's Dismiss button, offered once the command has finished.
class ChatShellDismissButton extends StatelessWidget {
const ChatShellDismissButton({
super.key,
required this.cupertino,
required this.onPressed,
});
final bool cupertino;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
if (!cupertino) {
return IconButton(
key: const Key('chat-shell-run-dismiss'),
tooltip: chatShellDismissLabel,
icon: const Icon(Icons.close),
onPressed: onPressed,
);
}
return Semantics(
label: chatShellDismissLabel,
button: true,
excludeSemantics: true,
child: CupertinoButton(
key: const Key('chat-shell-run-dismiss'),
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: onPressed,
child: const Icon(CupertinoIcons.xmark),
),
);
}
}
/// The conversation's "Dismiss all finished commands" More menu entry.
Widget chatShellDismissAllMenuAction({required VoidCallback onPressed}) =>
SegmentedOverflowAction(
key: const ValueKey<String>('chat-shell-dismiss-all-action'),
overflowEntry: SegmentedOverflowEntry(
label: chatShellDismissAllLabel,
icon: Icons.clear_all,
onPressed: onPressed,
),
child: const SizedBox.shrink(),
);
/// Dismisses [ids] for every client of the chat, then offers Undo for
/// [chatShellUndoDuration]: a Material SnackBar, or a transient Cupertino
/// notice floating over the conversation. A refusal goes to [onRefused].
Future<void> dismissChatShellRuns(
BuildContext context,
ChatShellRunsController controller,
List<String> ids, {
required bool cupertino,
void Function(String refusal)? onRefused,
}) async {
if (ids.isEmpty) return;
final messenger = cupertino ? null : ScaffoldMessenger.maybeOf(context);
final overlay = Overlay.maybeOf(context, rootOverlay: true);
final refusal = await controller.dismiss(ids);
if (refusal != null) {
onRefused?.call(refusal);
return;
}
void show(String message, {VoidCallback? onUndo}) {
if (messenger != null) {
messenger.hideCurrentSnackBar();
messenger.showSnackBar(
SnackBar(
key: const Key('chat-shell-undo-notice'),
content: Text(message),
duration: chatShellUndoDuration,
// An action would otherwise keep it until dismissed by hand.
persist: false,
action: onUndo == null
? null
: SnackBarAction(label: chatShellUndoLabel, onPressed: onUndo),
),
);
} else if (overlay != null && overlay.mounted) {
_showOverlayNotice(overlay, message, onUndo);
}
}
show(
ids.length == 1 ? 'Command dismissed' : '${ids.length} commands dismissed',
onUndo: () => unawaited(() async {
final failed = await controller.restore(ids);
if (failed != null) show("Couldn't restore: $failed");
}()),
);
}
OverlayEntry? _overlayNotice;
Timer? _overlayTimer;
void _hideOverlayNotice() {
_overlayTimer?.cancel();
_overlayTimer = null;
final entry = _overlayNotice;
_overlayNotice = null;
if (entry != null && entry.mounted) entry.remove();
}
void _showOverlayNotice(
OverlayState overlay,
String message,
VoidCallback? onUndo,
) {
_hideOverlayNotice();
final entry = OverlayEntry(
builder: (context) => Positioned(
left: 16,
right: 16,
bottom: 16,
child: SafeArea(
child: Semantics(
container: true,
explicitChildNodes: true,
liveRegion: true,
child: KeyedSubtree(
key: const Key('chat-shell-undo-notice'),
child: onUndo == null
? CupertinoPopupSurface(
child: Padding(
padding: const EdgeInsets.all(16),
child: DefaultTextStyle.merge(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: Text(message),
),
),
)
: ChatUndoBar(
message: message,
onUndo: () {
_hideOverlayNotice();
onUndo();
},
),
),
),
),
),
);
_overlayNotice = entry;
overlay.insert(entry);
_overlayTimer = Timer(chatShellUndoDuration, _hideOverlayNotice);
}