part of 'screen.dart'; /// The chat stack's groups ([ChatStackGroups]): which open chats belong to /// which named group. They travel with the open-chat set (each entry's /// `group`), so they persist and sync across devices like the order. mixin _ChatScreenStackGroups on State, _ChatScreenWorkspace { ChatStackGroupActions _stackGroupActions() => ChatStackGroupActions( join: (sessionIds, group, order) { _workspace.reorderSessions(order); _stackGroups.assign(sessionIds, group); }, leave: (sessionIds, order) { _workspace.reorderSessions(order); _stackGroups.unassign(sessionIds); }, rename: _stackGroups.rename, ungroup: _ungroupStack, closeAll: _closeStackGroup, ); void _stackSnack(String message, VoidCallback undo) { final messenger = ScaffoldMessenger.maybeOf(context); if (messenger == null) return; messenger ..hideCurrentSnackBar() ..showSnackBar( SnackBar( content: Text(message), action: SnackBarAction(label: 'Undo', onPressed: undo), ), ); } /// The group goes; its chats stay open. Undo puts the open ones back. void _ungroupStack(String group) { final members = _stackGroups.membersOf(group); if (members.isEmpty) return; _stackGroups.ungroup(group); _stackSnack('Ungrouped $group', () { if (!mounted) return; _stackGroups.assign(members.where(_workspace.holdsSession), group); }); } /// Every chat in the group leaves the open chats. Undo opens them again, /// in their places and grouped, keeping the chat on screen. void _closeStackGroup(String group) { final members = _stackGroups.membersOf(group).toSet(); if (members.isEmpty) return; final order = [for (final pane in _workspace.panes) pane.sessionId]; final rows = { for (final id in members) id: _rowFor(id) ?? {'id': id}, }; final active = _workspace.activePane?.sessionId; for (final pane in _workspace.panes.toList()) { if (members.contains(pane.sessionId)) _closePane(pane.id); } _stackSnack( 'Closed ${members.length} chats in $group', () => _reopenStackGroup(group, order, rows, active), ); } void _reopenStackGroup( String group, List order, Map rows, String? active, ) { if (!mounted) return; for (final MapEntry(key: id, value: row) in rows.entries) { if (_workspace.holdsSession(id)) continue; _openedSessions[id] = row; _workspace.splitActive(id, ChatSplitAxis.horizontal); } _workspace.reorderSessions(order); _stackGroups.assign(rows.keys, group); final kept = _workspace.panes .where((pane) => pane.sessionId == active) .firstOrNull; if (kept != null) _workspace.activate(kept.id); } }