Fix Ctrl+Tab chat window navigation

Keep Ctrl+Tab scoped to shown floating chats when windows are visible, and otherwise cycle only visible tiled panes. Remove the single-pane recent-chat swap and use stable floating-window open order so raising a window does not alter navigation.
This commit is contained in:
2026-11-12 01:59:52 +00:00
parent 2330f50268
commit c05a4da476
8 changed files with 173 additions and 125 deletions
+3 -46
View File
@@ -1,54 +1,11 @@
part of 'screen.dart';
/// How many chats the visit remembers for Ctrl+Tab over a single pane.
const chatRecentSessionLimit = 8;
/// Keys the Chat destination answers from anywhere in it, also while the
/// sidebar or a toolbar owns focus: a bare `/` puts the keyboard in the open
/// chat's composer, and Ctrl+Tab over a single pane returns to the chat shown
/// before it (and back again). Both are handled before the focus tree, like
/// the workspace's pane cycling, and leave other destinations, popups,
/// dialogs and any focused text field alone.
/// chat's composer. It is handled before the focus tree, like the workspace's
/// pane cycling, and leaves other destinations, popups, dialogs and any
/// focused text field alone.
mixin _ChatScreenKeys on State<ChatScreen>, _ChatScreenWorkspace {
void _open(JsonMap session, {bool focusComposer});
/// The chats this visit has shown in the workspace, most recent first; the
/// active one leads it. In memory only: a reopened app starts afresh.
final List<String> _recentSessions = [];
/// Moves the active pane's chat to the front. Called on every workspace
/// change, including a restored layout, so the chat opened before the
/// current one is always the second entry.
@override
void _noteRecentSession() {
final id = _workspace.activePane?.sessionId;
if (id == null || id.isEmpty || _recentSessions.firstOrNull == id) return;
_recentSessions
..remove(id)
..insert(0, id);
if (_recentSessions.length > chatRecentSessionLimit) {
_recentSessions.removeLast();
}
}
/// Drops [id]: a closed chat, or one `/new` moved to a fresh session.
void _forgetRecentSession(String id) => _recentSessions.remove(id);
/// Drops every remembered chat: ids belong to one server.
@override
void _forgetRecentSessions() => _recentSessions.clear();
/// Over a single pane, opens the chat shown before the active one with its
/// composer focused; false when this visit has shown no other chat.
@override
bool _toggleRecentChat() {
final current = _workspace.activePane?.sessionId;
final previous = _recentSessions.where((id) => id != current).firstOrNull;
if (previous == null) return false;
_open(_rowFor(previous) ?? {'id': previous}, focusComposer: true);
return true;
}
/// Whether the shown destination is Chat with no popup, dialog or other
/// route over it, so a key pressed now is meant for the open chat.
@override
@@ -5,9 +5,6 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
Map<String, JsonMap> get _openedSessions;
ChatComposerFocusRequests get _composerFocus;
bool _chatOwnsKeyboard();
bool _toggleRecentChat();
void _noteRecentSession();
void _forgetRecentSessions();
JsonMap? _rowFor(String id);
void _back();
bool get _pageOpen;
@@ -130,7 +127,6 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
_workspaceBinding = null;
if (_workspaceScope != null && scope != _workspaceScope) {
_workspace.restoreLayout(ChatWorkspaceSnapshot.fromLayout(null, null)!);
_forgetRecentSessions();
_workspaceVisible = false;
_usedWorkspace = true;
_sidebarMode = ChatSidebarMode.expanded;
@@ -152,29 +148,29 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
String? _publishedWorkspaceSession;
String _workspaceSignature = '';
/// Ctrl+Tab cycles retained panes even when the sidebar or toolbar owns
/// focus. Over a single pane it swaps in the chat shown before this one
/// instead, and back again (see [_toggleRecentChat]). Command+Tab stays
/// with the operating system's application switcher. While the app's
/// terminal is shown, Ctrl+Tab moves between its tabs instead. While a
/// floating chat window holds the keyboard, Ctrl+Tab cycles those windows
/// (see [ChatWindows]).
/// Ctrl+Tab cycles the floating chat windows while any is shown, from the
/// tiling as well (see [ChatWindows]). With none shown it cycles the
/// retained panes, even when the sidebar or toolbar owns focus, and over a
/// single pane it does nothing: it never brings up a chat that is not
/// visible in the tiling. Command+Tab stays with the operating system's
/// application switcher. While the app's terminal is shown, Ctrl+Tab moves
/// between its tabs instead. A floating window that is not a chat, or any
/// other window holding the keyboard, keeps the key.
bool _cycleChatKey(KeyEvent event) {
final keyboard = HardwareKeyboard.instance;
if (event is! KeyDownEvent ||
TerminalScope.peek(context)?.claimsTabKeys == true ||
keyboardWindow()?.entry.kind == chatWindowKind ||
keyboardWindow() != null ||
event.logicalKey != LogicalKeyboardKey.tab ||
!keyboard.isControlPressed ||
keyboard.isMetaPressed ||
keyboard.isAltPressed ||
!_workspaceVisible ||
_workspace.paneCount == 0 ||
!_chatOwnsKeyboard()) {
return false;
}
if (widget.windows?.raiseTopFromTiling() ?? false) return true;
if (!_workspaceVisible || _workspace.paneCount < 2) return false;
final panes = _workspace.panes;
if (panes.length == 1 && _toggleRecentChat()) return true;
final active = panes.indexWhere(
(pane) => pane.id == _workspace.activePaneId,
);
@@ -199,7 +195,6 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
void _workspaceChanged() {
if (!mounted) return;
_noteRecentSession();
if (_workspaceBinding?.isRestoring == true) return;
final signature =
'${_workspace.activePaneId}:${_workspace.panes.map((p) => '${p.id}=${p.sessionId}').join(',')}';
-4
View File
@@ -159,7 +159,6 @@ class _ChatScreenState extends State<ChatScreen>
_workspace.open(initial);
_workspaceVisible = true;
_publishedWorkspaceSession = initial;
_noteRecentSession();
}
_workspace.addListener(_workspaceChanged);
_bindWorkspacePersistence();
@@ -238,7 +237,6 @@ class _ChatScreenState extends State<ChatScreen>
void _openBySelection(JsonMap session) =>
_open(session, focusComposer: chatSelectionFocusesComposer(session));
@override
void _open(JsonMap session, {bool focusComposer = false}) {
final id = '${session['id'] ?? ''}';
if (id.isEmpty) return;
@@ -285,7 +283,6 @@ class _ChatScreenState extends State<ChatScreen>
/// A model switch accepted here in this visit moved with the chat.
void _followNewSession(String previousId, JsonMap session, String notice) {
_runtime.movedToSession(previousId, session, notice);
_forgetRecentSession(previousId);
_workspace.replaceSession(previousId, '${session['id'] ?? ''}');
if (_workspace.paneCount == 0) _open(session);
}
@@ -293,7 +290,6 @@ class _ChatScreenState extends State<ChatScreen>
/// `/close` was accepted: the conversation leaves, back to the list, which
/// re-reads the closed row.
void _closed(String sessionId) {
_forgetRecentSession(sessionId);
for (final pane
in _workspace.panes
.where((pane) => pane.sessionId == sessionId)
@@ -194,6 +194,7 @@ class ChatWindows {
/// session list, which nothing else reads while the Chat destination is
/// not built.
void _rowsChanged() {
_noteOpenOrder();
final ids = {for (final w in windows) w.contentId};
if (ids.isEmpty || !ready) return;
final sessions = runtime.sessions;
@@ -232,7 +233,7 @@ class ChatWindows {
}
/// Ctrl+Tab and Ctrl+Shift+Tab while a floating chat window holds the
/// keyboard: the next (previous) visible chat window, in stacking order and
/// keyboard: the next (previous) shown chat window, in open order and
/// wrapping, comes to the top. The window host then gives it the keyboard,
/// its composer. Left alone when another window or the tiling has the
/// keyboard, when the terminal claims tab keys, or with one window.
@@ -245,16 +246,47 @@ class ChatWindows {
_terminalClaimsTabs()) {
return KeyEventResult.ignored;
}
final target = chatWindowAfter(
windows.where(manager.isVisible).toList(),
scope.entry.id,
step,
);
final target = chatWindowAfter(shownInOpenOrder(), scope.entry.id, step);
if (target == null) return KeyEventResult.ignored;
manager.raise(target.id);
return KeyEventResult.handled;
}
/// The ids of the chat windows in the order they were opened; a window
/// restored from a saved layout keeps its saved place. Raising a window
/// changes the stacking order, not this one, so the Ctrl+Tab cycle is
/// stable. Kept in step on every change of the windows (see [_rowsChanged]).
final List<String> _openOrder = [];
void _noteOpenOrder() {
final ids = {for (final w in windows) w.id};
_openOrder.removeWhere((id) => !ids.contains(id));
for (final w in windows) {
if (!_openOrder.contains(w.id)) _openOrder.add(w.id);
}
}
/// The shown chat windows (not minimised, their group shown), in open order.
List<WindowEntry> shownInOpenOrder() {
_noteOpenOrder();
final shown = {
for (final w in windows)
if (manager.isVisible(w)) w.id: w,
};
return [for (final id in _openOrder) ?shown[id]];
}
/// Ctrl+Tab from the tiling or the chat list while chat windows are shown:
/// the top-most shown chat window comes to the top and its composer takes
/// the keyboard. False, doing nothing, when no chat window is shown.
bool raiseTopFromTiling() {
final top = windows.where(manager.isVisible).lastOrNull;
if (top == null) return false;
manager.raise(top.id);
kind.focusContent(top);
return true;
}
bool _terminalClaimsTabs() {
final context = FocusManager.instance.primaryFocus?.context;
return context != null &&