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. mixin _ChatScreenStack on State, _ChatScreenWorkspace { 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. @override bool _stacked(BuildContext context) => _navigation.stack && !showsListBesideDetail(context); /// 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 _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, ), ]; } /// The pile over the workspace; it draws only while linked to the chat on /// screen. @override Widget _stackOverlay() => ListenableBuilder( listenable: Listenable.merge([_workspace, _sessionsForSidebar]), builder: (context, _) => CompositedTransformFollower( link: _stackLink, showWhenUnlinked: false, child: ChatStackFab( entries: _stackEntries(), onOpen: (entry) => _openBySelection(entry.session), onAdd: _addPane, onRemove: _removeFromStack, ), ), ); void _removeFromStack(ChatStackEntry entry) { final messenger = ScaffoldMessenger.maybeOf(context); final wasActive = _workspace.activePaneId; _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), ), ), ); } void _restoreToStack(ChatStackEntry entry, String? wasActive) { if (!mounted || _workspace.holdsSession(entry.sessionId)) return; _openedSessions[entry.sessionId] = entry.session; _workspace.splitActive(entry.sessionId, ChatSplitAxis.horizontal); // 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); } } }