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

51 lines
1.5 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../core/api_gateway.dart';
import '../../ui/adaptive/segmented_bar_overflow.dart';
import 'terminal_scope.dart';
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(),
);
}