import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'chat_stack_column_items.dart'; import 'chat_stack_disc.dart'; import 'chat_stack_drag.dart'; import 'chat_stack_entry.dart'; import 'chat_stack_motion.dart'; import 'chat_stack_remove_target.dart'; /// The chat stack fanned out: a scrim over the screen and the open chats in /// a column rising from the stack's own slot, each avatar with its name in a /// small pill beside it, and Add chat at the top. /// /// Item 0 sits exactly on the collapsed stack, nearest the thumb. The chats /// keep the open-chat order (the order the server syncs), so a chat keeps /// its place in the column from one visit to the next; the chat on screen /// wears a ring in its colour. Add chat is last, at the top: the reachable /// bottom slots go to the frequent action (switching), and the rarer one /// ends the list where the operator reads "and more". /// /// A short hold lifts a chat ([ChatStackDrag]): dragged up or down it /// reorders the open chats, the others sliding aside to open its slot; /// dropped on the "×" that comes in at the bottom it leaves them. Add chat /// stays put. The same moves are semantic actions on each chat. /// /// Nothing here rebuilds per frame: every item moves through /// `ChatStackPoseTransition` and `ChatStackDragShift` and fades through /// [FadeTransition], all repaint-only; a drag rebuilds the column only as it /// starts and ends. class ChatStackColumn extends StatefulWidget { const ChatStackColumn({ super.key, required this.motion, required this.anchor, required this.entries, required this.depths, required this.interactive, required this.onOpen, required this.onAdd, required this.onDismiss, this.onRemove, this.onReorder, }); final ChatStackMotion motion; /// The collapsed stack's slot in this overlay's coordinates. final Rect anchor; final List entries; /// Each item's layer in the collapsed pile (null when it is not drawn /// there); the add item last. final List depths; /// False while folding back: taps fall through to nothing. final bool interactive; final ValueChanged onOpen; final VoidCallback onAdd; final VoidCallback onDismiss; /// Removes a chat from the open chats (dropped on the "×"); null when it /// cannot (the only open chat). final ValueChanged? onRemove; /// Moves a chat to an index of the open-chat order; null when the chats /// cannot be reordered. final void Function(ChatStackEntry entry, int index)? onReorder; static const removeLabel = 'Remove from open chats'; static const moveUpLabel = 'Move up'; static const moveDownLabel = 'Move down'; static const addLabel = 'Add chat'; @override State createState() => _ChatStackColumnState(); } class _ChatStackColumnState extends State with TickerProviderStateMixin { late final ChatStackDrag _drag = ChatStackDrag( vsync: this, pitch: kChatStackPitch, )..addListener(_dragChanged); /// The remove target's centre in this column's box, from the last layout. Offset? _removeCenter; List get _entries => widget.entries; List get _ids => [for (final entry in _entries) entry.paneId]; /// A hold lifts a chat when there is something to do with it. bool get _draggable => widget.interactive && ((widget.onReorder != null && _entries.length > 1) || widget.onRemove != null); @override void didChangeDependencies() { super.didChangeDependencies(); _drag.reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false; } @override void didUpdateWidget(covariant ChatStackColumn oldWidget) { super.didUpdateWidget(oldWidget); // Folding, or the open chats changing under the finger (another // device), puts the dragged chat back. if (!widget.interactive || (_drag.dragging && !listEquals(_ids, _drag.order))) { _drag.cancel(); } } @override void dispose() { _drag.dispose(); super.dispose(); } void _dragChanged() { if (mounted) setState(() {}); } void _start(ChatStackEntry entry) { if (_draggable) _drag.start(entry.paneId, _ids); } void _move(LongPressMoveUpdateDetails details) { if (!_drag.dragging) return; final box = context.findRenderObject() as RenderBox?; final center = _removeCenter; final over = widget.onRemove != null && box != null && center != null && (box.globalToLocal(details.globalPosition) - center).distance <= kChatStackRemoveReach; _drag.move(details.offsetFromOrigin, overRemove: over); } void _end(ChatStackEntry entry) { if (!_drag.dragging) return; switch (_drag.end()) { case ChatStackDropMove(:final index): widget.onReorder?.call(entry, index); case ChatStackDropRemove(): widget.onRemove?.call(entry); case ChatStackDropBack(): break; } } void _escape() => _drag.dragging ? _drag.cancel() : widget.onDismiss(); @override Widget build(BuildContext context) { final media = MediaQuery.of(context); final scheme = Theme.of(context).colorScheme; final dark = Theme.of(context).brightness == Brightness.dark; final rtl = Directionality.of(context) == TextDirection.rtl; final anchor = widget.anchor; final depths = widget.depths; return LayoutBuilder( builder: (context, constraints) { final size = constraints.biggest; final count = widget.motion.count; final height = (count - 1) * kChatStackPitch + kChatStackSlot; final top = media.padding.top + 8; final available = anchor.bottom - top; final fits = height <= available; // The column's own edge is the stack's; the labels take the rest of // the row back to a margin at the other side. final endInset = rtl ? anchor.left : size.width - anchor.right; const margin = 16.0; final width = size.width - endInset - margin; final labelMax = width - kChatStackSlot - kChatStackLabelGap; // Paint the pile's order while it opens: hidden layers first, the // top of the pile last; a lifted chat above them all. final order = [for (var i = 0; i < count; i++) i] ..sort((a, b) => (depths[b] ?? 99).compareTo(depths[a] ?? 99)); final lifted = _entries.indexWhere((e) => e.paneId == _drag.lifted); if (lifted >= 0) order.add(order.removeAt(order.indexOf(lifted))); // The remove target: centred in the band below the stack's slot // (over the composer), clear of the safe area. const r = kChatStackRemoveSize / 2; final floor = size.height - media.padding.bottom; final removeCenter = Offset( size.width / 2, math.max( anchor.bottom + 4, math.min((anchor.bottom + floor) / 2, floor - r - 8), ), ); _removeCenter = removeCenter; final items = ChatStackColumnItems( motion: widget.motion, entries: _entries, order: order, labelMax: labelMax, drag: _drag, draggable: _draggable, onOpen: widget.onOpen, onAdd: widget.onAdd, onRemove: widget.onRemove, onReorder: widget.onReorder, onDragStart: _start, onDragMove: _move, onDragEnd: _end, ); return CallbackShortcuts( bindings: {const SingleActivator(LogicalKeyboardKey.escape): _escape}, child: FocusScope( autofocus: true, child: IgnorePointer( ignoring: !widget.interactive, child: Stack( children: [ Positioned.fill( child: FadeTransition( opacity: widget.motion.scrim, child: ModalBarrier( color: scheme.scrim.withValues(alpha: dark ? .5 : .32), dismissible: true, onDismiss: widget.onDismiss, semanticsLabel: 'Close chats', ), ), ), // Under the column: a chat dragged onto it is drawn over it. if (widget.onRemove != null) Positioned( left: removeCenter.dx - r, top: removeCenter.dy - r, width: kChatStackRemoveSize, height: kChatStackRemoveSize, child: ChatStackRemoveTarget( shown: _drag.removeShown, hover: _drag.hover, reduced: _drag.reduced, ), ), PositionedDirectional( end: endInset, bottom: size.height - anchor.bottom, top: top, width: width, child: Align( alignment: AlignmentDirectional.bottomEnd, child: SingleChildScrollView( reverse: true, clipBehavior: fits ? Clip.none : Clip.hardEdge, child: SizedBox( width: width, height: height, child: Semantics( container: true, explicitChildNodes: true, label: 'Open chats', child: items, ), ), ), ), ), ], ), ), ), ); }, ); } }