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:
@@ -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();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user