import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'chat_copy_button.dart'; /// Keeps inline code as a TextSpan: only its hover control lives in an overlay. /// No recognizers or embedded widgets intercept text selection or wrapping. class ChatCodeHover { OverlayEntry? _entry; Timer? _exit; Object? _active; bool _disposed = false; void show( BuildContext context, Object token, String text, Offset pointer, { bool? cupertino, }) { if (_disposed) return; hide(); final overlay = Overlay.maybeOf(context, rootOverlay: true); final box = overlay?.context.findRenderObject(); if (overlay == null || box is! RenderBox) return; _active = token; Rect anchor = Rect.fromCenter(center: pointer, width: 1, height: 48); RenderParagraph? paragraph; void findParagraph(RenderObject object) { if (object is RenderParagraph) { paragraph = object; return; } object.visitChildren(findParagraph); } final render = context.findRenderObject(); if (render != null) findParagraph(render); final textRender = paragraph; if (textRender != null) { final position = textRender.getPositionForOffset( textRender.globalToLocal(pointer), ); final boxes = textRender.getBoxesForSelection( TextSelection( baseOffset: position.offset, extentOffset: position.offset + 1, ), ); if (boxes.isNotEmpty) { anchor = boxes.first.toRect().shift( textRender.localToGlobal(Offset.zero), ); } } final localAnchor = anchor.shift(-box.localToGlobal(Offset.zero)); _entry = OverlayEntry( builder: (context) => Positioned.fill( child: CustomSingleChildLayout( delegate: _CopyPosition(localAnchor), child: MouseRegion( onEnter: (_) => _exit?.cancel(), onExit: (_) => scheduleHide(token), child: Material( elevation: 3, borderRadius: BorderRadius.circular(8), child: ChatCopyButton(text: text, showLabel: true), ), ), ), ), ); overlay.insert(_entry!); } /// A short grace period lets the mouse cross from text to its Copy control. void scheduleHide(Object token) { if (_active != token) return; _exit?.cancel(); _exit = Timer(const Duration(milliseconds: 180), hide); } void hide() { _exit?.cancel(); _exit = null; _entry?.remove(); _entry?.dispose(); _entry = null; _active = null; } void dispose() { _disposed = true; hide(); } } class _CopyPosition extends SingleChildLayoutDelegate { _CopyPosition(this.anchor); final Rect anchor; @override BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints.loosen(); @override Offset getPositionForChild(Size size, Size childSize) { final above = anchor.top - childSize.height - 6; final y = above >= 4 ? above : anchor.bottom + 6; return Offset( anchor.left.clamp( 4.0, (size.width - childSize.width - 4).clamp(4.0, double.infinity), ), y.clamp( 4.0, (size.height - childSize.height - 4).clamp(4.0, double.infinity), ), ); } @override bool shouldRelayout(_CopyPosition oldDelegate) => anchor != oldDelegate.anchor; } /// The copy slot always occupies the same space, so hover never shifts code. /// Keyboard focus also reveals it; a touch on the code still selects normally. class ChatCodeCopyRegion extends StatefulWidget { const ChatCodeCopyRegion({ super.key, required this.code, this.cupertino, required this.label, required this.child, }); final String code; final bool? cupertino; final Widget label, child; @override State createState() => _ChatCodeCopyRegionState(); } class _ChatCodeCopyRegionState extends State { bool _hover = false, _focus = false; @override Widget build(BuildContext context) { final visible = _hover || _focus; return MouseRegion( onEnter: (_) => setState(() => _hover = true), onExit: (_) => setState(() => _hover = false), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.min, children: [ Row( children: [ Expanded(child: widget.label), Focus( skipTraversal: true, onFocusChange: (value) => setState(() => _focus = value), child: ExcludeSemantics( excluding: !visible, child: IgnorePointer( ignoring: !visible, child: Opacity( opacity: visible ? 1 : 0, child: SelectionContainer.disabled( child: ChatCopyButton( text: widget.code, showLabel: true, ), ), ), ), ), ), ], ), const SizedBox(height: 4), widget.child, ], ), ); } }