From 23d8658e80f810c81a9fed4bce07182dd4583374 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 1 Dec 2026 14:28:18 +0000 Subject: [PATCH] Chat: Ctrl+Tab cycles the list's chats, starred only when any are starred Ctrl+Tab / Ctrl+Shift+Tab on the Chat destination now steps through the chat list's open chats in list order (wrapping) instead of the workspace panes: only the starred chats when any open chat is starred, otherwise all open chats. From a chat outside the cycle it goes to the first (Shift: last). The target opens as a list click does, focusing the pane already showing it. The terminal's claim on Ctrl+Tab and floating-window cycling are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../features/chat/chat_screen_workspace.dart | 87 ++----- app/lib/features/chat/domain/chat_cycle.dart | 28 +++ app/lib/features/chat/screen.dart | 1 + .../settings/keyboard_shortcuts_registry.dart | 2 +- .../features/chat/chat_control_tab_test.dart | 2 +- .../chat/chat_keyboard_focus_test.dart | 8 +- .../chat/chat_starred_tab_cycle_test.dart | 217 ++++++++++++++++++ .../features/chat/domain/chat_cycle_test.dart | 33 +++ 8 files changed, 309 insertions(+), 69 deletions(-) create mode 100644 app/test/features/chat/chat_starred_tab_cycle_test.dart diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index 5514c364..36fb4e97 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -86,6 +86,7 @@ mixin _ChatScreenWorkspace on State { ChatSessionsController get _sessionsForSidebar; ChatSectionFolds get _foldsForSidebar; void _openSidebarSession(JsonMap session); + void _openByShortcut(JsonMap session); void _dropSession( ChatWorkspacePane target, @@ -315,10 +316,14 @@ mixin _ChatScreenWorkspace on State { String? _publishedWorkspaceSession; String _workspaceSignature = ''; - /// Ctrl+Tab cycles a single tiled pane through the open chats in sidebar - /// order. The pane is replaced in place so its retained view keeps the - /// transcript and draft. With multiple panes it cycles those panes. A - /// focused floating chat window handles the key itself (see [ChatWindows]). + /// Ctrl+Tab (Ctrl+Shift+Tab backwards) opens the next (previous) chat of + /// the list's cycle (see [chatTabCycle]): the starred chats when any is + /// starred, otherwise every open chat, wrapping. The chat opens as a click + /// on its row would: in the active pane, or by focusing the pane that + /// already shows it, so no chat gets a second pane. Its composer takes the + /// keyboard. Closed, hidden and floating chats are left out. It is handled + /// before the focus tree, so it works from the composer too. A focused + /// floating chat window handles the key itself (see [ChatWindows]). /// Command+Tab stays with the operating system's application switcher, and /// the terminal keeps Ctrl+Tab for its own tabs. bool _cycleChatKey(KeyEvent event) { @@ -333,67 +338,21 @@ mixin _ChatScreenWorkspace on State { !_chatOwnsKeyboard()) { return false; } - if (!_workspaceVisible || _workspace.paneCount == 0) return false; - if (_workspace.paneCount == 1) { - final pane = _workspace.activePane; - if (pane == null) return false; - final floating = widget.windows?.floatingSessionIds.toSet() ?? const {}; - final sessions = chatSessionsShown( - _sessionsForSidebar.sessions - .where( - (row) => - row['closed_at'] == null && - row['closed'] != true && - !floating.contains('${row['id'] ?? ''}'), - ) - .toList(), - query: '', - // Cycling is independent of a collapsed section: the sidebar's - // ordering is still used, but every open-chat row remains reachable. - isOpen: (_) => true, - ); - final active = sessions.indexWhere( - (row) => '${row['id'] ?? ''}' == pane.sessionId, - ); - if (sessions.length < 2 || active < 0) return false; - final direction = keyboard.isShiftPressed ? -1 : 1; - final target = sessions[(active + direction) % sessions.length]; - final targetId = '${target['id'] ?? ''}'; - if (targetId.isEmpty) return false; - _workspace.replacePaneSession(pane.id, targetId); - WidgetsBinding.instance.addPostFrameCallback((_) { - if (!mounted || - !_workspaceVisible || - _workspace.activePane?.sessionId != targetId) { - return; - } - final destination = ShellBarScope.maybeOf(context)?.registry.focus; - if (destination == null || destination == AppDestination.chat) { - _composerFocus.request(targetId); - } - }); - WidgetsBinding.instance.ensureVisualUpdate(); - return true; - } - final panes = _workspace.panes; - final active = panes.indexWhere( - (pane) => pane.id == _workspace.activePaneId, + final current = _workspaceVisible ? _workspace.activePane : null; + if (current == null) return false; + final floating = widget.windows?.floatingSessionIds.toSet() ?? const {}; + final cycle = chatTabCycle( + _sessionsForSidebar.sessions + .where((row) => !floating.contains('${row['id'] ?? ''}')) + .toList(), ); - final direction = keyboard.isShiftPressed ? -1 : 1; - final target = - panes[((active < 0 ? 0 : active) + direction) % panes.length]; - _workspace.activate(target.id); - WidgetsBinding.instance.addPostFrameCallback((_) { - if (!mounted || - !_workspaceVisible || - _workspace.activePaneId != target.id) { - return; - } - final destination = ShellBarScope.maybeOf(context)?.registry.focus; - if (destination == null || destination == AppDestination.chat) { - _composerFocus.request(target.sessionId); - } - }); + final target = chatTabTarget( + cycle, + current.sessionId, + keyboard.isShiftPressed ? -1 : 1, + ); + if (target == null || '${target['id'] ?? ''}'.isEmpty) return false; + _openByShortcut(target); WidgetsBinding.instance.ensureVisualUpdate(); return true; } diff --git a/app/lib/features/chat/domain/chat_cycle.dart b/app/lib/features/chat/domain/chat_cycle.dart index 509df930..f496552c 100644 --- a/app/lib/features/chat/domain/chat_cycle.dart +++ b/app/lib/features/chat/domain/chat_cycle.dart @@ -22,3 +22,31 @@ JsonMap? chatSessionAfter(List shown, String currentId) { if (others < 1) return null; return shown[(index + 1) % shown.length]; } + +/// The chats Ctrl+Tab steps through, in the order the list draws them: the +/// starred chats when any open chat is starred, otherwise every open chat +/// (Chats by directory). Closed chats never take part, and a folded section +/// does not hide its rows from the cycle. +List chatTabCycle(List sessions) { + final open = sessions + .where((row) => row['closed_at'] == null && row['closed'] != true) + .toList(); + final sections = chatSessionSections(open); + final starred = sections.where((section) => section.title == 'Starred'); + return chatSessionsInListOrder( + starred.isNotEmpty ? starred.toList() : sections, + isOpen: (_) => true, + ); +} + +/// The chat [step] (1 forward, -1 back) from [currentId] in [cycle], +/// wrapping. A chat outside the cycle (unstarred while starred chats exist) +/// moves to the first chat, or the last going back. Null when there is no +/// other chat to move to. +JsonMap? chatTabTarget(List cycle, String currentId, int step) { + if (cycle.isEmpty) return null; + final index = cycle.indexWhere((row) => '${row['id'] ?? ''}' == currentId); + if (index < 0) return step < 0 ? cycle.last : cycle.first; + if (cycle.length < 2) return null; + return cycle[(index + step) % cycle.length]; +} diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 50d1d6dd..4619cfe7 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -229,6 +229,7 @@ class _ChatScreenState extends State /// Opens [session] from the keyboard: its composer takes focus once built, /// also when it is the chat already open. + @override void _openByShortcut(JsonMap session) => _open(session, focusComposer: true); /// Opens [session] chosen by a tap, click or Next chat: its composer takes diff --git a/app/lib/features/settings/keyboard_shortcuts_registry.dart b/app/lib/features/settings/keyboard_shortcuts_registry.dart index c9aca0a3..0fcee87f 100644 --- a/app/lib/features/settings/keyboard_shortcuts_registry.dart +++ b/app/lib/features/settings/keyboard_shortcuts_registry.dart @@ -161,7 +161,7 @@ class KeyboardShortcutRegistry { action: 'Cycle chats and floating chat panes', keys: 'Ctrl+Tab, Ctrl+Shift+Tab', caveat: - 'With one tiled pane, cycles open sidebar chats (excluding closed, hidden and floating chats); with multiple panes, cycles panes. While the terminal is shown, Ctrl+Tab moves between its tabs instead.', + 'Opens the next or previous chat in list order: only the starred chats when any are starred, otherwise all open chats (not closed, hidden or floating ones). A chat already shown in another pane is focused there. While the terminal is shown, Ctrl+Tab moves between its tabs instead.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, diff --git a/app/test/features/chat/chat_control_tab_test.dart b/app/test/features/chat/chat_control_tab_test.dart index e81c2d1e..eacaebf9 100644 --- a/app/test/features/chat/chat_control_tab_test.dart +++ b/app/test/features/chat/chat_control_tab_test.dart @@ -106,7 +106,7 @@ void _focusSidebar(WidgetTester tester) { } void main() { - testWidgets('Control Tab wraps visual pane order and focuses each composer', ( + testWidgets('Control Tab wraps the chat list, focusing panes and composers', ( tester, ) async { await _setup(tester); diff --git a/app/test/features/chat/chat_keyboard_focus_test.dart b/app/test/features/chat/chat_keyboard_focus_test.dart index 6a8a3ef0..cb7c6173 100644 --- a/app/test/features/chat/chat_keyboard_focus_test.dart +++ b/app/test/features/chat/chat_keyboard_focus_test.dart @@ -168,7 +168,8 @@ void main() { expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); } - // Two panes still cycle panes, not the chat list. + // Two panes also cycle the chat list: b opens in the active pane, and + // c, already in the other pane, takes focus there. await _openRow(tester, 'c'); await tester.tap(_actions('a')); await tester.pumpAndSettle(); @@ -177,10 +178,11 @@ void main() { expect(workspace.paneCount, 2); expect(workspace.activePane?.sessionId, 'a'); await _controlTab(tester); - expect(workspace.activePane?.sessionId, 'c'); + expect(workspace.activePane?.sessionId, 'b'); await _controlTab(tester); - expect(workspace.activePane?.sessionId, 'a'); + expect(workspace.activePane?.sessionId, 'c'); expect(workspace.paneCount, 2); + expect({for (final pane in workspace.panes) pane.sessionId}, {'b', 'c'}); await tester.pumpWidget(const SizedBox.shrink()); }); } diff --git a/app/test/features/chat/chat_starred_tab_cycle_test.dart b/app/test/features/chat/chat_starred_tab_cycle_test.dart new file mode 100644 index 00000000..1d5147dd --- /dev/null +++ b/app/test/features/chat/chat_starred_tab_cycle_test.dart @@ -0,0 +1,217 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/chat/ui/sessions/session_actions.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; +import 'package:pipelines_app/features/terminal/terminal_scope.dart'; +import 'package:pipelines_app/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; + +import 'conversation/chat_thread_fixtures.dart'; + +/// Ctrl+Tab steps through the list's chats: the starred ones when any is +/// starred, otherwise every open chat, opening each as a click would. +class _Gateway extends FakeChatGateway { + _Gateway(this.rows, this.starred); + + final List rows; + final Set starred; + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_sessions') { + return Future.value({ + 'chat_sessions': [ + for (final id in rows) + { + 'id': id, + 'title': 'Chat $id', + if (starred.contains(id)) 'starred': true, + if (id == 'closed') 'closed_at': 1, + }, + ], + }); + } + if (type == 'chat_shells') return Future.value({'chat_shells': []}); + return super.call(type, fields); + } +} + +class _ClaimingTerminal implements TerminalActions { + const _ClaimingTerminal(); + @override + void toggle() {} + @override + bool get claimsTabKeys => true; + @override + Future openAt(ApiGateway gateway, String cwd) async => null; +} + +Finder _input(String id) => find.descendant( + of: find.byKey(ValueKey('chat-composer-$id')), + matching: find.byType(EditableText), +); +ChatWorkspaceController _workspace(WidgetTester tester) => + tester.widget(find.byType(ChatWorkspace)).controller; +String? _active(WidgetTester tester) => + _workspace(tester).activePane?.sessionId; +List _paneSessions(WidgetTester tester) => [ + for (final pane in _workspace(tester).panes) pane.sessionId, +]; + +Future _tab(WidgetTester tester, {bool reverse = false}) async { + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + if (reverse) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.tab); + if (reverse) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pumpAndSettle(); +} + +Future _setup( + WidgetTester tester, { + required List rows, + Set starred = const {}, + required String open, + Size size = const Size(1800, 1000), + bool terminal = false, +}) async { + SharedPreferences.setMockInitialValues({}); + tester.view.physicalSize = size; + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final gateway = _Gateway(rows, starred)..history = (_) async => chatPage([]); + addTearDown(gateway.close); + await tester.pumpWidget( + MaterialApp( + builder: terminal + ? (context, child) => + TerminalScope(actions: const _ClaimingTerminal(), child: child!) + : null, + home: AdaptiveShell( + destination: AppDestination.chat, + destinationBodies: { + AppDestination.chat: (_) => ChatScreen(gateway: gateway), + }, + ), + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(Key('chat-row-$open'))); + await tester.pumpAndSettle(); +} + +void main() { + testWidgets('starred chats only, in list order, wrapping both ways', ( + tester, + ) async { + // Starred sorts by title: Chat b, Chat d. + await _setup( + tester, + rows: ['a', 'd', 'c', 'b', 'closed'], + starred: {'b', 'd'}, + open: 'b', + ); + await _tab(tester); + expect(_active(tester), 'd'); + expect( + tester.widget(_input('d')).focusNode.hasPrimaryFocus, + isTrue, + ); + await _tab(tester); + expect(_active(tester), 'b'); + await _tab(tester, reverse: true); + expect(_active(tester), 'd'); + await _tab(tester, reverse: true); + expect(_active(tester), 'b'); + expect(_workspace(tester).paneCount, 1); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('from an unstarred chat: first starred, or last backwards', ( + tester, + ) async { + await _setup( + tester, + rows: ['a', 'd', 'c', 'b'], + starred: {'b', 'd'}, + open: 'a', + ); + await _tab(tester); + expect(_active(tester), 'b'); + await tester.tap(find.byKey(const Key('chat-row-c'))); + await tester.pumpAndSettle(); + await _tab(tester, reverse: true); + expect(_active(tester), 'd'); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('no starred chat: every open chat, closed left out', ( + tester, + ) async { + await _setup(tester, rows: ['a', 'b', 'c', 'closed'], open: 'a'); + for (final id in ['b', 'c', 'a']) { + await _tab(tester); + expect(_active(tester), id); + } + await _tab(tester, reverse: true); + expect(_active(tester), 'c'); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('a chat shown in another pane is focused, never duplicated', ( + tester, + ) async { + await _setup(tester, rows: ['a', 'b', 'c'], open: 'a'); + await tester.tap( + find.byWidgetPredicate( + (widget) => + widget is SessionActionsButton && widget.session['id'] == 'c', + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.text('Open to the right')); + await tester.pumpAndSettle(); + expect(_paneSessions(tester), ['a', 'c']); + expect(_active(tester), 'c'); + // c wraps to a, which already has a pane: that pane takes focus. + await _tab(tester); + expect(_paneSessions(tester), ['a', 'c']); + expect(_active(tester), 'a'); + // b has no pane: it opens in the active pane, as a click would. + await _tab(tester); + expect(_paneSessions(tester), ['b', 'c']); + expect(_active(tester), 'b'); + await _tab(tester); + expect(_paneSessions(tester), ['b', 'c']); + expect(_active(tester), 'c'); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('narrow single-chat view follows the same cycle', (tester) async { + await _setup( + tester, + rows: ['a', 'b', 'c'], + starred: {'a', 'c'}, + open: 'a', + size: const Size(400, 800), + ); + await _tab(tester); + expect(_active(tester), 'c'); + expect(find.byKey(const Key('chat-row-a')).hitTestable(), findsNothing); + await _tab(tester); + expect(_active(tester), 'a'); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('a shown terminal keeps Ctrl+Tab', (tester) async { + await _setup(tester, rows: ['a', 'b', 'c'], open: 'a', terminal: true); + await _tab(tester); + await _tab(tester, reverse: true); + expect(_active(tester), 'a'); + await tester.pumpWidget(const SizedBox.shrink()); + }); +} diff --git a/app/test/features/chat/domain/chat_cycle_test.dart b/app/test/features/chat/domain/chat_cycle_test.dart index ba63dfd4..f4790e47 100644 --- a/app/test/features/chat/domain/chat_cycle_test.dart +++ b/app/test/features/chat/domain/chat_cycle_test.dart @@ -63,4 +63,37 @@ void main() { expect(_after(const [], 'only'), isNull); expect(_after(one, 'elsewhere'), 'only'); }); + + String? tab(List cycle, String id, int step) => + chatTabTarget(cycle, id, step)?['id'] as String?; + + test('tab cycle: starred only when any open chat is starred', () { + final cycle = chatTabCycle(sessions); + expect(_ids(cycle), ['a', 'z']); + expect(tab(cycle, 'a', 1), 'z'); + expect(tab(cycle, 'z', 1), 'a'); + expect(tab(cycle, 'a', -1), 'z'); + // Outside the cycle: first forwards, last backwards. + expect(tab(cycle, 'r', 1), 'a'); + expect(tab(cycle, 'r', -1), 'z'); + }); + + test('tab cycle: every open chat when none is starred', () { + final unstarred = [ + for (final row in sessions) {...row}..remove('starred'), + ]; + final cycle = chatTabCycle(unstarred); + expect(_ids(cycle), ['r', 's', 'z', 'd', 'a']); + expect(tab(cycle, 'a', 1), 'r'); + expect(tab(cycle, 'r', -1), 'a'); + // A closed starred chat does not make a starred cycle. + final closedStar = [ + {'id': 'x', 'starred': true, 'closed_at': 1}, + {'id': 'y'}, + {'id': 'w', 'closed': true}, + ]; + expect(_ids(chatTabCycle(closedStar)), ['y']); + expect(tab(chatTabCycle(closedStar), 'y', 1), isNull); + expect(tab(const [], 'y', 1), isNull); + }); }