Files
pipelines/app/lib/features/chat/ui/transcript/notebook/notebook_gutter.dart
T

188 lines
6.5 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../../ui/text/monospace.dart';
import '../chat_message_bubble.dart';
/// Below this width a cell's gutter collapses: its label sits inline above
/// the cell instead of beside it, so a phone keeps the whole width for text.
const double notebookCompactWidth = 600;
/// The gap between a gutter label's end and its cell.
const double notebookGutterGap = 8;
/// An input cell's label: `In [n]:`, or `In [ ]:` for one not yet run.
String notebookInLabel(int? number) => 'In [${number ?? ' '}]:';
/// An output cell's label: `Out [n]:`, or `Out [ ]:` without a prompt.
String notebookOutLabel(int? number) => 'Out [${number ?? ' '}]:';
/// The style of gutter labels: the bundled monospace a step under the
/// reading size, in [color].
TextStyle notebookGutterStyle(BuildContext context, Color color) => TextStyle(
fontFamily: appMonospaceFamily,
fontSize: (chatMessageTextStyle(context)?.fontSize ?? 15) * 0.8,
height: 1.2,
letterSpacing: 0,
color: color,
);
/// The gutter's fixed width: ten monospace characters (`Out [123]:`) at the
/// label size, scaled with the operator's text, and the gap. Every cell
/// shares it, so labels right-align to one column.
double notebookGutterWidth(BuildContext context) {
final size = (chatMessageTextStyle(context)?.fontSize ?? 15) * 0.8;
// RobotoMono advances 0.6 em per character.
return MediaQuery.textScalerOf(context).scale(size) * 0.6 * 10 +
notebookGutterGap;
}
/// The muted colour of an `In` label: the accent pulled towards the
/// secondary ink, as a notebook tints its input prompt.
Color notebookInColor(ColorScheme scheme) =>
Color.lerp(scheme.onSurfaceVariant, scheme.primary, 0.55)!;
/// The muted colour of an `Out` label: the tertiary (a notebook's output
/// orange) pulled towards the secondary ink.
Color notebookOutColor(ColorScheme scheme) =>
Color.lerp(scheme.onSurfaceVariant, scheme.tertiary, 0.55)!;
/// Marks a subtree already inside a cell gutter, so a row nested in another
/// (an activity group's folded turns) does not indent twice.
class NotebookGutterScope extends InheritedWidget {
const NotebookGutterScope({super.key, required super.child});
static bool inside(BuildContext context) =>
context.getInheritedWidgetOfExactType<NotebookGutterScope>() != null;
@override
bool updateShouldNotify(NotebookGutterScope old) => false;
}
/// One transcript row in a notebook: a fixed-width gutter holding [label]
/// right-aligned (with an optional [marker] beneath it), then the cell.
/// [line] is the style of the cell's first line: the label sits on that
/// line's baseline, [top] below the row's top. Without a label the gutter
/// is empty, so rows that are not cells still keep to the cells' column.
/// Narrower than [notebookCompactWidth], the label moves inline above the
/// cell and the gutter goes.
class NotebookGutterRow extends StatelessWidget {
const NotebookGutterRow({
super.key,
this.label,
this.color,
this.top = 0,
this.line,
this.marker,
this.inline = true,
required this.child,
});
/// Whether a narrow row shows the label inline above the cell; false
/// drops it there (the composer, whose cell needs no number on a phone).
final bool inline;
final String? label;
final Color? color;
final double top;
final TextStyle? line;
final Widget? marker;
final Widget child;
@override
Widget build(BuildContext context) {
if (NotebookGutterScope.inside(context)) return child;
final scheme = Theme.of(context).colorScheme;
final style = notebookGutterStyle(
context,
color ?? scheme.onSurfaceVariant,
);
final text = label;
// Only an empty gutter can hold other rows (an activity group's folded
// turns), so only it marks its subtree.
final row = LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < notebookCompactWidth) {
if (text == null || !inline) return child;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(start: 2, bottom: 3),
child: Row(
children: [
_label(text, style, null, TextAlign.start),
if (marker case final marker?) ...[
const SizedBox(width: 6),
marker,
],
],
),
),
child,
],
);
}
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: notebookGutterWidth(context),
child: text == null
? null
: Padding(
padding: EdgeInsetsDirectional.only(
top: top,
end: notebookGutterGap,
),
child: marker == null
? _label(text, style, line, TextAlign.end)
: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
_label(text, style, line, TextAlign.end),
?marker,
],
),
),
),
Expanded(child: child),
],
);
},
);
return text == null ? NotebookGutterScope(child: row) : row;
}
/// The label, kept out of a transcript selection (a copied answer has no
/// prompt numbers in it) and out of semantics (the row already names
/// whose message it is). On [line]'s strut it shares that line's
/// baseline.
Widget _label(
String text,
TextStyle style,
TextStyle? line,
TextAlign align,
) => ExcludeSemantics(
child: SelectionContainer.disabled(
child: Text(
text,
key: const Key('chat-notebook-gutter-label'),
textAlign: align,
maxLines: 1,
softWrap: false,
style: style,
strutStyle: line == null
? null
: StrutStyle(
fontFamily: line.fontFamily,
fontFamilyFallback: line.fontFamilyFallback,
fontSize: line.fontSize,
height: line.height,
forceStrutHeight: true,
),
),
),
);
}