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:
@@ -13,6 +13,8 @@ import 'shell_rail_scope.dart';
|
||||
import 'list_pane_widths.dart';
|
||||
import 'shell_bar.dart';
|
||||
import 'shell_chat_shortcut.dart';
|
||||
import 'shell_cheatsheet.dart';
|
||||
import 'shell_cheatsheet_shortcut.dart';
|
||||
import 'shell_list_shortcuts.dart';
|
||||
import 'shell_runs_pipelines_shortcut.dart';
|
||||
import 'shell_search_shortcut_labels.dart';
|
||||
@@ -92,6 +94,7 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
// page of its own for the system Back to close first.
|
||||
final Map<AppDestination, bool> _nestedCanPop = {};
|
||||
final ShellBarRegistry _bar = ShellBarRegistry();
|
||||
final ShellCheatsheet _cheatsheet = ShellCheatsheet();
|
||||
late final ListPaneWidths _paneWidths = ListPaneWidths(
|
||||
store: widget.listPaneWidthStore,
|
||||
);
|
||||
@@ -390,6 +393,24 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
/// terminal their own Ctrl+A (select all, line start).
|
||||
bool _handleDestinationKeyEvent(KeyEvent event) {
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
// Ctrl+? opens the shortcuts cheatsheet from anywhere, including a focused
|
||||
// composer and floating windows; the terminal keeps the chord while it has
|
||||
// the keyboard. Escape closes the cheatsheet while it is open.
|
||||
if (shellCheatsheetShortcut(event, keyboard) &&
|
||||
!primaryFocusIsTypingSurface()) {
|
||||
if (event is KeyDownEvent) _cheatsheet.toggle(context);
|
||||
return true;
|
||||
}
|
||||
if (_cheatsheet.isOpen &&
|
||||
event is KeyDownEvent &&
|
||||
event.logicalKey == LogicalKeyboardKey.escape &&
|
||||
!keyboard.isControlPressed &&
|
||||
!keyboard.isMetaPressed &&
|
||||
!keyboard.isShiftPressed &&
|
||||
!keyboard.isAltPressed) {
|
||||
_cheatsheet.close(context);
|
||||
return true;
|
||||
}
|
||||
// Ctrl+K is the terminal's kill-line while the terminal has the keyboard;
|
||||
// Alt+K still opens Chat there.
|
||||
if (shellChatShortcut(event, keyboard) &&
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../features/settings/keyboard_shortcuts_help.dart';
|
||||
|
||||
/// Opens and closes the keyboard shortcuts cheatsheet from the shell's global
|
||||
/// key handler. Ctrl+? (or Escape) closes it, and it is only ever popped once:
|
||||
/// closing clears the token first, and a dismissal by the barrier or close
|
||||
/// button clears it when the route completes.
|
||||
class ShellCheatsheet {
|
||||
Object? _token;
|
||||
|
||||
bool get isOpen => _token != null;
|
||||
|
||||
void toggle(BuildContext context) {
|
||||
if (isOpen) {
|
||||
close(context);
|
||||
} else {
|
||||
open(context);
|
||||
}
|
||||
}
|
||||
|
||||
void open(BuildContext context) {
|
||||
if (isOpen) return;
|
||||
final token = _token = Object();
|
||||
showKeyboardShortcutsHelp(context).whenComplete(() {
|
||||
if (identical(_token, token)) _token = null;
|
||||
});
|
||||
}
|
||||
|
||||
void close(BuildContext context) {
|
||||
if (!isOpen) return;
|
||||
_token = null;
|
||||
Navigator.of(context, rootNavigator: true).maybePop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'shell_list_shortcuts.dart';
|
||||
|
||||
/// Whether [event] opens or closes the keyboard shortcuts cheatsheet: Ctrl+?
|
||||
/// (Cmd+? on macOS). The chord is the one that types '?', so it is matched
|
||||
/// by the logical '?' key or character, or by the slash key held with Shift on
|
||||
/// US layouts. Ctrl+/ without Shift is Search and never matches here, and
|
||||
/// Alt is never part of this chord. Only the first press of a held chord acts.
|
||||
bool shellCheatsheetShortcut(
|
||||
KeyEvent event,
|
||||
HardwareKeyboard keyboard, {
|
||||
TargetPlatform? platform,
|
||||
}) {
|
||||
if (event is! KeyDownEvent ||
|
||||
keyboard.isAltPressed ||
|
||||
!shellPrimaryModifier(keyboard, platform: platform)) {
|
||||
return false;
|
||||
}
|
||||
return event.logicalKey == LogicalKeyboardKey.question ||
|
||||
event.character == '?' ||
|
||||
(event.logicalKey == LogicalKeyboardKey.slash && keyboard.isShiftPressed);
|
||||
}
|
||||
|
||||
/// The chord as it reads on this platform, for the help page. macOS accepts
|
||||
/// Cmd or Ctrl (as Search does), so both are shown there.
|
||||
String shellCheatsheetShortcutLabel([TargetPlatform? platform]) =>
|
||||
(platform ?? defaultTargetPlatform) == TargetPlatform.macOS
|
||||
? 'Cmd+?, Ctrl+?'
|
||||
: 'Ctrl+?';
|
||||
Reference in New Issue
Block a user