diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index 866bf905..ee4dbd06 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -20,6 +20,51 @@ mixin _ChatScreenWorkspace on State { String? _publishedWorkspaceSession; String _workspaceSignature = ''; + /// Ctrl+Tab cycles retained panes even when the sidebar or toolbar owns + /// focus. Command+Tab stays with the operating system's application switcher. + bool _cycleChatKey(KeyEvent event) { + final keyboard = HardwareKeyboard.instance; + if (event is! KeyDownEvent || + event.logicalKey != LogicalKeyboardKey.tab || + !keyboard.isControlPressed || + keyboard.isMetaPressed || + keyboard.isAltPressed || + !_workspaceVisible || + _workspace.paneCount == 0) { + return false; + } + final destination = ShellBarScope.maybeOf(context)?.registry.focus; + if (destination != null && destination != AppDestination.chat) return false; + final focusContext = FocusManager.instance.primaryFocus?.context; + final route = focusContext == null + ? ModalRoute.of(context) + : ModalRoute.of(focusContext); + if (route is PopupRoute || (route != null && !route.isCurrent)) { + return false; + } + final panes = _workspace.panes; + final active = panes.indexWhere( + (pane) => pane.id == _workspace.activePaneId, + ); + 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); + } + }); + WidgetsBinding.instance.ensureVisualUpdate(); + return true; + } + void _workspaceChanged() { if (!mounted) return; final signature = diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 12a96903..06f90257 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -8,6 +8,7 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; +import 'package:flutter/services.dart'; import '../../core/api_gateway.dart'; import 'conversation/chat_models.dart'; @@ -126,6 +127,7 @@ class _ChatScreenState extends State with _ChatScreenWorkspace { _publishedWorkspaceSession = initial; } _workspace.addListener(_workspaceChanged); + HardwareKeyboard.instance.addHandler(_cycleChatKey); // Seen from now on, so the press that chooses the first chat counts. LastPointerKind.instance; _sessions.addListener(_nextChanged); @@ -365,6 +367,7 @@ class _ChatScreenState extends State with _ChatScreenWorkspace { @override void dispose() { + HardwareKeyboard.instance.removeHandler(_cycleChatKey); _page.dispose(); _workspace.removeListener(_workspaceChanged); _workspace.dispose(); diff --git a/app/notes/control-tab-chat-panes.md b/app/notes/control-tab-chat-panes.md new file mode 100644 index 00000000..8d2745f2 --- /dev/null +++ b/app/notes/control-tab-chat-panes.md @@ -0,0 +1,25 @@ +# Ctrl+Tab chat-pane cycling + +Ctrl+Tab moves forward through the open chat panes in visual tree order; +Ctrl+Shift+Tab moves backward. Both wrap and focus the selected chat's composer +after layout. A single open pane receives input focus without replacing it. +The active Chat destination handles the chord even when its sidebar or toolbar +has focus. Empty workspaces, other destinations, popup menus/dialogs and +Alt/Meta-modified Tab are left alone. Command+Tab remains the OS app switcher. +Existing Ctrl/Command+Shift+Arrow navigation and single-pane text selection are +unchanged. Closing/cycling never sends a chat-close, interrupt or prompt request. + +The handler is registered only for the ChatScreen lifetime and removed on +dispose. It uses the existing workspace controller and composer focus requests; +no new shell registry, server API or background polling is needed. + +Checks: 16 focused ChatScreen/workspace integration tests passed through the +watchdog (peak1669MB,13s). New cases cover forward/backward wrap, sidebar focus, +single/empty workspaces, draft preservation, exact modifiers, popup and Search +isolation. Bounded analysis is clean. Root owns combined browser acceptance. + +Browser limitation: ordinary Chrome/other browsers commonly reserve physical +Ctrl+Tab for switching browser tabs before web content receives it. App handling +works when the host delivers the chord (including native Flutter); widget/CDP +injection alone does not prove a browser will surrender its reserved shortcut. +Ctrl+Shift+Arrow remains the reliable in-app web alternative. diff --git a/app/test/features/chat/chat_control_tab_test.dart b/app/test/features/chat/chat_control_tab_test.dart new file mode 100644 index 00000000..06c68556 --- /dev/null +++ b/app/test/features/chat/chat_control_tab_test.dart @@ -0,0 +1,172 @@ +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/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; + +import 'conversation/chat_thread_fixtures.dart'; + +class _Gateway extends FakeChatGateway { + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_sessions') { + return Future.value({ + 'chat_sessions': [ + for (final id in ['a', 'b', 'c']) {'id': id, 'title': 'Chat $id'}, + ], + }); + } + if (type == 'chat_shells') return Future.value({'chat_shells': []}); + return super.call(type, fields); + } +} + +Finder _input(String id) => find.descendant( + of: find.byKey(ValueKey('chat-composer-$id')), + matching: find.byType(EditableText), +); +EditableText _editable(WidgetTester tester, String id) => + tester.widget(_input(id)); +Finder _actions(String id) => find.byWidgetPredicate( + (widget) => widget is SessionActionsButton && widget.session['id'] == id, +); +ChatWorkspaceController _workspace(WidgetTester tester) => + tester.widget(find.byType(ChatWorkspace)).controller; +Future _chord( + WidgetTester tester, { + bool reverse = false, + bool alt = false, + bool meta = false, +}) async { + await tester.sendKeyDownEvent( + meta ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft, + ); + if (reverse) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); + if (alt) await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.tab); + if (alt) await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft); + if (reverse) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyUpEvent( + meta ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft, + ); + await tester.pumpAndSettle(); +} + +Future _setup(WidgetTester tester, {bool open = true}) async { + SharedPreferences.setMockInitialValues({}); + tester.view.physicalSize = const Size(1800, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final gateway = _Gateway()..history = (_) async => chatPage([]); + addTearDown(gateway.close); + await tester.pumpWidget( + MaterialApp( + home: AdaptiveShell( + destination: AppDestination.chat, + destinationBodies: { + AppDestination.chat: (_) => ChatScreen(gateway: gateway), + AppDestination.search: (_) => const Text('Search destination'), + }, + ), + ), + ); + await tester.pumpAndSettle(); + if (open) { + await tester.tap(find.byKey(const Key('chat-row-a'))); + await tester.pumpAndSettle(); + } +} + +Future _split(WidgetTester tester, String id, String action) async { + await tester.tap(_actions(id)); + await tester.pumpAndSettle(); + await tester.tap(find.text(action)); + await tester.pumpAndSettle(); +} + +void _focusSidebar(WidgetTester tester) { + final icon = find + .descendant(of: _actions('a'), matching: find.byType(Icon)) + .first; + Focus.of(tester.element(icon)).requestFocus(); +} + +void main() { + testWidgets('Control Tab wraps visual pane order and focuses each composer', ( + tester, + ) async { + await _setup(tester); + await tester.enterText(_input('a'), 'Draft A'); + await _split(tester, 'b', 'Open to the right'); + await tester.enterText(_input('b'), 'Draft B'); + await _split(tester, 'c', 'Open below'); + await tester.enterText(_input('c'), 'Draft C'); + final workspace = _workspace(tester); + for (final id in ['a', 'b', 'c', 'a']) { + await _chord(tester); + expect(workspace.activePane?.sessionId, id); + expect(_editable(tester, id).focusNode.hasPrimaryFocus, isTrue); + } + await _chord(tester, reverse: true); + expect(workspace.activePane?.sessionId, 'c'); + expect(_editable(tester, 'c').focusNode.hasPrimaryFocus, isTrue); + _focusSidebar(tester); + await tester.pumpAndSettle(); + expect(_editable(tester, 'c').focusNode.hasPrimaryFocus, isFalse); + await _chord(tester); + expect(workspace.activePane?.sessionId, 'a'); + expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); + for (final id in ['a', 'b', 'c']) { + expect( + _editable(tester, id).controller.text, + 'Draft ${id.toUpperCase()}', + ); + } + await _chord(tester, alt: true); + await _chord(tester, meta: true); + expect(workspace.activePane?.sessionId, 'a'); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('single pane focuses input; empty workspace does not consume', ( + tester, + ) async { + await _setup(tester, open: false); + await _chord(tester); + expect(find.byType(ChatWorkspace), findsNothing); + await tester.tap(find.byKey(const Key('chat-row-a'))); + await tester.pumpAndSettle(); + _focusSidebar(tester); + await tester.pumpAndSettle(); + await _chord(tester); + expect(_editable(tester, 'a').focusNode.hasPrimaryFocus, isTrue); + expect(_editable(tester, 'a').controller.text, isEmpty); + await tester.pumpWidget(const SizedBox.shrink()); + }); + + testWidgets('popup menus and other destinations retain their own keyboard', ( + tester, + ) async { + await _setup(tester); + await _split(tester, 'b', 'Open to the right'); + final workspace = _workspace(tester); + await tester.tap(_actions('a')); + await tester.pumpAndSettle(); + await _chord(tester); + expect(workspace.activePane?.sessionId, 'b'); + expect(find.text('Open to the right'), findsOneWidget); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const ValueKey('shell.destination.search'))); + await tester.pumpAndSettle(); + await _chord(tester); + expect(find.text('Search destination'), findsOneWidget); + expect(workspace.activePane?.sessionId, 'b'); + await tester.pumpWidget(const SizedBox.shrink()); + }); +}