import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'chat_shell_run.dart'; import 'chat_shell_runs_controller.dart'; const String chatShellStopLabel = 'Stop'; const String chatShellStoppingLabel = 'Stopping…'; const String chatShellCopyLabel = 'Copy output'; const String chatShellCopiedLabel = 'Copied'; const String chatShellShareLabel = 'Send to agent'; const String chatShellSharingLabel = 'Sending…'; const String chatShellSharedLabel = 'Sent to agent'; const String chatShellFullScreenLabel = 'Full screen'; /// The card's host buttons: Stop while running; Copy output, Send to agent /// once finished; Full screen for the terminal. class ChatShellCardActions extends StatefulWidget { const ChatShellCardActions({ super.key, required this.run, required this.controller, required this.cupertino, required this.onFullScreen, }); final ChatShellRun run; final ChatShellRunsController controller; final bool cupertino; final VoidCallback onFullScreen; @override State createState() => _ChatShellCardActionsState(); } class _ChatShellCardActionsState extends State { bool _copied = false; Timer? _reset; @override void dispose() { _reset?.cancel(); super.dispose(); } /// Copies the plain text of everything the server kept (no terminal /// escape sequences), read from the server. Future _copy() async { final text = await widget.controller.plainText(widget.run.id); if (text == null) return; await Clipboard.setData(ClipboardData(text: text)); if (!mounted) return; setState(() => _copied = true); _reset?.cancel(); _reset = Timer(const Duration(seconds: 2), () { if (mounted) setState(() => _copied = false); }); } @override Widget build(BuildContext context) { final run = widget.run; final controller = widget.controller; final share = controller.shareState(run.id); final stopping = controller.stopping(run.id); return Wrap( spacing: 8, runSpacing: 4, crossAxisAlignment: WrapCrossAlignment.center, children: [ if (run.running) _button( key: const Key('chat-shell-run-stop'), label: stopping ? chatShellStoppingLabel : chatShellStopLabel, material: Icons.stop, cupertino: CupertinoIcons.stop_fill, destructive: true, onPressed: stopping ? null : () => unawaited(controller.stop(run.id)), ), if (!run.running && run.outputBytes > 0) _button( key: const Key('chat-shell-run-copy'), label: _copied ? chatShellCopiedLabel : chatShellCopyLabel, material: _copied ? Icons.check : Icons.copy_outlined, cupertino: _copied ? CupertinoIcons.checkmark : CupertinoIcons.doc_on_doc, onPressed: () => unawaited(_copy()), ), if (!run.running) _button( key: const Key('chat-shell-run-share'), label: switch (share) { ChatShellShareState.idle => chatShellShareLabel, ChatShellShareState.sending => chatShellSharingLabel, ChatShellShareState.sent => chatShellSharedLabel, }, material: share == ChatShellShareState.sent ? Icons.check : Icons.send_outlined, cupertino: share == ChatShellShareState.sent ? CupertinoIcons.checkmark : CupertinoIcons.paperplane, onPressed: share == ChatShellShareState.idle ? () => unawaited(controller.share(run.id)) : null, ), if (run.running || run.output.isNotEmpty) _button( key: const Key('chat-shell-run-expand'), label: chatShellFullScreenLabel, material: Icons.fullscreen, cupertino: CupertinoIcons.fullscreen, onPressed: widget.onFullScreen, ), ], ); } Widget _button({ required Key key, required String label, required IconData material, required IconData cupertino, required VoidCallback? onPressed, bool destructive = false, }) { if (widget.cupertino) { final color = destructive ? CupertinoColors.systemRed.resolveFrom(context) : null; return CupertinoButton( key: key, padding: const EdgeInsets.symmetric(horizontal: 8), onPressed: onPressed, child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon(cupertino, color: onPressed == null ? null : color), const SizedBox(width: 6), Text( label, style: onPressed == null || color == null ? null : TextStyle(color: color), ), ], ), ); } final style = destructive ? TextButton.styleFrom( foregroundColor: Theme.of(context).colorScheme.error, ) : null; return TextButton.icon( key: key, style: style, onPressed: onPressed, icon: Icon(material), label: Text(label), ); } }