import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:xterm/core.dart'; import 'chat_shell_terminal_key_button.dart'; import 'chat_shell_terminal_key_row.dart'; import 'chat_shell_terminal_session.dart'; /// One key of the desktop 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 running terminal's toolbar offers. With [touch] it is the /// touch key row ([ChatShellTerminalKeyRow]: sticky Ctrl and Alt and the /// keys a phone keyboard lacks); otherwise Paste and a few keys for a mouse. class ChatShellTerminalKeys extends StatelessWidget { const ChatShellTerminalKeys({ super.key, required this.session, required this.touch, }); final ChatShellTerminalSession session; final bool touch; Terminal get _terminal => session.terminal; Future _paste() async { final data = await Clipboard.getData(Clipboard.kTextPlain); final text = data?.text; if (text != null && text.isNotEmpty && session.live) { _terminal.paste(text); } } @override Widget build(BuildContext context) { if (touch) return ChatShellTerminalKeyRow(session: session); return Wrap( spacing: 4, runSpacing: 4, children: [ chatShellKeyButton('paste', 'Paste', Icons.content_paste, _paste), for (final key in _keys) chatShellKeyButton( key.id, key.label, key.material, () => key.send(_terminal), ), ], ); } }