import 'dart:async'; import 'package:flutter/material.dart'; import '../chat/shell/chat_shell_terminal_keys.dart'; import '../chat/shell/chat_shell_terminal_view.dart'; import 'terminal_hub.dart'; import 'terminal_tab.dart'; /// The selected terminal: its emulator (focused when shown), what the /// operator should know about it (ended, machine offline, ends with the /// server) and, with [keys], the touch key toolbar. With [focus] it is that /// focus scope, which the surface focuses whenever the terminal shows. class TerminalBody extends StatelessWidget { const TerminalBody({ super.key, required this.hub, this.keys = false, this.focus, }); final TerminalHub hub; final bool keys; final FocusScopeNode? focus; @override Widget build(BuildContext context) { final focus = this.focus; final body = _body(context); return focus == null ? body : FocusScope(node: focus, child: body); } Widget _body(BuildContext context) { final tab = hub.selected; if (tab == null) return _empty(context); final notes = [ if (!tab.connected) '${tab.machineName} is not connected; the terminal resumes when it is.' else if (!tab.info.running) tab.info.endedText, ?tab.error, ?hub.error, ]; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( child: Padding( padding: const EdgeInsets.fromLTRB(8, 0, 8, 8), child: ChatShellTerminalView( key: ValueKey('terminal-view-${tab.key}'), session: tab.session, fill: true, autofocus: true, ), ), ), for (final note in notes) Padding( padding: const EdgeInsets.fromLTRB(12, 0, 12, 8), child: Text(note, style: _noteStyle(context)), ), if (keys && tab.session.live && tab.connected) Padding( padding: const EdgeInsets.fromLTRB(8, 0, 8, 8), child: ChatShellTerminalKeys( session: tab.session, field: chatShellTouchFirst(), ), ), if (!tab.info.running && tab.connected) _closeEnded(tab), ], ); } TextStyle? _noteStyle(BuildContext context) => Theme.of(context) .textTheme .bodySmall ?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant); Widget _closeEnded(TerminalTab tab) { void close() => unawaited(hub.close(tab)); return Padding( padding: const EdgeInsets.fromLTRB(8, 0, 8, 8), child: Align( alignment: AlignmentDirectional.centerStart, child: TextButton(onPressed: close, child: const Text('Close tab')), ), ); } Widget _empty(BuildContext context) { final machines = hub.connected; final text = machines.isEmpty ? 'No machine is connected.' : 'No terminals are open.'; void open() => unawaited(hub.open()); return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(text, style: _noteStyle(context)), if (hub.error case final error?) Padding( padding: const EdgeInsets.all(8), child: Text(error, style: _noteStyle(context)), ), if (machines.isNotEmpty) FilledButton.tonal( onPressed: open, child: const Text('New terminal'), ), ], ), ); } }