Files
pipelines/app/lib/features/chat/ui/transcript/chat_turn_chip.dart
T
2026-12-03 23:22:56 +00:00

191 lines
5.7 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../ui/adaptive/app_popups.dart';
import 'chat_turn_activity_view.dart';
import '../../../../ui/theme/app_design_tokens.dart';
/// The quiet chip under an agent turn (its tools, its permissions) and the
/// native sheet it opens.
///
/// Its leading icon starts at the reply text's edge: the chip sits at the
/// row's edge with the same inset as the turn's activity line. Its chevron is
/// the transcript's expand affordance and turns down while the sheet is open.
class ChatTurnChip extends StatefulWidget {
const ChatTurnChip({
super.key,
required this.chipKey,
required this.leading,
required this.label,
required this.color,
required this.semanticsLabel,
this.sheet,
this.open,
this.ellipsizeLabel = false,
this.labelSpans,
this.detail,
}) : assert(sheet != null || open != null);
final Key chipKey;
final Widget leading;
final String label;
final Color color;
final String semanticsLabel;
/// The detail content the chip opens, unless [open] presents it instead.
final Widget? sheet;
/// A custom presentation (a full-screen route on a narrow window).
final Future<void> Function(BuildContext context)? open;
/// Shrink a long label to one ellipsized line. Only for a chip whose parent
/// bounds its width (a turn footer's Wrap, an aligned row).
final bool ellipsizeLabel;
/// Styled pieces of [label] (e.g. coloured counts); [label] stays the
/// plain text. Pieces without their own style inherit the chip's style.
final List<InlineSpan> Function(BuildContext context)? labelSpans;
/// A second line under the label, shown in full (a question's answer).
final Widget? detail;
@override
State<ChatTurnChip> createState() => _ChatTurnChipState();
}
class _ChatTurnChipState extends State<ChatTurnChip> {
bool _open = false;
@override
Widget build(BuildContext context) {
final labelRow = Row(
mainAxisSize: MainAxisSize.min,
children: [
if (widget.ellipsizeLabel || widget.detail != null)
Flexible(child: _label(maxLines: 1, overflow: TextOverflow.ellipsis))
else
_label(),
const SizedBox(width: 2),
AnimatedRotation(
turns: _open ? 0.25 : 0,
duration: const Duration(milliseconds: 150),
child: Icon(Icons.chevron_right, size: 18, color: widget.color),
),
],
);
final content = Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(top: 2),
child: widget.leading,
),
const SizedBox(width: 6),
Flexible(
child: widget.detail == null
? labelRow
: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [labelRow, widget.detail!],
),
),
],
);
final corners = AppDesignTokens.of(
context,
).corners(BorderRadius.circular(8));
return Semantics(
button: true,
expanded: _open,
// The child's own action is excluded with its label, so the node
// carries the activation for assistive technology.
onTap: _show,
label: widget.semanticsLabel,
excludeSemantics: true,
child: Padding(
padding: const EdgeInsets.only(top: 2),
child: _glass(
context,
corners,
InkWell(
key: widget.chipKey,
borderRadius: corners,
onTap: _show,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: chatAgentContentInset,
vertical: AppDesignTokens.of(context).density.space(6),
),
child: content,
),
),
),
),
);
}
/// Under a glass design the chip is a slim glass card (a tint and a
/// light top edge); elsewhere it is the bare ink row.
Widget _glass(BuildContext context, BorderRadius corners, Widget chip) {
final glass = AppDesignTokens.of(context).glass;
if (glass == null) return chip;
return Material(
color: glass.card,
shape: GlassBorder(
borderRadius: corners,
highlight: glass.highlight,
edge: glass.edge,
),
child: chip,
);
}
Widget _label({int? maxLines, TextOverflow? overflow}) {
final style = chatTurnChipTextStyle(context, widget.color);
final spans = widget.labelSpans;
if (spans == null) {
return Text(
widget.label,
maxLines: maxLines,
overflow: overflow,
style: style,
);
}
return Builder(
builder: (context) => Text.rich(
TextSpan(children: spans(context)),
maxLines: maxLines,
overflow: overflow,
style: style,
),
);
}
Future<void> _show() async {
if (_open) return;
setState(() => _open = true);
try {
if (widget.open case final open?) {
await open(context);
} else {
await showChatTurnSheet(context, widget.sheet!);
}
} finally {
if (mounted) setState(() => _open = false);
}
}
}
/// The secondary text of a turn's meta rows (chips, the activity line):
/// smaller and quieter than the reply's prose.
TextStyle? chatTurnChipTextStyle(BuildContext context, Color color) => Theme.of(
context,
).textTheme.bodyMedium?.copyWith(color: color, fontWeight: FontWeight.w400);
/// The detail surface a turn chip opens: a dialog on desktop and a bottom
/// sheet on a phone.
Future<void> showChatTurnSheet(BuildContext context, Widget sheet) {
return showAppDetails<void>(context, builder: (_) => sheet);
}