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>
125 lines
3.6 KiB
Dart
125 lines
3.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:xterm/core.dart';
|
|
|
|
import 'chat_shell_terminal_input.dart';
|
|
|
|
/// How a sticky modifier of the touch key row is held.
|
|
enum ChatShellStickyState {
|
|
off,
|
|
|
|
/// Applies to the next key or typed character, then turns off.
|
|
once,
|
|
|
|
/// Applies to every key until tapped again.
|
|
locked,
|
|
}
|
|
|
|
/// The touch key row's Ctrl and Alt, which a phone keyboard lacks: a tap
|
|
/// holds one for the next key (one-shot), a second tap soon after locks it,
|
|
/// a tap on a held one releases it. The key row's keys and what the soft
|
|
/// keyboard types both use them.
|
|
class ChatShellStickyModifiers extends ChangeNotifier {
|
|
ChatShellStickyModifiers({DateTime Function()? clock})
|
|
: _clock = clock ?? DateTime.now;
|
|
|
|
/// A second tap within this long of the first locks the modifier.
|
|
static const Duration doubleTap = Duration(milliseconds: 400);
|
|
|
|
final DateTime Function() _clock;
|
|
ChatShellStickyState _ctrl = ChatShellStickyState.off;
|
|
ChatShellStickyState _alt = ChatShellStickyState.off;
|
|
DateTime? _ctrlTapped;
|
|
DateTime? _altTapped;
|
|
|
|
ChatShellStickyState get ctrl => _ctrl;
|
|
ChatShellStickyState get alt => _alt;
|
|
|
|
/// Whether either modifier applies to the next key.
|
|
bool get active =>
|
|
_ctrl != ChatShellStickyState.off || _alt != ChatShellStickyState.off;
|
|
|
|
void tapCtrl() {
|
|
final now = _clock();
|
|
_ctrl = _next(_ctrl, _ctrlTapped, now);
|
|
_ctrlTapped = now;
|
|
notifyListeners();
|
|
}
|
|
|
|
void tapAlt() {
|
|
final now = _clock();
|
|
_alt = _next(_alt, _altTapped, now);
|
|
_altTapped = now;
|
|
notifyListeners();
|
|
}
|
|
|
|
ChatShellStickyState _next(
|
|
ChatShellStickyState state,
|
|
DateTime? tapped,
|
|
DateTime now,
|
|
) {
|
|
switch (state) {
|
|
case ChatShellStickyState.off:
|
|
return ChatShellStickyState.once;
|
|
case ChatShellStickyState.once:
|
|
final quick = tapped != null && now.difference(tapped) <= doubleTap;
|
|
return quick ? ChatShellStickyState.locked : ChatShellStickyState.off;
|
|
case ChatShellStickyState.locked:
|
|
return ChatShellStickyState.off;
|
|
}
|
|
}
|
|
|
|
/// The held modifiers for one key, releasing the one-shot ones.
|
|
({bool ctrl, bool alt}) take() {
|
|
final held = (
|
|
ctrl: _ctrl != ChatShellStickyState.off,
|
|
alt: _alt != ChatShellStickyState.off,
|
|
);
|
|
var changed = false;
|
|
if (_ctrl == ChatShellStickyState.once) {
|
|
_ctrl = ChatShellStickyState.off;
|
|
changed = true;
|
|
}
|
|
if (_alt == ChatShellStickyState.once) {
|
|
_alt = ChatShellStickyState.off;
|
|
changed = true;
|
|
}
|
|
if (changed) notifyListeners();
|
|
return held;
|
|
}
|
|
}
|
|
|
|
/// The bytes a non-printing [key] sends with the held modifiers, encoded as
|
|
/// a hardware key is (modifier parameter, DECCKM and keypad mode from
|
|
/// [state]). Alt is always Meta here: the key row has no Option symbols.
|
|
String? chatShellStickyKeyBytes(
|
|
TerminalKey key, {
|
|
required bool ctrl,
|
|
required bool alt,
|
|
required TerminalState state,
|
|
}) => chatShellSpecialKeyBytes(
|
|
key,
|
|
shift: false,
|
|
alt: alt,
|
|
meta: alt,
|
|
ctrl: ctrl,
|
|
state: state,
|
|
);
|
|
|
|
/// [text] as typed with the held modifiers: Ctrl maps its first character
|
|
/// to its control code (left as typed when it has none), Alt prefixes ESC;
|
|
/// the rest follows unmodified.
|
|
String chatShellStickyText(
|
|
String text, {
|
|
required bool ctrl,
|
|
required bool alt,
|
|
}) {
|
|
if (text.isEmpty || !ctrl && !alt) return text;
|
|
final first = String.fromCharCode(text.runes.first);
|
|
final rest = text.substring(first.length);
|
|
final bytes =
|
|
chatShellCharBytes(first, alt: alt, ctrl: ctrl) ??
|
|
chatShellCharBytes(first, alt: alt, ctrl: false) ??
|
|
first;
|
|
return '$bytes$rest';
|
|
}
|