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

127 lines
3.9 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
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.
class TerminalBody extends StatelessWidget {
const TerminalBody({
super.key,
required this.hub,
required this.cupertino,
this.keys = false,
});
final TerminalHub hub;
final bool cupertino;
final bool keys;
@override
Widget build(BuildContext context) {
final tab = hub.selected;
if (tab == null) return _empty(context);
final notes = <String>[
if (!tab.connected)
'${tab.machineName} is not connected; the terminal resumes when it is.'
else if (!tab.info.running)
tab.info.endedText
else if (!tab.info.persistent)
'This shell ends if the server on ${tab.machineName} restarts '
'(tmux is not installed there).',
?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,
cupertino: cupertino,
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,
cupertino: cupertino,
field: chatShellTouchFirst(),
),
),
if (!tab.info.running && tab.connected) _closeEnded(tab),
],
);
}
TextStyle? _noteStyle(BuildContext context) => cupertino
? CupertinoTheme.of(context).textTheme.textStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(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: cupertino
? CupertinoButton(onPressed: close, child: const Text('Close tab'))
: 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)
cupertino
? CupertinoButton(
onPressed: open,
child: const Text('New terminal'),
)
: FilledButton.tonal(
onPressed: open,
child: const Text('New terminal'),
),
],
),
);
}
}