5c3248a48d
Show Expand only when output can overflow and drop the final newline from the drawn output. The full-app probe lists shell runs with ?shell=1 so the cards, Shell mode and Run command dialog can be captured in Chrome.
178 lines
5.6 KiB
Dart
178 lines
5.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'chat_shell_metrics.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 chatShellExpandLabel = 'Expand';
|
|
const String chatShellCollapseLabel = 'Collapse';
|
|
|
|
/// The card's host buttons: Stop while running; Copy output, Send to agent
|
|
/// once finished; Expand/Collapse for the output area.
|
|
class ChatShellCardActions extends StatefulWidget {
|
|
const ChatShellCardActions({
|
|
super.key,
|
|
required this.run,
|
|
required this.controller,
|
|
required this.cupertino,
|
|
required this.expanded,
|
|
required this.onToggleExpanded,
|
|
});
|
|
|
|
final ChatShellRun run;
|
|
final ChatShellRunsController controller;
|
|
final bool cupertino;
|
|
final bool expanded;
|
|
final VoidCallback onToggleExpanded;
|
|
|
|
@override
|
|
State<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
|
}
|
|
|
|
class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
|
bool _copied = false;
|
|
Timer? _reset;
|
|
|
|
@override
|
|
void dispose() {
|
|
_reset?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
/// Copies everything the server kept, reading it first when this card
|
|
/// holds only the newest part.
|
|
Future<void> _copy() async {
|
|
final full = await widget.controller.loadAll(widget.run.id);
|
|
if (full == null) return;
|
|
await Clipboard.setData(ClipboardData(text: full.output));
|
|
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 (widget.expanded || chatShellOutputMayOverflow(run.output))
|
|
_button(
|
|
key: const Key('chat-shell-run-expand'),
|
|
label: widget.expanded
|
|
? chatShellCollapseLabel
|
|
: chatShellExpandLabel,
|
|
material: widget.expanded ? Icons.unfold_less : Icons.unfold_more,
|
|
cupertino: widget.expanded
|
|
? CupertinoIcons.chevron_up
|
|
: CupertinoIcons.chevron_down,
|
|
onPressed: widget.onToggleExpanded,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
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),
|
|
);
|
|
}
|
|
}
|