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, _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: 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 _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: (context) => _addPane(context, title: ChatStackColumn.addLabel), onRemove: _removeFromStack, onReorder: (entry, index) => _workspace.movePane(entry.paneId, index), ), ), ); 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); } } }