8e98651db6
Keep the Material-only chat shell path while preserving public Cupertino parameters for callers. Narrow matching tests to the supported Material host.
137 lines
4.2 KiB
Dart
137 lines
4.2 KiB
Dart
import 'dart:async';
|
|
|
|
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<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
|
}
|
|
|
|
class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
|
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<void> _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,
|
|
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,
|
|
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,
|
|
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,
|
|
onPressed: widget.onFullScreen,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _button({
|
|
required Key key,
|
|
required String label,
|
|
required IconData material,
|
|
required VoidCallback? onPressed,
|
|
bool destructive = false,
|
|
}) {
|
|
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),
|
|
);
|
|
}
|
|
}
|