c5af000453
- Desktop and web terminals use the text-input path too, so IMEs, dead keys, the emoji picker and dictation work; the key handler still encodes special keys and chords first (handled keys are never typed again), composing text is drawn at the cursor and sent on commit, the platform editable sits at the cursor, and a closed input reopens on the next key. The editable terminal is a semantics text field so web typing also works with semantics on. - Web: "Capture browser keys" (terminal menu and right-click menu) goes full screen and calls navigator.keyboard.lock(); hold Esc to leave. No-op where unsupported; cheatsheet and menu note give alternatives. - Touch: scrollable key row with sticky Ctrl/Alt (tap once, double-tap lock), Esc, Tab, arrows, Home/End, PgUp/PgDn and symbols through the hardware encoder; soft keyboard types per key (delete detection, Return as Enter); the line field moves behind the row's Line button. - Capture kit: compose=, --semantics false, --user-agent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.4 KiB
Dart
118 lines
4.4 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 text fields they toggle rather than type. While a
|
|
/// terminal has the keyboard ([inTerminal]) only Cmd+T and Ctrl+` toggle:
|
|
/// Ctrl+T (transpose-chars) and Alt+T (ESC t, transpose-words) reach the
|
|
/// shell.
|
|
///
|
|
/// A held chord's repeats match too (and are swallowed); only its first
|
|
/// press toggles.
|
|
bool terminalToggleShortcut(
|
|
KeyEvent event,
|
|
HardwareKeyboard keyboard, {
|
|
bool inTerminal = false,
|
|
}) {
|
|
if (event is KeyUpEvent) 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;
|
|
if (inTerminal) return meta && !control && !alt;
|
|
return [control, meta, alt].where((pressed) => pressed).length == 1;
|
|
}
|
|
|
|
/// The step Ctrl+Tab (1) or Ctrl+Shift+Tab (-1) moves between terminal
|
|
/// tabs (again as it repeats), or null for any other key.
|
|
int? terminalTabShortcut(KeyEvent event, HardwareKeyboard keyboard) {
|
|
if (event is KeyUpEvent ||
|
|
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. The Alt chord leads (the
|
|
/// button's tooltip names it): every platform and browser delivers it.
|
|
List<String> get toggle => [
|
|
_apple ? 'Option+T' : 'Alt+T',
|
|
if (!web && _apple) 'Cmd+T',
|
|
if (!web || _apple) 'Ctrl+T',
|
|
'Ctrl+`',
|
|
];
|
|
|
|
/// The keys that still hide the terminal while it has the keyboard: its
|
|
/// Ctrl+T and Alt+T are the shell's.
|
|
List<String> get toggleInTerminal => [if (!web && _apple) 'Cmd+T', 'Ctrl+`'];
|
|
|
|
/// How the in-terminal exception reads in the menu and the cheatsheet.
|
|
String get inTerminalNote =>
|
|
'Inside the terminal, ${_apple ? 'Option+T' : 'Alt+T'} and Ctrl+T '
|
|
'reach the shell; ${toggleInTerminal.join(' or ')} hides it.';
|
|
|
|
/// 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(', ')}',
|
|
inTerminalNote,
|
|
if (tabs.isNotEmpty) 'Next or previous tab: ${tabs.join(' / ')}',
|
|
'Copy selected text: $copy',
|
|
?browserKeysNote,
|
|
].join('\n');
|
|
|
|
/// A browser outside macOS keeps Ctrl+W, Ctrl+T and Ctrl+N (close and
|
|
/// open tabs and windows) from the page: how to send them anyway, or
|
|
/// what to press instead. Null where the terminal receives them (native
|
|
/// builds; Safari and Chrome on a Mac use Cmd for those).
|
|
String? get browserKeysNote => web && !_apple
|
|
? 'The browser keeps Ctrl+W, Ctrl+T and Ctrl+N: choose Capture '
|
|
'browser keys (Chrome, Edge; hold Esc to leave), or press '
|
|
'Alt+Backspace instead of Ctrl+W to delete a word.'
|
|
: null;
|
|
|
|
/// The keys that copy the terminal's selected text. A browser keeps
|
|
/// Ctrl+Shift+C for its developer tools.
|
|
String get copy => _apple
|
|
? 'Cmd+C'
|
|
: web
|
|
? 'Ctrl+C'
|
|
: 'Ctrl+C, Ctrl+Shift+C';
|
|
}
|