30934aa71c
Alt+T, Ctrl+T and Cmd+T join Ctrl+` as early focus-manager key handlers, so they toggle the panel, window or narrow page from anywhere without reaching the shell or a text field. Opening with no terminal starts one. The shown terminal takes the keyboard (also on reload and tab changes) and hiding gives it back. The panel slides from the bottom edge as the app shrinks, the window scales and fades, both on Motion.standard and off under reduced motion. While shown, Ctrl+Tab/Ctrl+Shift+Tab move between terminal tabs instead of chat panes. The tooltip and menu list the shortcuts the platform delivers.
78 lines
2.8 KiB
Dart
78 lines
2.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
/// Whether [event] shows or hides the terminal: Alt+T, Ctrl+T, Cmd+T
|
|
/// (Meta+T) or Ctrl+` (Shift allowed only with Ctrl+`). They are global key
|
|
/// handlers, so inside the terminal they toggle rather than reaching the
|
|
/// shell (whose Ctrl+T, transpose-chars, is then not available) and inside
|
|
/// text fields they toggle rather than type.
|
|
bool terminalToggleShortcut(KeyEvent event, HardwareKeyboard keyboard) {
|
|
if (event is! KeyDownEvent) return false;
|
|
final control = keyboard.isControlPressed;
|
|
final meta = keyboard.isMetaPressed;
|
|
final alt = keyboard.isAltPressed;
|
|
if (event.logicalKey == LogicalKeyboardKey.backquote) {
|
|
// Cmd+` is the macOS window switcher.
|
|
return control && !meta && !alt;
|
|
}
|
|
// Option+T types a symbol (†) on a Mac, so with Alt the key is also
|
|
// recognised by its position.
|
|
final t =
|
|
event.logicalKey == LogicalKeyboardKey.keyT ||
|
|
alt && event.physicalKey == PhysicalKeyboardKey.keyT;
|
|
if (!t || keyboard.isShiftPressed) return false;
|
|
return [control, meta, alt].where((pressed) => pressed).length == 1;
|
|
}
|
|
|
|
/// The step Ctrl+Tab (1) or Ctrl+Shift+Tab (-1) moves between terminal
|
|
/// tabs, or null for any other key.
|
|
int? terminalTabShortcut(KeyEvent event, HardwareKeyboard keyboard) {
|
|
if (event is! KeyDownEvent ||
|
|
event.logicalKey != LogicalKeyboardKey.tab ||
|
|
!keyboard.isControlPressed ||
|
|
keyboard.isMetaPressed ||
|
|
keyboard.isAltPressed) {
|
|
return null;
|
|
}
|
|
return keyboard.isShiftPressed ? -1 : 1;
|
|
}
|
|
|
|
/// The terminal's shortcuts as they work on this platform, for its tooltip
|
|
/// and menu. A browser tab never receives Cmd+T, Ctrl+Tab or Ctrl+Shift+Tab
|
|
/// (new tab, next and previous tab), nor Ctrl+T outside macOS, where it
|
|
/// also opens a tab; native builds get them all.
|
|
class TerminalShortcutLabels {
|
|
const TerminalShortcutLabels({required this.web, required this.platform});
|
|
|
|
TerminalShortcutLabels.current()
|
|
: web = kIsWeb,
|
|
platform = defaultTargetPlatform;
|
|
|
|
final bool web;
|
|
final TargetPlatform platform;
|
|
|
|
bool get _apple =>
|
|
platform == TargetPlatform.macOS || platform == TargetPlatform.iOS;
|
|
|
|
/// The keys that show or hide the terminal, most convenient first.
|
|
List<String> get toggle => [
|
|
if (!web && _apple) 'Cmd+T',
|
|
if (!web || _apple) 'Ctrl+T',
|
|
_apple ? 'Option+T' : 'Alt+T',
|
|
'Ctrl+`',
|
|
];
|
|
|
|
/// The keys that move between tabs, or none where the browser keeps them.
|
|
List<String> get tabs =>
|
|
web ? const [] : const ['Ctrl+Tab', 'Ctrl+Shift+Tab'];
|
|
|
|
/// The toggle button's tooltip.
|
|
String get button => 'Terminal (${toggle.first})';
|
|
|
|
/// Every shortcut, one per line, for the terminal's menu.
|
|
String get summary => [
|
|
'Show or hide: ${toggle.join(', ')}',
|
|
if (tabs.isNotEmpty) 'Next or previous tab: ${tabs.join(' / ')}',
|
|
].join('\n');
|
|
}
|