import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:xterm/core.dart'; import 'chat_shell_terminal_session.dart'; /// One key of the terminal toolbar: its label, icon and what it sends. typedef _Key = ({ String id, String label, IconData? material, void Function(Terminal terminal) send, }); final List<_Key> _keys = [ ( id: 'enter', label: 'Enter', material: Icons.keyboard_return, send: (t) => t.keyInput(TerminalKey.enter), ), ( id: 'ctrl-c', label: 'Ctrl-C', material: null, send: (t) => t.textInput('\x03'), ), ( id: 'tab', label: 'Tab', material: Icons.keyboard_tab, send: (t) => t.keyInput(TerminalKey.tab), ), ( id: 'esc', label: 'Esc', material: null, send: (t) => t.keyInput(TerminalKey.escape), ), ( id: 'left', label: 'Left', material: Icons.keyboard_arrow_left, send: (t) => t.keyInput(TerminalKey.arrowLeft), ), ( id: 'up', label: 'Up', material: Icons.keyboard_arrow_up, send: (t) => t.keyInput(TerminalKey.arrowUp), ), ( id: 'down', label: 'Down', material: Icons.keyboard_arrow_down, send: (t) => t.keyInput(TerminalKey.arrowDown), ), ( id: 'right', label: 'Right', material: Icons.keyboard_arrow_right, send: (t) => t.keyInput(TerminalKey.arrowRight), ), ]; /// The keys a touch keyboard lacks, and Paste, for a running terminal; with /// [field], also a text line whose text is typed into the process on Enter /// (the soft keyboard's own way in). The field is emptied as soon as its /// text is sent and can hide what is typed, for a token or password. class ChatShellTerminalKeys extends StatefulWidget { const ChatShellTerminalKeys({ super.key, required this.session, required this.field, }); final ChatShellTerminalSession session; final bool field; @override State createState() => _ChatShellTerminalKeysState(); } class _ChatShellTerminalKeysState extends State { final TextEditingController _text = TextEditingController(); bool _hidden = false; @override void dispose() { _text.dispose(); super.dispose(); } Terminal get _terminal => widget.session.terminal; Future _paste() async { final data = await Clipboard.getData(Clipboard.kTextPlain); final text = data?.text; if (text != null && text.isNotEmpty && widget.session.live) { _terminal.paste(text); } } /// Types the field's text and Enter, then forgets it. void _submit() { final text = _text.text; _text.clear(); if (text.isNotEmpty) _terminal.textInput(text); _terminal.keyInput(TerminalKey.enter); } @override Widget build(BuildContext context) { final keys = Wrap( spacing: 4, runSpacing: 4, children: [ _button('paste', 'Paste', Icons.content_paste, _paste), for (final key in _keys) _button(key.id, key.label, key.material, () => key.send(_terminal)), ], ); if (!widget.field) return keys; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.min, children: [ Padding(padding: const EdgeInsets.only(bottom: 8), child: _field()), keys, ], ); } Widget _field() { final toggle = _hidden ? 'Show typing' : 'Hide typing'; return TextField( key: const Key('chat-shell-terminal-field'), controller: _text, obscureText: _hidden, autocorrect: false, enableSuggestions: false, textInputAction: TextInputAction.send, onSubmitted: (_) => _submit(), decoration: InputDecoration( labelText: 'Type input, then Enter', suffixIcon: IconButton( key: const Key('chat-shell-terminal-hide'), tooltip: toggle, onPressed: () => setState(() => _hidden = !_hidden), icon: Icon(_hidden ? Icons.visibility : Icons.visibility_off), ), ), ); } Widget _button( String id, String label, IconData? material, VoidCallback onPressed, ) { final key = Key('chat-shell-key-$id'); if (material == null) { return OutlinedButton( key: key, style: OutlinedButton.styleFrom(visualDensity: VisualDensity.compact), onPressed: onPressed, child: Text(label), ); } return IconButton.outlined( key: key, visualDensity: VisualDensity.compact, tooltip: label, onPressed: onPressed, icon: Icon(material), ); } }