import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:xterm/xterm.dart'; import '../ui/transcript/chat_link_launcher.dart'; import 'chat_shell_metrics.dart'; import 'chat_shell_terminal_links.dart'; import 'chat_shell_terminal_session.dart'; import 'chat_shell_terminal_theme.dart'; /// Whether the soft keyboard is the main way to type here, so the terminal /// takes typing from the key toolbar's field rather than its own hidden one. bool chatShellTouchFirst() => defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS; /// One run's terminal emulator view. In a card it is bounded to /// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its /// output); with [fill] it takes the space its parent gives (full screen). /// It sizes the terminal to its own layout; a running process is resized to /// match. Tapping a printed web link opens it in the browser. class ChatShellTerminalView extends StatefulWidget { const ChatShellTerminalView({ super.key, required this.session, required this.cupertino, this.fill = false, this.autofocus = false, this.autoResize = true, }); final ChatShellTerminalSession session; final bool cupertino; final bool fill; final bool autofocus; /// False while another view of the same terminal (full screen) sizes it. final bool autoResize; @override State createState() => _ChatShellTerminalViewState(); } class _ChatShellTerminalViewState extends State { final TerminalController _controller = TerminalController(); final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal'); int? _fitRows; @override void dispose() { _controller.dispose(); _focus.dispose(); super.dispose(); } void _tapped(TapUpDetails details, CellOffset offset) { final uri = chatShellLinkAt(widget.session.terminal.buffer, offset); if (uri != null) openChatLink(context, uri); } /// A finished run's box shrinks to the rows its output uses. void _fit() { if (!mounted || widget.fill || widget.session.live) return; final used = widget.session.terminal.buffer.absoluteCursorY + 1; final rows = used.clamp(1, kChatShellOutputCollapsedLines); if (rows != _fitRows) setState(() => _fitRows = rows); } @override Widget build(BuildContext context) { final session = widget.session; final live = session.live; final style = chatShellTerminalStyle(context, widget.cupertino); const padding = EdgeInsets.all(8); Widget view = Semantics( identifier: 'chat-shell-terminal', label: live ? 'Terminal, type to send input' : 'Terminal output', child: TerminalView( session.terminal, controller: _controller, focusNode: _focus, autofocus: widget.autofocus && live, autoResize: widget.autoResize, theme: chatShellTerminalTheme(context, widget.cupertino), textStyle: style, padding: padding, readOnly: !live, hardwareKeyboardOnly: !live || chatShellTouchFirst(), keyboardType: TextInputType.visiblePassword, cursorType: TerminalCursorType.block, onTapUp: _tapped, ), ); // The emulator would otherwise inset itself by the window's safe area. view = MediaQuery.removePadding( context: context, removeTop: true, removeBottom: true, removeLeft: true, removeRight: true, child: view, ); view = ClipRRect(borderRadius: BorderRadius.circular(8), child: view); if (widget.fill) return view; if (!live) WidgetsBinding.instance.addPostFrameCallback((_) => _fit()); final rows = live ? kChatShellOutputCollapsedLines : (_fitRows ?? kChatShellOutputCollapsedLines); final line = chatShellTerminalLineHeight(context, style); // A pixel of slack keeps rounding from losing the last row. return SizedBox(height: rows * line + padding.vertical + 1, child: view); } }