part of 'chat_stack_column.dart'; /// The open column's groups: opening one, dragging and dropping between /// items and groups, and the semantic actions that do the same. mixin _ChatStackColumnGroups on State, TickerProviderStateMixin { ChatStackDrag get _drag; void _move(LongPressMoveUpdateDetails details); /// The group being opened or open; null on the top level. String? _openGroup; /// The group drawn (also while it folds back into its pile). ChatStackGroupNode? _shownGroup; int _shownSlot = 0; late final AnimationController _groupOpen = AnimationController( vsync: this, duration: chatStackOpenDuration, reverseDuration: chatStackCloseDuration, animationBehavior: AnimationBehavior.preserve, )..addStatusListener(_groupStatus); late final Animation _topOpacity = ReverseAnimation( _groupOpen.drive(CurveTween(curve: const Interval(0, .6))), ); ChatStackMotion? _groupMotion; Object? _groupMotionKey; bool get _reduced => _drag.reduced; List get _nodes => widget.nodes; ChatStackGroupActions? get _groups => widget.groups; /// A hold lifts an item when there is something to do with it. bool get _draggable => widget.interactive && ((widget.onReorder != null && _nodes.length > 1) || widget.onRemove != null || (_groups != null && _nodes.length > 1)); void _initGroups() { widget.group.addListener(_groupChanged); // Folded and opened again: straight back into the open group. final node = switch (widget.group.value) { final name? => _groupNamed(name), null => null, }; if (node == null) { widget.group.value = null; return; } _openGroup = node.name; _shownGroup = node; _shownSlot = _nodes.indexOf(node) + 1; _groupOpen.value = 1; } void _groupsUpdated(ChatStackColumn oldWidget) { if (!identical(oldWidget.group, widget.group)) { oldWidget.group.removeListener(_groupChanged); widget.group.addListener(_groupChanged); } final open = _openGroup; if (open == null) return; final node = _groupNamed(open); if (node != null) { _shownGroup = node; } else { // The group dissolved (or left on another device): back to the top. WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted && widget.group.value == open) widget.group.value = null; }); } } void _disposeGroups() { widget.group.removeListener(_groupChanged); _groupMotion?.dispose(); _groupOpen.dispose(); } ChatStackGroupNode? _groupNamed(String name) => _nodes .whereType() .where((node) => node.name == name) .firstOrNull; void _groupChanged() { final name = widget.group.value; _drag.cancel(); if (name != null) { final node = _groupNamed(name); if (node == null) { widget.group.value = null; return; } setState(() { _openGroup = name; _shownGroup = node; _shownSlot = _nodes.indexOf(node) + 1; }); if (_reduced) { _groupOpen.value = 1; } else { _groupOpen.forward(); } } else { setState(() => _openGroup = null); if (_reduced) { _groupOpen.value = 0; } else { _groupOpen.reverse(); } } } void _groupStatus(AnimationStatus status) { if (status == AnimationStatus.dismissed && _openGroup == null && mounted) { setState(() => _shownGroup = null); } } /// The open group's motion: its chats rise from its pile to the column. ChatStackMotion? _openGroupMotion(bool rtl) { final node = _shownGroup; if (node == null) return null; final entries = node.entries; final current = entries.indexWhere((entry) => entry.current); final top = current < 0 ? 0 : current; final n = entries.length; final depths = [ for (var i = 0; i < n; i++) if ((i - top) % n case final depth when depth < chatStackGroupLayers) depth else null, null, ]; final key = Object.hashAll([_reduced, rtl, _shownSlot, ...depths]); if (_groupMotion != null && key == _groupMotionKey) return _groupMotion; _groupMotion?.dispose(); _groupMotionKey = key; return _groupMotion = ChatStackMotion( parent: _groupOpen, depths: depths, reduced: _reduced, rtl: rtl, originSlot: _shownSlot, pileScale: chatStackGroupPileScale, ); } List get _groupItems => [ for (final entry in _shownGroup?.entries ?? const []) ChatStackChatNode(entry), ]; Widget _groupLayer(ChatStackMotion motion, double labelMax) { final group = _shownGroup!; final items = _groupItems; final order = [for (var i = 0; i <= items.length; i++) i] ..sort( // The pile's deeper (smaller) layers first, its top last. (a, b) => motion.from[a].scale.compareTo(motion.from[b].scale), ); final lifted = items.indexWhere((node) => node.key == _drag.lifted); if (lifted >= 0) order.add(order.removeAt(order.indexOf(lifted))); return IgnorePointer( ignoring: _openGroup == null, child: ChatStackColumnItems( key: ValueKey('chat-stack-group-${group.name}'), motion: motion, nodes: items, tail: ChatStackHeaderTail( group, onMenu: _groups == null ? null : (at) => _menu(group.name, at), ), order: order, labelMax: labelMax, drag: _drag, draggable: widget.interactive && _openGroup != null, onOpen: (node) => widget.onOpen(node.entries.single), onTail: () => widget.group.value = null, onDragStart: _startInGroup, onDragMove: _move, onDragEnd: _endInGroup, actionsFor: _inGroupActions, tailActions: _groupActions(group.name), ), ); } /// The keys of the items a drag moves among, as it started. List _dragOrder() => _openGroup != null ? [for (final node in _groupItems) node.key, ChatStackHeaderTail.key] : [for (final node in _nodes) node.key]; void _openNode(ChatStackNode node) => switch (node) { ChatStackChatNode(:final entry) => widget.onOpen(entry), ChatStackGroupNode(:final name) => widget.group.value = name, }; void _startTop(ChatStackNode node) { if (!_draggable) return; _drag.start( node.key, _dragOrder(), canDropOnto: _groups == null ? null : (_) => true, ); } void _endTop(ChatStackNode dragged) { if (!_drag.dragging) return; final nodes = _nodes; final from = nodes.indexOf(dragged); switch (_drag.end()) { case ChatStackDropMove(:final index): widget.onReorder?.call(chatStackMoved(nodes, from, index)); case ChatStackDropOnto(:final index): _join(from, index); case ChatStackDropRemove(): switch (dragged) { case ChatStackChatNode(:final entry): widget.onRemove?.call(entry); case ChatStackGroupNode(:final name): _groups?.ungroup(name); } case ChatStackDropBack(): break; } } void _join(int from, int onto) { final groups = _groups; if (groups == null || from < 0) return; final joined = chatStackJoined(_nodes, from, onto, _nextName); groups.join(joined.join, joined.group, joined.order); // A new group asks for its name at once; the default stands if not. if (joined.created) _rename(joined.group, title: 'Name the group'); } String _nextName() { final used = { for (final node in _nodes) if (node is ChatStackGroupNode) node.name, }; for (var n = 1; ; n++) { if (!used.contains('Group $n')) return 'Group $n'; } } void _startInGroup(ChatStackNode node) { final order = _dragOrder(); _drag.start( node.key, order, canDropOnto: _groups == null ? null : (i) => i == order.length - 1, lastSlot: order.length - 2, ); } void _endInGroup(ChatStackNode dragged) { if (!_drag.dragging) return; final group = _shownGroup; if (group == null) return; final items = _groupItems; final from = items.indexWhere((node) => node.key == dragged.key); final id = dragged.entries.single.sessionId; switch (_drag.end()) { case ChatStackDropMove(:final index): _regroup(group.name, chatStackMoved(items, from, index)); case ChatStackDropOnto(): _leave(group.name, id); case ChatStackDropRemove(): widget.onRemove?.call(dragged.entries.single); case ChatStackDropBack(): break; } } void _regroup(String group, List groupOrder) => widget.onReorder?.call(chatStackRegrouped(_nodes, group, groupOrder)); void _leave(String group, String sessionId) => _groups?.leave([sessionId], chatStackLeft(_nodes, group, sessionId)); Future _rename(String group, {String title = 'Rename group'}) async { final name = await showChatStackRenameDialog( context, current: group, title: title, ); if (!mounted || name == null || name == group) return; _groups?.rename(group, name); if (widget.group.value == group) widget.group.value = name; } Future _menu(String group, Rect at) async { final action = await showChatStackGroupMenu(context, anchor: at); if (mounted && action != null) _groupAction(group, action); } void _groupAction(String group, ChatStackGroupAction action) { final groups = _groups; if (groups == null) return; switch (action) { case ChatStackGroupAction.rename: _rename(group); case ChatStackGroupAction.ungroup: widget.group.value = null; groups.ungroup(group); case ChatStackGroupAction.closeAll: widget.group.value = null; groups.closeAll(group); } } Map _groupActions(String group) => { if (_groups != null) for (final action in ChatStackGroupAction.values) CustomSemanticsAction(label: action.label): () => _groupAction(group, action), }; /// Up is toward the top of the column: a later place in the order. Map _moves( int index, int count, void Function(int to) move, ) => { if (widget.onReorder != null && index < count - 1) const CustomSemanticsAction(label: ChatStackColumn.moveUpLabel): () => move(index + 1), if (widget.onReorder != null && index > 0) const CustomSemanticsAction(label: ChatStackColumn.moveDownLabel): () => move(index - 1), }; Map _topActions( ChatStackNode node, int index, ) { final nodes = _nodes; final remove = widget.onRemove; final groupNodes = nodes.whereType().toList(); return { ..._moves( index, nodes.length, (to) => widget.onReorder?.call(chatStackMoved(nodes, index, to)), ), if (node case ChatStackChatNode(:final entry)) ...{ if (_groups != null && groupNodes.isNotEmpty) const CustomSemanticsAction( label: ChatStackColumn.moveToGroupLabel, ): () => _moveToGroup(index, groupNodes), if (remove != null) const CustomSemanticsAction(label: ChatStackColumn.removeLabel): () => remove(entry), }, if (node case ChatStackGroupNode(:final name)) ..._groupActions(name), }; } Future _moveToGroup(int index, List groups) async { final name = await showChatStackMoveToGroupDialog( context, groups: [for (final group in groups) group.name], ); if (!mounted || name == null) return; final onto = _nodes.indexWhere( (node) => node is ChatStackGroupNode && node.name == name, ); if (onto >= 0 && index < _nodes.length) _join(index, onto); } Map _inGroupActions( ChatStackNode node, int index, ) { final group = _shownGroup; if (group == null) return const {}; final items = _groupItems; final entry = node.entries.single; final remove = widget.onRemove; return { ..._moves( index, items.length, (to) => _regroup(group.name, chatStackMoved(items, index, to)), ), if (_groups != null) const CustomSemanticsAction( label: ChatStackColumn.leaveGroupLabel, ): () => _leave(group.name, entry.sessionId), if (remove != null) const CustomSemanticsAction(label: ChatStackColumn.removeLabel): () => remove(entry), }; } }