Add hover copy actions for fenced and inline chat code

This commit is contained in:
2026-11-07 02:52:29 +00:00
parent 9e6d55942f
commit 4340c3e72f
6 changed files with 549 additions and 45 deletions
@@ -17,10 +17,12 @@ class ChatCopyButton extends StatefulWidget {
super.key,
required this.text,
required this.cupertino,
this.showLabel = false,
});
final String text;
final bool cupertino;
final bool showLabel;
@override
State<ChatCopyButton> createState() => _ChatCopyButtonState();
@@ -30,6 +32,15 @@ class _ChatCopyButtonState extends State<ChatCopyButton> {
bool _copied = false;
Timer? _reset;
@override
void didUpdateWidget(ChatCopyButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.text != widget.text) {
_reset?.cancel();
_copied = false;
}
}
@override
void dispose() {
_reset?.cancel();
@@ -37,8 +48,9 @@ class _ChatCopyButtonState extends State<ChatCopyButton> {
}
Future<void> _copy() async {
await Clipboard.setData(ClipboardData(text: widget.text));
if (!mounted) return;
final text = widget.text;
await Clipboard.setData(ClipboardData(text: text));
if (!mounted || widget.text != text) return;
setState(() => _copied = true);
_reset?.cancel();
_reset = Timer(const Duration(seconds: 2), () {
@@ -49,6 +61,27 @@ class _ChatCopyButtonState extends State<ChatCopyButton> {
@override
Widget build(BuildContext context) {
final label = _copied ? chatCopiedLabel : chatCopyLabel;
if (widget.showLabel) {
final icon = Icon(
_copied
? (widget.cupertino ? CupertinoIcons.checkmark : Icons.check)
: (widget.cupertino
? CupertinoIcons.doc_on_doc
: Icons.copy_outlined),
);
return Semantics(
identifier: 'chat-code-copy',
child: widget.cupertino
? CupertinoButton(
onPressed: _copy,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [icon, const SizedBox(width: 6), Text(label)],
),
)
: TextButton.icon(onPressed: _copy, icon: icon, label: Text(label)),
);
}
if (widget.cupertino) {
return Semantics(
button: true,