import 'package:flutter/material.dart'; import '../../../../ui/adaptive/platform.dart'; /// The area whose hover reveals a message's actions ([ChatHoverReveal]). On a /// touch device it is the bare [child]: there is no hover and the actions /// always show. class ChatHoverRegion extends StatefulWidget { const ChatHoverRegion({super.key, required this.child}); final Widget child; @override State createState() => _ChatHoverRegionState(); } class _ChatHoverRegionState extends State { bool _hovered = false; @override Widget build(BuildContext context) { if (!prefersCompactHitTargets(context)) return widget.child; return MouseRegion( onEnter: (_) => setState(() => _hovered = true), onExit: (_) => setState(() => _hovered = false), child: _ChatHoverScope(hovered: _hovered, child: widget.child), ); } } class _ChatHoverScope extends InheritedWidget { const _ChatHoverScope({required this.hovered, required super.child}); final bool hovered; @override bool updateShouldNotify(_ChatHoverScope oldWidget) => hovered != oldWidget.hovered; } /// A message's action buttons, shown on a pointer device only while the /// enclosing [ChatHoverRegion] is hovered, one of them has focus, or [pinned] /// (a reply reading aloud). Hidden actions stay in the focus order and the /// semantics tree, so keyboard and assistive users still reach them. Touch /// devices always show them. class ChatHoverReveal extends StatefulWidget { const ChatHoverReveal({ super.key, required this.child, this.pinned = false, this.visibilityKey, }); final Widget child; final bool pinned; /// The key of the fading widget, for tests. final Key? visibilityKey; @override State createState() => _ChatHoverRevealState(); } class _ChatHoverRevealState extends State { bool _focused = false; @override Widget build(BuildContext context) { final hoverDevice = prefersCompactHitTargets(context); final hovered = context .dependOnInheritedWidgetOfExactType<_ChatHoverScope>() ?.hovered ?? false; final shown = !hoverDevice || widget.pinned || hovered || _focused; return Focus( canRequestFocus: false, skipTraversal: true, onFocusChange: (focused) => setState(() => _focused = focused), child: AnimatedOpacity( key: widget.visibilityKey, opacity: shown ? 1 : 0, duration: const Duration(milliseconds: 120), alwaysIncludeSemantics: true, child: widget.child, ), ); } } /// The compact, muted style of a message's icon actions (Copy, Read aloud): /// smaller than the text they follow. Touch devices keep a full-size tap /// target around the small icon. ButtonStyle chatMessageActionStyle(BuildContext context) { final compact = prefersCompactHitTargets(context); return IconButton.styleFrom( foregroundColor: Theme.of(context).colorScheme.onSurfaceVariant, iconSize: 18, padding: const EdgeInsets.all(8), minimumSize: const Size.square(34), tapTargetSize: compact ? MaterialTapTargetSize.shrinkWrap : MaterialTapTargetSize.padded, ); }