Files
pipelines/app/lib/features/terminal/terminal_tab_strip.dart
T
2026-11-26 02:44:31 +00:00

159 lines
4.6 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'terminal_browser_keys.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_menu.dart';
import 'terminal_shortcuts.dart';
import 'terminal_tab.dart';
import 'terminal_tab_chip.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<Widget> trailing;
/// False on the narrow page, which has no panel or window to choose.
final bool showModes;
/// The browser keys capture the menu offers (tests replace the instance).
TerminalBrowserKeys get browserKeys => TerminalBrowserKeys.instance;
@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)
ListenableBuilder(
listenable: browserKeys,
builder: (context, _) => _options(context),
),
...trailing,
],
);
}
/// The options menu: the modes, the browser keys capture (on a browser
/// that can) and Hide terminal, with the shortcuts under them.
Widget _options(BuildContext context) => 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}'),
),
if (browserKeys.supported)
TerminalChoice(
const _Option(null, browserKeys: true),
'Capture browser keys',
checked: browserKeys.active,
key: const ValueKey('terminal-browser-keys'),
),
const TerminalChoice(
_Option(null),
'Hide terminal',
key: ValueKey('terminal-hide'),
),
],
onSelected: (option) {
if (option.browserKeys) {
unawaited(toggleTerminalBrowserKeys(context, browserKeys));
} else if (option.mode == null) {
layout.hide();
} else {
layout.setMode(option.mode!);
}
},
);
String _label(TerminalTab tab) =>
hub.multiMachine ? '${tab.title} · ${tab.machineName}' : tab.title;
Widget _tab(TerminalTab tab, bool selected) {
final title = _label(tab);
return TerminalTabChip(
title: title,
// The whole title (the tab may ellipsise it), then where it runs.
tooltip: '$title\n${tab.machineName}: ${tab.cwd}',
selected: selected,
running: tab.info.running,
bell: tab.bellUnseen,
onSelect: () => hub.select(tab),
onClose: () => unawaited(hub.close(tab)),
);
}
Widget _newTerminal() {
final machines = hub.connected;
if (machines.length > 1) {
return TerminalMenuButton<String>(
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, (null) hiding the terminal, or the browser keys
/// capture.
class _Option {
const _Option(this.mode, {this.browserKeys = false});
final TerminalMode? mode;
final bool browserKeys;
}