Files
pipelines/app/lib/features/chat/chat_screen_stack_groups.dart
T
alan efaa0d9fca Chat stack: groups of open chats, synced with the open-chat set
A chat dropped onto the middle of another makes a named group (asked at
once, "Group N" by default); onto a group it joins, a group dropped onto
a chat takes it in, groups merge. The top level shows each group as a
small pile with its name and count; a tap opens it in place under a
header that leads back (also the scrim, Back and Escape). In a group,
dropping a chat on the header leaves the group; a group of one
dissolves. The close button ungroups a dropped group with Undo. Rename,
Ungroup, Close all (with Undo), Move to group and Remove from group are
in the group menu and as semantic actions.

Each open-chat entry carries its group name (protocol field "group",
omitted when ungrouped; older clients ignore it), stored and pushed by
the server like the order; names are trimmed, printable, at most 80
characters. Needs a server restart to keep groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:18:44 +00:00

84 lines
2.8 KiB
Dart

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<ChatScreen>, _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<String> order,
Map<String, JsonMap> 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);
}
}