import 'package:flutter/material.dart'; import '../../../../ui/adaptive/platform.dart'; /// The width of the strip left of the reading column that an answer row /// draws its Copy button in (`chatCopyGutter`); 0 when there is no room, and /// Copy stays in the answer's footer. class ChatCopyGutter extends InheritedWidget { const ChatCopyGutter({super.key, required this.width, required super.child}); final double width; static double of(BuildContext context) => context.dependOnInheritedWidgetOfExactType()?.width ?? 0; @override bool updateShouldNotify(ChatCopyGutter oldWidget) => width != oldWidget.width; } /// The transcript's [children] when the list starts [gutter] left of the /// reading column: every child except the transcript [rows] (which inset /// themselves) is inset past the gutter, so it keeps to the column. List chatCopyGutterInset( double gutter, List rows, List children, ) { if (gutter <= 0) return children; final own = Set.identity()..addAll(rows); return [ for (final child in children) if (own.contains(child)) child else Padding( key: child.key, padding: EdgeInsets.only(left: gutter), child: child, ), ]; } /// An answer with its Copy button in the gutter to its left, bottom-aligned /// with the answer's footer row. On a desktop the button shows while the /// pointer is over the answer or the button has focus; it stays in the /// focus order and the semantics tree when hidden. Touch platforms always /// show it. class ChatCopyGutterRow extends StatefulWidget { const ChatCopyGutterRow({ super.key, required this.width, required this.copy, required this.child, }); final double width; /// The Copy button, or null when the answer has nothing to copy yet. final Widget? copy; final Widget child; @override State createState() => _ChatCopyGutterRowState(); } class _ChatCopyGutterRowState extends State { bool _hovered = false; bool _focused = false; @override Widget build(BuildContext context) { final hover = prefersCompactHitTargets(context); final copy = widget.copy; final row = Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [ SizedBox( key: const Key('chat-copy-gutter'), width: widget.width, child: copy == null ? null : Align( alignment: Alignment.bottomRight, child: Focus( canRequestFocus: false, skipTraversal: true, onFocusChange: (focused) => setState(() => _focused = focused), child: AnimatedOpacity( key: const Key('chat-copy-gutter-visibility'), opacity: !hover || _hovered || _focused ? 1 : 0, duration: const Duration(milliseconds: 120), alwaysIncludeSemantics: true, child: copy, ), ), ), ), Flexible(child: widget.child), ], ); if (!hover || copy == null) return row; return MouseRegion( onEnter: (_) => setState(() => _hovered = true), onExit: (_) => setState(() => _hovered = false), child: row, ); } }