Files
pipelines/app/lib/features/settings/design_quick_switch_match.dart
T
alan 4268ce20eb Design switcher: Alt+L lists every design, previews live, Enter keeps it
The chat switcher's popup and keys over the AppDesign registry: filter,
Ctrl+N/P and arrows preview the selected design app-wide without storing
it (AppDesignController.preview), Enter keeps it, Esc or Alt+L again
returns to the design it opened with. The current design is checked.
Listed in Settings -> Keyboard shortcuts and the cheatsheet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:10:19 +00:00

47 lines
1.6 KiB
Dart

import '../../ui/theme/app_design.dart';
/// [designs] narrowed to [query] and ranked, best first, for the design
/// switcher (Alt+L).
///
/// Every whitespace-separated word of the query must occur, ignoring case, in
/// the design's name or its one-line description. A word ranks by where it
/// matched: the start of the name, anywhere in the name, the start of a word
/// in the description, anywhere in it. A design's rank is the sum over the
/// query's words; equal ranks keep [designs]' order (the registry's).
List<AppDesign> designQuickSwitchMatches(
List<AppDesign> designs,
String query,
) {
final words = query.toLowerCase().split(RegExp(r'\s+'))
..removeWhere((word) => word.isEmpty);
if (words.isEmpty) return designs;
final ranked = <({int rank, int index, AppDesign design})>[];
for (final (index, design) in designs.indexed) {
final name = design.label.toLowerCase();
final description = design.description.toLowerCase();
var rank = 0;
for (final word in words) {
final tier = name.startsWith(word)
? 0
: name.contains(word)
? 1
: RegExp(
'(^|[^\\p{L}\\p{N}])${RegExp.escape(word)}',
unicode: true,
).hasMatch(description)
? 2
: description.contains(word)
? 3
: null;
if (tier == null) {
rank = -1;
break;
}
rank += tier;
}
if (rank >= 0) ranked.add((rank: rank, index: index, design: design));
}
ranked.sort((a, b) => a.rank != b.rank ? a.rank - b.rank : a.index - b.index);
return [for (final hit in ranked) hit.design];
}