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

255 lines
9.0 KiB
Dart

import '../../../ui/theme/app_theme.dart';
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_shells.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../../../ui/text/monospace.dart';
/// One bounded output page: the web's 220 px pane is twelve 18 px monospace
/// lines (`web/chat.css`), so the bound is read from the host's own output type
/// and the user's text scale rather than a fixed pixel row. A longer page
/// scrolls inside this box and never moves the transcript or composer.
const _outputVisibleLines = 12;
const _outputLineHeight = 1.5;
/// Even at a large text scale one pane keeps this share of the bar's own height
/// budget, so the bar's chrome and list keep room and the conversation below it
/// is never crowded out.
const _outputMaxBudgetShare = 0.45;
/// The selected background task's read-only detail and bounded output page.
///
/// Every value is a recorded fact: the server's command, the shell identity, the
/// running status, and one lazy `chat_shell_output` page. The output area keeps
/// the five states apart, so an unavailable or empty page and a refused read are
/// never shown as output. Output is selectable and reads in a monospace body.
class ChatBackgroundTaskDetail extends StatelessWidget {
const ChatBackgroundTaskDetail({
super.key,
required this.shell,
required this.output,
required this.heightBudget,
required this.onRefresh,
});
final JsonMap shell;
final ChatShellOutputSnapshot? output;
/// The space the enclosing task bar has for its whole body. The output pane is
/// measured against this, not the window, so a list of rows, a narrow phone or
/// a large text scale cannot make the detail outgrow its own bar.
final double heightBudget;
final VoidCallback onRefresh;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final content = DefaultTextStyle.merge(
style: cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: DefaultTextStyle.of(context).style,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_label(context, cupertino, shellOutputCommandLabel),
Text(
blankShellValue(shell['command'])
? shellOutputUnavailableLabel
: '${shell['command']}',
),
_label(context, cupertino, shellOutputTaskIdentityLabel),
Text(
shellIdOf(shell),
style: const TextStyle(fontFamily: appMonospaceFamily),
),
Text(shellStatusRunningLabel),
_outputArea(context, cupertino),
],
),
);
if (cupertino) {
return CupertinoListSection.insetGrouped(
backgroundColor: AppColors.groupedBackground,
children: [Padding(padding: const EdgeInsets.all(12), child: content)],
);
}
return Card(
margin: EdgeInsets.zero,
child: Padding(padding: const EdgeInsets.all(12), child: content),
);
}
Widget _label(BuildContext context, bool cupertino, String text) {
final style = cupertino
? CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
)
: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
return Text(text, style: style);
}
/// 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();
return math.max(2, 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,
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: [
_label(context, cupertino, shellOutputLabel),
_selectableOutput(
context,
cupertino,
page.output,
style,
_outputVisibleLineCount(context, style),
),
if (page.truncated) Text(shellOutputBoundedNote, style: noteStyle),
Align(
alignment: AlignmentDirectional.centerStart,
child: ChatBackgroundTaskButton(
label: shellOutputRefreshLabel,
onPressed: onRefresh,
),
),
],
);
}
}
}
/// The host's own text button for a task action (Retry/Refresh).
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 (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onPressed,
child: Text(label),
);
}
return TextButton(onPressed: onPressed, child: Text(label));
}
}