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 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 14:28:18 +00:00
parent 364e0cb3c9
commit 23d8658e80
8 changed files with 309 additions and 69 deletions
@@ -86,6 +86,7 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
ChatSessionsController get _sessionsForSidebar; ChatSessionsController get _sessionsForSidebar;
ChatSectionFolds get _foldsForSidebar; ChatSectionFolds get _foldsForSidebar;
void _openSidebarSession(JsonMap session); void _openSidebarSession(JsonMap session);
void _openByShortcut(JsonMap session);
void _dropSession( void _dropSession(
ChatWorkspacePane target, ChatWorkspacePane target,
@@ -315,10 +316,14 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
String? _publishedWorkspaceSession; String? _publishedWorkspaceSession;
String _workspaceSignature = ''; String _workspaceSignature = '';
/// Ctrl+Tab cycles a single tiled pane through the open chats in sidebar /// Ctrl+Tab (Ctrl+Shift+Tab backwards) opens the next (previous) chat of
/// order. The pane is replaced in place so its retained view keeps the /// the list's cycle (see [chatTabCycle]): the starred chats when any is
/// transcript and draft. With multiple panes it cycles those panes. A /// starred, otherwise every open chat, wrapping. The chat opens as a click
/// focused floating chat window handles the key itself (see [ChatWindows]). /// 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 /// Command+Tab stays with the operating system's application switcher, and
/// the terminal keeps Ctrl+Tab for its own tabs. /// the terminal keeps Ctrl+Tab for its own tabs.
bool _cycleChatKey(KeyEvent event) { bool _cycleChatKey(KeyEvent event) {
@@ -333,67 +338,21 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
!_chatOwnsKeyboard()) { !_chatOwnsKeyboard()) {
return false; return false;
} }
if (!_workspaceVisible || _workspace.paneCount == 0) return false; final current = _workspaceVisible ? _workspace.activePane : null;
if (_workspace.paneCount == 1) { if (current == null) return false;
final pane = _workspace.activePane; final floating = widget.windows?.floatingSessionIds.toSet() ?? const {};
if (pane == null) return false; final cycle = chatTabCycle(
final floating = widget.windows?.floatingSessionIds.toSet() ?? const {}; _sessionsForSidebar.sessions
final sessions = chatSessionsShown( .where((row) => !floating.contains('${row['id'] ?? ''}'))
_sessionsForSidebar.sessions .toList(),
.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 direction = keyboard.isShiftPressed ? -1 : 1; final target = chatTabTarget(
final target = cycle,
panes[((active < 0 ? 0 : active) + direction) % panes.length]; current.sessionId,
_workspace.activate(target.id); keyboard.isShiftPressed ? -1 : 1,
WidgetsBinding.instance.addPostFrameCallback((_) { );
if (!mounted || if (target == null || '${target['id'] ?? ''}'.isEmpty) return false;
!_workspaceVisible || _openByShortcut(target);
_workspace.activePaneId != target.id) {
return;
}
final destination = ShellBarScope.maybeOf(context)?.registry.focus;
if (destination == null || destination == AppDestination.chat) {
_composerFocus.request(target.sessionId);
}
});
WidgetsBinding.instance.ensureVisualUpdate(); WidgetsBinding.instance.ensureVisualUpdate();
return true; return true;
} }
@@ -22,3 +22,31 @@ JsonMap? chatSessionAfter(List<JsonMap> shown, String currentId) {
if (others < 1) return null; if (others < 1) return null;
return shown[(index + 1) % shown.length]; 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<JsonMap> chatTabCycle(List<JsonMap> 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<JsonMap> 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];
}
+1
View File
@@ -229,6 +229,7 @@ class _ChatScreenState extends State<ChatScreen>
/// Opens [session] from the keyboard: its composer takes focus once built, /// Opens [session] from the keyboard: its composer takes focus once built,
/// also when it is the chat already open. /// also when it is the chat already open.
@override
void _openByShortcut(JsonMap session) => _open(session, focusComposer: true); void _openByShortcut(JsonMap session) => _open(session, focusComposer: true);
/// Opens [session] chosen by a tap, click or Next chat: its composer takes /// Opens [session] chosen by a tap, click or Next chat: its composer takes
@@ -161,7 +161,7 @@ class KeyboardShortcutRegistry {
action: 'Cycle chats and floating chat panes', action: 'Cycle chats and floating chat panes',
keys: 'Ctrl+Tab, Ctrl+Shift+Tab', keys: 'Ctrl+Tab, Ctrl+Shift+Tab',
caveat: 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( const KeyboardShortcutEntry(
group: KeyboardShortcutGroup.chat, group: KeyboardShortcutGroup.chat,
@@ -106,7 +106,7 @@ void _focusSidebar(WidgetTester tester) {
} }
void main() { 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, tester,
) async { ) async {
await _setup(tester); await _setup(tester);
@@ -168,7 +168,8 @@ void main() {
expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); 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 _openRow(tester, 'c');
await tester.tap(_actions('a')); await tester.tap(_actions('a'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
@@ -177,10 +178,11 @@ void main() {
expect(workspace.paneCount, 2); expect(workspace.paneCount, 2);
expect(workspace.activePane?.sessionId, 'a'); expect(workspace.activePane?.sessionId, 'a');
await _controlTab(tester); await _controlTab(tester);
expect(workspace.activePane?.sessionId, 'c'); expect(workspace.activePane?.sessionId, 'b');
await _controlTab(tester); await _controlTab(tester);
expect(workspace.activePane?.sessionId, 'a'); expect(workspace.activePane?.sessionId, 'c');
expect(workspace.paneCount, 2); expect(workspace.paneCount, 2);
expect({for (final pane in workspace.panes) pane.sessionId}, {'b', 'c'});
await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpWidget(const SizedBox.shrink());
}); });
} }
@@ -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<String> rows;
final Set<String> starred;
@override
Future<JsonMap> 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<String?> 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<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
String? _active(WidgetTester tester) =>
_workspace(tester).activePane?.sessionId;
List<String> _paneSessions(WidgetTester tester) => [
for (final pane in _workspace(tester).panes) pane.sessionId,
];
Future<void> _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<void> _setup(
WidgetTester tester, {
required List<String> rows,
Set<String> 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<EditableText>(_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());
});
}
@@ -63,4 +63,37 @@ void main() {
expect(_after(const [], 'only'), isNull); expect(_after(const [], 'only'), isNull);
expect(_after(one, 'elsewhere'), 'only'); expect(_after(one, 'elsewhere'), 'only');
}); });
String? tab(List<JsonMap> 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 = <JsonMap>[
{'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);
});
} }