Chat: Ctrl+K / Alt+K on Chat opens a quick switcher for the open chats

On Chat the chord now toggles a centred dialog (bottom sheet on a phone)
with an autofocused filter over the open chats in list order; Ctrl+N/Down
and Ctrl+P/Up move the wrapping selection, Enter opens the chat as Ctrl+Tab
does (pane reused, composer focused), Esc or the chord closes it. From other
destinations the chord still jumps to Chat. Ctrl+N/Ctrl+P are handled so the
web page prevents their defaults (Ctrl+P no longer prints); a browser tab
keeps Ctrl+N reserved, documented in the help and notes/chat-quick-switch.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 12:27:39 +00:00
parent 26b1c00094
commit 141661e355
17 changed files with 1127 additions and 4 deletions
@@ -0,0 +1,226 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_quick_switch_dialog.dart';
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart';
import '../ui/sessions/chat_test_harness.dart' show MemoryChatSessionLocalStore;
import '../ui/sessions/fake_chat_gateway.dart';
final rows = <JsonMap>[
{'id': 'one', 'title': 'Build app', 'directory': '/home/a/app'},
{
'id': 'two',
'title': 'Deploy server',
'directory': '/home/a/server',
'backend': 'codex',
'running': true,
},
{'id': 'three', 'title': 'Notes', 'directory': '/home/a/app'},
{'id': 'gone', 'title': 'Closed build', 'closed_at': 3},
];
class _Switcher {
ModalRoute<JsonMap>? route;
final chosen = <String>[];
}
Future<_Switcher> _open(
WidgetTester tester, {
Size size = const Size(900, 800),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final gateway = FakeChatGateway()..sessionsReply = {'chat_sessions': rows};
final controller = ChatSessionsController(
ChatRepository(gateway),
localStore: MemoryChatSessionLocalStore(),
);
addTearDown(controller.dispose);
addTearDown(gateway.pushed.close);
await controller.refresh();
final switcher = _Switcher();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => Center(
child: TextButton(
onPressed: () => switcher.route = showChatQuickSwitch(
context,
sessions: controller,
onSelected: (row) => switcher.chosen.add('${row['id']}'),
),
child: const Text('Open'),
),
),
),
),
),
);
await tester.tap(find.text('Open'));
await tester.pump(const Duration(milliseconds: 400));
return switcher;
}
List<String> _shown(WidgetTester tester) => [
for (final tile in tester.widgetList<ListTile>(
find.byWidgetPredicate(
(widget) =>
widget is ListTile &&
'${widget.key}'.contains('chat-quick-switch-row-'),
),
))
(tile.key! as ValueKey<String>).value.replaceFirst(
'chat-quick-switch-row-',
'',
),
];
String? _selected(WidgetTester tester) {
for (final id in _shown(tester)) {
final tile = tester.widget<ListTile>(
find.byKey(Key('chat-quick-switch-row-$id')),
);
if (tile.selected) return id;
}
return null;
}
Future<bool> _chord(WidgetTester tester, LogicalKeyboardKey key) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
final handled = await tester.sendKeyDownEvent(key);
await tester.sendKeyUpEvent(key);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
return handled;
}
Future<void> _key(WidgetTester tester, LogicalKeyboardKey key) async {
await tester.sendKeyEvent(key);
await tester.pump();
}
void main() {
testWidgets(
'opens focused on its field with the open chats, first selected',
(tester) async {
await _open(tester);
expect(find.byType(Dialog), findsOneWidget);
final field = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('chat-quick-switch-field')),
matching: find.byType(EditableText),
),
);
expect(field.focusNode.hasPrimaryFocus, isTrue);
expect(_shown(tester), ['one', 'three', 'two']);
expect(_selected(tester), 'one');
// The list's avatar carries the harness, colour and activity badge.
expect(find.byType(ChatSessionAvatar), findsNWidgets(3));
expect(find.text('~/app'), findsNWidgets(2));
},
);
testWidgets(
'Ctrl+N, Ctrl+P, Down and Up move the selection and wrap',
(tester) async {
await _open(tester);
expect(await _chord(tester, LogicalKeyboardKey.keyN), isTrue);
expect(_selected(tester), 'three');
await _key(tester, LogicalKeyboardKey.arrowDown);
expect(_selected(tester), 'two');
await _key(tester, LogicalKeyboardKey.arrowDown);
expect(_selected(tester), 'one');
expect(await _chord(tester, LogicalKeyboardKey.keyP), isTrue);
expect(_selected(tester), 'two');
await _key(tester, LogicalKeyboardKey.arrowUp);
expect(_selected(tester), 'three');
},
variant: TargetPlatformVariant.desktop(),
);
testWidgets('typing filters, ranks and selects the first match', (
tester,
) async {
final switcher = await _open(tester);
await _key(tester, LogicalKeyboardKey.arrowDown);
await tester.enterText(
find.byKey(const Key('chat-quick-switch-field')),
'app',
);
await tester.pump();
expect(_shown(tester), ['one', 'three']);
expect(_selected(tester), 'one');
await tester.enterText(
find.byKey(const Key('chat-quick-switch-field')),
'build',
);
await tester.pump();
// The closed chat titled "Closed build" is not offered.
expect(_shown(tester), ['one']);
await tester.enterText(
find.byKey(const Key('chat-quick-switch-field')),
'zzz',
);
await tester.pump();
expect(find.byKey(const Key('chat-quick-switch-empty')), findsOneWidget);
// Ctrl+P still belongs to the switcher with nothing to select, so the
// browser does not print.
expect(await _chord(tester, LogicalKeyboardKey.keyP), isTrue);
await _key(tester, LogicalKeyboardKey.enter);
expect(switcher.chosen, isEmpty);
});
testWidgets('Enter chooses the selected chat and closes', (tester) async {
final switcher = await _open(tester);
await _key(tester, LogicalKeyboardKey.arrowDown);
await _key(tester, LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(switcher.chosen, ['three']);
expect(find.byType(Dialog), findsNothing);
});
testWidgets('a click chooses a row; Esc closes without choosing', (
tester,
) async {
final switcher = await _open(tester);
await _key(tester, LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsNothing);
expect(switcher.chosen, isEmpty);
await tester.tap(find.text('Open'));
await tester.pump(const Duration(milliseconds: 400));
await tester.tap(find.text('Deploy server'));
await tester.pumpAndSettle();
expect(switcher.chosen, ['two']);
});
testWidgets('closeChatQuickSwitch closes it from outside', (tester) async {
final switcher = await _open(tester);
closeChatQuickSwitch(switcher.route!);
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsNothing);
expect(switcher.chosen, isEmpty);
// A second close of a closed switcher does nothing.
closeChatQuickSwitch(switcher.route!);
await tester.pump();
});
testWidgets('a phone shows it as a full-width bottom sheet', (tester) async {
await _open(tester, size: const Size(400, 800));
expect(find.byType(Dialog), findsNothing);
expect(find.byType(BottomSheet), findsOneWidget);
expect(
tester.getSize(find.byKey(const Key('chat-quick-switch-row-one'))).width,
400,
);
expect(_selected(tester), 'one');
});
}
@@ -0,0 +1,84 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_quick_switch.dart';
final rows = <JsonMap>[
{'id': 'r1', 'title': 'Recent work', 'directory': '/home/a/alpha'},
{'id': 'z', 'title': 'Zeta notes', 'starred': true},
{
'id': 'r2',
'title': 'Older',
'directory': '/home/a/beta',
'backend': 'codex',
},
{'id': 'r3', 'title': 'Same folder', 'directory': '/home/a/alpha'},
{'id': 'a', 'title': 'Alpha starred', 'starred': true},
{'id': 'c', 'title': 'Closed one', 'closed_at': 5},
{'id': 'x', 'title': 'Closed flag', 'closed': true},
{
'id': 'f',
'title': 'Far away',
'_server_id': 'm2',
'_server_name': 'Studio',
},
];
List<String> ids(List<JsonMap> rows) => [
for (final row in rows) '${row['id']}',
];
void main() {
test('no query lists open chats in list order, starred first', () {
// Starred by name, then Chats by directory group in server recency.
expect(ids(chatQuickSwitchCandidates(rows)), [
'a',
'z',
'r1',
'r3',
'r2',
'f',
]);
});
test('a query narrows by title, directory, harness and machine', () {
final open = chatQuickSwitchCandidates(rows);
expect(ids(chatQuickSwitchMatches(open, 'beta')), ['r2']);
expect(ids(chatQuickSwitchMatches(open, 'CODEX')), ['r2']);
expect(ids(chatQuickSwitchMatches(open, 'studio')), ['f']);
expect(
ids(chatQuickSwitchMatches(open, 'lab', serverName: (_) => 'Lab box')),
['a', 'z', 'r1', 'r3', 'r2', 'f'],
);
expect(ids(chatQuickSwitchMatches(open, 'closed')), isEmpty);
expect(ids(chatQuickSwitchMatches(open, 'nothing here')), isEmpty);
expect(ids(chatQuickSwitchMatches(open, ' ')), ids(open));
});
test('title starts rank before word starts, then substrings', () {
final open = chatQuickSwitchCandidates(<JsonMap>[
{'id': 'mid', 'title': 'kalpha'},
{'id': 'dir', 'title': 'Other', 'directory': '/src/alpha'},
{'id': 'word', 'title': 'The alpha plan'},
{'id': 'start', 'title': 'Alpha first'},
{'id': 'inner', 'title': 'Other', 'directory': '/src/xalphax'},
]);
expect(ids(chatQuickSwitchMatches(open, 'alp')), [
'start',
'word',
'dir',
'mid',
'inner',
]);
});
test('every word must match; ranks add up', () {
final open = chatQuickSwitchCandidates(rows);
expect(ids(chatQuickSwitchMatches(open, 'same alpha')), ['r3']);
expect(ids(chatQuickSwitchMatches(open, 'alpha')), ['a', 'r1', 'r3']);
});
test('an unnamed harness is OpenCode', () {
expect(chatQuickSwitchHarness({'id': 'q'}), 'opencode');
expect(chatQuickSwitchHarness({'backend': 'claude'}), 'claude');
});
}