part of 'screen.dart'; /// The quick switcher Ctrl+K / Alt+K (Cmd+K on a Mac) shows when pressed on /// the Chat destination: the open chats to jump to, most recently used first /// with the one used before the current chat selected, filtered as the /// operator types (see [showChatQuickSwitch]). Pressing the shortcut again /// closes it. A chosen chat opens as a keyboard choice from the list does: /// its pane is reused when it is already open, and its composer takes the /// keyboard. A closed chat a query found is reopened first. mixin _ChatScreenQuickSwitch on State, _ChatScreenWorkspace { ChatRuntime get _runtime; ModalRoute? _quickSwitchRoute; void _bindQuickSwitch() { _runtime.quickSwitch.attach(_toggleQuickSwitch); unawaited(_runtime.recency.load()); } void _unbindQuickSwitch() { _runtime.quickSwitch.detach(_toggleQuickSwitch); final open = _quickSwitchRoute; _quickSwitchRoute = null; // The navigator is locked while this screen is torn down; close after. if (open != null) { WidgetsBinding.instance.addPostFrameCallback( (_) => closeChatQuickSwitch(open), ); } } @override void _touchActiveChat() { final id = _workspaceVisible ? _workspace.activePane?.sessionId : null; if (id != null) _runtime.recency.touch(id); } void _toggleQuickSwitch() { final open = _quickSwitchRoute; if (open != null && open.isActive) { _quickSwitchRoute = null; closeChatQuickSwitch(open); return; } // Another popup or dialog over the chat keeps the keyboard. if (!mounted || !_chatOwnsKeyboard()) return; final route = showChatQuickSwitch( context, sessions: _runtime.sessions, recent: () => _runtime.recency.ids, currentId: _workspaceVisible ? _workspace.activePane?.sessionId : null, serverName: _quickSwitchServerName, onSelected: (row) { if (mounted) unawaited(_openSwitched(row)); }, ); _quickSwitchRoute = route; route.popped.whenComplete(() { if (identical(_quickSwitchRoute, route)) _quickSwitchRoute = null; }); } /// Opens a chosen chat, reopening it first when it was closed. A refused /// reopen leaves it closed, with the list row showing the server's words. Future _openSwitched(JsonMap row) async { if (chatQuickSwitchIsClosed(row)) { final id = '${row['id'] ?? ''}'; await _runtime.sessions.reopen(id); if (!mounted || _runtime.sessions.errorFor(id) != null) return; row = {...row} ..remove('closed_at') ..remove('closed'); } if (mounted) _openByShortcut(row); } /// The machine a federated row is on, as the list names it. String? _quickSwitchServerName(JsonMap row) { final id = '${row['_server_id'] ?? ''}'; final named = mounted && id.isNotEmpty ? ServerContext.nameFor(context, id) : null; return named ?? row['_server_name'] as String?; } }