import 'dart:async'; import 'package:flutter/material.dart'; import 'terminal_hub.dart'; import 'terminal_layout.dart'; import 'terminal_menu.dart'; import 'terminal_shortcuts.dart'; import 'terminal_tab.dart'; /// The terminal's header: one tab per terminal (title, and the machine when /// tabs come from several), New terminal (choosing the machine when more /// than one is connected), the mode menu, and the host's own [trailing] /// window buttons. class TerminalTabStrip extends StatelessWidget { const TerminalTabStrip({ super.key, required this.hub, required this.layout, this.trailing = const [], this.showModes = true, }); final TerminalHub hub; final TerminalLayout layout; final List trailing; /// False on the narrow page, which has no panel or window to choose. final bool showModes; @override Widget build(BuildContext context) { final selected = hub.selected; return Row( children: [ Expanded( child: SingleChildScrollView( scrollDirection: Axis.horizontal, padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), child: Row( children: [ for (final (index, tab) in hub.tabs.indexed) Padding( padding: const EdgeInsetsDirectional.only(end: 4), child: Semantics( identifier: 'terminal-tab-$index', child: _tab(tab, tab == selected), ), ), ], ), ), ), _newTerminal(), if (showModes) TerminalMenuButton<_Option>( label: 'Terminal options', info: TerminalShortcutLabels.current().summary, icon: Icons.more_vert, choices: [ for (final mode in TerminalMode.values) TerminalChoice( _Option(mode), mode.label, checked: layout.mode == mode, key: ValueKey('terminal-mode-${mode.name}'), ), const TerminalChoice( _Option(null), 'Hide terminal', key: ValueKey('terminal-hide'), ), ], onSelected: (option) => option.mode == null ? layout.hide() : layout.setMode(option.mode!), ), ...trailing, ], ); } String _label(TerminalTab tab) => hub.multiMachine ? '${tab.title} ยท ${tab.machineName}' : tab.title; Widget _tab(TerminalTab tab, bool selected) { final label = Text( _label(tab), maxLines: 1, overflow: TextOverflow.ellipsis, style: tab.info.running ? null : const TextStyle(fontStyle: FontStyle.italic), ); void close() => unawaited(hub.close(tab)); return InputChip( label: label, selected: selected, showCheckmark: false, tooltip: '${tab.machineName}: ${tab.info.cwd}', onSelected: (_) => hub.select(tab), onDeleted: close, deleteButtonTooltipMessage: 'Close terminal', ); } Widget _newTerminal() { final machines = hub.connected; if (machines.length > 1) { return TerminalMenuButton( label: 'New terminal', icon: Icons.add, choices: [ for (final machine in machines) TerminalChoice( machine.id, machine.name, key: ValueKey('terminal-new-${machine.id}'), ), ], onSelected: (id) => unawaited(hub.open(machineId: id)), ); } final enabled = machines.isNotEmpty; void open() => unawaited(hub.open()); return IconButton( tooltip: 'New terminal', onPressed: enabled ? open : null, icon: const Icon(Icons.add), ); } } /// A mode to switch to, or (null) hiding the terminal. class _Option { const _Option(this.mode); final TerminalMode? mode; }