diff --git a/app/lib/shell/adaptive_shell.dart b/app/lib/shell/adaptive_shell.dart index d267b440..17a7eb51 100644 --- a/app/lib/shell/adaptive_shell.dart +++ b/app/lib/shell/adaptive_shell.dart @@ -1,6 +1,7 @@ import '../ui/motion/motion.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../ui/adaptive/segmented_bar.dart'; import '../ui/adaptive/floating_bar_layout.dart'; @@ -50,6 +51,7 @@ class _AdaptiveShellState extends State { @override void dispose() { + HardwareKeyboard.instance.removeHandler(_handleDestinationKeyEvent); _bar.dispose(); super.dispose(); } @@ -59,6 +61,7 @@ class _AdaptiveShellState extends State { super.initState(); _selected = widget.destination ?? AppDestination.pipelines; _visited.add(_selected); + HardwareKeyboard.instance.addHandler(_handleDestinationKeyEvent); } @override @@ -214,6 +217,25 @@ class _AdaptiveShellState extends State { /// Leaves a page destination (Settings) for the tab it belongs to. void _closePage() => _select(shellTabFor(_selected)); + /// Handles this before focused text controls can insert or traverse on + /// Ctrl+Tab. The handler is scoped to the shell's lifetime, not onboarding. + bool _handleDestinationKeyEvent(KeyEvent event) { + if (event is! KeyDownEvent || + event.logicalKey != LogicalKeyboardKey.tab || + !HardwareKeyboard.instance.isControlPressed) { + return false; + } + final current = shellTabDestinations.indexOf(shellTabFor(_selected)); + final direction = HardwareKeyboard.instance.isShiftPressed ? -1 : 1; + final next = (current + direction) % shellTabDestinations.length; + _select( + shellTabDestinations[next < 0 + ? next + shellTabDestinations.length + : next], + ); + return true; + } + List _destinationItems() => shellTabDestinations.map((destination) { final item = destinationDetails(destination); diff --git a/app/test/shell/destination_shortcuts_test.dart b/app/test/shell/destination_shortcuts_test.dart new file mode 100644 index 00000000..104a0ee9 --- /dev/null +++ b/app/test/shell/destination_shortcuts_test.dart @@ -0,0 +1,89 @@ +import 'package:flutter/cupertino.dart'; +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'; + +void main() { + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + + testWidgets('$host Ctrl+Tab changes destinations and wraps from the ends', ( + tester, + ) async { + final app = AdaptiveShell( + cupertino: cupertino, + destinationBodies: { + for (final destination in shellTabDestinations) + destination: (_) => Focus( + autofocus: destination == AppDestination.pipelines, + child: Text(destination.name), + ), + }, + ); + await tester.pumpWidget( + cupertino ? CupertinoApp(home: app) : MaterialApp(home: app), + ); + + Future press({required bool shift}) async { + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + if (shift) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.tab); + if (shift) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + } + + expect(find.text('pipelines'), findsOneWidget); + await press(shift: true); + expect(find.text('search'), findsOneWidget); + await press(shift: false); + expect(find.text('pipelines'), findsOneWidget); + }); + + testWidgets('$host Ctrl+Tab works in a text field and plain Tab traverses', ( + tester, + ) async { + final first = FocusNode(); + final second = FocusNode(); + addTearDown(first.dispose); + addTearDown(second.dispose); + final app = AdaptiveShell( + cupertino: cupertino, + destinationBodies: { + AppDestination.pipelines: (_) => Column( + children: [ + cupertino + ? CupertinoTextField(focusNode: first) + : TextField(focusNode: first), + cupertino + ? CupertinoTextField(focusNode: second) + : TextField(focusNode: second), + ], + ), + AppDestination.runs: (_) => const Text('runs'), + }, + ); + await tester.pumpWidget( + cupertino ? CupertinoApp(home: app) : MaterialApp(home: app), + ); + first.requestFocus(); + await tester.pump(); + + // Plain Tab retains normal traversal semantics and does not select a tab. + await tester.sendKeyEvent(LogicalKeyboardKey.tab); + await tester.pump(); + expect(second.hasFocus, isTrue); + + first.requestFocus(); + await tester.pump(); + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.tab); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + expect(find.text('runs'), findsOneWidget); + expect(first.hasFocus, isFalse); + }); + } +}