Show selection over inline code; link URLs inside code

An opaque inline-code fill painted over the selection highlight, so a
selected code span looked unselected; it now keeps half the fill. Bare
URLs inside inline code, fenced blocks and operator messages' backticks
are now links like any other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 19:23:47 +00:00
parent fc3bb5a004
commit 54be08994b
8 changed files with 98 additions and 55 deletions
@@ -265,8 +265,14 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
padding: const EdgeInsets.all(12),
child: ChatTranscriptHorizontalScrollView(
label: 'code block',
child: Text(
code,
child: Text.rich(
TextSpan(
children: _linked(
context,
code,
(part) => TextSpan(text: part),
),
),
style: const TextStyle(fontFamily: appMonospaceFamily),
),
),
@@ -378,23 +384,60 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
MarkdownSourceCursor source,
) {
final token = text;
final highlighted = _highlightedText(context, text, source);
TextSpan piece(String part) {
final highlighted = _highlightedText(context, part, source);
return TextSpan(
text: highlighted.text,
children: highlighted.children,
onEnter: (event) {
if (_route?.isCurrent != false) {
_codeHover.show(context, token, text, event.position);
}
},
onExit: (_) => _codeHover.scheduleHide(token),
);
}
return TextSpan(
style: TextStyle(
fontFamily: appMonospaceFamily,
backgroundColor: _codeFill(context),
// A span's background is painted over the selection highlight, so an
// opaque fill hid a selected code span; a partial fill keeps code
// distinct and lets the selection show through.
backgroundColor: _codeFill(context).withValues(alpha: _inlineCodeAlpha),
),
text: highlighted.text,
children: highlighted.children,
onEnter: (event) {
if (_route?.isCurrent != false) {
_codeHover.show(context, token, text, event.position);
}
},
onExit: (_) => _codeHover.scheduleHide(token),
children: _linked(context, text, piece),
);
}
/// [text]'s bare URLs as links and the runs between them as [piece] spans,
/// so a URL quoted in code is highlighted and opens like one in prose.
List<InlineSpan> _linked(
BuildContext context,
String text,
TextSpan Function(String part) piece,
) {
final spans = <InlineSpan>[];
var at = 0;
for (final link in findTextLinks(text)) {
if (link.start > at) spans.add(piece(text.substring(at, link.start)));
spans.add(
_links.link(context, link.target, [
piece(text.substring(link.start, link.end)),
], onOpen: (uri) => _open(context, uri)),
);
at = link.end;
}
if (at < text.length || spans.isEmpty) spans.add(piece(text.substring(at)));
return spans;
}
void _open(BuildContext context, Uri uri) {
final onOpenLink = widget.onOpenLink;
if (onOpenLink != null) return onOpenLink(uri.toString());
unawaited(openChatLink(context, uri));
}
InlineSpan _link(
BuildContext context,
String url,
@@ -404,11 +447,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
context,
url,
_spans(context, spans, source),
onOpen: (uri) {
final onOpenLink = widget.onOpenLink;
if (onOpenLink != null) return onOpenLink(uri.toString());
unawaited(openChatLink(context, uri));
},
onOpen: (uri) => _open(context, uri),
);
/// A leaf's spans, recording where it renders so a spoken word in it can
@@ -473,6 +512,10 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
return theme.dividerTheme.color ?? theme.dividerColor;
}
/// How much of the code fill an inline code span keeps, so a selection
/// behind it stays visible.
static const double _inlineCodeAlpha = 0.5;
Color _codeFill(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerHighest;