import 'package:flutter/foundation.dart'; import '../chat/workspace/chat_center_stage_controller.dart'; import '../chat/workspace/chat_float_shortcut.dart'; import '../terminal/terminal_shortcuts.dart'; import '../../shell/windows/window_shortcuts.dart'; import '../../shell/shell_chat_shortcut.dart'; import '../../shell/shell_cheatsheet_shortcut.dart'; import '../../shell/shell_runs_pipelines_shortcut.dart'; import '../../shell/shell_search_shortcut_labels.dart'; /// The headings the help page groups its shortcuts under, in display order. enum KeyboardShortcutGroup { navigation('Navigation'), chat('Chat'), windows('Windows'), terminal('Terminal'), lists('Lists'), search('Search'); const KeyboardShortcutGroup(this.title); final String title; } /// A shortcut entry shown in the in-app help. Keeping these entries together /// prevents the help page from inventing chords that the handlers do not use. class KeyboardShortcutEntry { const KeyboardShortcutEntry({ required this.group, required this.action, required this.keys, this.caveat, }); final KeyboardShortcutGroup group; final String action; final String keys; final String? caveat; } class KeyboardShortcutRegistry { const KeyboardShortcutRegistry({required this.platform, required this.web}); factory KeyboardShortcutRegistry.current() => KeyboardShortcutRegistry(platform: defaultTargetPlatform, web: kIsWeb); final TargetPlatform platform; final bool web; bool get _apple => platform == TargetPlatform.macOS || platform == TargetPlatform.iOS; bool get _macOS => platform == TargetPlatform.macOS; /// The entries of one [group], in the order they are listed. List inGroup(KeyboardShortcutGroup group) => [ for (final entry in entries) if (entry.group == group) entry, ]; List get entries => [ ..._navigation, ..._chat, ..._windows, ..._terminal, ..._lists, ..._search, ]; String get _cheatsheetKeys => shellCheatsheetShortcutLabel(platform); List get _navigation => [ KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, action: 'Show keyboard shortcuts', keys: _cheatsheetKeys, caveat: 'Press again or Esc to close. Ctrl+/ still opens Search. In the terminal the key goes to the terminal.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, action: 'Open Search', keys: shellSearchShortcutLabels(platform).join(', '), caveat: web ? 'The browser may reserve some keyboard shortcuts.' : null, ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, action: 'Leave Search', keys: 'Esc', caveat: 'Focuses the search field first, then goes back to the previous page.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, action: 'Open Chat', keys: shellChatShortcutLabels(platform).join(', '), caveat: web ? 'Shows the open chat and focuses its message input. The browser may reserve Ctrl+K.' : 'Shows the open chat and focuses its message input. In the terminal Ctrl+K stays kill-line; ${_apple ? 'Option+K and Cmd+K still open' : 'Alt+K still opens'} Chat.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, action: 'Switch between Runs and Pipelines', keys: shellRunsPipelinesShortcutLabel, caveat: 'Goes to Runs from other destinations and to Pipelines from Runs. In a text field or the terminal Ctrl+A keeps its own meaning.', ), ]; List get _chat => [ KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Focus the active chat composer', keys: '/', caveat: 'Only outside text fields and other popup surfaces.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Send a chat message', keys: _apple ? 'Cmd+Enter' : 'Ctrl+Enter', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Choose a slash command', keys: 'Up, Down, Enter', caveat: 'While the slash command list is open above the composer. Esc dismisses it.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Toggle Files', keys: _apple ? 'Cmd+E' : 'Ctrl+E', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Toggle Center Stage', keys: '${chatCenterStageShortcutLabel(platform)}${_apple ? ', Cmd+Shift+E' : ', Ctrl+Shift+E'}', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Stop a running chat', keys: 'Esc, Esc', caveat: 'Press Esc twice within one second while a turn runs; the first press shows a hint.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Focus the neighbouring chat pane', keys: _apple ? 'Cmd+Shift+Arrows' : 'Ctrl+Shift+Arrows', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Move the active chat pane', keys: _apple ? 'Cmd+Option+Shift+Arrows' : 'Ctrl+Alt+Shift+Arrows', ), if (!web) const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Cycle chats and floating chat panes', keys: 'Ctrl+Tab, Ctrl+Shift+Tab', caveat: 'With one tiled pane, cycles open sidebar chats (excluding closed, hidden and floating chats); with multiple panes, cycles panes. While the terminal is shown, Ctrl+Tab moves between its tabs instead.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Resize panes with a focused divider', keys: 'Arrow keys', caveat: 'While the divider between panes has keyboard focus.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.chat, action: 'Scroll a wide transcript block', keys: 'Left, Right', caveat: 'While the block has keyboard focus.', ), ]; List get _windows => [ KeyboardShortcutEntry( group: KeyboardShortcutGroup.windows, action: 'Show or hide floating chats', keys: _apple ? '${chatFloatShortcutLabel(platform)}, Ctrl+Y, Cmd+Y' : '${chatFloatShortcutLabel(platform)}, Ctrl+Y', caveat: 'Brings a chat forward from Terminal and focuses its message input.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.windows, action: 'Cycle floating windows', keys: _cycleKeys, ), ]; /// Alt+` (Option+` on macOS) and the same chord with Shift, backwards. String get _cycleKeys { final forward = windowCycleShortcutLabel(platform); return '$forward, ${forward.replaceFirst('+', '+Shift+')}'; } List get _terminal => [ KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Show or hide terminal', keys: TerminalShortcutLabels( web: web, platform: platform, ).toggle.join(', '), caveat: web ? 'Browser tab shortcuts such as Cmd+T are not available to the app.' : null, ), if (!web) const KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Move between terminal tabs', keys: 'Ctrl+Tab, Ctrl+Shift+Tab', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Copy selected terminal text', keys: TerminalShortcutLabels(web: web, platform: platform).copy, caveat: _apple ? null : 'Ctrl+C copies only while text is selected; otherwise it reaches the process.', ), KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Paste into the terminal', keys: _apple ? 'Cmd+V' : 'Ctrl+V, Ctrl+Shift+V', ), if (_apple) const KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Select all terminal text', keys: 'Cmd+A', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.terminal, action: 'Resize the terminal panel', keys: 'Up, Down', caveat: 'While the panel has keyboard focus.', ), ]; List get _lists => [ KeyboardShortcutEntry( group: KeyboardShortcutGroup.lists, action: 'Open a chat list item', keys: _macOS ? 'Cmd+1…9' : 'Ctrl+1…9', caveat: 'Available when the Chat list is mounted; numbers select its visible item.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.lists, action: 'Resize a list pane', keys: 'Left, Right', caveat: 'While the divider beside a list has keyboard focus.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.lists, action: 'Previous or next artifact or changed file', keys: 'Left, Right', caveat: 'In an artifact or file-diff viewer. Esc closes an artifact viewer.', ), ]; List get _search => [ const KeyboardShortcutEntry( group: KeyboardShortcutGroup.search, action: 'Move through Search results', keys: 'Ctrl+N, Ctrl+P, Up, Down', caveat: 'While the Search field is focused. Uses Control rather than Cmd on macOS; Enter opens the highlighted result.', ), ]; }