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.

9.3 KB
Raw Blame
import 'package:harness/shared/theme/app_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../shared/layouts/widgets/rail_section_header.dart';
import '../shared/layouts/widgets/sidebar_item.dart';
import '../shared/theme/app_theme.dart' as grid;
import '../shared/widgets/section_scaffold.dart';
import '../widgets/finger_target.dart';
import 'settings_section.dart';
import '../widgets/laid_out_list.dart';

/// The settings list: the way back, a field that narrows the list to what you
/// type, then one row per screen with the open one highlighted.
class SettingsNav extends StatefulWidget {
  const SettingsNav({
    super.key,
    required this.section,
    required this.onSelect,
    this.railWidth = SettingsNav.width,
    this.autofocusSearch = true,
    this.compact = false,
  });

  final SettingsSection section;
  final ValueChanged<SettingsSection> onSelect;

  /// [width] beside a section; the whole width where it is the whole screen
  /// (a phone's list of sections).
  final double railWidth;

  /// Whether the filter takes the keys on open. Not on a phone, where focus
  /// raises the on-screen keyboard over half the list.
  final bool autofocusSearch;

  /// True on a phone: the way back is a finger's size, and Keyboard
  /// shortcuts is left out.
  final bool compact;

  /// The workspace sidebar's width with its rule, so Settings opens without the
  /// left column jumping.
  static const double width = 276;

  @override
  State<SettingsNav> createState() => _SettingsNavState();
}

class _SettingsNavState extends State<SettingsNav> {
  String _query = '';
  final _search = TextEditingController();
  final _searchFocus = FocusNode(debugLabel: 'Search settings');

  @override
  void dispose() {
    _search.dispose();
    _searchFocus.dispose();
    super.dispose();
  }

  bool get _composing =>
      _search.value.composing.isValid && !_search.value.composing.isCollapsed;

  void _chooseMatch() {
    if (_composing || _query.trim().isEmpty) return;
    final match = _visible.expand((group) => group.sections).firstOrNull;
    if (match != null) widget.onSelect(match);
  }

  KeyEventResult _searchKey(FocusNode node, KeyEvent event) {
    if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
      return KeyEventResult.ignored;
    }
    final keys = HardwareKeyboard.instance;
    if (keys.isControlPressed ||
        keys.isMetaPressed ||
        keys.isAltPressed ||
        keys.isShiftPressed) {
      return KeyEventResult.ignored;
    }
    final key = event.logicalKey;
    final enter =
        key == LogicalKeyboardKey.enter ||
        key == LogicalKeyboardKey.numpadEnter;
    final down = key == LogicalKeyboardKey.arrowDown;
    final up = key == LogicalKeyboardKey.arrowUp;
    if (!enter && !down && !up) return KeyEventResult.ignored;
    if (_composing) return KeyEventResult.skipRemainingHandlers;
    if (enter) {
      if (event is KeyDownEvent) _chooseMatch();
    } else if (down) {
      if (_visible.isNotEmpty) _searchFocus.nextFocus();
    } else {
      _searchFocus.previousFocus();
    }
    return KeyEventResult.handled;
  }

  static List<SettingsGroup> _withoutKeyboardShortcuts(
    List<SettingsGroup> groups,
  ) => [
    for (final group in groups)
      SettingsGroup(group.title, [
        for (final section in group.sections)
          if (section != SettingsSection.shortcuts) section,
      ]),
  ];

  /// The groups, narrowed to the query. Matching is a plain case-insensitive
  /// substring of the label or a row's keywords ([SettingsSection.matches]) —
  /// this list is short, so anything cleverer would be machinery no one can
  /// feel. A group's own title matches too, so typing "help" surfaces the
  /// whole run rather than nothing.
  List<SettingsGroup> get _visible {
    final query = _query.trim().toLowerCase();
    // The list this build actually has: [settingsGroups] drops the developer
    // rows a release build does not ship. A phone has no keyboard to learn
    // (audit finding S8).
    final all = widget.compact
        ? _withoutKeyboardShortcuts(settingsGroups)
        : settingsGroups;
    if (query.isEmpty) return all;
    final groups = <SettingsGroup>[];
    for (final group in all) {
      if (group.title.toLowerCase().contains(query)) {
        groups.add(group);
        continue;
      }
      final rows = [
        for (final target in group.sections)
          if (target.matches(query)) target,
      ];
      if (rows.isNotEmpty) groups.add(SettingsGroup(group.title, rows));
    }
    return groups;
  }

  @override
  Widget build(BuildContext context) {
    // The rail and the section begin below the native title bar.
    grid.AppTheme.watch(context);
    return Container(
      width: widget.railWidth,
      color: grid.AppPalette.swarmWelcome,
      child: Padding(
        // Rows add their own icon gutter: their glyphs and group captions sit
        // on the same 24px content inset as the section on the right.
        padding: const EdgeInsets.symmetric(
          horizontal: SectionScaffold.contentPadding - SidebarItem.iconGutter,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const SizedBox(height: SectionScaffold.contentPadding),
            // A SidebarItem like the rows below, so the way out hovers,
            // highlights and aligns exactly like them instead of being a
            // shrink-wrapped button in its own grey.
            SizedBox(
              height: fingerHeight(
                SectionScaffold.headingHeight(context),
                enabled: widget.compact,
              ),
              child: Center(
                child: fingerTarget(
                  enabled: widget.compact,
                  SidebarItem(
                    key: const Key('settings-back-button'),
                    icon: AppIcons.arrowLeft,
                    label: 'Back to app',
                    onTap: () => Navigator.of(context).maybePop(),
                  ),
                ),
              ),
            ),
            const SizedBox(height: 12),
            Focus(
              onKeyEvent: _searchKey,
              skipTraversal: true,
              child: _SearchField(
                controller: _search,
                focusNode: _searchFocus,
                autofocus: widget.autofocusSearch,
                onChanged: (value) => setState(() => _query = value),
                onSubmitted: (_) => _chooseMatch(),
              ),
            ),
            const SizedBox(height: 8),
            Expanded(child: _navList()),
          ],
        ),
      ),
    );
  }

  Widget _navList() {
    final visible = _visible;
    if (visible.isEmpty) return const _NoMatches();
    return LaidOutListView(
      padding: const EdgeInsets.only(bottom: 10),
      children: [
        for (final group in visible) ...[
          if (group.title.isNotEmpty)
            RailSectionHeader(label: group.title)
          else if (group != visible.first)
            const SizedBox(height: 8),
          for (final target in group.sections)
            // On a phone a row is a finger's height; a mouse keeps 38.
            fingerTarget(
              enabled: widget.compact,
              SidebarItem(
                icon: target.icon,
                label: target.label,
                selected: target == widget.section,
                onTap: () => widget.onSelect(target),
              ),
            ),
        ],
      ],
    );
  }
}

/// The rail's filter, styled like the machine rail's own — the app has one
/// shape for "narrow this list".
class _SearchField extends StatelessWidget {
  const _SearchField({
    required this.controller,
    required this.focusNode,
    required this.autofocus,
    required this.onChanged,
    required this.onSubmitted,
  });

  final TextEditingController controller;
  final FocusNode focusNode;
  final bool autofocus;
  final ValueChanged<String> onChanged;
  final ValueChanged<String> onSubmitted;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    // Same as the rail's filter field: sized and filled by the theme at
    // AppControl.heightField (36) rather than clamped to a button's 32.
    return TextField(
      // Keyed because the Appearance pane now carries fields of its own, so a
      // test reaching for "the" TextField would find three.
      key: const Key('settings-search-field'),
      controller: controller,
      focusNode: focusNode,
      autofocus: autofocus,
      onChanged: onChanged,
      onSubmitted: onSubmitted,
      onEditingComplete: () {},
      textInputAction: TextInputAction.search,
      style: grid.kFieldTextStyle,
      decoration: InputDecoration(
        hintText: 'Search settings',
        prefixIcon: Icon(
          AppIcons.search,
          size: grid.kFieldIconSize,
          color: grid.AppPalette.textFaint,
        ),
      ),
    );
  }
}

/// What the rail shows when the query matches nothing — so a typo reads as "no
/// results" rather than a rail that mysteriously emptied.
class _NoMatches extends StatelessWidget {
  const _NoMatches();

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return Padding(
      padding: const EdgeInsets.fromLTRB(10, 16, 10, 0),
      child: Text(
        'No settings match',
        style: grid.AppType.body(color: grid.AppPalette.textFaint),
      ),
    );
  }
}