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'; }