import 'dart:math' as math; import 'package:flutter/material.dart'; import '../../../ui/text/monospace.dart'; import '../domain/chat_shells.dart'; import 'chat_task_presentation.dart'; const _outputVisibleLines = 12; const _outputLineHeight = 1.5; const _outputMaxBudgetShare = 0.45; /// The selected task's existing lazy output page on a separate reading surface. class ChatBackgroundTaskOutput extends StatelessWidget { const ChatBackgroundTaskOutput({ super.key, required this.output, required this.heightBudget, required this.onRefresh, }); final ChatShellOutputSnapshot? output; final double heightBudget; final VoidCallback onRefresh; @override Widget build(BuildContext context) { final state = output?.state ?? ChatShellOutputState.none; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Wrap( alignment: WrapAlignment.spaceBetween, crossAxisAlignment: WrapCrossAlignment.center, spacing: 12, runSpacing: 8, children: [ Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.terminal, size: 18, color: taskSecondary(context)), const SizedBox(width: 8), Flexible( child: Text(shellOutputLabel, style: taskHeading(context)), ), ], ), if (state != ChatShellOutputState.loading && state != ChatShellOutputState.error) ChatBackgroundTaskButton( label: shellOutputRefreshLabel, onPressed: onRefresh, ), ], ), const SizedBox(height: 8), DecoratedBox( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceContainerLowest, borderRadius: BorderRadius.circular(8), ), child: Padding( padding: const EdgeInsets.all(12), child: _outputArea(context), ), ), ], ); } /// The host's own monospace body for one output page. `height` is set so the /// visible-line bound below is measured on the same line box that is drawn. TextStyle _outputStyle(BuildContext context) => (Theme.of(context).textTheme.bodySmall ?? const TextStyle()).copyWith( fontFamily: appMonospaceFamily, height: _outputLineHeight, textBaseline: TextBaseline.alphabetic, ); /// The number of output lines the pane shows before it scrolls: up to twelve, /// fewer when the user's text scale or the bar's remaining height would let /// the pane crowd out the conversation. int _outputVisibleLineCount(BuildContext context, TextStyle style) { final scaledLine = MediaQuery.textScalerOf(context).scale(style.fontSize ?? 12) * _outputLineHeight; final fit = (heightBudget * _outputMaxBudgetShare / scaledLine).floor(); final lines = output?.page?.output.split('\n').length ?? 2; return math.max(2, math.min(lines, math.min(_outputVisibleLines, fit))); } /// Selectable output in the host's own control. Widget _selectableOutput(String output, TextStyle style, int maxLines) => SelectableText( output, semanticsLabel: output, key: const Key('chat-bg-task-output'), style: style, // A multiline selectable body scrolls itself: the host's text control // keeps the page selectable and gives it its own scrollbar/drag, so no // nested scroller can steal the conversation's gestures. maxLines: maxLines, ); Widget _outputArea(BuildContext context) { final state = output?.state ?? ChatShellOutputState.none; final muted = Theme.of(context).colorScheme.onSurfaceVariant; final noteStyle = Theme.of( context, ).textTheme.bodySmall?.copyWith(color: muted); switch (state) { case ChatShellOutputState.none: return const SizedBox.shrink(); case ChatShellOutputState.loading: return const Align( alignment: AlignmentDirectional.centerStart, child: CircularProgressIndicator(), ); case ChatShellOutputState.error: return Row( key: const Key('chat-bg-task-output-error'), crossAxisAlignment: CrossAxisAlignment.start, spacing: 8, children: [ Expanded( child: Text( output?.error ?? '', style: noteStyle?.copyWith( color: Theme.of(context).colorScheme.error, ), ), ), ChatBackgroundTaskButton( label: shellOutputRetryLabel, onPressed: onRefresh, ), ], ); case ChatShellOutputState.unavailable: return Text(shellOutputUnavailableNote, style: noteStyle); case ChatShellOutputState.empty: return Text(shellOutputEmptyNote, style: noteStyle); case ChatShellOutputState.ready: final page = output!.page!; final style = _outputStyle(context); return Column( crossAxisAlignment: CrossAxisAlignment.start, spacing: 4, children: [ _selectableOutput( page.output, style, _outputVisibleLineCount(context, style), ), if (page.truncated) Text(shellOutputBoundedNote, style: noteStyle), ], ); } } } class ChatBackgroundTaskButton extends StatelessWidget { const ChatBackgroundTaskButton({ super.key, required this.label, required this.onPressed, }); final String label; final VoidCallback onPressed; @override Widget build(BuildContext context) => TextButton(onPressed: onPressed, child: Text(label)); }