Files
pipelines/app/lib/features/settings/keyboard_shortcuts_help.dart
T
alan 3b0da38f70 Redesign the keyboard shortcuts cheatsheet with fuzzy search
One fixed-size list of grouped cards: actions and notes on the leading
side, keys drawn as keycaps in an aligned trailing column that never ends
a line on a joining word. A search field, focused when Ctrl+? opens it,
filters fuzzily by action and keys (exactly by group and note) and
highlights the matched letters.
2026-11-12 15:02:11 +00:00

334 lines
10 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../../ui/adaptive/app_popups.dart';
import 'keyboard_shortcut_keys.dart';
import 'keyboard_shortcuts_filter.dart';
import 'keyboard_shortcuts_registry.dart';
const keyboardShortcutsLabel = 'Keyboard shortcuts';
/// Opens the cheatsheet: a fixed-size dialog on a desktop layout, a tall
/// bottom sheet on a phone. [focusSearch] puts the keyboard in its filter
/// field, as the Ctrl+? shortcut does.
Future<void> showKeyboardShortcutsHelp(
BuildContext context, {
bool focusSearch = false,
}) {
final help = KeyboardShortcutsHelp(focusSearch: focusSearch);
if (!usesDesktopPopups(context)) {
return showModalBottomSheet<void>(
context: context,
useRootNavigator: true,
showDragHandle: true,
isScrollControlled: true,
useSafeArea: true,
builder: (sheet) => Padding(
padding: EdgeInsets.only(bottom: MediaQuery.viewInsetsOf(sheet).bottom),
child: FractionallySizedBox(heightFactor: 0.92, child: help),
),
);
}
return showDialog<void>(
context: context,
useRootNavigator: true,
builder: (dialog) {
// A fixed size, so the dialog does not jump while the filter narrows it.
final height = math.min(MediaQuery.sizeOf(dialog).height * 0.85, 760.0);
return Dialog(
clipBehavior: Clip.antiAlias,
child: SizedBox(width: 720, height: height, child: help),
);
},
);
}
/// Every shortcut of this platform, grouped, with a fuzzy filter field above
/// them. It fills the height it is given: the header and field stay put and
/// the list scrolls under them.
class KeyboardShortcutsHelp extends StatefulWidget {
const KeyboardShortcutsHelp({
super.key,
this.registry,
this.focusSearch = false,
});
final KeyboardShortcutRegistry? registry;
final bool focusSearch;
@override
State<KeyboardShortcutsHelp> createState() => _KeyboardShortcutsHelpState();
}
class _KeyboardShortcutsHelpState extends State<KeyboardShortcutsHelp> {
final _query = TextEditingController();
final _field = FocusNode(debugLabel: 'keyboard-shortcuts-search');
@override
void dispose() {
_query.dispose();
_field.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final source = widget.registry ?? KeyboardShortcutRegistry.current();
return Semantics(
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
label: 'Keyboard shortcuts help',
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(24, 16, 12, 0),
child: Row(
children: [
Expanded(
child: Text(
keyboardShortcutsLabel,
style: theme.textTheme.headlineSmall,
),
),
IconButton(
tooltip: 'Close keyboard shortcuts',
onPressed: () => Navigator.of(context).maybePop(),
icon: const Icon(Icons.close),
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(24, 12, 24, 8),
child: ListenableBuilder(
listenable: _query,
builder: (context, _) => SearchBar(
key: const Key('keyboard-shortcuts-search'),
controller: _query,
focusNode: _field,
autoFocus: widget.focusSearch,
hintText: 'Search shortcuts',
elevation: const WidgetStatePropertyAll(0),
backgroundColor: WidgetStatePropertyAll(
theme.colorScheme.surfaceContainerHighest,
),
padding: const WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: 16),
),
leading: const Icon(Icons.search),
trailing: [
if (_query.text.isNotEmpty)
IconButton(
tooltip: 'Clear search',
onPressed: () {
_query.clear();
_field.requestFocus();
},
icon: const Icon(Icons.close),
),
],
),
),
),
Expanded(
child: ListenableBuilder(
listenable: _query,
builder: (context, _) => _ShortcutList(
matches: filterShortcuts(source.entries, _query.text),
query: _query.text.trim(),
),
),
),
],
),
);
}
}
/// The matching shortcuts under their group headings. Groups keep their
/// usual order; while filtering, the group with the best match comes first.
class _ShortcutList extends StatelessWidget {
const _ShortcutList({required this.matches, required this.query});
final List<ShortcutMatch> matches;
final String query;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (matches.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.search_off,
size: 32,
color: theme.colorScheme.onSurfaceVariant,
),
const SizedBox(height: 12),
Text(
'No shortcuts match “$query”',
textAlign: TextAlign.center,
style: theme.textTheme.bodyLarge,
),
],
),
),
);
}
final groups = <KeyboardShortcutGroup, List<ShortcutMatch>>{};
for (final match in matches) {
(groups[match.entry.group] ??= []).add(match);
}
final order = query.isEmpty
? KeyboardShortcutGroup.values.where(groups.containsKey).toList()
: groups.keys.toList();
return ListView(
key: const Key('keyboard-shortcuts-list'),
padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
children: [
for (final group in order)
_GroupCard(group: group, matches: groups[group]!),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text(
'On macOS, Option is shown for Alt. Browsers can reserve system '
'or tab shortcuts.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
],
);
}
}
class _GroupCard extends StatelessWidget {
const _GroupCard({required this.group, required this.matches});
final KeyboardShortcutGroup group;
final List<ShortcutMatch> matches;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(4, 16, 4, 8),
child: Text(
group.title,
style: theme.textTheme.titleSmall?.copyWith(
color: theme.colorScheme.primary,
),
),
),
DecoratedBox(
decoration: BoxDecoration(
border: Border.all(color: theme.colorScheme.outlineVariant),
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
for (final (index, match) in matches.indexed) ...[
if (index > 0)
Divider(
height: 1,
indent: 16,
endIndent: 16,
color: theme.colorScheme.outlineVariant,
),
_ShortcutRow(match: match),
],
],
),
),
],
);
}
}
/// The action and its note on the leading side; the keys in a column of
/// their own on the trailing side, so every row's keys line up.
class _ShortcutRow extends StatelessWidget {
const _ShortcutRow({required this.match});
final ShortcutMatch match;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final entry = match.entry;
return Semantics(
container: true,
label:
'${entry.action}: ${entry.keys}${entry.caveat == null ? '' : '. ${entry.caveat}'}',
child: ExcludeSemantics(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: LayoutBuilder(
builder: (context, constraints) => Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
_highlighted(
context,
entry.action,
match.actionMatches,
),
style: theme.textTheme.bodyLarge,
),
if (entry.caveat case final caveat?) ...[
const SizedBox(height: 2),
Text(
caveat,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
],
),
),
const SizedBox(width: 16),
SizedBox(
// The keys column is the same width on every row.
width: math.min(300, constraints.maxWidth * 0.45),
child: ShortcutKeys(keys: entry.keys),
),
],
),
),
),
),
);
}
TextSpan _highlighted(BuildContext context, String text, Set<int> marks) {
if (marks.isEmpty) return TextSpan(text: text);
final strong = TextStyle(
color: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w700,
);
return TextSpan(
children: [
for (final (index, char) in text.split('').indexed)
TextSpan(text: char, style: marks.contains(index) ? strong : null),
],
);
}
}