import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import 'package:flutter/services.dart'; import 'chat_stack_actions.dart'; import 'chat_stack_close_target.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_group_dialogs.dart'; import 'chat_stack_group_ops.dart'; import 'chat_stack_group_pile.dart'; import 'chat_stack_motion.dart'; import 'chat_stack_node.dart'; part 'chat_stack_column_groups.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. /// /// The stack's own slot turns into a close button ([ChatStackCloseTarget]); /// item 0 sits a pitch above it, nearest the thumb. The items keep the /// open-chat order (the order the server syncs), so a chat keeps its place /// from one visit to the next; the chat on screen wears a ring in its /// colour. A group shows as one item, its chats piled; a tap opens it: the /// top level fades and the group's chats rise from its pile, under a /// header that leads back. Add chat is last, at the top: the reachable /// bottom slots go to the frequent action (switching). /// /// A short hold lifts an item ([ChatStackDrag]): dragged up or down it /// reorders; dropped on the middle of another it groups them (or joins the /// group); dropped on the close button (armed as a remove target while /// dragging) a chat leaves the open chats and a group is ungrouped. In an /// open group a chat dropped on the header leaves the group. Add chat stays /// put. The same moves are semantic actions. /// /// 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.nodes, required this.depths, required this.interactive, required this.onOpen, required this.onAdd, required this.onDismiss, required this.group, this.onRemove, this.onReorder, this.groups, }); final ChatStackMotion motion; /// The collapsed stack's slot in this overlay's coordinates. final Rect anchor; /// The top level: chats and groups in the open-chat order. final List nodes; /// 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; /// The open group's name, or null on the top level; Back sets it null. final ValueNotifier group; /// Removes a chat from the open chats; null when it cannot (the only open /// chat). final ValueChanged? onRemove; /// Puts the open chats in this order (session ids); null when they cannot /// be reordered. final ValueChanged>? onReorder; /// Grouping; null when the host keeps no groups. final ChatStackGroupActions? groups; static const removeLabel = 'Remove from open chats'; static const moveUpLabel = 'Move up'; static const moveDownLabel = 'Move down'; static const moveToGroupLabel = 'Move to group…'; static const leaveGroupLabel = 'Remove from group'; static const addLabel = ChatStackAddTail.label; @override State createState() => _ChatStackColumnState(); } class _ChatStackColumnState extends State with TickerProviderStateMixin, _ChatStackColumnGroups { @override 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; @override void initState() { super.initState(); _initGroups(); } @override void didChangeDependencies() { super.didChangeDependencies(); _drag.reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false; } @override void didUpdateWidget(covariant ChatStackColumn oldWidget) { super.didUpdateWidget(oldWidget); // Folding, or the items changing under the finger (another device), // puts the dragged item back. if (!widget.interactive || (_drag.dragging && !listEquals(_dragOrder(), _drag.order))) { _drag.cancel(); } _groupsUpdated(oldWidget); } @override void dispose() { _drag.dispose(); _disposeGroups(); super.dispose(); } void _dragChanged() { if (mounted) setState(() {}); } @override 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 _escape() { if (_drag.dragging) { _drag.cancel(); } else if (widget.group.value != null) { widget.group.value = null; } else { widget.onDismiss(); } } /// A tap on the scrim: out of an open group first, then folded. void _scrim() => widget.group.value != null ? widget.group.value = null : 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; return LayoutBuilder( builder: (context, constraints) { final size = constraints.biggest; final open = _openGroupMotion(rtl); final count = open == null || open.count < widget.motion.count ? widget.motion.count : open.count; // The stack's own slot (the close button) and the items above it. final height = count * kChatStackPitch + kChatStackSlot; final top = media.padding.top + 8; final fits = height <= anchor.bottom - top; // 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; // The close button takes the pile's place; while an item is dragged // it is the remove target. _removeCenter = anchor.center; final close = Positioned.fromRect( key: const ValueKey('chat-stack-close'), rect: anchor, child: IgnorePointer( ignoring: _drag.lifted != null, child: ChatStackCloseTarget( open: widget.motion.close, armed: _drag.removeShown, hover: _drag.hover, reduced: _drag.reduced, onClose: widget.onDismiss, ), ), ); final layers = Stack( clipBehavior: Clip.none, children: [ _topLayer(labelMax, inGroup: _openGroup != null), if (open != null) _groupLayer(open, labelMax), ], ); 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: _scrim, semanticsLabel: 'Dismiss', ), ), ), // A dragged item is drawn over the button (it shrinks into // it); otherwise the button is on top, to take its tap. if (_drag.lifted != null) close, PositionedDirectional( key: const ValueKey('chat-stack-column'), 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: layers, ), ), ), ), ), if (_drag.lifted == null) close, ], ), ), ), ); }, ); } /// The top level: chats, groups and Add chat. Behind an open group it /// fades out and takes no input. Widget _topLayer(double labelMax, {required bool inGroup}) { final nodes = widget.nodes; final depths = widget.depths; final order = [for (var i = 0; i <= nodes.length; i++) i] ..sort((a, b) => (depths[b] ?? 99).compareTo(depths[a] ?? 99)); _liftToTop(order, nodes); final items = ChatStackColumnItems( key: const ValueKey('chat-stack-top'), motion: widget.motion, nodes: nodes, tail: const ChatStackAddTail(), order: order, labelMax: labelMax, drag: _drag, draggable: _draggable && !inGroup, onOpen: _openNode, onTail: widget.onAdd, onDragStart: _startTop, onDragMove: _move, onDragEnd: _endTop, actionsFor: _topActions, ); return IgnorePointer( ignoring: inGroup, child: ExcludeSemantics( excluding: inGroup, child: FadeTransition(opacity: _topOpacity, child: items), ), ); } /// Paints a lifted item above the others. void _liftToTop(List order, List nodes) { final lifted = nodes.indexWhere((node) => node.key == _drag.lifted); if (lifted >= 0) order.add(order.removeAt(order.indexOf(lifted))); } }