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.

10.4 KB
Raw Blame
import 'package:harness/shared/theme/app_icons.dart';
import 'package:flutter/material.dart';
import 'package:harness/terminal/terminal_text.dart';

import '../shared/theme/app_theme.dart' as grid;
import '../state/swarm_search.dart';
import '../terminal/terminal_theme.dart';
import '../terminal/terminal_theme_store.dart';
import 'box_chrome.dart';
import 'desktop_chrome.dart';

/// The shared input for the start page, Open Agent and split searches.
/// Flutter owns the caret and result navigation; native chrome only opens it.
class SwarmSearchInput extends StatelessWidget {
  const SwarmSearchInput({
    super.key,
    required this.inputKey,
    required this.controller,
    required this.focusNode,
    required this.search,
    required this.onClose,
    required this.onChanged,
    this.onOpen,
    this.groupId,
    this.onTapOutside,
    this.showClose = false,
    this.autofocus = true,
    this.hintText,
    this.rounded = false,
    this.prominent = false,
    this.outlined = false,
    this.fillColor,
    this.trailing,
    this.height,
    this.cursorWidth,
    this.prompt,
    this.terminal = false,
    this.bios = false,
  });

  final Key inputKey;
  final TextEditingController controller;
  final FocusNode focusNode;
  final SwarmSearchController? search;
  final VoidCallback onClose;
  final ValueChanged<String> onChanged;
  final VoidCallback? onOpen;
  final Object? groupId;
  final VoidCallback? onTapOutside;
  final bool showClose, autofocus;
  final String? hintText;
  final bool rounded;
  final bool prominent;
  final bool outlined;
  final Color? fillColor;
  final Widget? trailing;

  /// The input's height, when it is not the start page's 56 or 64: New
  /// Harness sizes its agent search to the tiles under it.
  final double? height;
  final double? cursorWidth;

  /// The typed text and the hint; the search glyph grows with it.
  double get fontSize => bios ? terminalFontStore.size : grid.AppType.monoSize;
  final String? prompt;

  /// Plain monospace input in a TerminalBox, without a decorative search glyph.
  final bool terminal;
  final bool bios;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return ListenableBuilder(
      // Command mode changes with the editor value. Result highlights do not,
      // so arrow navigation must not rebuild the text field.
      listenable: controller,
      builder: (context, _) => _buildInput(context),
    );
  }

  Widget _buildInput(BuildContext context) {
    if (DesktopChrome.of(context)) {
      return TextField(
        key: inputKey,
        groupId: groupId ?? EditableText,
        controller: controller,
        focusNode: focusNode,
        autofocus: autofocus,
        onChanged: onChanged,
        onTap: onOpen,
        onTapAlwaysCalled: true,
        onTapOutside: (_) => onTapOutside?.call(),
        autocorrect: false,
        enableSuggestions: false,
        style: DesktopChrome.text(size: 17),
        cursorColor: DesktopChrome.foreground,
        cursorWidth: 2,
        textAlignVertical: TextAlignVertical.center,
        decoration: InputDecoration(
          constraints: BoxConstraints.tightFor(
            height: MediaQuery.textScalerOf(context).scale(17) * 1.45 + 24,
          ),
          hintText: hintText ?? search?.hint ?? kSwarmSearchHint,
          hintStyle: DesktopChrome.text(size: 17, color: DesktopChrome.hint),
          hintMaxLines: 1,
          border: InputBorder.none,
          enabledBorder: InputBorder.none,
          focusedBorder: InputBorder.none,
          filled: false,
          isDense: true,
          contentPadding: const EdgeInsets.symmetric(vertical: 12),
        ),
      );
    }
    final open = search != null;
    final terminalStyle = terminal || prompt != null;
    final theme = terminalThemeFor(
      grid.AppTheme.palette.value,
      terminalThemeStore.value,
    );
    final style = bios
        ? terminalContentStyle(color: theme.foreground)
        : boxMonoStyle();
    final cell = bios ? terminalCellSizeOf(context) : Size.zero;
    final hint =
        search?.isCommandMode == true ||
            search?.isHelpMode == true ||
            search?.isGroupMode == true
        ? search!.hint
        : hintText ?? search?.hint ?? kSwarmSearchHint;
    final border = OutlineInputBorder(
      borderRadius: BorderRadius.vertical(
        top: Radius.circular(rounded ? (prominent ? 32 : 28) : 12),
        bottom: Radius.circular(
          open && !outlined
              ? 0
              : rounded
              ? (prominent ? 32 : 28)
              : 12,
        ),
      ),
      borderSide: outlined ? BorderSide(color: boxInk(.10)) : BorderSide.none,
    );
    final field = TextField(
      key: inputKey,
      groupId: groupId ?? EditableText,
      controller: controller,
      focusNode: focusNode,
      autofocus: autofocus,
      onTap: onOpen,
      onTapAlwaysCalled: true,
      onTapOutside: onTapOutside == null ? null : (_) => onTapOutside!(),
      onChanged: onChanged,
      style: terminalStyle ? style : grid.AppType.mono(color: boxText(1)),
      cursorColor: bios ? theme.cursor : grid.AppPalette.swarmAccent,
      cursorWidth: cursorWidth ?? (bios ? cell.width : 2),
      cursorRadius: Radius.zero,
      cursorOpacityAnimates: !bios,
      autocorrect: !bios,
      enableSuggestions: !bios,
      textAlignVertical: TextAlignVertical.center,
      decoration: bios
          ? InputDecoration(
              hintText: hint,
              hintStyle: style.copyWith(color: theme.muted),
              hintMaxLines: 1,
              isDense: true,
              isCollapsed: true,
              constraints: const BoxConstraints(),
              contentPadding: EdgeInsets.zero,
              border: InputBorder.none,
              enabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
              filled: false,
            )
          : InputDecoration(
              hintText: hint,
              hintStyle: terminalStyle
                  ? style.copyWith(color: kBoxFaint)
                  : grid.AppType.mono(color: boxText(.60)),
              hintMaxLines: 1,
              prefixIcon: prompt != null
                  ? Padding(
                      padding: const EdgeInsets.only(left: 14, right: 10),
                      child: Center(
                        widthFactor: 1,
                        heightFactor: 1,
                        child: Text(
                          prompt!,
                          key: const ValueKey('swarm-search-prompt'),
                          textAlign: TextAlign.center,
                          style: style.copyWith(
                            color: grid.AppPalette.swarmAccent,
                          ),
                        ),
                      ),
                    )
                  : terminal
                  ? null
                  : Icon(
                      AppIcons.search,
                      size: fontSize + 4,
                      color: boxText(.60),
                    ),
              prefixIconConstraints: BoxConstraints(
                minWidth: prompt != null
                    ? 36
                    : fontSize >= 20
                    ? 64
                    : 52,
                minHeight: height ?? (prominent ? 64 : 56),
              ),
              suffixIcon: showClose || trailing != null
                  ? Padding(
                      padding: const EdgeInsets.only(right: 12),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          ?trailing,
                          if (showClose)
                            TextButton(
                              onPressed: onClose,
                              style: TextButton.styleFrom(
                                foregroundColor: boxText(.60),
                                minimumSize: const Size(36, 28),
                              ),
                              child: Text(
                                'esc',
                                style: grid.AppType.monoMeta(),
                              ),
                            ),
                        ],
                      ),
                    )
                  : null,
              // TerminalBox owns the dock surface. An unfilled field also avoids
              // Material's extra inset, keeping the input aligned with result text.
              filled: !terminal,
              fillColor:
                  fillColor ??
                  (terminalStyle
                      ? grid.AppPalette.swarmField
                      : grid.AppPalette.swarmSearchSurface),
              hoverColor: Colors.transparent,
              contentPadding: EdgeInsets.symmetric(
                horizontal: 18,
                vertical: height == null
                    ? (prominent ? 22 : 18)
                    : ((height! - fontSize * 1.2) / 2).clamp(
                        0,
                        double.infinity,
                      ),
              ),
              isDense: true,
              border: terminalStyle ? InputBorder.none : border,
              enabledBorder: terminalStyle ? InputBorder.none : border,
              focusedBorder: terminalStyle ? InputBorder.none : border,
            ),
    );
    // Keep the field at the same depth when a prefix switches layouts so the
    // existing EditableText and its platform input connection can be retained.
    return Padding(
      // Keep the editable text aligned with result titles, without a separate
      // prompt. Resource prefixes are ordinary text in the controller.
      padding: bios
          ? EdgeInsets.fromLTRB(
              cell.width * (prompt == null ? 4 : 2),
              cell.height,
              cell.width * 2,
              cell.height,
            )
          : EdgeInsets.zero,
      child: SizedBox(
        height: bios ? cell.height : null,
        child: bios && prompt != null
            ? Row(
                crossAxisAlignment: CrossAxisAlignment.baseline,
                textBaseline: TextBaseline.alphabetic,
                children: [
                  SizedBox(
                    width: cell.width * 2,
                    child: Text(
                      prompt!,
                      key: const ValueKey('swarm-search-prompt'),
                      textAlign: TextAlign.center,
                      style: style,
                    ),
                  ),
                  Expanded(child: field),
                ],
              )
            : field,
      ),
    );
  }
}