import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../../ui/text/monospace.dart'; import '../domain/chat_session_handoff.dart'; import '../ui/sessions/chat_create_fields.dart'; import 'chat_export_controller.dart'; /// The info sheet's "Session" section: the harness's native session id with a /// copy control, the harness and the working directory, the verified terminal /// command that resumes the session, and the export actions. class ChatSessionSection extends StatefulWidget { const ChatSessionSection({ super.key, required this.handoff, required this.exporter, required this.cupertino, }); final ChatSessionHandoff handoff; final ChatExportController exporter; final bool cupertino; @override State createState() => _ChatSessionSectionState(); } class _ChatSessionSectionState extends State { /// Which copy control last succeeded, shown as "Copied" for a moment. String? _copied; Timer? _reset; @override void dispose() { _reset?.cancel(); super.dispose(); } Future _copy(String key, String text) async { await Clipboard.setData(ClipboardData(text: text)); if (!mounted) return; setState(() => _copied = key); _reset?.cancel(); _reset = Timer(const Duration(seconds: 2), () { if (mounted) setState(() => _copied = null); }); } @override Widget build(BuildContext context) { final cupertino = widget.cupertino; final handoff = widget.handoff; final secondary = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final body = cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodyLarge; final title = cupertino ? CupertinoTheme.of( context, ).textTheme.textStyle.copyWith(fontWeight: FontWeight.w600) : Theme.of(context).textTheme.titleSmall; final label = (cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodySmall) ?.copyWith(color: secondary); final mono = body?.copyWith(fontFamily: appMonospaceFamily); final muted = body?.copyWith(color: secondary); Widget fact(String key, String name, String value, {bool code = false}) { final known = value.isNotEmpty; return Padding( key: Key('chat-session-$key'), padding: const EdgeInsets.only(top: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(name, style: label), Text( known ? value : 'Unavailable', key: Key('chat-session-value-$key'), style: !known ? muted : (code ? mono : body), ), ], ), ); } return AnimatedBuilder( animation: widget.exporter, builder: (context, _) { final exporter = widget.exporter; final status = exporter.status; return Padding( key: const Key('chat-session-section'), padding: const EdgeInsetsDirectional.fromSTEB(24, 12, 24, 6), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text('Session', style: title), Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: fact('id', 'Session ID', handoff.id, code: true), ), _ActionButton( key: const Key('chat-session-copy-id'), cupertino: cupertino, label: _copied == 'id' ? 'Copied' : 'Copy', semanticsLabel: _copied == 'id' ? 'Copied' : 'Copy session ID', icon: _copied == 'id' ? (cupertino ? CupertinoIcons.checkmark : Icons.check) : (cupertino ? CupertinoIcons.doc_on_doc : Icons.copy_outlined), onPressed: handoff.id.isEmpty ? null : () => _copy('id', handoff.id), ), ], ), fact('harness', 'Harness', chatBackendLabel(handoff.backend)), fact('directory', 'Directory', handoff.directory, code: true), if (handoff.resumeCommand.isNotEmpty) fact( 'resume', 'Resume in a terminal', handoff.resumeCommand, code: true, ), const SizedBox(height: 8), Wrap( spacing: 8, runSpacing: 4, children: [ if (handoff.resumeCommand.isNotEmpty) _ActionButton( key: const Key('chat-session-copy-resume'), cupertino: cupertino, label: _copied == 'resume' ? 'Copied' : 'Copy resume command', icon: _copied == 'resume' ? (cupertino ? CupertinoIcons.checkmark : Icons.check) : (cupertino ? CupertinoIcons .chevron_left_slash_chevron_right : Icons.terminal), onPressed: () => _copy('resume', handoff.resumeCommand), ), _exportButton( ChatExportFormat.markdown, 'Export conversation', cupertino ? CupertinoIcons.square_arrow_up : Icons.ios_share, ), _exportButton( ChatExportFormat.json, 'Export JSON', cupertino ? CupertinoIcons.doc_text : Icons.data_object, ), if (handoff.nativeExport) _exportButton( ChatExportFormat.native, 'Export native session', cupertino ? CupertinoIcons.doc : Icons.description_outlined, ), ], ), if (status != null && status.isNotEmpty) Padding( padding: const EdgeInsets.only(top: 6), child: Semantics( liveRegion: true, child: Text( status, key: const Key('chat-session-export-status'), style: label, ), ), ), ], ), ); }, ); } Widget _exportButton(ChatExportFormat format, String label, IconData icon) { final exporter = widget.exporter; return _ActionButton( key: Key('chat-session-export-${format.wire}'), cupertino: widget.cupertino, label: label, icon: icon, busy: exporter.active == format, onPressed: exporter.busy ? null : () => exporter.export(format), ); } } /// A standard text button with a leading icon, in the host's style. class _ActionButton extends StatelessWidget { const _ActionButton({ super.key, required this.cupertino, required this.label, required this.icon, required this.onPressed, this.semanticsLabel, this.busy = false, }); final bool cupertino; final String label; final String? semanticsLabel; final IconData icon; final VoidCallback? onPressed; final bool busy; @override Widget build(BuildContext context) { final Widget leading = busy ? (cupertino ? const CupertinoActivityIndicator() : const SizedBox.square( dimension: 18, child: CircularProgressIndicator(strokeWidth: 2), )) : Icon(icon, size: 18); final child = Row( mainAxisSize: MainAxisSize.min, children: [ leading, const SizedBox(width: 6), Flexible(child: Text(label, semanticsLabel: semanticsLabel)), ], ); final button = cupertino ? CupertinoButton( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6), onPressed: onPressed, child: child, ) : TextButton(onPressed: onPressed, child: child); return button; } }