Files
pipelines/app/lib/features/terminal/terminal_browser_keys_web.dart
T
alan c5af000453 Terminal: text input for IMEs, Capture browser keys, touch key row
- 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>
2026-11-26 02:43:57 +00:00

74 lines
2.0 KiB
Dart

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:web/web.dart' as web;
import 'terminal_browser_keys.dart';
/// The Keyboard Lock API (`navigator.keyboard.lock()`), which Chrome and
/// Edge honour only in full screen: the page enters full screen, then locks
/// every key. The browser keeps holding Esc as the way out; leaving full
/// screen ends the lock.
class TerminalBrowserKeysPlatform implements TerminalBrowserKeysBackend {
TerminalBrowserKeysPlatform() {
web.document.addEventListener(
'fullscreenchange',
((web.Event _) {
if (web.document.fullscreenElement == null) _onReleased?.call();
}).toJS,
);
}
void Function()? _onReleased;
JSObject? get _keyboard {
final keyboard = (web.window.navigator as JSObject)['keyboard'];
return keyboard.isUndefinedOrNull ? null : keyboard as JSObject;
}
@override
bool get supported {
final keyboard = _keyboard;
return keyboard != null &&
keyboard.has('lock') &&
web.document.fullscreenEnabled;
}
@override
Future<bool> lock() async {
final keyboard = _keyboard;
final root = web.document.documentElement;
if (keyboard == null || root == null) return false;
try {
if (web.document.fullscreenElement == null) {
await root.requestFullscreen().toDart;
}
await keyboard.callMethod<JSPromise<JSAny?>>('lock'.toJS).toDart;
return true;
} catch (_) {
// Refused (no user gesture, a policy, an iframe): back out of full
// screen so the page is as it was.
await _exitFullscreen();
return false;
}
}
@override
Future<void> unlock() async {
_keyboard?.callMethod<JSAny?>('unlock'.toJS);
await _exitFullscreen();
}
Future<void> _exitFullscreen() async {
if (web.document.fullscreenElement == null) return;
try {
await web.document.exitFullscreen().toDart;
} catch (_) {
// Already leaving full screen.
}
}
@override
set onReleased(void Function() callback) => _onReleased = callback;
}