From 3e066622d86a6fc8592ce0d9c5a7674a54ba6217 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 3 Dec 2026 22:04:02 +0000 Subject: [PATCH 1/2] Send button: fixed size in every design and a clear 3px gap to its usage ring Co-Authored-By: Claude Opus 5.5 --- .../chat/conversation/chat_composer_bar.dart | 10 +++--- .../conversation/chat_send_usage_ring.dart | 31 +++++++++++++------ .../chat_composer_focus_ring_test.dart | 6 +++- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 863ea02d..415dc12c 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -1394,12 +1394,10 @@ class _ChatComposerBarState extends State { child: IconButton.filled( onLongPress: open, key: const Key('chat-composer-primary'), - style: tinted == null - ? null - : IconButton.styleFrom( - backgroundColor: tinted.primary, - foregroundColor: tinted.onPrimary, - ), + style: chatSendButtonStyle( + background: tinted?.primary, + foreground: tinted?.onPrimary, + ), onPressed: enabled ? press : null, icon: const Icon(Icons.arrow_upward), ), diff --git a/app/lib/features/chat/conversation/chat_send_usage_ring.dart b/app/lib/features/chat/conversation/chat_send_usage_ring.dart index 27f7e7f9..b190dcf2 100644 --- a/app/lib/features/chat/conversation/chat_send_usage_ring.dart +++ b/app/lib/features/chat/conversation/chat_send_usage_ring.dart @@ -132,15 +132,23 @@ class _ChatSendUsageRingState extends State { } } -/// The filled send button's visible circle: a Material 3 icon button's 40 -/// logical pixels adjusted by its visual density, inside a target that may -/// be padded beyond it. -double chatSendButtonDiameter(BuildContext context) { - final density = - IconButtonTheme.of(context).style?.visualDensity ?? - Theme.of(context).visualDensity; - return 40 + density.baseSizeAdjustment.dx; -} +/// The send button's visible size in every design: the composer pins it +/// ([chatSendButtonStyle]), so the ring's gap is the same everywhere. +const double chatSendButtonSize = 40; + +/// The send button's fixed size, whatever the design's density. +ButtonStyle chatSendButtonStyle({Color? background, Color? foreground}) => + IconButton.styleFrom( + backgroundColor: background, + foregroundColor: foreground, + fixedSize: const Size.square(chatSendButtonSize), + minimumSize: const Size.square(chatSendButtonSize), + maximumSize: const Size.square(chatSendButtonSize), + padding: EdgeInsets.zero, + ); + +/// The filled send button's visible size. +double chatSendButtonDiameter(BuildContext context) => chatSendButtonSize; /// The ring: a faint full track and the used arc clockwise from the top, a /// hairline gap outside the button's circle of diameter [circle], centred @@ -164,7 +172,10 @@ class ChatUsageRingPainter extends CustomPainter { final double? cornerRadius; static const double stroke = 2; - static const double gap = 1; + + /// A clear gap between the button and the ring, so the ring reads as its + /// own feature. + static const double gap = 3; /// The ring's centre-line radius. double get radius => circle / 2 + gap + stroke / 2; diff --git a/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart b/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart index aa8f772b..ab70b1f2 100644 --- a/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart +++ b/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart @@ -103,7 +103,11 @@ void main() { await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); final send = find.byKey(const Key('chat-composer-primary')); - expect(tester.widget(send).style, isNull); + // Only the fixed size is set: no colours, so the host's filled colours + // apply. + final style = tester.widget(send).style!; + expect(style.backgroundColor, isNull); + expect(style.foregroundColor, isNull); expect(chatSchemeOf(tester.element(send), null), isNull); }); From 4268ce20eb23c773e6df3c8c88cecd74f28747cc Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 3 Dec 2026 22:10:19 +0000 Subject: [PATCH 2/2] 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 --- app/lib/application/pipelines_app.dart | 5 + .../settings/design_quick_switch.dart | 272 ++++++++++++++++++ .../settings/design_quick_switch_match.dart | 46 +++ .../settings/keyboard_shortcuts_registry.dart | 8 + app/lib/shell/adaptive_shell.dart | 12 + app/lib/shell/shell_design_shortcut.dart | 35 +++ app/lib/ui/theme/app_design_controller.dart | 25 +- .../settings/design_quick_switch_test.dart | 219 ++++++++++++++ .../shell/shell_design_shortcut_test.dart | 49 ++++ .../ui/theme/app_design_controller_test.dart | 27 ++ 10 files changed, 692 insertions(+), 6 deletions(-) create mode 100644 app/lib/features/settings/design_quick_switch.dart create mode 100644 app/lib/features/settings/design_quick_switch_match.dart create mode 100644 app/lib/shell/shell_design_shortcut.dart create mode 100644 app/test/features/settings/design_quick_switch_test.dart create mode 100644 app/test/shell/shell_design_shortcut_test.dart diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index bfdf0b82..70a86128 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -38,6 +38,7 @@ import '../features/artifacts/domain/artifact_record.dart'; import '../features/artifacts/ui/artifact_viewer_page.dart'; import '../features/artifacts/domain/gallery_navigation.dart'; import '../features/search/domain/search_target.dart'; +import '../features/settings/design_quick_switch.dart'; import '../features/settings/settings_screen.dart'; import '../features/settings/settings_store.dart'; import '../features/walkthrough/first_run_walkthrough.dart'; @@ -915,6 +916,9 @@ class _PipelinesAppState extends State if (!_chatRuntime.quickSwitch.toggle()) _focusOpenChatComposer(); } + /// Alt+L: the design switcher, over whatever is shown. + final DesignQuickSwitchToggle _designSwitch = DesignQuickSwitchToggle(); + /// Alt+N / Alt+J: the shell has shown Chat; once it has built (a first /// visit mounts the chat screen), the chat screen acts. void _chatAction(ShellChatAction action) { @@ -1191,6 +1195,7 @@ class _PipelinesAppState extends State onOpenChat: _focusOpenChatComposer, onChatQuickSwitch: _toggleChatQuickSwitch, onChatAction: _chatAction, + onDesignSwitch: _designSwitch.toggle, destinationBodies: { AppDestination.pipelines: (_) => FirstRunWalkthroughGate( store: _walkthroughStore, diff --git a/app/lib/features/settings/design_quick_switch.dart b/app/lib/features/settings/design_quick_switch.dart new file mode 100644 index 00000000..dc533839 --- /dev/null +++ b/app/lib/features/settings/design_quick_switch.dart @@ -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? _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 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 route = usesDesktopPopups(context) + ? DialogRoute( + 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( + 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 createState() => _DesignQuickSwitchState(); +} + +class _DesignQuickSwitchState extends State { + final _query = TextEditingController(); + final _rowKeys = {}; + + /// The stored design when the switcher opened: marked, and what closing + /// without a choice returns to. + late final AppDesign _current = widget.designs.chosen; + List _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, + ); + } +} diff --git a/app/lib/features/settings/design_quick_switch_match.dart b/app/lib/features/settings/design_quick_switch_match.dart new file mode 100644 index 00000000..0af613b5 --- /dev/null +++ b/app/lib/features/settings/design_quick_switch_match.dart @@ -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 designQuickSwitchMatches( + List 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]; +} diff --git a/app/lib/features/settings/keyboard_shortcuts_registry.dart b/app/lib/features/settings/keyboard_shortcuts_registry.dart index dbbf6a45..5161df77 100644 --- a/app/lib/features/settings/keyboard_shortcuts_registry.dart +++ b/app/lib/features/settings/keyboard_shortcuts_registry.dart @@ -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', diff --git a/app/lib/shell/adaptive_shell.dart b/app/lib/shell/adaptive_shell.dart index 8bef446f..3e9d1324 100644 --- a/app/lib/shell/adaptive_shell.dart +++ b/app/lib/shell/adaptive_shell.dart @@ -16,6 +16,7 @@ import 'shell_chat_actions_shortcut.dart'; import 'shell_chat_shortcut.dart'; import 'shell_cheatsheet.dart'; import 'shell_cheatsheet_shortcut.dart'; +import 'shell_design_shortcut.dart'; import 'shell_list_shortcuts.dart'; import 'shell_runs_pipelines_shortcut.dart'; import 'shell_search_shortcut_labels.dart'; @@ -35,6 +36,7 @@ class AdaptiveShell extends StatefulWidget { this.onOpenChat, this.onChatQuickSwitch, this.onChatAction, + this.onDesignSwitch, this.attentionCount = 0, this.runningRunCount, this.listPaneWidthStore = const SharedPreferencesListPaneWidthStore(), @@ -84,6 +86,11 @@ class AdaptiveShell extends StatefulWidget { /// needing the operator. Null leaves both chords unhandled. final ValueChanged? onChatAction; + /// Runs on Alt+L (see [shellDesignShortcut]) from any destination: the host + /// toggles its design switcher, shown from the given context. Null leaves + /// the chord unhandled. + final ValueChanged? onDesignSwitch; + /// Runs needing the operator today, retained in the tab's accessible label. final int attentionCount; @@ -419,6 +426,11 @@ class _AdaptiveShellState extends State { } // A terminal keeps its keys (Escape, Ctrl+/, Ctrl+digits, Alt+N, Alt+J). if (primaryFocusIsTypingSurface()) return false; + final designSwitch = widget.onDesignSwitch; + if (designSwitch != null && shellDesignShortcut(event, keyboard)) { + if (event is KeyDownEvent) designSwitch(context); + return true; + } final chatAction = widget.onChatAction == null ? null : shellChatActionShortcut(event, keyboard); diff --git a/app/lib/shell/shell_design_shortcut.dart b/app/lib/shell/shell_design_shortcut.dart new file mode 100644 index 00000000..372c9e61 --- /dev/null +++ b/app/lib/shell/shell_design_shortcut.dart @@ -0,0 +1,35 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +/// Whether [event] opens or closes the design switcher: Alt+L (Option+L on +/// a Mac, L for look) with no other modifier, from any destination. Option+L +/// types a symbol (¬) on a Mac, so the key is matched by position as well. +/// Repeats of a held chord match too, so the caller can swallow them; only +/// the first press acts. +/// +/// The chord sits beside Alt+K (the chat switcher) and works the same way. +/// Alt+T (the terminal) and Alt+D (the browser's address bar) are taken, and +/// no Ctrl or Cmd chord is free for it: Ctrl+L and Cmd+L are the browser's +/// address bar and the terminal's clear screen, Ctrl+Alt+T and Ctrl+Alt+L +/// open a terminal and lock the screen on Linux desktops. No browser, +/// GNOME, KDE or macOS takes Alt+L. The terminal keeps it for readline +/// (downcase-word), so the caller leaves it to the terminal while it has the +/// keyboard. +bool shellDesignShortcut(KeyEvent event, HardwareKeyboard keyboard) { + if (event is KeyUpEvent || + !keyboard.isAltPressed || + keyboard.isControlPressed || + keyboard.isMetaPressed || + keyboard.isShiftPressed) { + return false; + } + return event.logicalKey == LogicalKeyboardKey.keyL || + event.physicalKey == PhysicalKeyboardKey.keyL; +} + +/// The chord as it reads on [platform], for the help page. +String shellDesignShortcutLabel([TargetPlatform? platform]) { + final target = platform ?? defaultTargetPlatform; + final apple = target == TargetPlatform.macOS || target == TargetPlatform.iOS; + return apple ? 'Option+L' : 'Alt+L'; +} diff --git a/app/lib/ui/theme/app_design_controller.dart b/app/lib/ui/theme/app_design_controller.dart index baf81fe5..0dc9ed66 100644 --- a/app/lib/ui/theme/app_design_controller.dart +++ b/app/lib/ui/theme/app_design_controller.dart @@ -72,10 +72,23 @@ class AppDesignController extends ChangeNotifier { final AppDesignStore _store; AppDesign _design = AppDesign.defaultDesign; + AppDesign? _preview; int _revision = 0; bool _saveFailed = false; - AppDesign get design => _design; + /// The design shown: a [preview] while there is one, else the choice. + AppDesign get design => _preview ?? _design; + + /// The stored choice, whatever a [preview] shows meanwhile. + AppDesign get chosen => _design; + + /// Shows [design] app-wide without storing it (the design switcher's live + /// preview); null returns to the choice. [select] ends a preview. + void preview(AppDesign? design) { + final shown = this.design; + _preview = design; + if (this.design != shown) notifyListeners(); + } /// The last choice could not be stored on this device. bool get saveFailed => _saveFailed; @@ -88,17 +101,17 @@ class AppDesignController extends ChangeNotifier { final design = AppDesign.fromId(id); if (design == _design) return; _design = design; - notifyListeners(); + if (_preview == null) notifyListeners(); } /// Applies [design] now and stores it. Future select(AppDesign design) async { _revision++; final revision = _revision; - if (design != _design) { - _design = design; - notifyListeners(); - } + final shown = this.design; + _preview = null; + _design = design; + if (design != shown) notifyListeners(); final saved = await _store.save(design.id); if (revision != _revision || saved != _saveFailed) return; _saveFailed = !saved; diff --git a/app/test/features/settings/design_quick_switch_test.dart b/app/test/features/settings/design_quick_switch_test.dart new file mode 100644 index 00000000..087f3d21 --- /dev/null +++ b/app/test/features/settings/design_quick_switch_test.dart @@ -0,0 +1,219 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/design_quick_switch.dart'; +import 'package:pipelines_app/features/settings/design_quick_switch_match.dart'; +import 'package:pipelines_app/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; +import 'package:pipelines_app/ui/input/keyboard_owner.dart'; +import 'package:pipelines_app/ui/theme/app_design.dart'; +import 'package:pipelines_app/ui/theme/app_design_controller.dart'; +import 'package:pipelines_app/ui/theme/app_design_tokens.dart'; + +final _field = find.byKey(const Key('design-quick-switch-field')); +Finder _row(AppDesign design) => + find.byKey(Key('design-quick-switch-row-${design.id}')); +bool _selected(WidgetTester tester, AppDesign design) => + tester.widget(_row(design)).selected; + +class _App { + _App(this.store) : designs = AppDesignController(store: store); + final MemoryAppDesignStore store; + final AppDesignController designs; + final terminal = FocusNode(); + + /// The design the app's theme is drawn in, read from the shell's context. + AppDesign shown(WidgetTester tester) => + AppDesignTokens.of(tester.element(find.byKey(const Key('body')))).design; +} + +Future<_App> _pump(WidgetTester tester, {String? stored}) async { + tester.view.physicalSize = const Size(1200, 800); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final app = _App(MemoryAppDesignStore(stored)); + addTearDown(app.terminal.dispose); + await app.designs.restore(); + final toggle = DesignQuickSwitchToggle(designs: app.designs); + await tester.pumpWidget( + ListenableBuilder( + listenable: app.designs, + builder: (context, _) => MaterialApp( + theme: app.designs.design.light(), + themeAnimationDuration: Duration.zero, + home: AdaptiveShell( + onDesignSwitch: toggle.toggle, + destinationBodies: { + AppDestination.pipelines: (_) => TypingSurface( + child: Focus( + focusNode: app.terminal, + child: const Text('body', key: Key('body')), + ), + ), + }, + ), + ), + ), + ); + return app; +} + +Future _chord( + WidgetTester tester, + LogicalKeyboardKey modifier, + LogicalKeyboardKey key, +) async { + await tester.sendKeyDownEvent(modifier); + await tester.sendKeyEvent(key); + await tester.sendKeyUpEvent(modifier); + await tester.pumpAndSettle(); +} + +Future _altL(WidgetTester tester) => + _chord(tester, LogicalKeyboardKey.altLeft, LogicalKeyboardKey.keyL); + +void main() { + test('a query narrows the designs by name, then summary', () { + expect(designQuickSwitchMatches(AppDesign.values, ''), AppDesign.values); + expect(designQuickSwitchMatches(AppDesign.values, 'PAP'), [ + AppDesign.paper, + ]); + expect(designQuickSwitchMatches(AppDesign.values, 'mono'), [ + AppDesign.terminal, + ]); + expect(designQuickSwitchMatches(AppDesign.values, 'paper serif'), [ + AppDesign.paper, + ]); + expect(designQuickSwitchMatches(AppDesign.values, 'zzz'), isEmpty); + }); + + testWidgets('Alt+L lists every design with its swatch, the current one ' + 'marked and selected; Alt+L again closes it', (tester) async { + final app = await _pump(tester); + await _altL(tester); + expect(_field, findsOneWidget); + for (final design in AppDesign.values) { + expect(_row(design), findsOneWidget); + expect( + find.descendant(of: _row(design), matching: find.text(design.label)), + findsOneWidget, + ); + expect( + find.descendant( + of: _row(design), + matching: find.text(design.description), + ), + findsOneWidget, + ); + } + expect(find.byType(ListTile), findsNWidgets(AppDesign.values.length)); + expect( + find.descendant( + of: _row(AppDesign.terminal), + matching: find.byKey(const Key('design-quick-switch-current')), + ), + findsOneWidget, + ); + expect(find.byKey(const Key('design-quick-switch-current')), findsOne); + expect(_selected(tester, AppDesign.terminal), isTrue); + + await _altL(tester); + expect(_field, findsNothing); + expect(app.shown(tester), AppDesign.terminal); + }); + + testWidgets('Ctrl+N and Ctrl+P preview live without storing; Esc goes back ' + 'to the design it opened with', (tester) async { + final app = await _pump(tester); + await _altL(tester); + final order = AppDesign.values; + final start = order.indexOf(AppDesign.terminal); + final next = order[(start + 1) % order.length]; + final previous = order[(start - 1) % order.length]; + + await _chord( + tester, + LogicalKeyboardKey.controlLeft, + LogicalKeyboardKey.keyN, + ); + expect(_selected(tester, next), isTrue); + expect(app.shown(tester), next); + expect(app.designs.chosen, AppDesign.terminal); + expect(app.store.id, isNull); + + await _chord( + tester, + LogicalKeyboardKey.controlLeft, + LogicalKeyboardKey.keyP, + ); + await _chord( + tester, + LogicalKeyboardKey.controlLeft, + LogicalKeyboardKey.keyP, + ); + expect(_selected(tester, previous), isTrue); + expect(app.shown(tester), previous); + + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(_field, findsNothing); + expect(app.shown(tester), AppDesign.terminal); + expect(app.designs.design, AppDesign.terminal); + expect(app.store.id, isNull); + }); + + testWidgets('a filter selects the best match and Enter keeps it', ( + tester, + ) async { + final app = await _pump(tester, stored: AppDesign.standard.id); + await _altL(tester); + expect(_selected(tester, AppDesign.standard), isTrue); + + await tester.enterText(_field, 'pap'); + await tester.pumpAndSettle(); + expect(find.byType(ListTile), findsOneWidget); + expect(_selected(tester, AppDesign.paper), isTrue); + expect(app.shown(tester), AppDesign.paper); + expect(app.store.id, AppDesign.standard.id); + + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pumpAndSettle(); + expect(_field, findsNothing); + expect(app.shown(tester), AppDesign.paper); + expect(app.designs.chosen, AppDesign.paper); + expect(app.store.id, AppDesign.paper.id); + + // Reopened, Paper is now the marked one. + await _altL(tester); + expect( + find.descendant( + of: _row(AppDesign.paper), + matching: find.byKey(const Key('design-quick-switch-current')), + ), + findsOneWidget, + ); + }); + + testWidgets('a filter matching nothing shows the design it opened with', ( + tester, + ) async { + final app = await _pump(tester); + await _altL(tester); + await tester.enterText(_field, 'pap'); + await tester.pumpAndSettle(); + expect(app.shown(tester), AppDesign.paper); + await tester.enterText(_field, 'zzz'); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('design-quick-switch-empty')), findsOne); + expect(app.shown(tester), AppDesign.terminal); + }); + + testWidgets('the terminal keeps Alt+L', (tester) async { + final app = await _pump(tester); + app.terminal.requestFocus(); + await tester.pump(); + await _altL(tester); + expect(_field, findsNothing); + }); +} diff --git a/app/test/shell/shell_design_shortcut_test.dart b/app/test/shell/shell_design_shortcut_test.dart new file mode 100644 index 00000000..ff2d0277 --- /dev/null +++ b/app/test/shell/shell_design_shortcut_test.dart @@ -0,0 +1,49 @@ +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/shell/shell_design_shortcut.dart'; + +KeyDownEvent _down(PhysicalKeyboardKey physical, LogicalKeyboardKey logical) => + KeyDownEvent( + physicalKey: physical, + logicalKey: logical, + timeStamp: Duration.zero, + ); + +void main() { + test('Alt+L alone is the design switcher, Option+L by position', () { + bool chord(Set held, KeyEvent event) => + shellDesignShortcut(event, _FakeKeyboard(held)); + final l = _down(PhysicalKeyboardKey.keyL, LogicalKeyboardKey.keyL); + const alt = LogicalKeyboardKey.altLeft; + expect(chord({alt}, l), isTrue); + expect(chord({}, l), isFalse); + expect(chord({alt, LogicalKeyboardKey.shiftLeft}, l), isFalse); + expect(chord({alt, LogicalKeyboardKey.controlLeft}, l), isFalse); + expect(chord({alt, LogicalKeyboardKey.metaLeft}, l), isFalse); + expect(chord({LogicalKeyboardKey.controlLeft}, l), isFalse); + expect(chord({LogicalKeyboardKey.metaLeft}, l), isFalse); + // Option+L types ¬ on a Mac. + final symbol = _down( + PhysicalKeyboardKey.keyL, + const LogicalKeyboardKey(0x00ac), + ); + expect(chord({alt}, symbol), isTrue); + final k = _down(PhysicalKeyboardKey.keyK, LogicalKeyboardKey.keyK); + expect(chord({alt}, k), isFalse); + final up = KeyUpEvent( + physicalKey: PhysicalKeyboardKey.keyL, + logicalKey: LogicalKeyboardKey.keyL, + timeStamp: Duration.zero, + ); + expect(chord({alt}, up), isFalse); + expect(shellDesignShortcutLabel(TargetPlatform.macOS), 'Option+L'); + expect(shellDesignShortcutLabel(TargetPlatform.linux), 'Alt+L'); + }); +} + +class _FakeKeyboard extends HardwareKeyboard { + _FakeKeyboard(this.pressed); + final Set pressed; + @override + bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key); +} diff --git a/app/test/ui/theme/app_design_controller_test.dart b/app/test/ui/theme/app_design_controller_test.dart index 7b34f241..2b7c6999 100644 --- a/app/test/ui/theme/app_design_controller_test.dart +++ b/app/test/ui/theme/app_design_controller_test.dart @@ -66,6 +66,33 @@ void main() { expect(designs.design, AppDesign.standard); }); + test('a preview shows a design without storing it; select ends it', () async { + final store = MemoryAppDesignStore('default'); + final designs = AppDesignController(store: store); + await designs.restore(); + var notified = 0; + designs.addListener(() => notified++); + designs.preview(AppDesign.paper); + expect(designs.design, AppDesign.paper); + expect(designs.chosen, AppDesign.standard); + expect(notified, 1); + designs.preview(AppDesign.paper); + expect(notified, 1); + designs.preview(null); + expect(designs.design, AppDesign.standard); + expect(notified, 2); + expect(store.id, 'default'); + + designs.preview(AppDesign.terminal); + await designs.select(AppDesign.terminal); + expect(notified, 3); + expect(designs.design, AppDesign.terminal); + expect(store.id, 'terminal'); + designs.preview(null); + expect(designs.design, AppDesign.terminal); + expect(notified, 3); + }); + test('shared preferences keep the choice per device', () async { SharedPreferences.setMockInitialValues({}); const store = SharedPreferencesAppDesignStore();