Files
pipelines/app/lib/features/terminal/terminal_open_action.dart
T
alan a792b30513 Open one login terminal on first show; toggling only hides it
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.
2026-11-10 00:42:59 +00:00

74 lines
2.4 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../core/api_gateway.dart';
import '../../ui/adaptive/segmented_bar_overflow.dart';
import 'terminal_hub.dart';
import 'terminal_scope.dart';
/// The first time the terminal is shown in an app run it starts one
/// terminal, as New terminal does (it shows the machine's login prompt),
/// unless one is already running once every machine's list has arrived.
/// Every later show only shows it: a hidden terminal stays logged in, and
/// a new one (each asking for the password) is opened by hand.
Future<void> openTerminalIfNone(TerminalHub hub) {
if (hub.firstOpenDone) return Future.value();
return hub.firstOpening ??= _openFirst(
hub,
).whenComplete(() => hub.firstOpening = null);
}
Future<void> _openFirst(TerminalHub hub) async {
hub.activate();
await hub.listed();
// With no machine connected yet, a later show tries again.
if (hub.firstOpenDone || hub.connected.isEmpty) return;
hub.firstOpenDone = true;
if (hub.tabs.any((tab) => tab.info.running)) return;
await hub.open();
}
const String terminalOpenActionLabel = 'Open in terminal';
/// Opens a terminal in [directory] on [gateway]'s machine through the app's
/// terminal surface; a refusal goes to [onRefused]. Does nothing outside a
/// [TerminalScope].
void openInTerminal(
BuildContext context,
ApiGateway gateway,
String directory, {
void Function(String refusal)? onRefused,
}) {
final actions = TerminalScope.maybeOf(context);
if (actions == null) return;
unawaited(
actions.openAt(gateway, directory).then((refusal) {
if (refusal != null) onRefused?.call(refusal);
}),
);
}
/// The chat actions menu's "Open in terminal" for the chat's folder, or
/// null when the app has no terminal surface or the chat no folder.
Widget? terminalOpenMenuAction(
BuildContext context, {
required ApiGateway gateway,
required String directory,
void Function(String refusal)? onRefused,
}) {
if (TerminalScope.maybeOf(context) == null || directory.isEmpty) {
return null;
}
return SegmentedOverflowAction(
key: const ValueKey<String>('chat-open-terminal-action'),
overflowEntry: SegmentedOverflowEntry(
label: terminalOpenActionLabel,
icon: Icons.terminal,
onPressed: () =>
openInTerminal(context, gateway, directory, onRefused: onRefused),
),
child: const SizedBox.shrink(),
);
}