Merge branch 'worktree-agent-ab5b11bc6eab1d077'

This commit is contained in:
2026-12-03 22:14:27 +00:00
10 changed files with 692 additions and 6 deletions
@@ -0,0 +1,272 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../ui/adaptive/app_popups.dart';
import '../../ui/theme/app_design.dart';
import '../../ui/theme/app_design_controller.dart';
import '../chat/conversation/chat_quick_switch_keys.dart';
import 'design_quick_switch_match.dart';
import 'settings_design_section.dart';
/// Opens and closes the design switcher for the app-wide shortcut (Alt+L):
/// a second press while it is open closes it, as Alt+K does the chat
/// switcher.
class DesignQuickSwitchToggle {
DesignQuickSwitchToggle({AppDesignController? designs})
: _designs = designs ?? AppDesignController.instance;
final AppDesignController _designs;
ModalRoute<AppDesign>? _route;
bool get isOpen => _route?.isActive ?? false;
void toggle(BuildContext context) {
final route = _route;
if (route != null && route.isActive) {
final navigator = route.navigator!;
route.isCurrent ? navigator.pop() : navigator.removeRoute(route);
_route = null;
return;
}
_route = showDesignQuickSwitch(context, designs: _designs);
}
}
/// Shows the design switcher: the chat switcher's popup (a search field with
/// the keyboard, Ctrl+N / Ctrl+P and Down / Up to move, wrapping, Enter or a
/// click to choose, Esc to close) over every registered [AppDesign], with its
/// swatch and one-line summary, the current one marked and selected.
///
/// Moving the selection previews that design app-wide at once
/// ([AppDesignController.preview], nothing stored); Enter or a click keeps it
/// ([AppDesignController.select]) and closing any other way returns to the
/// design the switcher opened with. The popup takes the app's theme rather
/// than the opener's, so it previews too.
ModalRoute<AppDesign> showDesignQuickSwitch(
BuildContext context, {
AppDesignController? designs,
}) {
final controller = designs ?? AppDesignController.instance;
final navigator = Navigator.of(context, rootNavigator: true);
Widget content(BuildContext context) =>
DesignQuickSwitch(designs: controller);
final ModalRoute<AppDesign> route = usesDesktopPopups(context)
? DialogRoute<AppDesign>(
context: navigator.context,
barrierLabel: 'Close the design switcher',
builder: (context) => Dialog(
alignment: const Alignment(0, -0.6),
clipBehavior: Clip.antiAlias,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 560,
maxHeight: MediaQuery.sizeOf(context).height * 0.7,
),
child: content(context),
),
),
)
: ModalBottomSheetRoute<AppDesign>(
builder: content,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
barrierLabel: 'Close the design switcher',
modalBarrierColor: Theme.of(
context,
).bottomSheetTheme.modalBarrierColor,
);
navigator.push(route).then((design) {
if (design == null) {
controller.preview(null);
} else {
controller.select(design);
}
});
return route;
}
/// The switcher's content: the field and the matching designs.
class DesignQuickSwitch extends StatefulWidget {
const DesignQuickSwitch({super.key, required this.designs});
final AppDesignController designs;
@override
State<DesignQuickSwitch> createState() => _DesignQuickSwitchState();
}
class _DesignQuickSwitchState extends State<DesignQuickSwitch> {
final _query = TextEditingController();
final _rowKeys = <AppDesign, GlobalKey>{};
/// The stored design when the switcher opened: marked, and what closing
/// without a choice returns to.
late final AppDesign _current = widget.designs.chosen;
List<AppDesign> _shown = AppDesign.values;
late int _selected = _shown.indexOf(_current);
@override
void dispose() {
_query.dispose();
super.dispose();
}
AppDesign? get _selection =>
_shown.isEmpty ? null : _shown[_selected.clamp(0, _shown.length - 1)];
/// With no query the current design is selected; with one, the best match.
void _queryChanged(String query) {
setState(() {
_shown = designQuickSwitchMatches(AppDesign.values, query);
_selected = query.trim().isEmpty ? _shown.indexOf(_current) : 0;
});
widget.designs.preview(_selection ?? _current);
}
/// Moves the selection [step] rows, wrapping, previews it and scrolls it
/// into view at the edge it moved towards.
void _step(int step) {
if (_shown.isEmpty) return;
setState(() => _selected = (_selected + step) % _shown.length);
final design = _selection!;
widget.designs.preview(design);
final policy = step < 0
? ScrollPositionAlignmentPolicy.keepVisibleAtStart
: ScrollPositionAlignmentPolicy.keepVisibleAtEnd;
WidgetsBinding.instance.addPostFrameCallback((_) {
final row = _rowKeys[design]?.currentContext;
if (row != null) Scrollable.ensureVisible(row, alignmentPolicy: policy);
});
}
void _choose([AppDesign? design]) {
final chosen = design ?? _selection;
if (chosen != null) Navigator.of(context).pop(chosen);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final field = Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Focus(
canRequestFocus: false,
skipTraversal: true,
onKeyEvent: (_, event) => chatQuickSwitchKey(
event,
HardwareKeyboard.instance,
onStep: _step,
onChoose: _choose,
),
child: TextField(
key: const Key('design-quick-switch-field'),
controller: _query,
autofocus: true,
onChanged: _queryChanged,
textInputAction: TextInputAction.go,
decoration: const InputDecoration(
hintText: 'Switch design',
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(),
isDense: true,
),
),
),
);
final Widget list = _shown.isEmpty
? const Padding(
padding: EdgeInsets.all(24),
child: Text(
'No designs match.',
key: Key('design-quick-switch-empty'),
textAlign: TextAlign.center,
),
)
: SingleChildScrollView(
padding: const EdgeInsets.only(bottom: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
for (final (index, design) in _shown.indexed)
DesignQuickSwitchRow(
key: _rowKeys.putIfAbsent(design, GlobalKey.new),
design: design,
brightness: theme.brightness,
selected: index == _selected,
current: design == _current,
onTap: () => _choose(design),
),
],
),
);
return Semantics(
label: 'Switch design',
explicitChildNodes: true,
child: Material(
type: MaterialType.transparency,
child: Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.viewInsetsOf(context).bottom,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
field,
Flexible(child: list),
],
),
),
),
);
}
}
/// One design in the switcher: its swatch, name and summary, and a check on
/// the [current] one. The selected row takes the selection fill, as in the
/// chat switcher.
class DesignQuickSwitchRow extends StatelessWidget {
const DesignQuickSwitchRow({
super.key,
required this.design,
required this.brightness,
required this.selected,
required this.current,
required this.onTap,
});
final AppDesign design;
final Brightness brightness;
final bool selected;
final bool current;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return ListTile(
key: Key('design-quick-switch-row-${design.id}'),
selected: selected,
selectedTileColor: scheme.secondaryContainer,
selectedColor: scheme.onSecondaryContainer,
visualDensity: VisualDensity.compact,
leading: DesignSwatchPreview(swatch: design.swatch(brightness)),
title: Text(design.label, maxLines: 1, overflow: TextOverflow.ellipsis),
subtitle: Text(
design.description,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
trailing: current
? Icon(
Icons.check,
key: const Key('design-quick-switch-current'),
color: selected ? null : scheme.primary,
semanticLabel: 'Current design',
)
: null,
onTap: onTap,
);
}
}
@@ -0,0 +1,46 @@
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];
}
@@ -9,6 +9,7 @@ import '../../shell/windows/window_shortcuts.dart';
import '../../shell/shell_chat_actions_shortcut.dart';
import '../../shell/shell_chat_shortcut.dart';
import '../../shell/shell_cheatsheet_shortcut.dart';
import '../../shell/shell_design_shortcut.dart';
import '../../shell/shell_runs_pipelines_shortcut.dart';
import '../../shell/shell_search_shortcut_labels.dart';
@@ -102,6 +103,13 @@ class KeyboardShortcutRegistry {
? 'From another destination: shows the open chat and focuses its message input. On Chat it opens the chat switcher. The browser may reserve Ctrl+K.'
: 'From another destination: shows the open chat and focuses its message input. On Chat it opens the chat switcher. In the terminal Ctrl+K stays kill-line; ${_apple ? 'Option+K and Cmd+K still open' : 'Alt+K still opens'} Chat.',
),
KeyboardShortcutEntry(
group: KeyboardShortcutGroup.navigation,
action: 'Switch design',
keys: shellDesignShortcutLabel(platform),
caveat:
'From any destination: every design with its preview, the current one marked. Type to filter, Ctrl+N or Down and Ctrl+P or Up to move (the app shows the selected design at once), Enter to keep it, Esc or the shortcut again to go back to the design you had. In the terminal the key goes to the terminal.${web ? ' A browser tab keeps Ctrl+N (new window) for itself; use Down, or the installed app.' : ''}',
),
const KeyboardShortcutEntry(
group: KeyboardShortcutGroup.navigation,
action: 'Switch between Runs and Pipelines',