From c05a4da476f2e70f181b5403f48a36f36d13a168 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 12 Nov 2026 01:59:52 +0000 Subject: [PATCH] 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. --- app/lib/features/chat/chat_screen_keys.dart | 49 +------------ .../features/chat/chat_screen_workspace.dart | 27 +++----- app/lib/features/chat/screen.dart | 4 -- .../features/chat/windows/chat_windows.dart | 44 ++++++++++-- app/notes/chat-floating-pane.md | 68 +++++++++++++------ .../features/chat/chat_control_tab_test.dart | 9 +-- .../chat/chat_floating_pane_test.dart | 68 ++++++++++++++++--- .../chat/chat_keyboard_focus_test.dart | 29 +++----- 8 files changed, 173 insertions(+), 125 deletions(-) diff --git a/app/lib/features/chat/chat_screen_keys.dart b/app/lib/features/chat/chat_screen_keys.dart index 7a3a51ca..d19d6e75 100644 --- a/app/lib/features/chat/chat_screen_keys.dart +++ b/app/lib/features/chat/chat_screen_keys.dart @@ -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, _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 _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 diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index edd24e01..faf1f847 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -5,9 +5,6 @@ mixin _ChatScreenWorkspace on State { Map 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 { _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 { 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 { void _workspaceChanged() { if (!mounted) return; - _noteRecentSession(); if (_workspaceBinding?.isRestoring == true) return; final signature = '${_workspace.activePaneId}:${_workspace.panes.map((p) => '${p.id}=${p.sessionId}').join(',')}'; diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 3d723db4..1cb25567 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -159,7 +159,6 @@ class _ChatScreenState extends State _workspace.open(initial); _workspaceVisible = true; _publishedWorkspaceSession = initial; - _noteRecentSession(); } _workspace.addListener(_workspaceChanged); _bindWorkspacePersistence(); @@ -238,7 +237,6 @@ class _ChatScreenState extends State 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 /// 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 /// `/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) diff --git a/app/lib/features/chat/windows/chat_windows.dart b/app/lib/features/chat/windows/chat_windows.dart index c2c7fb59..683332d7 100644 --- a/app/lib/features/chat/windows/chat_windows.dart +++ b/app/lib/features/chat/windows/chat_windows.dart @@ -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 _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 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 && diff --git a/app/notes/chat-floating-pane.md b/app/notes/chat-floating-pane.md index e17dcb38..c640eba6 100644 --- a/app/notes/chat-floating-pane.md +++ b/app/notes/chat-floating-pane.md @@ -29,14 +29,25 @@ every destination and come back at app start without Chat being opened. terminal). On macOS Ctrl+Y in a terminal always goes to the shell. - **Focus**: a window that shows or comes to the top puts the keyboard in its composer; hiding the last window gives it back to what had it. -- **Cycle**: while a floating chat window's content holds the keyboard (a - composer, a Files page inside it), Ctrl+Tab brings the next visible chat - window to the top and Ctrl+Shift+Tab the previous one, wrapping round. The - order is the stacking order, bottom to top, and the window's composer takes - the keyboard. Command+Tab stays with the system. With the tiling focused, - Ctrl+Tab still cycles its panes; while the terminal claims tab keys (shown) - it switches terminal tabs. Alt+` (`windowCycleShortcut`) still cycles every - kind of window, not only chats. +- **Cycle**: Ctrl+Tab and Ctrl+Shift+Tab move between the chats the user can + see, and never bring up one that is not shown. + - **While any chat window is shown** (not minimised, its group shown), the + keys cycle those windows only, from wherever the keyboard is in Chat: a + window's composer, a Files page inside it, or the tiling (the chat list + included). From a window, the next (previous) shown window in open order + comes to the top, wrapping round. From the tiling, Ctrl+Tab takes the + top-most shown window to the top first. Either way the window's composer + takes the keyboard. Open order is the order the windows were opened (a + restored window keeps its saved place), so a raise does not change the + cycle; the stacking order does change on a raise. + - **With no chat window shown**, Ctrl+Tab cycles the tiled panes only and + the keys focus the chosen pane's composer. With one tiled pane it does + nothing. A chat shown before, or a minimised or floating chat, is never + swapped in. + - Command+Tab stays with the system. While the terminal claims tab keys + (shown) Ctrl+Tab switches terminal tabs. A keyboard held by another window + (a run, terminal) is left to that window. Alt+` (`windowCycleShortcut`) + still cycles every kind of window, not only chats. - **Popups**: sheets, dialogs, menus and tooltips opened from a floating chat are above every window. Files opens as a page inside the window, and a file opened from it stays in the window. Because the window's own title bar names @@ -68,17 +79,36 @@ every destination and come back at app start without Chat being opened. ## Checks -- Focused, 2026-11-12, `tool/memwatch/full.sh` with - `test/features/chat/chat_floating_pane_test.dart` and - `test/features/terminal/terminal_chat_tab_test.dart`: 16 passed, 0 failed. - New in the first: Ctrl+Tab and Ctrl+Shift+Tab over three floating chats - (order, wrap, composer focus, Command+Tab ignored); with the tiling focused - Ctrl+Tab moves the active pane and leaves the windows alone, and with one - window focused it does nothing; a terminal that claims tab keys keeps - Ctrl+Tab over a focused chat window. -- `tool/analyze.sh` on the changed paths: clean. -- Affected states: two or more floating chats; one floating chat; tiling - focused; a window focused with the terminal shown (claimed); Command+Tab. +- Focused, 2026-11-12, `tool/memwatch/full.sh` over + `test/features/chat/chat_floating_pane_test.dart`, + `test/features/chat/chat_keyboard_focus_test.dart`, + `test/features/chat/chat_control_tab_test.dart`, + `test/features/chat/windows/chat_windows_test.dart` and + `test/features/terminal/terminal_chat_tab_test.dart`: 38 passed, 0 failed. + Related Ctrl+Tab users, `tool/memwatch/full.sh` over + `test/shell/destination_shortcuts_test.dart`, + `test/shell/tab_history_back_test.dart`, `test/shell/search_escape_test.dart` + and `test/features/chat/shell/chat_shell_terminal_test.dart`: 26 passed, + 0 failed. +- New in the first file: Ctrl+Tab and Ctrl+Shift+Tab over three floating chats + cycle in open order, not stacking order (order, wrap, composer focus, + Command+Tab ignored). With the tiling focused Ctrl+Tab goes to the top-most + shown window and leaves the panes alone; with one shown window it does + nothing; with the window minimised the panes cycle and the window stays + hidden. With one tiled pane and a minimised chat, Ctrl+Tab does nothing and + shows nothing. A terminal that claims tab keys keeps Ctrl+Tab over a focused + chat window. +- Removed: the single-pane recent-chat swap (the chat shown before, opened + back into the pane), with its tests in `chat_keyboard_focus_test.dart`. A + single pane with no shown window now does nothing on Ctrl+Tab, so + `chat_control_tab_test.dart` no longer expects the composer to take focus + from the sidebar. +- `tool/analyze.sh` on `lib/features/chat` and the three chat test files: + clean. +- Affected states: two or more floating chats; one floating chat shown; + floating chat minimised with one tiled pane; tiling focused with windows + shown and with none; a window focused; the terminal shown (claimed); + Command+Tab. - Not covered: the terminal's own tab switching (the test app has no terminal surface), real browser or device keys, and captures (ui-review's). - Observed, not changed: in the widget harness, floating from a pane's More diff --git a/app/test/features/chat/chat_control_tab_test.dart b/app/test/features/chat/chat_control_tab_test.dart index 06c68556..2c110416 100644 --- a/app/test/features/chat/chat_control_tab_test.dart +++ b/app/test/features/chat/chat_control_tab_test.dart @@ -133,9 +133,8 @@ void main() { await tester.pumpWidget(const SizedBox.shrink()); }); - testWidgets('single pane focuses input; empty workspace does not consume', ( - tester, - ) async { + testWidgets('single pane with no shown window does nothing; empty workspace ' + 'does not consume', (tester) async { await _setup(tester, open: false); await _chord(tester); expect(find.byType(ChatWorkspace), findsNothing); @@ -144,7 +143,9 @@ void main() { _focusSidebar(tester); await tester.pumpAndSettle(); await _chord(tester); - expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); + expect(find.byType(ChatWorkspace), findsOneWidget); + expect(_workspace(tester).paneCount, 1); + expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isFalse); expect(_editable(tester, 'a').controller.text, isEmpty); await tester.pumpWidget(const SizedBox.shrink()); }); diff --git a/app/test/features/chat/chat_floating_pane_test.dart b/app/test/features/chat/chat_floating_pane_test.dart index ceb1a11e..2471b615 100644 --- a/app/test/features/chat/chat_floating_pane_test.dart +++ b/app/test/features/chat/chat_floating_pane_test.dart @@ -526,25 +526,36 @@ void main() { await tester.pumpAndSettle(); expect(app.floating, ['b', 'c', 'a']); + // The cycle follows the order the windows were opened in (b, c, a), not + // the stacking order, which each raise changes. From a (opened last) the + // next is b, the first opened. await _tab(tester); expect(app.floating, ['c', 'a', 'b']); expect(_composerFocused(tester, 'b'), isTrue); - // Past the top, the bottom window comes up again. await _tab(tester); expect(app.floating, ['a', 'b', 'c']); expect(_composerFocused(tester, 'c'), isTrue); + await _tab(tester); + expect(app.floating, ['b', 'c', 'a']); + expect(_composerFocused(tester, 'a'), isTrue); + await _tab(tester, shift: true); + expect(app.floating, ['b', 'a', 'c']); + expect(_composerFocused(tester, 'c'), isTrue); await _tab(tester, shift: true); expect(app.floating, ['a', 'c', 'b']); expect(_composerFocused(tester, 'b'), isTrue); + // Back past the first opened: the last opened (a) comes up. await _tab(tester, shift: true); - expect(app.floating, ['a', 'b', 'c']); - expect(_composerFocused(tester, 'c'), isTrue); + expect(app.floating, ['c', 'b', 'a']); + expect(_composerFocused(tester, 'a'), isTrue); expect(tester.takeException(), isNull); await app.stop(tester); }); - testWidgets('with the tiling focused Ctrl+Tab cycles its panes and leaves ' - 'the windows alone', (tester) async { + testWidgets('with the tiling focused Ctrl+Tab goes to the top-most shown ' + 'window, and cycles the panes only when no window is shown', ( + tester, + ) async { await _saved('a', 'b', 'c'); final app = _App(_Store()); await app.start(tester); @@ -552,26 +563,61 @@ void main() { expect(app.floating, ['c']); final controller = _workspace(tester); + // A window is shown: from the tiling, Ctrl+Tab takes the keyboard to it + // and the panes stay where they are. await tester.tapAt(tester.getCenter(_input('a'))); await tester.pump(); expect(controller.activePane?.sessionId, 'a'); await _tab(tester); - expect(controller.activePane?.sessionId, 'b'); - expect(_composerFocused(tester, 'b'), isTrue); + expect(controller.activePane?.sessionId, 'a'); + expect(_composerFocused(tester, 'c'), isTrue); expect(app.floating, ['c']); - // With a window focused, the panes stay where they are; a single window - // has nothing to cycle to. - await tester.tapAt(tester.getCenter(_input('c'))); - await tester.pump(); + // A single shown window has nothing to cycle to. + await _tab(tester); + expect(controller.activePane?.sessionId, 'a'); expect(_composerFocused(tester, 'c'), isTrue); + expect(app.floating, ['c']); + + // No window shown: the panes cycle and the hidden window stays hidden. + await _chord(tester); + expect(_shown('Chat c'), findsNothing); + await tester.tapAt(tester.getCenter(_input('a'))); + await tester.pump(); await _tab(tester); expect(controller.activePane?.sessionId, 'b'); + expect(_composerFocused(tester, 'b'), isTrue); + expect(_shown('Chat c'), findsNothing); expect(app.floating, ['c']); expect(tester.takeException(), isNull); await app.stop(tester); }); + testWidgets('with one tiled pane and no shown window Ctrl+Tab does nothing; ' + 'a minimised chat is not brought up', (tester) async { + await _saved('a', 'b'); + final app = _App(_Store()); + await app.start(tester); + await _menu(tester, 'b', 'Float (Alt+Y)'); + await tester.tap(find.byTooltip('Minimise')); + await tester.pumpAndSettle(); + expect(_shown('Chat b'), findsNothing); + final controller = _workspace(tester); + expect(controller.panes.map((p) => p.sessionId), ['a']); + + await tester.tapAt(tester.getCenter(_input('a'))); + await tester.pump(); + await _tab(tester); + await _tab(tester, shift: true); + expect(controller.panes.map((p) => p.sessionId), ['a']); + expect(controller.activePane?.sessionId, 'a'); + expect(_shown('Chat b'), findsNothing); + expect(app.floating, ['b']); + expect(app.opened, isEmpty); + expect(tester.takeException(), isNull); + await app.stop(tester); + }); + testWidgets('a terminal that claims tab keys keeps Ctrl+Tab over a focused ' 'floating chat', (tester) async { await _saved('a', 'b', 'c'); diff --git a/app/test/features/chat/chat_keyboard_focus_test.dart b/app/test/features/chat/chat_keyboard_focus_test.dart index ce38721e..653f42d5 100644 --- a/app/test/features/chat/chat_keyboard_focus_test.dart +++ b/app/test/features/chat/chat_keyboard_focus_test.dart @@ -150,38 +150,29 @@ void main() { await tester.pumpWidget(const SizedBox.shrink()); }); - testWidgets('Control Tab over one pane returns to the previous chat', ( - tester, - ) async { + testWidgets('Control Tab over one pane does nothing: it never brings up ' + 'another chat', (tester) async { await _setup(tester); await _openRow(tester, 'a'); final workspace = _workspace(tester); - // Nothing shown before: the chord only focuses the composer. await _controlTab(tester); expect(workspace.activePane?.sessionId, 'a'); - expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); + expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isFalse); + // Opening b replaces the lone pane; a is no longer shown anywhere. await _openRow(tester, 'b'); await tester.enterText(_input('b'), 'Draft B'); await _focusSidebar(tester); - for (final id in ['a', 'b', 'a']) { - await _controlTab(tester); + for (final shift in [false, true]) { + await _controlTab(tester, shift: shift); expect(workspace.paneCount, 1); - expect(workspace.activePane?.sessionId, id); - expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); + expect(workspace.activePane?.sessionId, 'b'); + expect(_editable(tester, 'b').focusNode.hasPrimaryFocus, isFalse); + expect(_editable(tester, 'b').controller.text, 'Draft B'); } - await _controlTab(tester, shift: true); - expect(workspace.activePane?.sessionId, 'b'); - expect(_editable(tester, 'b').controller.text, 'Draft B'); - // A third chat makes the pair c and b; a is no longer one step away. + // Two panes cycle the panes, not the history. await _openRow(tester, 'c'); - await _controlTab(tester); - expect(workspace.activePane?.sessionId, 'b'); - await _controlTab(tester); - expect(workspace.activePane?.sessionId, 'c'); - - // Two panes cycle the panes as before, not the history. await tester.tap(_actions('a')); await tester.pumpAndSettle(); await tester.tap(find.text('Open to the right'));