Files
pipelines/app/lib/features/chat/conversation/chat_background_task_output.dart
T

228 lines
7.6 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
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 cupertino = taskCupertino(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(
cupertino
? CupertinoIcons.chevron_left_slash_chevron_right
: 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: cupertino
? CupertinoColors.tertiarySystemGroupedBackground.resolveFrom(
context,
)
: Theme.of(context).colorScheme.surfaceContainerLowest,
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: _outputArea(context, cupertino),
),
),
],
);
}
/// 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, bool cupertino) =>
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall ?? const TextStyle())
.copyWith(
fontFamily: appMonospaceFamily,
height: _outputLineHeight,
// Cupertino's style is not inheriting; a selectable body needs a
// baseline from its own style.
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. A Cupertino-only app has no
/// `MaterialLocalizations`, which `SelectableText` asserts on, so one English
/// default scope is supplied for its context menu (the chat markdown view's
/// own pattern).
Widget _selectableOutput(
BuildContext context,
bool cupertino,
String output,
TextStyle style,
int maxLines,
) {
final text = 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,
);
if (!cupertino) return text;
return Localizations(
locale: Localizations.maybeLocaleOf(context) ?? const Locale('en'),
delegates: const [
DefaultWidgetsLocalizations.delegate,
DefaultMaterialLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
child: text,
);
}
Widget _outputArea(BuildContext context, bool cupertino) {
final state = output?.state ?? ChatShellOutputState.none;
final muted = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final noteStyle =
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall)
?.copyWith(color: muted);
switch (state) {
case ChatShellOutputState.none:
return const SizedBox.shrink();
case ChatShellOutputState.loading:
return Align(
alignment: AlignmentDirectional.centerStart,
child: cupertino
? const CupertinoActivityIndicator()
: const 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: cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: 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, cupertino);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_selectableOutput(
context,
cupertino,
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) {
if (taskCupertino(context)) {
return CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onPressed,
child: Text(label),
);
}
return TextButton(onPressed: onPressed, child: Text(label));
}
}