import 'package:flutter/material.dart'; import '../../ui/adaptive/app_popups.dart'; import 'keyboard_shortcuts_registry.dart'; const keyboardShortcutsLabel = 'Keyboard shortcuts'; /// The narrowest content width (after the page padding) that shows the groups /// in two columns. Narrower content, such as a phone sheet, stacks them. const _twoColumnMinWidth = 520.0; Future showKeyboardShortcutsHelp(BuildContext context) => showAppDetails( context, maxWidth: 640, builder: (_) => const KeyboardShortcutsHelp(), ); class KeyboardShortcutsHelp extends StatelessWidget { const KeyboardShortcutsHelp({super.key, this.registry}); final KeyboardShortcutRegistry? registry; @override Widget build(BuildContext context) { final source = registry ?? KeyboardShortcutRegistry.current(); final groups = [ for (final group in KeyboardShortcutGroup.values) if (source.inGroup(group) case final entries when entries.isNotEmpty) (group: group, entries: entries), ]; final content = Semantics( scopesRoute: true, namesRoute: true, explicitChildNodes: true, label: 'Keyboard shortcuts help', child: Padding( padding: const EdgeInsets.fromLTRB(24, 8, 24, 24), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( children: [ Expanded( child: Text( keyboardShortcutsLabel, style: Theme.of(context).textTheme.headlineSmall, ), ), IconButton( tooltip: 'Close keyboard shortcuts', onPressed: () => Navigator.of(context).pop(), icon: const Icon(Icons.close), ), ], ), const SizedBox(height: 8), Text( 'Shortcuts available in the current app and browser environment.', style: Theme.of(context).textTheme.bodyMedium, ), const SizedBox(height: 16), _ShortcutGroups(groups: groups), const SizedBox(height: 4), Text( 'On macOS, Option is shown for Alt. Browsers can reserve system or tab shortcuts.', style: Theme.of(context).textTheme.bodySmall, ), ], ), ), ); // The desktop details dialog supplies its own bounded scroll view. The // narrow details sheet does not, so keep the help readable at large text // scales and on short phone screens there as well. return usesDesktopPopups(context) ? content : SingleChildScrollView(child: content); } } typedef _Group = ({ KeyboardShortcutGroup group, List entries, }); /// The groups in display order: one column when narrow, otherwise two. Each /// group goes to the shorter column so the two stay about the same height. class _ShortcutGroups extends StatelessWidget { const _ShortcutGroups({required this.groups}); final List<_Group> groups; @override Widget build(BuildContext context) => LayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth < _twoColumnMinWidth) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [for (final group in groups) _GroupBlock(group: group)], ); } final left = <_Group>[]; final right = <_Group>[]; var leftRows = 0; var rightRows = 0; for (final group in groups) { if (leftRows <= rightRows) { left.add(group); leftRows += group.entries.length + 1; } else { right.add(group); rightRows += group.entries.length + 1; } } return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: _GroupColumn(groups: left)), const SizedBox(width: 24), Expanded(child: _GroupColumn(groups: right)), ], ); }, ); } class _GroupColumn extends StatelessWidget { const _GroupColumn({required this.groups}); final List<_Group> groups; @override Widget build(BuildContext context) => Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [for (final group in groups) _GroupBlock(group: group)], ); } class _GroupBlock extends StatelessWidget { const _GroupBlock({required this.group}); final _Group group; @override Widget build(BuildContext context) => Padding( padding: const EdgeInsets.only(bottom: 12), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(group.group.title, style: Theme.of(context).textTheme.titleSmall), const SizedBox(height: 4), for (final (index, entry) in group.entries.indexed) ...[ if (index > 0) const Divider(height: 1), Padding( padding: const EdgeInsets.symmetric(vertical: 10), child: _ShortcutRow(entry: entry), ), ], ], ), ); } class _ShortcutRow extends StatelessWidget { const _ShortcutRow({required this.entry}); final KeyboardShortcutEntry entry; @override Widget build(BuildContext context) => Semantics( container: true, label: '${entry.action}: ${entry.keys}${entry.caveat == null ? '' : '. ${entry.caveat}'}', child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: Text(entry.action)), const SizedBox(width: 16), Flexible( child: Text( entry.keys, textAlign: TextAlign.end, style: const TextStyle(fontWeight: FontWeight.w600), ), ), ], ), if (entry.caveat case final caveat?) ...[ const SizedBox(height: 4), Text(caveat, style: Theme.of(context).textTheme.bodySmall), ], ], ), ); }