Add hover copy actions for fenced and inline chat code
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
|
||||
import 'chat_transcript_selection.dart';
|
||||
import 'chat_code_hover.dart';
|
||||
import '../../../../ui/text/monospace.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
@@ -42,12 +43,16 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
onChanged: () => setState(() => _rendered = null),
|
||||
);
|
||||
Widget? _rendered;
|
||||
final _codeHover = ChatCodeHover();
|
||||
ScrollPosition? _scroll;
|
||||
ModalRoute<dynamic>? _route;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ChatMarkdownView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.text != oldWidget.text) {
|
||||
_rendered = null;
|
||||
_codeHover.hide();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,10 +62,17 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
// 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();
|
||||
}
|
||||
@@ -74,20 +86,30 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = chatMarkdownCache.blocksFor(widget.text);
|
||||
return _rendered = _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], cupertino),
|
||||
),
|
||||
],
|
||||
return _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], cupertino),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -200,25 +222,22 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.labelSmall,
|
||||
);
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 4,
|
||||
children: [
|
||||
Text(markdownCodeLabel(language), style: labelStyle),
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(color: _codeFill(context, cupertino)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: ChatTranscriptHorizontalScrollView(
|
||||
child: Text(
|
||||
code,
|
||||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||||
),
|
||||
return ChatCodeCopyRegion(
|
||||
code: code,
|
||||
cupertino: cupertino,
|
||||
label: Text(markdownCodeLabel(language), style: labelStyle),
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(color: _codeFill(context, cupertino)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: ChatTranscriptHorizontalScrollView(
|
||||
child: Text(
|
||||
code,
|
||||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -252,9 +271,11 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
required bool cupertino,
|
||||
TextStyle? style,
|
||||
}) {
|
||||
return Text.rich(
|
||||
TextSpan(children: _spans(context, parseInline(text), cupertino)),
|
||||
style: style,
|
||||
return Builder(
|
||||
builder: (textContext) => Text.rich(
|
||||
TextSpan(children: _spans(textContext, parseInline(text), cupertino)),
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -266,14 +287,10 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
for (final span in spans)
|
||||
switch (span) {
|
||||
MarkdownPlain(text: final text) => TextSpan(text: text),
|
||||
MarkdownInlineCode(text: final text) => TextSpan(
|
||||
text: text,
|
||||
// The same host-theme surface and monospace family as a fenced
|
||||
// block mark the run as code; the size stays the surrounding text's.
|
||||
style: TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
backgroundColor: _codeFill(context, cupertino),
|
||||
),
|
||||
MarkdownInlineCode(text: final text) => _inlineCode(
|
||||
context,
|
||||
text,
|
||||
cupertino,
|
||||
),
|
||||
MarkdownStrong(spans: final inner) => TextSpan(
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
@@ -292,6 +309,29 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
},
|
||||
];
|
||||
|
||||
TextSpan _inlineCode(BuildContext context, String text, bool cupertino) {
|
||||
final token = text;
|
||||
return TextSpan(
|
||||
text: text,
|
||||
style: TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
backgroundColor: _codeFill(context, cupertino),
|
||||
),
|
||||
onEnter: (event) {
|
||||
if (_route?.isCurrent != false) {
|
||||
_codeHover.show(
|
||||
context,
|
||||
token,
|
||||
text,
|
||||
event.position,
|
||||
cupertino: cupertino,
|
||||
);
|
||||
}
|
||||
},
|
||||
onExit: (_) => _codeHover.scheduleHide(token),
|
||||
);
|
||||
}
|
||||
|
||||
InlineSpan _link(
|
||||
BuildContext context,
|
||||
String url,
|
||||
|
||||
Reference in New Issue
Block a user