a792b30513
The first show of an app run starts one terminal (the machine's login prompt) unless one is running; later toggles never open another. Drop the tmux field and restart note, and update the Settings copy and the terminal notes.
133 lines
4.1 KiB
Dart
133 lines
4.1 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. With [focus] it is that
|
|
/// focus scope, which the surface focuses whenever the terminal shows.
|
|
class TerminalBody extends StatelessWidget {
|
|
const TerminalBody({
|
|
super.key,
|
|
required this.hub,
|
|
required this.cupertino,
|
|
this.keys = false,
|
|
this.focus,
|
|
});
|
|
|
|
final TerminalHub hub;
|
|
final bool cupertino;
|
|
final bool keys;
|
|
final FocusScopeNode? focus;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final focus = this.focus;
|
|
final body = _body(context);
|
|
return focus == null ? body : FocusScope(node: focus, child: body);
|
|
}
|
|
|
|
Widget _body(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,
|
|
?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'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|