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.

4.4 KB
Raw Blame
import 'dart:math' as math;

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

import '../shared/theme/app_theme.dart' as grid;
import 'app_shortcuts.dart';

/// One key, drawn as a key.
///
/// A chord printed as running text (`⇧⌘]`) is three glyphs the eye has to
/// separate before it can read them; drawn as caps it is three objects and
/// reads at a glance. It also gives the right-hand column a shape, which is
/// what a list of shortcuts is scanned by.
///
/// A recessed well, not a bordered box: §1 puts depth in the fill, and
/// [grid.AppSurface.wellFill] is an overlay, so a cap keeps its edge on a
/// raised card and on a recessed one without being picked for either.
class KeyCap extends StatelessWidget {
  const KeyCap(this.label, {super.key, this.textStyle});

  final String label;
  final TextStyle? textStyle;

  /// Square at a single glyph, so ⌘ and W sit in caps of the same size and the
  /// column stays a column. A longer label ("esc", "1 – 9") grows past it.
  static const double _minWidth = 22;
  static const double height = 22;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    final style = textStyle ?? grid.AppType.monoMeta();
    final iconSize = MediaQuery.textScalerOf(context).scale(style.fontSize!);
    final minEdge = math.max(height, iconSize + 8);
    return Container(
      // A minimum, not a fixed height: large text and a multi-stroke custom
      // binding must be able to grow without clipping their glyphs.
      constraints: BoxConstraints(
        minWidth: math.max(_minWidth, minEdge),
        minHeight: minEdge,
      ),
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
      decoration: BoxDecoration(
        color: grid.AppSurface.wellFill,
        borderRadius: BorderRadius.circular(6),
      ),
      // `Center(widthFactor: 1)`, never `Container(alignment:)` — a cap has to
      // be the width of the glyph on it, and an Align given a bounded width
      // FILLS it (`shrinkWrapWidth` is false unless a factor is set or the
      // constraint is infinite). The chord's Wrap bounds its children, so the
      // plain alignment blew every cap out to the whole row: one cap per line,
      // and a label squeezed to a character per line beside it.
      child: Center(
        widthFactor: 1,
        heightFactor: 1,
        child: label == '⇥' || label == '↵' || label == '⏎'
            ? Semantics(
                label: label == '⇥' ? 'Tab' : 'Return',
                child: Icon(
                  label == '⇥'
                      ? AppIcons.arrowRightToLine
                      : AppIcons.cornerDownLeft,
                  size: iconSize,
                  color: grid.AppPalette.textPrimary,
                ),
              )
            : Text(
                label,
                style: style.copyWith(
                  color: grid.AppPalette.textPrimary,
                  height: 1,
                  // Tabular so ⌘1 – ⌘9 and ⌘W keep the same cap width.
                  fontFeatures: const [FontFeature.tabularFigures()],
                ),
              ),
      ),
    );
  }
}

/// A whole chord — `⇧` `⌘` `]` — and, where an action has more than one, the
/// alternates after an "or".
///
/// The alternates are printed rather than hidden: `⌃⇥` is the chord people
/// arrive already knowing, and a screen that only lists `⌘]` teaches them the
/// app doesn't have the key they are about to press.
class KeyChordView extends StatelessWidget {
  const KeyChordView({super.key, required this.chords, this.textStyle});

  final List<KeyChord> chords;

  /// Shortcut help can follow the terminal while other app hints keep their scale.
  final TextStyle? textStyle;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return Wrap(
      spacing: 4,
      runSpacing: 4,
      crossAxisAlignment: WrapCrossAlignment.center,
      children: [
        for (var i = 0; i < chords.length; i++) ...[
          if (i > 0)
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 2),
              child: Text(
                'or',
                style: (textStyle ?? grid.AppType.caption()).copyWith(
                  color: grid.AppPalette.textFaint,
                ),
              ),
            ),
          for (final key in chords[i]) KeyCap(key, textStyle: textStyle),
        ],
      ],
    );
  }
}