import 'dart:async'; import 'package:flutter/material.dart'; import '../../core/api_gateway.dart'; import '../../shell/list_detail_policy.dart'; import '../../ui/adaptive/segmented_bar_overflow.dart'; import '../chat/ui/sessions/chat_adaptive.dart'; import 'terminal_hub.dart'; import 'terminal_scope.dart'; import 'terminal_toggle_button.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 openTerminalIfNone(TerminalHub hub) { if (hub.firstOpenDone) return Future.value(); return hub.firstOpening ??= _openFirst( hub, ).whenComplete(() => hub.firstOpening = null); } Future _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 open chat's own bar button that shows or hides the terminal, on a /// narrow layout. It is a plain [ChatBarAction] like Files and More, so it /// matches them in style, size and states. A wide layout returns null: its /// shell bar keeps the terminal button over the list column. Null too with /// no terminal surface. Widget? terminalChatBarAction(BuildContext context) { if (showsListBesideDetail(context)) return null; final actions = TerminalScope.maybeOf(context); if (actions == null) return null; return ChatBarAction( label: TerminalToggleButton.label, identifier: 'chat-terminal-toggle', onPressed: actions.toggle, materialIcon: Icons.terminal, ); } /// 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('chat-open-terminal-action'), overflowEntry: SegmentedOverflowEntry( label: terminalOpenActionLabel, icon: Icons.terminal, onPressed: () => openInTerminal(context, gateway, directory, onRefused: onRefused), ), child: const SizedBox.shrink(), ); }