import 'package:flutter/material.dart'; /// The chords of a shortcut's [keys] text, as the registry writes them: /// alternatives separated by `, ` and the keys of one chord joined by `+` /// (`Option+K, Ctrl+K` is two chords of two keys each). List> shortcutChords(String keys) => [ for (final chord in keys.split(', ')) if (chord.trim().isNotEmpty) [ for (final key in chord.trim().split('+')) if (key.isNotEmpty) key, ], ]; /// A shortcut's keys drawn as keycaps. Each chord stays on one line; the /// chords wrap as a whole and line up at the trailing edge. Between two chords /// a muted word says how they relate: `then` for a key pressed again /// (`Esc, Esc`), otherwise `or`. class ShortcutKeys extends StatelessWidget { const ShortcutKeys({super.key, required this.keys}); final String keys; @override Widget build(BuildContext context) { final chords = shortcutChords(keys); final muted = Theme.of(context).textTheme.labelSmall?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ); return ExcludeSemantics( child: Wrap( alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, spacing: 6, runSpacing: 6, children: [ // The joining word travels with the chord after it, so a line // never ends on a dangling `or`. for (final (index, chord) in chords.indexed) index == 0 ? _Chord(keys: chord) : Wrap( crossAxisAlignment: WrapCrossAlignment.center, spacing: 6, runSpacing: 6, children: [ Text( _sameChord(chord, chords[index - 1]) ? 'then' : 'or', style: muted, ), _Chord(keys: chord), ], ), ], ), ); } static bool _sameChord(List a, List b) => a.length == b.length && a.indexed.every((e) => e.$2 == b[e.$1]); } /// One chord's keys side by side. A chord too wide for the keys column (very /// large text) breaks between its keys rather than overflowing. class _Chord extends StatelessWidget { const _Chord({required this.keys}); final List keys; @override Widget build(BuildContext context) => Wrap( alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, spacing: 3, runSpacing: 3, children: [for (final key in keys) ShortcutKeycap(label: key)], ); } /// One key, drawn as a small raised cap in the theme's surface colours. class ShortcutKeycap extends StatelessWidget { const ShortcutKeycap({super.key, required this.label}); final String label; @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; final edge = scheme.outlineVariant; return Container( constraints: const BoxConstraints(minWidth: 26), padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 3), decoration: BoxDecoration( color: scheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(6), border: Border( top: BorderSide(color: edge), left: BorderSide(color: edge), right: BorderSide(color: edge), // A heavier lower edge reads as a key, not a chip. bottom: BorderSide(color: edge, width: 2), ), ), child: Text( label, textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelMedium?.copyWith( color: scheme.onSurface, fontWeight: FontWeight.w600, fontFeatures: const [FontFeature.tabularFigures()], ), ), ); } }