Add global shortcuts for opening Chat

Allow Ctrl+K and Alt+K to switch to Chat from any destination, while preserving terminal Ctrl+K behavior and focusing the active composer. Register the shortcuts in help and cover matching, navigation, terminal handling, and composer focus with regression tests.
This commit is contained in:
2026-11-12 01:08:18 +00:00
parent 4f1b45571f
commit ac5f28bb83
8 changed files with 389 additions and 1 deletions
@@ -0,0 +1,93 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import 'chat_registration_test.dart'
show replyHistory, replySessions, replySubscribe, webServer;
bool _composerFocused(WidgetTester tester, String id) => tester
.widget<EditableText>(
find.descendant(
of: find.byKey(ValueKey('chat-composer-$id')),
matching: find.byType(EditableText),
),
)
.focusNode
.hasPrimaryFocus;
Future<void> _chord(WidgetTester tester, LogicalKeyboardKey modifier) async {
await tester.sendKeyDownEvent(modifier);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(modifier);
await tester.pump();
await tester.pump();
}
void main() {
testWidgets(
'Ctrl+K and Alt+K from another destination focus the open chat composer',
(tester) async {
tester.view.physicalSize = const Size(900, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/s1')),
);
addTearDown(provider.dispose);
await tester.pumpWidget(
PipelinesApp(
androidSupported: false,
server: webServer(),
client: client,
routeInformationProvider: provider,
),
);
await tester.pump();
await replySessions(tester, channel);
await replySubscribe(tester, channel);
await replyHistory(tester, channel);
await tester.pumpAndSettle();
expect(find.byType(ChatThreadView), findsOneWidget);
expect(_composerFocused(tester, 's1'), isFalse);
// Leave Chat for another destination, then clear any focus left over.
await tester.tap(find.byKey(const ValueKey('shell.destination.runs')));
await tester.pump(const Duration(milliseconds: 600));
FocusManager.instance.primaryFocus?.unfocus();
await tester.pump();
expect(provider.value.uri.path, '/runs');
await _chord(tester, LogicalKeyboardKey.altLeft);
expect(provider.value.uri.path, '/chat/s1');
expect(find.byType(ChatThreadView), findsOneWidget);
expect(_composerFocused(tester, 's1'), isTrue);
expect(tester.takeException(), isNull);
// Ctrl+K does the same from another destination.
await tester.tap(find.byKey(const ValueKey('shell.destination.runs')));
await tester.pump(const Duration(milliseconds: 600));
FocusManager.instance.primaryFocus?.unfocus();
await tester.pump();
expect(_composerFocused(tester, 's1'), isFalse);
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(provider.value.uri.path, '/chat/s1');
expect(find.byType(ChatThreadView), findsOneWidget);
expect(_composerFocused(tester, 's1'), isTrue);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
},
);
}
@@ -23,6 +23,23 @@ void main() {
expect(search.caveat, contains('browser'));
});
test(
'Open Chat lists Option+K or Alt+K with Ctrl+K and the terminal rule',
() {
KeyboardShortcutEntry openChat(TargetPlatform platform) =>
KeyboardShortcutRegistry(
platform: platform,
web: false,
).entries.firstWhere((entry) => entry.action == 'Open Chat');
expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Ctrl+K');
final linux = openChat(TargetPlatform.linux);
expect(linux.keys, 'Alt+K, Ctrl+K');
expect(linux.caveat, contains('kill-line'));
expect(openChat(TargetPlatform.windows).keys, 'Alt+K, Ctrl+K');
},
);
test('uses the shell primary modifier rule on iOS', () {
final entries = const KeyboardShortcutRegistry(
platform: TargetPlatform.iOS,
@@ -0,0 +1,181 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/shell_chat_shortcut.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
const _k = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyK,
logicalKey: LogicalKeyboardKey.keyK,
timeStamp: Duration.zero,
);
void main() {
test('Ctrl+K and Alt+K open Chat; other modifiers and plain K do not', () {
bool chord(Set<LogicalKeyboardKey> held, [KeyEvent event = _k]) =>
shellChatShortcut(event, _FakeKeyboard(held));
expect(chord({}), isFalse);
expect(chord({LogicalKeyboardKey.controlLeft}), isTrue);
expect(chord({LogicalKeyboardKey.altLeft}), isTrue);
expect(
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}),
isFalse,
);
expect(
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}),
isFalse,
);
expect(chord({LogicalKeyboardKey.metaLeft}), isFalse);
expect(
chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}),
isFalse,
);
expect(
shellChatShortcut(
const KeyUpEvent(
physicalKey: PhysicalKeyboardKey.keyK,
logicalKey: LogicalKeyboardKey.keyK,
timeStamp: Duration.zero,
),
_FakeKeyboard({LogicalKeyboardKey.controlLeft}),
),
isFalse,
);
});
test('Option+K matches by key position though it types a symbol', () {
// On a Mac, Option+K produces ˚: the logical key is not K.
const ring = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyK,
logicalKey: LogicalKeyboardKey(0x02da),
character: '˚',
timeStamp: Duration.zero,
);
expect(
shellChatShortcut(ring, _FakeKeyboard({LogicalKeyboardKey.altLeft})),
isTrue,
);
// Without Alt the same symbol is ordinary typing.
expect(shellChatShortcut(ring, _FakeKeyboard({})), isFalse);
});
test('labels name the Option chord on Apple platforms', () {
expect(shellChatShortcutLabels(TargetPlatform.linux), ['Alt+K', 'Ctrl+K']);
expect(shellChatShortcutLabels(TargetPlatform.macOS), [
'Option+K',
'Ctrl+K',
]);
});
testWidgets('Ctrl+K and Alt+K show Chat from another destination', (
tester,
) async {
var opened = 0;
await tester.pumpWidget(
MaterialApp(
home: AdaptiveShell(
destinationBodies: {
for (final destination in shellTabDestinations)
destination: (_) => Text('${destination.name} body'),
},
onOpenChat: () => opened++,
),
),
);
expect(find.text('pipelines body'), isOnstage);
// Chat is built on its first visit only.
expect(find.text('chat body'), findsNothing);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
// The outgoing tab fades out while still mounted, so Chat being built
// and onstage is the switch.
expect(find.text('chat body'), isOnstage);
expect(opened, 1);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
expect(find.text('chat body'), isOnstage);
// Already in Chat, the chord still hands the keyboard to the composer.
expect(opened, 2);
expect(tester.takeException(), isNull);
});
testWidgets('a controlled shell reports the switch to Chat', (tester) async {
final selected = <AppDestination>[];
await tester.pumpWidget(
MaterialApp(
home: AdaptiveShell(
destination: AppDestination.runs,
onDestinationSelected: selected.add,
destinationBodies: {
for (final destination in shellTabDestinations)
destination: (_) => Text('${destination.name} body'),
},
),
),
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
expect(selected, [AppDestination.chat]);
});
testWidgets(
'terminal keeps Ctrl+K for kill-line but Alt+K still opens Chat',
(tester) async {
final terminal = FocusNode();
addTearDown(terminal.dispose);
var opened = 0;
await tester.pumpWidget(
MaterialApp(
home: AdaptiveShell(
onOpenChat: () => opened++,
destinationBodies: {
AppDestination.pipelines: (_) => TypingSurface(
child: Focus(
focusNode: terminal,
child: const Text('terminal'),
),
),
AppDestination.chat: (_) => const Text('chat body'),
},
),
),
);
terminal.requestFocus();
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(find.text('chat body'), findsNothing);
expect(find.text('terminal'), isOnstage);
expect(opened, 0);
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyK);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pump();
expect(find.text('chat body'), isOnstage);
expect(opened, 1);
expect(tester.takeException(), isNull);
},
);
}
class _FakeKeyboard extends HardwareKeyboard {
_FakeKeyboard(this.pressed);
final Set<LogicalKeyboardKey> pressed;
@override
bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key);
}