Files
pipelines/app/lib/features/terminal/terminal_tab_strip.dart
T

182 lines
5.4 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
import 'terminal_menu.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,
required this.cupertino,
this.trailing = const [],
this.showModes = true,
});
final TerminalHub hub;
final TerminalLayout layout;
final bool cupertino;
final List<Widget> 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(context, tab, tab == selected),
),
),
],
),
),
),
_newTerminal(context),
if (showModes)
TerminalMenuButton<_Option>(
cupertino: cupertino,
label: 'Terminal options',
icon: Icons.more_vert,
cupertinoIcon: CupertinoIcons.ellipsis,
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(BuildContext context, 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));
if (cupertino) {
final color = selected
? CupertinoTheme.of(context).primaryColor
: CupertinoColors.label.resolveFrom(context);
return Row(
mainAxisSize: MainAxisSize.min,
children: [
CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: () => hub.select(tab),
child: DefaultTextStyle.merge(
style: TextStyle(
color: color,
fontWeight: selected ? FontWeight.w600 : null,
),
child: label,
),
),
CupertinoButton(
padding: EdgeInsets.zero,
onPressed: close,
child: const Icon(
CupertinoIcons.xmark_circle_fill,
semanticLabel: 'Close terminal',
),
),
],
);
}
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(BuildContext context) {
final machines = hub.connected;
if (machines.length > 1) {
return TerminalMenuButton<String>(
cupertino: cupertino,
label: 'New terminal',
icon: Icons.add,
cupertinoIcon: CupertinoIcons.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 cupertino
? CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: enabled ? open : null,
child: const Icon(
CupertinoIcons.add,
semanticLabel: 'New terminal',
),
)
: 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;
}