Add global keyboard shortcuts cheatsheet
Expose the existing shortcut help as a global Ctrl+?/Cmd+? cheatsheet while preserving Ctrl+/ search behavior. Register the current shortcuts and cover composer, terminal, layout, and close interactions with focused tests.
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/settings/keyboard_shortcuts_help.dart';
|
||||
import 'package:pipelines_app/features/settings/keyboard_shortcuts_registry.dart';
|
||||
|
||||
const _implemented = [
|
||||
'Show keyboard shortcuts',
|
||||
'Open Search',
|
||||
'Leave Search',
|
||||
'Open Chat',
|
||||
'Switch between Runs and Pipelines',
|
||||
'Focus the active chat composer',
|
||||
'Send a chat message',
|
||||
'Choose a slash command',
|
||||
'Toggle Center Stage',
|
||||
'Stop a running chat',
|
||||
'Focus the neighbouring chat pane',
|
||||
'Move the active chat pane',
|
||||
'Cycle chat panes and floating chats',
|
||||
'Resize panes with a focused divider',
|
||||
'Scroll a wide transcript block',
|
||||
'Show or hide floating chats',
|
||||
'Cycle floating windows',
|
||||
'Show or hide terminal',
|
||||
'Move between terminal tabs',
|
||||
'Copy selected terminal text',
|
||||
'Paste into the terminal',
|
||||
'Resize the terminal panel',
|
||||
'Open a chat list item',
|
||||
'Resize a list pane',
|
||||
'Previous or next artifact or changed file',
|
||||
];
|
||||
|
||||
KeyboardShortcutEntry _entry(
|
||||
KeyboardShortcutRegistry registry,
|
||||
String action,
|
||||
) => registry.entries.firstWhere((entry) => entry.action == action);
|
||||
|
||||
void main() {
|
||||
test('registry lists every implemented shortcut once, in its group', () {
|
||||
for (final platform in [
|
||||
TargetPlatform.macOS,
|
||||
TargetPlatform.windows,
|
||||
TargetPlatform.iOS,
|
||||
]) {
|
||||
final registry = KeyboardShortcutRegistry(platform: platform, web: false);
|
||||
final actions = registry.entries.map((entry) => entry.action).toList();
|
||||
for (final action in _implemented) {
|
||||
expect(
|
||||
actions.where((listed) => listed == action),
|
||||
hasLength(1),
|
||||
reason: '$action on ${platform.name}',
|
||||
);
|
||||
}
|
||||
expect(actions.toSet(), hasLength(actions.length), reason: platform.name);
|
||||
for (final group in KeyboardShortcutGroup.values) {
|
||||
expect(registry.inGroup(group), isNotEmpty, reason: group.title);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('Ctrl+? is labelled per platform and is in Navigation', () {
|
||||
final mac = KeyboardShortcutRegistry(
|
||||
platform: TargetPlatform.macOS,
|
||||
web: false,
|
||||
);
|
||||
final windows = KeyboardShortcutRegistry(
|
||||
platform: TargetPlatform.windows,
|
||||
web: false,
|
||||
);
|
||||
final entry = _entry(mac, 'Show keyboard shortcuts');
|
||||
expect(entry.group, KeyboardShortcutGroup.navigation);
|
||||
expect(entry.keys, 'Cmd+?, Ctrl+?');
|
||||
expect(entry.caveat, contains('Esc'));
|
||||
expect(_entry(windows, 'Show keyboard shortcuts').keys, 'Ctrl+?');
|
||||
});
|
||||
|
||||
test('pane and terminal chords read as their handlers accept them', () {
|
||||
KeyboardShortcutEntry at(TargetPlatform platform, String action) => _entry(
|
||||
KeyboardShortcutRegistry(platform: platform, web: false),
|
||||
action,
|
||||
);
|
||||
|
||||
expect(
|
||||
at(TargetPlatform.macOS, 'Focus the neighbouring chat pane').keys,
|
||||
'Cmd+Shift+Arrows',
|
||||
);
|
||||
expect(
|
||||
at(TargetPlatform.linux, 'Move the active chat pane').keys,
|
||||
'Ctrl+Alt+Shift+Arrows',
|
||||
);
|
||||
expect(at(TargetPlatform.macOS, 'Stop a running chat').keys, 'Esc, Esc');
|
||||
expect(
|
||||
at(TargetPlatform.macOS, 'Copy selected terminal text').keys,
|
||||
'Cmd+C',
|
||||
);
|
||||
expect(
|
||||
at(TargetPlatform.windows, 'Copy selected terminal text').keys,
|
||||
'Ctrl+C, Ctrl+Shift+C',
|
||||
);
|
||||
expect(
|
||||
at(TargetPlatform.windows, 'Cycle floating windows').keys,
|
||||
'Alt+`, Alt+Shift+`',
|
||||
);
|
||||
expect(
|
||||
at(TargetPlatform.macOS, 'Cycle floating windows').keys,
|
||||
'Option+`, Option+Shift+`',
|
||||
);
|
||||
});
|
||||
|
||||
test('browsers keep Ctrl+Tab, so the tab entries are left out', () {
|
||||
final web = KeyboardShortcutRegistry(
|
||||
platform: TargetPlatform.linux,
|
||||
web: true,
|
||||
);
|
||||
final actions = web.entries.map((entry) => entry.action);
|
||||
expect(actions, isNot(contains('Cycle chat panes and floating chats')));
|
||||
expect(actions, isNot(contains('Move between terminal tabs')));
|
||||
});
|
||||
|
||||
testWidgets('wide help splits groups into two columns', (tester) async {
|
||||
const registry = KeyboardShortcutRegistry(
|
||||
platform: TargetPlatform.windows,
|
||||
web: false,
|
||||
);
|
||||
tester.view.physicalSize = const Size(900, 1400);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
// The desktop dialog scrolls its content; mirror that here.
|
||||
body: SingleChildScrollView(
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 640,
|
||||
child: KeyboardShortcutsHelp(registry: registry),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final navigation = tester.getTopLeft(find.text('Navigation'));
|
||||
final chat = tester.getTopLeft(find.text('Chat'));
|
||||
final windows = tester.getTopLeft(find.text('Windows'));
|
||||
final lists = tester.getTopLeft(find.text('Lists'));
|
||||
expect(chat.dx, greaterThan(navigation.dx));
|
||||
expect(lists.dx, greaterThan(navigation.dx));
|
||||
expect(windows.dx, navigation.dx);
|
||||
expect(windows.dy, greaterThan(navigation.dy));
|
||||
});
|
||||
|
||||
testWidgets('narrow help stacks groups in one column', (tester) async {
|
||||
const registry = KeyboardShortcutRegistry(
|
||||
platform: TargetPlatform.iOS,
|
||||
web: false,
|
||||
);
|
||||
tester.view.physicalSize = const Size(360, 720);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(body: KeyboardShortcutsHelp(registry: registry)),
|
||||
),
|
||||
);
|
||||
|
||||
final navigation = tester.getTopLeft(find.text('Navigation'));
|
||||
final chat = tester.getTopLeft(find.text('Chat'));
|
||||
expect(chat.dx, navigation.dx);
|
||||
expect(chat.dy, greaterThan(navigation.dy));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/shell/shell_cheatsheet_shortcut.dart';
|
||||
import 'package:pipelines_app/shell/shell_list_shortcuts.dart';
|
||||
|
||||
class _Held extends HardwareKeyboard {
|
||||
_Held({
|
||||
this.control = false,
|
||||
this.meta = false,
|
||||
this.shift = false,
|
||||
this.alt = false,
|
||||
});
|
||||
|
||||
final bool control;
|
||||
final bool meta;
|
||||
final bool shift;
|
||||
final bool alt;
|
||||
|
||||
@override
|
||||
bool get isControlPressed => control;
|
||||
@override
|
||||
bool get isMetaPressed => meta;
|
||||
@override
|
||||
bool get isShiftPressed => shift;
|
||||
@override
|
||||
bool get isAltPressed => alt;
|
||||
}
|
||||
|
||||
KeyDownEvent _down(LogicalKeyboardKey key, {String? character}) => KeyDownEvent(
|
||||
physicalKey: PhysicalKeyboardKey.slash,
|
||||
logicalKey: key,
|
||||
character: character,
|
||||
timeStamp: Duration.zero,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('Ctrl+? matches the typed question mark on US and other layouts', () {
|
||||
const linux = TargetPlatform.linux;
|
||||
// US layout: Shift+/ arrives as the slash key with Shift held.
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
_down(LogicalKeyboardKey.slash, character: '?'),
|
||||
_Held(control: true, shift: true),
|
||||
platform: linux,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
// A layout whose '?' is its own logical key, with or without Shift.
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
_down(LogicalKeyboardKey.question, character: '?'),
|
||||
_Held(control: true),
|
||||
platform: linux,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
_down(LogicalKeyboardKey.question),
|
||||
_Held(control: true, shift: true),
|
||||
platform: linux,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('Ctrl+/ without Shift is Search and never the cheatsheet', () {
|
||||
final slash = _down(LogicalKeyboardKey.slash, character: '/');
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
slash,
|
||||
_Held(control: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
shellSearchShortcut(
|
||||
slash,
|
||||
_Held(control: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('Ctrl+Shift+/ does not open Search', () {
|
||||
final shiftedSlash = _down(LogicalKeyboardKey.slash, character: '?');
|
||||
expect(
|
||||
shellSearchShortcut(
|
||||
shiftedSlash,
|
||||
_Held(control: true, shift: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
shellSearchShortcut(
|
||||
shiftedSlash,
|
||||
_Held(control: true, shift: true),
|
||||
platform: TargetPlatform.macOS,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('Alt, Cmd off macOS, and key releases do not match', () {
|
||||
final question = _down(LogicalKeyboardKey.question, character: '?');
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
question,
|
||||
_Held(control: true, alt: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
question,
|
||||
_Held(meta: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
const KeyUpEvent(
|
||||
physicalKey: PhysicalKeyboardKey.slash,
|
||||
logicalKey: LogicalKeyboardKey.question,
|
||||
timeStamp: Duration.zero,
|
||||
),
|
||||
_Held(control: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
_down(LogicalKeyboardKey.keyA),
|
||||
_Held(control: true),
|
||||
platform: TargetPlatform.linux,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('macOS accepts Cmd or Ctrl, as Search does', () {
|
||||
final question = _down(LogicalKeyboardKey.question, character: '?');
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
question,
|
||||
_Held(meta: true),
|
||||
platform: TargetPlatform.macOS,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
shellCheatsheetShortcut(
|
||||
question,
|
||||
_Held(control: true),
|
||||
platform: TargetPlatform.macOS,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
expect(shellCheatsheetShortcutLabel(TargetPlatform.macOS), 'Cmd+?, Ctrl+?');
|
||||
expect(shellCheatsheetShortcutLabel(TargetPlatform.windows), 'Ctrl+?');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
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/search/search_screen.dart';
|
||||
import 'package:pipelines_app/features/search/ui/search_bar_controls.dart';
|
||||
import 'package:pipelines_app/features/settings/keyboard_shortcuts_help.dart';
|
||||
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||
import 'package:pipelines_app/shell/destination.dart';
|
||||
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
|
||||
|
||||
class _Gateway implements ApiGateway {
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {
|
||||
'search_results': {'results': [], 'truncated': false},
|
||||
};
|
||||
}
|
||||
|
||||
/// Presses [key] with [modifiers] held, as one chord.
|
||||
Future<void> _chord(
|
||||
WidgetTester tester,
|
||||
List<LogicalKeyboardKey> modifiers,
|
||||
LogicalKeyboardKey key,
|
||||
) async {
|
||||
for (final modifier in modifiers) {
|
||||
await tester.sendKeyDownEvent(modifier);
|
||||
}
|
||||
await tester.sendKeyEvent(key);
|
||||
for (final modifier in modifiers.reversed) {
|
||||
await tester.sendKeyUpEvent(modifier);
|
||||
}
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
/// Ctrl+? as a US keyboard types it: Shift with the slash key.
|
||||
Future<void> _cheatsheetChord(
|
||||
WidgetTester tester,
|
||||
LogicalKeyboardKey primary,
|
||||
) => _chord(tester, [
|
||||
primary,
|
||||
LogicalKeyboardKey.shiftLeft,
|
||||
], LogicalKeyboardKey.slash);
|
||||
|
||||
Future<void> _pumpShell(
|
||||
WidgetTester tester, {
|
||||
required Map<AppDestination, WidgetBuilder> bodies,
|
||||
AppDestination destination = AppDestination.pipelines,
|
||||
TargetPlatform? platform,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: platform == null ? null : ThemeData(platform: platform),
|
||||
home: AdaptiveShell(destination: destination, destinationBodies: bodies),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final platform in [TargetPlatform.macOS, TargetPlatform.windows]) {
|
||||
final primary = platform == TargetPlatform.macOS
|
||||
? LogicalKeyboardKey.metaLeft
|
||||
: LogicalKeyboardKey.controlLeft;
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+? opens the cheatsheet from another destination on $platform',
|
||||
(tester) async {
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.runs,
|
||||
bodies: {
|
||||
AppDestination.runs: (_) => const Text('runs body'),
|
||||
AppDestination.pipelines: (_) => const Text('pipelines body'),
|
||||
},
|
||||
);
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsNothing);
|
||||
|
||||
await _cheatsheetChord(tester, primary);
|
||||
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsOneWidget);
|
||||
expect(find.text('Keyboard shortcuts'), findsOneWidget);
|
||||
},
|
||||
variant: TargetPlatformVariant.only(platform),
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+? opens the cheatsheet from a focused composer on $platform',
|
||||
(tester) async {
|
||||
final composer = TextEditingController(text: 'draft');
|
||||
final focus = FocusNode();
|
||||
addTearDown(composer.dispose);
|
||||
addTearDown(focus.dispose);
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.chat,
|
||||
platform: platform,
|
||||
bodies: {
|
||||
AppDestination.chat: (_) =>
|
||||
TextField(controller: composer, focusNode: focus),
|
||||
},
|
||||
);
|
||||
focus.requestFocus();
|
||||
await tester.pump();
|
||||
|
||||
await _cheatsheetChord(tester, primary);
|
||||
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsOneWidget);
|
||||
expect(composer.text, 'draft');
|
||||
},
|
||||
variant: TargetPlatformVariant.only(platform),
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+/ still opens Search, not the cheatsheet, on $platform',
|
||||
(tester) async {
|
||||
final composer = TextEditingController();
|
||||
final focus = FocusNode();
|
||||
addTearDown(composer.dispose);
|
||||
addTearDown(focus.dispose);
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.chat,
|
||||
platform: platform,
|
||||
bodies: {
|
||||
AppDestination.chat: (_) =>
|
||||
TextField(controller: composer, focusNode: focus),
|
||||
AppDestination.search: (_) => SearchScreen(
|
||||
gateway: _Gateway(),
|
||||
onOpen: (_) {},
|
||||
embedded: true,
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
await _chord(tester, [primary], LogicalKeyboardKey.slash);
|
||||
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsNothing);
|
||||
final search = tester.widget<TextField>(find.byKey(searchFieldKey));
|
||||
expect(search.focusNode!.hasFocus, isTrue);
|
||||
},
|
||||
variant: TargetPlatformVariant.only(platform),
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Ctrl+Shift+/ opens the cheatsheet and not Search on $platform',
|
||||
(tester) async {
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.chat,
|
||||
platform: platform,
|
||||
bodies: {
|
||||
AppDestination.chat: (_) => const Text('chat body'),
|
||||
AppDestination.search: (_) => SearchScreen(
|
||||
gateway: _Gateway(),
|
||||
onOpen: (_) {},
|
||||
embedded: true,
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
await _cheatsheetChord(tester, primary);
|
||||
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsOneWidget);
|
||||
expect(find.byKey(searchFieldKey), findsNothing);
|
||||
},
|
||||
variant: TargetPlatformVariant.only(platform),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Ctrl+? is ignored while a terminal has the keyboard', (
|
||||
tester,
|
||||
) async {
|
||||
final seen = <LogicalKeyboardKey>[];
|
||||
final terminal = FocusNode();
|
||||
addTearDown(terminal.dispose);
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.chat,
|
||||
bodies: {
|
||||
AppDestination.chat: (_) => TypingSurface(
|
||||
child: Focus(
|
||||
focusNode: terminal,
|
||||
autofocus: true,
|
||||
onKeyEvent: (node, event) {
|
||||
if (event is KeyDownEvent) seen.add(event.logicalKey);
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
child: const SizedBox.expand(),
|
||||
),
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
await _cheatsheetChord(tester, LogicalKeyboardKey.controlLeft);
|
||||
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsNothing);
|
||||
expect(seen, contains(LogicalKeyboardKey.slash));
|
||||
});
|
||||
|
||||
testWidgets('Escape and a second Ctrl+? close the cheatsheet', (
|
||||
tester,
|
||||
) async {
|
||||
await _pumpShell(
|
||||
tester,
|
||||
destination: AppDestination.chat,
|
||||
bodies: {AppDestination.chat: (_) => const Text('chat body')},
|
||||
);
|
||||
|
||||
await _cheatsheetChord(tester, LogicalKeyboardKey.controlLeft);
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsOneWidget);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsNothing);
|
||||
|
||||
await _cheatsheetChord(tester, LogicalKeyboardKey.controlLeft);
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsOneWidget);
|
||||
await _cheatsheetChord(tester, LogicalKeyboardKey.controlLeft);
|
||||
expect(find.byType(KeyboardShortcutsHelp), findsNothing);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user