460 lines
15 KiB
Dart
460 lines
15 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/rendering.dart';
|
||
|
||
import 'chat_transcript_selection.dart';
|
||
import 'chat_code_hover.dart';
|
||
import '../../../../ui/text/monospace.dart';
|
||
import 'chat_link_launcher.dart';
|
||
import 'chat_link_spans.dart';
|
||
import 'chat_markdown.dart';
|
||
import 'chat_markdown_cache.dart';
|
||
import 'chat_markdown_speech.dart';
|
||
import 'chat_message_bubble.dart';
|
||
import '../../speech/speech.dart';
|
||
import 'chat_table_column_width.dart';
|
||
|
||
/// Renders one assistant text part with native text widgets and the host's own
|
||
/// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open
|
||
/// through [onOpenLink] when given, else the [ChatLinkScope] launcher, which by
|
||
/// default opens them outside the app. No font family, size, colour or component
|
||
/// dimension is overridden; code keeps its exact whitespace and scrolls
|
||
/// sideways instead of widening the transcript, while table cells wrap.
|
||
class ChatMarkdownView extends StatefulWidget {
|
||
const ChatMarkdownView({
|
||
super.key,
|
||
required this.text,
|
||
this.messageId,
|
||
this.onOpenLink,
|
||
});
|
||
|
||
final String text;
|
||
final String? messageId;
|
||
final ValueChanged<String>? onOpenLink;
|
||
|
||
@override
|
||
State<ChatMarkdownView> createState() => _ChatMarkdownViewState();
|
||
}
|
||
|
||
/// The transcript's `ChatMessageTextBuilder` seam: a caller that already has the
|
||
/// markdown renderer can pass this function as `textBuilder`.
|
||
Widget chatMarkdownText(
|
||
BuildContext context,
|
||
String text, {
|
||
ValueChanged<String>? onOpenLink,
|
||
}) => ChatMarkdownView(text: text, onOpenLink: onOpenLink);
|
||
|
||
class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||
late final ChatLinkSpans _links = ChatLinkSpans(
|
||
onChanged: () => setState(() => _rendered = null),
|
||
);
|
||
Widget? _rendered;
|
||
final _codeHover = ChatCodeHover();
|
||
ScrollPosition? _scroll;
|
||
ModalRoute<dynamic>? _route;
|
||
var _sourceCursor = 0;
|
||
|
||
SpeechRange? _revealed;
|
||
final _textKeys = <GlobalKey>[];
|
||
var _textCount = 0;
|
||
final _segments = <MarkdownSpokenSegment>[];
|
||
|
||
@override
|
||
void didUpdateWidget(ChatMarkdownView oldWidget) {
|
||
super.didUpdateWidget(oldWidget);
|
||
if (widget.text != oldWidget.text) {
|
||
_rendered = null;
|
||
_codeHover.hide();
|
||
}
|
||
}
|
||
|
||
@override
|
||
void didChangeDependencies() {
|
||
super.didChangeDependencies();
|
||
// Cached spans use host theme/localization roles. Never freeze those when
|
||
// brightness, typography, locale or surrounding text styles change.
|
||
_rendered = null;
|
||
_codeHover.hide();
|
||
_route = ModalRoute.of(context);
|
||
_scroll?.removeListener(_codeHover.hide);
|
||
_scroll = Scrollable.maybeOf(context)?.position;
|
||
_scroll?.addListener(_codeHover.hide);
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_codeHover.dispose();
|
||
_scroll?.removeListener(_codeHover.hide);
|
||
_links.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// Keep the state subscribed to inherited styling even when the cached
|
||
// subtree is returned without rebuilding its Markdown spans.
|
||
Theme.of(context);
|
||
final speech = speechController;
|
||
return ListenableBuilder(
|
||
listenable: Listenable.merge([speech, speech.word]),
|
||
builder: (context, _) {
|
||
final range = spokenSourceIn(speech, widget.messageId, widget.text);
|
||
if (range == null) {
|
||
_revealed = null;
|
||
} else if (range != _revealed) {
|
||
_revealed = range;
|
||
WidgetsBinding.instance.addPostFrameCallback((_) => _reveal(range));
|
||
}
|
||
// The text is built once and never restyled while speaking; the
|
||
// spoken word is painted behind it, so nothing shifts or rewraps.
|
||
final rendered = _rendered ??= _buildRendered(context);
|
||
return CustomPaint(
|
||
key: _paintKey,
|
||
painter: SpokenWordPainter(
|
||
anchor: _paintKey,
|
||
segments: _segments,
|
||
range: () => spokenSourceIn(speech, widget.messageId, widget.text),
|
||
color: Theme.of(context).colorScheme.primaryContainer,
|
||
repaint: Listenable.merge([speech, speech.word]),
|
||
),
|
||
child: rendered,
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
final _paintKey = GlobalKey();
|
||
|
||
Widget _buildRendered(BuildContext context) {
|
||
_sourceCursor = 0;
|
||
_textCount = 0;
|
||
_segments.clear();
|
||
final gap = chatParagraphGap(context);
|
||
final blocks = chatMarkdownCache.blocksFor(widget.text);
|
||
final rendered = Listener(
|
||
onPointerDown: (_) => _codeHover.hide(),
|
||
onPointerSignal: (_) => _codeHover.hide(),
|
||
child: NotificationListener<ScrollNotification>(
|
||
onNotification: (_) {
|
||
_codeHover.hide();
|
||
return false;
|
||
},
|
||
child: _links.selectionArea(
|
||
context,
|
||
Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
for (var index = 0; index < blocks.length; index += 1)
|
||
Padding(
|
||
padding: EdgeInsets.only(
|
||
top: index == 0 ? 0 : _gapAfter(blocks[index - 1], gap),
|
||
),
|
||
child: _block(context, blocks[index]),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
_rendered = rendered;
|
||
return rendered;
|
||
}
|
||
|
||
/// Blocks are separated by an empty line of body text, like paragraphs;
|
||
/// a heading stays close to the content it introduces.
|
||
double _gapAfter(MarkdownBlock previous, double paragraphGap) =>
|
||
previous is MarkdownHeading ? 8 : paragraphGap;
|
||
|
||
Widget _block(BuildContext context, MarkdownBlock block) {
|
||
switch (block) {
|
||
case MarkdownHeading(level: final level, text: final text):
|
||
return Semantics(
|
||
container: true,
|
||
headingLevel: level,
|
||
child: _text(context, text, style: _headingStyle(context, level)),
|
||
);
|
||
case MarkdownParagraph(text: final text):
|
||
return _text(context, text);
|
||
case MarkdownRule():
|
||
return Divider(color: _dividerColor(context));
|
||
case MarkdownQuote(lines: final lines):
|
||
return DecoratedBox(
|
||
decoration: BoxDecoration(
|
||
border: BorderDirectional(
|
||
start: BorderSide(color: _dividerColor(context)),
|
||
),
|
||
),
|
||
child: Padding(
|
||
padding: const EdgeInsetsDirectional.only(start: 12),
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
spacing: 4,
|
||
children: [
|
||
for (final line in lines)
|
||
if (line.trim().isNotEmpty) _text(context, line),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
case MarkdownList(ordered: final ordered, items: final items):
|
||
return Semantics(
|
||
container: true,
|
||
explicitChildNodes: true,
|
||
role: SemanticsRole.list,
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
spacing: 4,
|
||
children: [
|
||
for (var index = 0; index < items.length; index += 1)
|
||
Semantics(
|
||
container: true,
|
||
role: SemanticsRole.listItem,
|
||
child: Row(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Padding(
|
||
padding: const EdgeInsetsDirectional.only(end: 8),
|
||
child: Text(ordered ? '${index + 1}.' : '•'),
|
||
),
|
||
Expanded(child: _text(context, items[index])),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
case MarkdownTable(header: final header, rows: final rows):
|
||
// Cells wrap to the transcript width; the table never scrolls sideways.
|
||
return Table(
|
||
defaultColumnWidth: ChatTableColumnWidth(header.length),
|
||
border: TableBorder.all(color: _dividerColor(context)),
|
||
children: [
|
||
TableRow(
|
||
decoration: BoxDecoration(color: _headerFill(context)),
|
||
children: [
|
||
for (final cell in header) _cell(context, cell, header: true),
|
||
],
|
||
),
|
||
for (final row in rows)
|
||
TableRow(
|
||
children: [for (final cell in row) _cell(context, cell)],
|
||
),
|
||
],
|
||
);
|
||
case MarkdownCode(language: final language, code: final code):
|
||
_skipCodeBlock(code);
|
||
final labelStyle = _roleStyle(
|
||
context,
|
||
Theme.of(context).textTheme.labelSmall,
|
||
);
|
||
return ChatCodeCopyRegion(
|
||
code: code,
|
||
label: Text(markdownCodeLabel(language), style: labelStyle),
|
||
child: DecoratedBox(
|
||
decoration: BoxDecoration(color: _codeFill(context)),
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(12),
|
||
child: ChatTranscriptHorizontalScrollView(
|
||
label: 'code block',
|
||
child: Text(
|
||
code,
|
||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
void _skipCodeBlock(String code) {
|
||
_sourceCursor = fencedCodeSpan(
|
||
widget.text,
|
||
_sourceCursor,
|
||
code,
|
||
).end.clamp(0, widget.text.length);
|
||
}
|
||
|
||
Widget _cell(BuildContext context, String text, {bool header = false}) {
|
||
final style = header
|
||
? _roleStyle(context, Theme.of(context).textTheme.labelLarge)
|
||
: null;
|
||
return Semantics(
|
||
role: header ? SemanticsRole.columnHeader : null,
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(8),
|
||
child: _text(context, text, style: style),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _text(BuildContext context, String text, {TextStyle? style}) {
|
||
if (text.isEmpty) return const SizedBox.shrink();
|
||
final sourceStart = widget.text.indexOf(text, _sourceCursor);
|
||
if (sourceStart >= 0) _sourceCursor = sourceStart + text.length;
|
||
// Keys follow render order, which is stable for the same text, so the
|
||
// revealed word's paragraph can be found after the frame.
|
||
if (_textCount == _textKeys.length) _textKeys.add(GlobalKey());
|
||
final key = _textKeys[_textCount++];
|
||
final source = MarkdownSourceCursor(
|
||
widget.text,
|
||
sourceStart < 0 ? _sourceCursor : sourceStart,
|
||
key,
|
||
);
|
||
return Builder(
|
||
builder: (textContext) => Text.rich(
|
||
key: key,
|
||
TextSpan(children: _spans(textContext, parseInline(text), source)),
|
||
style: style,
|
||
),
|
||
);
|
||
}
|
||
|
||
List<InlineSpan> _spans(
|
||
BuildContext context,
|
||
List<MarkdownInline> spans,
|
||
MarkdownSourceCursor source,
|
||
) => [
|
||
for (final span in spans)
|
||
switch (span) {
|
||
MarkdownPlain(text: final text) => _leaf(context, text, source),
|
||
MarkdownInlineCode(text: final text) => _inlineCode(
|
||
context,
|
||
text,
|
||
source,
|
||
),
|
||
MarkdownStrong(spans: final inner) => TextSpan(
|
||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||
children: _spans(context, inner, source),
|
||
),
|
||
MarkdownEmphasis(spans: final inner) => TextSpan(
|
||
style: const TextStyle(fontStyle: FontStyle.italic),
|
||
children: _spans(context, inner, source),
|
||
),
|
||
MarkdownLink(url: final url, spans: final inner) => _link(
|
||
context,
|
||
url,
|
||
inner,
|
||
source,
|
||
),
|
||
},
|
||
];
|
||
|
||
TextSpan _leaf(
|
||
BuildContext context,
|
||
String text,
|
||
MarkdownSourceCursor source,
|
||
) => _highlightedText(context, text, source);
|
||
|
||
TextSpan _inlineCode(
|
||
BuildContext context,
|
||
String text,
|
||
MarkdownSourceCursor source,
|
||
) {
|
||
final token = text;
|
||
final highlighted = _highlightedText(context, text, source);
|
||
return TextSpan(
|
||
style: TextStyle(
|
||
fontFamily: appMonospaceFamily,
|
||
backgroundColor: _codeFill(context),
|
||
),
|
||
text: highlighted.text,
|
||
children: highlighted.children,
|
||
onEnter: (event) {
|
||
if (_route?.isCurrent != false) {
|
||
_codeHover.show(context, token, text, event.position);
|
||
}
|
||
},
|
||
onExit: (_) => _codeHover.scheduleHide(token),
|
||
);
|
||
}
|
||
|
||
InlineSpan _link(
|
||
BuildContext context,
|
||
String url,
|
||
List<MarkdownInline> spans,
|
||
MarkdownSourceCursor source,
|
||
) => _links.link(
|
||
context,
|
||
url,
|
||
_spans(context, spans, source),
|
||
onOpen: (uri) {
|
||
final onOpenLink = widget.onOpenLink;
|
||
if (onOpenLink != null) return onOpenLink(uri.toString());
|
||
unawaited(openChatLink(context, uri));
|
||
},
|
||
);
|
||
|
||
/// A leaf's spans, recording where it renders so a spoken word in it can
|
||
/// be revealed, with the spoken characters highlighted.
|
||
TextSpan _highlightedText(
|
||
BuildContext context,
|
||
String text,
|
||
MarkdownSourceCursor source,
|
||
) {
|
||
final offsets = source.consume(text);
|
||
final renderedStart = source.visibleOffset;
|
||
source.visibleOffset += text.length;
|
||
if (offsets.isNotEmpty) {
|
||
_segments.add(
|
||
MarkdownSpokenSegment(
|
||
source.key,
|
||
offsets.reduce((a, b) => a < b ? a : b),
|
||
offsets.reduce((a, b) => a > b ? a : b) + 1,
|
||
renderedStart,
|
||
source.visibleOffset,
|
||
offsets,
|
||
),
|
||
);
|
||
}
|
||
return TextSpan(text: text);
|
||
}
|
||
|
||
void _reveal(SpeechRange range) {
|
||
final scroll = _scroll;
|
||
if (!mounted || range != _revealed || scroll == null) return;
|
||
revealSpokenWord(
|
||
scroll,
|
||
_segments,
|
||
range,
|
||
reduceMotion: MediaQuery.maybeDisableAnimationsOf(context) ?? false,
|
||
);
|
||
}
|
||
|
||
/// Heading roles are a non-increasing sequence from the host's own text
|
||
/// theme. H3–H6 share one title role, matching the web renderer's flat
|
||
/// lower-heading treatment, and no level falls back to a body role, so a
|
||
/// heading never reads like a paragraph.
|
||
TextStyle? _headingStyle(BuildContext context, int level) {
|
||
final text = Theme.of(context).textTheme;
|
||
final role = switch (level) {
|
||
1 => text.headlineSmall,
|
||
2 => text.titleLarge,
|
||
_ => text.titleMedium,
|
||
};
|
||
return _roleStyle(context, role);
|
||
}
|
||
|
||
/// Merges a host theme role into the ambient text style.
|
||
TextStyle? _roleStyle(BuildContext context, TextStyle? role) => role == null
|
||
? null
|
||
: DefaultTextStyle.of(
|
||
context,
|
||
).style.copyWith(inherit: true).merge(role.copyWith(inherit: true));
|
||
|
||
Color _dividerColor(BuildContext context) {
|
||
final theme = Theme.of(context);
|
||
return theme.dividerTheme.color ?? theme.dividerColor;
|
||
}
|
||
|
||
Color _codeFill(BuildContext context) =>
|
||
Theme.of(context).colorScheme.surfaceContainerHighest;
|
||
|
||
Color _headerFill(BuildContext context) =>
|
||
Theme.of(context).colorScheme.surfaceContainerHigh;
|
||
}
|