Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

8.3 KB
Raw Blame
import 'dart:async';

import 'package:harness/shared/theme/app_icons.dart';
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:harness/shared/theme/app_type.dart';

import '../shared/theme/app_theme.dart' show AppPalette;

import '../core/desktop_window.dart';
import '../shared/widgets/app_dialog.dart';
import '../shared/widgets/app_select_field.dart';
import '../state/app_state.dart';
import '../shortcuts/app_keymap.dart';
import '../state/swarm_catalog.dart';
import 'machines_panel.dart';
import 'remote_folder_picker.dart';
import 'clone_repository_dialog.dart';
import 'desktop_chrome.dart';
import 'terminal_name_prompt.dart';
import 'terminal_prompt.dart';

Future<String?> showSwarmRenameDialog(
  BuildContext context,
  String name, {
  AppKeymap? keymap,
}) => showTerminalPrompt<String>(
  context,
  keymap: keymap,
  builder: (_) => TerminalNamePrompt(
    title: 'Rename Tab',
    name: name,
    fieldKey: const Key('tab-rename-input'),
    fieldLabel: 'Tab name',
    maxLength: 80,
  ),
);

Future<SavedSwarmProject?> showSwarmProjectDialog(
  BuildContext context,
  AppNotifier notifier,
) => showAppDialog<SavedSwarmProject>(
  context: context,
  builder: (_) => _ProjectDialog(notifier: notifier),
);

class _ProjectDialog extends StatefulWidget {
  const _ProjectDialog({required this.notifier});
  final AppNotifier notifier;
  @override
  State<_ProjectDialog> createState() => _ProjectDialogState();
}

class _ProjectDialogState extends State<_ProjectDialog> {
  late String? machineId =
      (widget.notifier.machineStates.values
                  .where((m) => m.isLocalMachine)
                  .firstOrNull ??
              widget.notifier.machineStates.values.firstOrNull)
          ?.machine
          .machineId;
  String? path;
  String? error;
  bool picking = false;
  int _machineRevision = 0;
  String get folderName =>
      (path
                  ?.split(RegExp(r'[/\\]'))
                  .where((part) => part.isNotEmpty)
                  .lastOrNull ??
              path ??
              '')
          .characters
          .take(80)
          .join();

  Future<void> browse() async {
    final id = machineId;
    if (id == null || picking) return;
    final revision = _machineRevision;
    setState(() {
      picking = true;
      error = null;
    });
    try {
      final folder = widget.notifier.stateOf(id)?.isLocalMachine == true
          ? await whileNativePicker(
              () => getDirectoryPath(initialDirectory: path),
            )
          : await showRemoteFolderPicker(
              context,
              notifier: widget.notifier,
              machineId: id,
              initialPath: path,
              desktop: true,
            );
      if (!mounted || revision != _machineRevision) return;
      setState(() {
        path = folder ?? path;
      });
    } catch (_) {
      if (mounted && revision == _machineRevision) {
        setState(
          () =>
              error = 'Could not browse this machine. Reconnect and try again.',
        );
      }
    } finally {
      if (mounted) setState(() => picking = false);
    }
  }

  Future<void> clone() async {
    if (picking) return;
    final revision = _machineRevision;
    setState(() => picking = true);
    final folder = await showCloneRepositoryDialog(
      context,
      initialFolder: path,
    );
    if (!mounted) return;
    setState(() {
      picking = false;
      if (folder != null && revision == _machineRevision) {
        path = folder;
        error = null;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      shape: DesktopChrome.bareDialogShape,
      insetPadding: const EdgeInsets.all(24),
      child: SizedBox(
        width: 508,
        child: DesktopDialogSurface(
          child: Padding(
            padding: const EdgeInsets.all(24),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text('Add project', style: DesktopChrome.heading()),
                const SizedBox(height: 20),
                Flexible(
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Choose an existing working folder.',
                          style: AppType.body(color: AppPalette.textSecondary),
                        ),
                        const SizedBox(height: 20),
                        if (machineId != null)
                          AppSelectField<String>(
                            value: machineId!,
                            options: [
                              for (final machine
                                  in widget.notifier.machineStates.values)
                                SelectOption(
                                  value: machine.machine.machineId,
                                  label: machine.isLocalMachine
                                      ? 'This computer'
                                      : machine.machine.displayName,
                                ),
                            ],
                            onChanged: (value) => setState(() {
                              if (machineId == value) return;
                              _machineRevision++;
                              machineId = value;
                              path = null;
                              error = null;
                            }),
                          ),
                        const SizedBox(height: 16),
                        OutlinedButton.icon(
                          onPressed: machineId == null || picking
                              ? null
                              : browse,
                          icon: const Icon(AppIcons.folderOpen, size: 16),
                          label: Text(
                            path ?? 'Choose folder',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        if (widget.notifier
                                .stateOf(machineId ?? '')
                                ?.isLocalMachine ==
                            true)
                          TextButton(
                            onPressed: picking ? null : clone,
                            child: const Text('Clone repository…'),
                          ),
                        if (error != null)
                          Text(
                            error!,
                            style: AppType.body(
                              color: Theme.of(context).colorScheme.error,
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
                const SizedBox(height: 24),
                OverflowBar(
                  alignment: MainAxisAlignment.end,
                  spacing: 8,
                  overflowSpacing: 8,
                  children: [
                    TextButton(
                      onPressed: () => Navigator.pop(context),
                      child: const Text('Cancel'),
                    ),
                    FilledButton(
                      onPressed: path == null || folderName.isEmpty || picking
                          ? null
                          : () => Navigator.pop(
                              context,
                              SavedSwarmProject(
                                machineId: machineId!,
                                path: path!,
                                name: folderName,
                              ),
                            ),
                      child: const Text('Add project'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// Compatibility entry point: every setup entry opens the same Machines panel.
Future<void> showSwarmLinkDialog(
  BuildContext context,
  AppNotifier notifier, {
  AppKeymap? keymap,
}) async {
  await showMachinesPanel(context, notifier, keymap: keymap);
}