4268ce20eb
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>
273 lines
8.9 KiB
Dart
273 lines
8.9 KiB
Dart
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,
|
|
);
|
|
}
|
|
}
|