Files
pipelines/app/lib/features/chat/chat_screen_stack.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

123 lines
4.3 KiB
Dart

part of 'screen.dart';
/// The phone's chat stack (Settings → Chat navigation → Chat stack): one
/// floating pile of the open chats above the workspace, following the spot
/// the chat on screen marks above its composer. Its chats are the
/// workspace's panes, the same open chats the server syncs; choosing one
/// opens it as a tap on its list row does, Add chat is the pane menu's Add
/// chat pane, and removing one closes its pane.
/// Whether this is a phone or tablet (Android or iOS, also a browser on one).
bool chatStackOnMobile() => switch (defaultTargetPlatform) {
TargetPlatform.android || TargetPlatform.iOS => true,
_ => false,
};
mixin _ChatScreenStack
on State<ChatScreen>, _ChatScreenWorkspace, _ChatScreenStackGroups {
final LayerLink _stackLink = LayerLink();
void _openBySelection(JsonMap session);
@override
MobileChatNavigationController get _navigation =>
MobileChatNavigationController.instance;
/// Whether this layout navigates with the stack rather than the peek: on
/// a phone-width window, and on a phone or tablet at any width (an
/// unfolded foldable keeps the stack above the active pane). A wide
/// desktop or web window keeps its panes alone.
@override
bool _stacked(BuildContext context) =>
_navigation.stack &&
(!showsListBesideDetail(context) || chatStackOnMobile());
/// Gives the active pane the stack's spot.
@override
Widget _stackScope(BuildContext context, bool active, Widget child) =>
ChatStackAnchorScope(
link: active && _stacked(context) ? _stackLink : null,
child: child,
);
List<ChatStackEntry> _stackEntries() {
final live = {
for (final row in _sessionsForSidebar.sessions) '${row['id'] ?? ''}': row,
};
final active = _workspace.activePaneId;
return [
for (final pane in _workspace.panes)
ChatStackEntry(
paneId: pane.id,
session:
live[pane.sessionId] ??
_rowFor(pane.sessionId) ??
{'id': pane.sessionId},
current: pane.id == active,
group: _stackGroups.groupOf(pane.sessionId),
),
];
}
/// The pile over the workspace; it draws only while linked to the chat on
/// screen.
@override
Widget _stackOverlay() => ListenableBuilder(
listenable: Listenable.merge([
_workspace,
_sessionsForSidebar,
_stackGroups,
]),
builder: (context, _) => CompositedTransformFollower(
link: _stackLink,
showWhenUnlinked: false,
child: ChatStackFab(
entries: _stackEntries(),
onOpen: (entry) => _openBySelection(entry.session),
onAdd: (context) => _addPane(context, title: ChatStackColumn.addLabel),
onRemove: _removeFromStack,
onReorder: _workspace.reorderSessions,
groups: _stackGroupActions(),
),
),
);
void _removeFromStack(ChatStackEntry entry) {
final messenger = ScaffoldMessenger.maybeOf(context);
final wasActive = _workspace.activePaneId;
final index = _workspace.panes.indexWhere((p) => p.id == entry.paneId);
_closePane(entry.paneId);
if (messenger == null) return;
messenger
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text('Removed ${entry.title} from open chats'),
action: SnackBarAction(
label: 'Undo',
onPressed: () => _restoreToStack(entry, wasActive, index),
),
),
);
}
/// Undo: the chat comes back at its old place in the open-chat order.
void _restoreToStack(ChatStackEntry entry, String? wasActive, int index) {
if (!mounted || _workspace.holdsSession(entry.sessionId)) return;
_openedSessions[entry.sessionId] = entry.session;
_workspace.splitActive(entry.sessionId, ChatSplitAxis.horizontal);
final restored = _workspace.panes
.where((pane) => pane.sessionId == entry.sessionId)
.firstOrNull;
if (restored != null && index >= 0) {
_workspace.movePane(restored.id, index);
}
// The chat on screen stays on screen unless it was the one removed.
if (wasActive != null && wasActive != entry.paneId) {
final kept = _workspace.panes
.where((pane) => pane.id == wasActive)
.firstOrNull;
if (kept != null) _workspace.activate(kept.id);
}
}
}