From ccc80a9a8d8cc1c6fbf98ef57d6b722263731ec6 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 13 Nov 2026 15:51:00 +0000 Subject: [PATCH 1/3] Add keyboard navigation to search results Keep the search field focused while selecting results with Control or arrow keys, and open the selected result with Enter. Document the field-scoped shortcuts and cover navigation, reset, submission, and typing behavior with regression tests. --- app/lib/features/search/search_screen.dart | 63 ++++++ .../search/ui/material_search_body.dart | 6 + .../search/ui/search_bar_controls.dart | 3 + .../features/search/ui/search_result_row.dart | 14 +- .../search/ui/search_result_slivers.dart | 38 +++- .../settings/keyboard_shortcuts_registry.dart | 14 +- app/lib/ui/input/app_search_field.dart | 69 +++--- .../features/search/search_screen_test.dart | 196 ++++++++++++++++++ .../keyboard_shortcuts_cheatsheet_test.dart | 13 ++ 9 files changed, 372 insertions(+), 44 deletions(-) diff --git a/app/lib/features/search/search_screen.dart b/app/lib/features/search/search_screen.dart index 6451a7ea..e4973d9f 100644 --- a/app/lib/features/search/search_screen.dart +++ b/app/lib/features/search/search_screen.dart @@ -1,4 +1,7 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../../core/api_gateway.dart'; import '../../shell/shell_bar.dart'; @@ -6,6 +9,7 @@ import '../../shell/shell_search_focus.dart'; import '../../ui/adaptive/segmented_bar.dart'; import 'data/search_repository.dart'; import 'domain/search_controller.dart'; +import 'domain/search_row.dart'; import 'ui/material_search_body.dart'; import 'ui/search_bar_controls.dart'; import 'ui/search_bounds_note.dart'; @@ -38,6 +42,9 @@ class _SearchScreenState extends State { late final OmniSearchController _controller; late final TextEditingController _text; final FocusNode _focus = FocusNode(debugLabel: 'omni-search'); + final Map _rowKeys = {}; + var _selectedIndex = 0; + String _resultIdentity = ''; @override void initState() { @@ -57,9 +64,62 @@ class _SearchScreenState extends State { selection: TextSelection.collapsed(offset: query.length), ); } + final identity = jsonEncode({ + 'query': _controller.parsed.query, + 'kind': _controller.parsed.kind.wire, + 'results': _results, + }); + if (identity != _resultIdentity) { + _resultIdentity = identity; + _selectedIndex = 0; + } if (mounted) setState(() {}); } + List get _results => [ + ..._controller.shownChats, + ..._controller.shownResults, + ]; + + KeyEventResult _handleFieldKey(FocusNode _, KeyEvent event) { + if (event is! KeyDownEvent && event is! KeyRepeatEvent) { + return KeyEventResult.ignored; + } + final key = event.logicalKey; + var delta = 0; + final keyboard = HardwareKeyboard.instance; + if (!keyboard.isAltPressed && + !keyboard.isMetaPressed && + !keyboard.isShiftPressed && + (key == LogicalKeyboardKey.arrowDown || + (keyboard.isControlPressed && key == LogicalKeyboardKey.keyN))) { + delta = 1; + } else if (!keyboard.isAltPressed && + !keyboard.isMetaPressed && + !keyboard.isShiftPressed && + (key == LogicalKeyboardKey.arrowUp || + (keyboard.isControlPressed && key == LogicalKeyboardKey.keyP))) { + delta = -1; + } else if (key == LogicalKeyboardKey.enter && _results.isNotEmpty) { + final hit = _results[_selectedIndex.clamp(0, _results.length - 1)]; + if (searchRowView(hit, _controller.parsed.query).openable) { + widget.onOpen(hit); + } + return KeyEventResult.handled; + } else { + return KeyEventResult.ignored; + } + if (_results.isEmpty) return KeyEventResult.handled; + final next = (_selectedIndex + delta).clamp(0, _results.length - 1); + if (next == _selectedIndex) return KeyEventResult.handled; + setState(() => _selectedIndex = next); + WidgetsBinding.instance.addPostFrameCallback((_) { + final context = _rowKeys[next]?.currentContext; + if (context != null) Scrollable.ensureVisible(context); + }); + return KeyEventResult.handled; + } + void _clear() { _text.clear(); _controller.updateQuery(''); @@ -140,6 +200,7 @@ class _SearchScreenState extends State { onSubmitted: _controller.submit, onCleared: _clear, onKind: _controller.selectKind, + onKeyEvent: _handleFieldKey, ); /// The server's result and scan bounds, only when they cut the answer @@ -173,6 +234,8 @@ class _SearchScreenState extends State { query: _controller.parsed.query, onOpen: widget.onOpen, header: header, + selectedIndex: _selectedIndex, + rowKeys: _rowKeys, ), ); } diff --git a/app/lib/features/search/ui/material_search_body.dart b/app/lib/features/search/ui/material_search_body.dart index 60254304..a9e7ffb3 100644 --- a/app/lib/features/search/ui/material_search_body.dart +++ b/app/lib/features/search/ui/material_search_body.dart @@ -14,12 +14,16 @@ class MaterialSearchBody extends StatelessWidget { required this.query, required this.onOpen, required this.header, + required this.selectedIndex, + required this.rowKeys, }); final OmniSearchController controller; final String query; final ValueChanged onOpen; final Widget header; + final int selectedIndex; + final Map rowKeys; @override Widget build(BuildContext context) { @@ -55,6 +59,8 @@ class MaterialSearchBody extends StatelessWidget { query: query, onOpen: onOpen, message: (text) => _SearchMessage(message: text), + selectedIndex: selectedIndex, + rowKeys: rowKeys, ); case SearchBodyKind.empty: return [ diff --git a/app/lib/features/search/ui/search_bar_controls.dart b/app/lib/features/search/ui/search_bar_controls.dart index fba770f3..d18c5c8e 100644 --- a/app/lib/features/search/ui/search_bar_controls.dart +++ b/app/lib/features/search/ui/search_bar_controls.dart @@ -28,6 +28,7 @@ class SearchBarControls extends StatelessWidget { required this.onKind, this.autofocus = true, this.focusNode, + this.onKeyEvent, }); final TextEditingController text; @@ -38,6 +39,7 @@ class SearchBarControls extends StatelessWidget { final ValueChanged onKind; final bool autofocus; final FocusNode? focusNode; + final FocusOnKeyEventCallback? onKeyEvent; @override // The pill grows with the bar, so the field and the results share both @@ -55,6 +57,7 @@ class SearchBarControls extends StatelessWidget { controller: text, autofocus: autofocus, focusNode: focusNode, + onKeyEvent: onKeyEvent, inBar: true, placeholder: 'Search pipelines, runs, artifacts and chats', onChanged: onChanged, diff --git a/app/lib/features/search/ui/search_result_row.dart b/app/lib/features/search/ui/search_result_row.dart index 6cfd6a7d..2a4b0127 100644 --- a/app/lib/features/search/ui/search_result_row.dart +++ b/app/lib/features/search/ui/search_result_row.dart @@ -20,19 +20,26 @@ class SearchResultRow extends StatelessWidget { required this.query, required this.onOpen, this.divider = true, + this.selected = false, + this.visibilityKey, + this.rowKey, }); final JsonMap hit; final String query; final ValueChanged onOpen; final bool divider; + final bool selected; + final GlobalKey? visibilityKey; + final Key? rowKey; @override Widget build(BuildContext context) { final row = searchRowView(hit, query); final kind = '${hit['kind'] ?? ''}'; - return AppListRow( - key: ValueKey('search.result.${hit['target']}'), + final listRow = AppListRow( + key: rowKey ?? ValueKey('search.result.${hit['target']}'), + selected: selected, divider: divider, onTap: row.openable ? () => onOpen(hit) : null, leading: @@ -55,6 +62,9 @@ class SearchResultRow extends StatelessWidget { : null, menu: searchArtifactActions(context, hit), ); + return visibilityKey == null + ? listRow + : KeyedSubtree(key: visibilityKey, child: listRow); } TextSpan _line(SearchRowView row) => row.snippet == null diff --git a/app/lib/features/search/ui/search_result_slivers.dart b/app/lib/features/search/ui/search_result_slivers.dart index 7d4731fb..ff7cc211 100644 --- a/app/lib/features/search/ui/search_result_slivers.dart +++ b/app/lib/features/search/ui/search_result_slivers.dart @@ -20,19 +20,39 @@ List searchResultSlivers({ required String query, required ValueChanged onOpen, required Widget Function(String message) message, + required int selectedIndex, + required Map rowKeys, }) { final chats = controller.shownChats; final others = controller.shownResults; + final allHits = [...chats, ...others]; + final targetCounts = {}; + for (final hit in allHits) { + final target = '${hit['target'] ?? ''}'; + targetCounts[target] = (targetCounts[target] ?? 0) + 1; + } final grouped = controller.showsChatSection; final note = controller.chatsNote; - Widget rows(List hits) => SliverList.builder( + Widget rows(List hits, int offset) => SliverList.builder( itemCount: hits.length, - itemBuilder: (context, index) => SearchResultRow( - hit: hits[index], - query: query, - onOpen: onOpen, - divider: index < hits.length - 1, - ), + itemBuilder: (context, index) { + final hit = hits[index]; + final target = '${hit['target'] ?? ''}'; + final indexInResults = offset + index; + final key = rowKeys.putIfAbsent(indexInResults, GlobalKey.new); + final rowKey = target.isEmpty || targetCounts[target]! > 1 + ? ValueKey('search.result.$target.$indexInResults') + : ValueKey('search.result.$target'); + return SearchResultRow( + rowKey: rowKey, + hit: hit, + query: query, + onOpen: onOpen, + divider: index < hits.length - 1, + selected: indexInResults == selectedIndex, + visibilityKey: key, + ); + }, ); final empty = controller.emptyMessage; return [ @@ -44,7 +64,7 @@ List searchResultSlivers({ header: true, ), ), - if (chats.isNotEmpty) rows(chats), + if (chats.isNotEmpty) rows(chats, 0), if (note != null) SliverToBoxAdapter( child: Padding( @@ -60,7 +80,7 @@ List searchResultSlivers({ header: true, ), ), - if (others.isNotEmpty) rows(others), + if (others.isNotEmpty) rows(others, chats.length), if (others.isEmpty && controller.parsed.kind != SearchKind.chat && empty != null) diff --git a/app/lib/features/settings/keyboard_shortcuts_registry.dart b/app/lib/features/settings/keyboard_shortcuts_registry.dart index e2d50aad..7e0aa75a 100644 --- a/app/lib/features/settings/keyboard_shortcuts_registry.dart +++ b/app/lib/features/settings/keyboard_shortcuts_registry.dart @@ -15,7 +15,8 @@ enum KeyboardShortcutGroup { chat('Chat'), windows('Windows'), terminal('Terminal'), - lists('Lists'); + lists('Lists'), + search('Search'); const KeyboardShortcutGroup(this.title); @@ -64,6 +65,7 @@ class KeyboardShortcutRegistry { ..._windows, ..._terminal, ..._lists, + ..._search, ]; String get _cheatsheetKeys => shellCheatsheetShortcutLabel(platform); @@ -265,4 +267,14 @@ class KeyboardShortcutRegistry { 'In an artifact or file-diff viewer. Esc closes an artifact viewer.', ), ]; + + List get _search => [ + const KeyboardShortcutEntry( + group: KeyboardShortcutGroup.search, + action: 'Move through Search results', + keys: 'Ctrl+N, Ctrl+P, Up, Down', + caveat: + 'While the Search field is focused. Uses Control rather than Cmd on macOS; Enter opens the highlighted result.', + ), + ]; } diff --git a/app/lib/ui/input/app_search_field.dart b/app/lib/ui/input/app_search_field.dart index a90a7afb..8755d70e 100644 --- a/app/lib/ui/input/app_search_field.dart +++ b/app/lib/ui/input/app_search_field.dart @@ -18,6 +18,7 @@ class AppSearchField extends StatelessWidget { this.onCleared, this.autofocus = false, this.focusNode, + this.onKeyEvent, this.inBar = false, }); @@ -37,6 +38,7 @@ class AppSearchField extends StatelessWidget { final VoidCallback? onCleared; final bool autofocus; final FocusNode? focusNode; + final FocusOnKeyEventCallback? onKeyEvent; final bool inBar; void _clear() { @@ -58,38 +60,41 @@ class AppSearchField extends StatelessWidget { ); return ValueListenableBuilder( valueListenable: controller, - builder: (context, value, _) => TextField( - key: fieldKey, - controller: controller, - focusNode: focusNode, - autofocus: autofocus, - textInputAction: TextInputAction.search, - onChanged: onChanged, - onSubmitted: onSubmitted, - decoration: InputDecoration( - hintText: placeholder, - prefixIcon: const Icon(Icons.search), - suffixIcon: value.text.isEmpty - ? null - : IconButton( - key: clearKey, - tooltip: 'Clear search', - onPressed: _clear, - icon: const Icon(Icons.close), - ), - filled: !inBar, - fillColor: scheme.surfaceContainerHigh, - isDense: true, - border: stadium, - enabledBorder: stadium, - // In the bar the pill is the field's outline; on a page the focused - // field shows the host's focus ring. - focusedBorder: inBar - ? stadium - : OutlineInputBorder( - borderRadius: radius, - borderSide: BorderSide(color: scheme.primary, width: 2), - ), + builder: (context, value, _) => Focus( + onKeyEvent: onKeyEvent, + child: TextField( + key: fieldKey, + controller: controller, + focusNode: focusNode, + autofocus: autofocus, + textInputAction: TextInputAction.search, + onChanged: onChanged, + onSubmitted: onSubmitted, + decoration: InputDecoration( + hintText: placeholder, + prefixIcon: const Icon(Icons.search), + suffixIcon: value.text.isEmpty + ? null + : IconButton( + key: clearKey, + tooltip: 'Clear search', + onPressed: _clear, + icon: const Icon(Icons.close), + ), + filled: !inBar, + fillColor: scheme.surfaceContainerHigh, + isDense: true, + border: stadium, + enabledBorder: stadium, + // In the bar the pill is the field's outline; on a page the focused + // field shows the host's focus ring. + focusedBorder: inBar + ? stadium + : OutlineInputBorder( + borderRadius: radius, + borderSide: BorderSide(color: scheme.primary, width: 2), + ), + ), ), ), ); diff --git a/app/test/features/search/search_screen_test.dart b/app/test/features/search/search_screen_test.dart index 661fa859..ed9c4c9a 100644 --- a/app/test/features/search/search_screen_test.dart +++ b/app/test/features/search/search_screen_test.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/search/search_screen.dart'; +import 'package:pipelines_app/ui/layout/app_list_row.dart'; class ImmediateGateway implements ApiGateway { ImmediateGateway(this.reply); @@ -161,6 +163,200 @@ void main() { expect(opened, isNull); }); + testWidgets('field keys select results, retain focus, and Enter opens', ( + tester, + ) async { + useNarrowSurface(tester); + final second = hit( + kind: 'field', + target: 'run:11:4:output', + payload: { + 'matched_field': 'output', + 'field': { + 'run_id': 11, + 'sequence': 4, + 'field': 'output', + 'snippet': 'the release is ready', + }, + }, + ); + final gateway = ImmediateGateway((_, _) => response([pipeline, second])); + JsonMap? opened; + await tester.pumpWidget( + MaterialApp( + home: SearchScreen(gateway: gateway, onOpen: (value) => opened = value), + ), + ); + await search(tester, 'release'); + final fieldFinder = find.byKey(const ValueKey('search.field')); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.pipeline:7')), + ) + .selected, + isTrue, + ); + + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.run:11:4:output')), + ) + .selected, + isTrue, + ); + expect(FocusManager.instance.primaryFocus?.debugLabel, 'omni-search'); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyP); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.pipeline:7')), + ) + .selected, + isTrue, + ); + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyN); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.run:11:4:output')), + ) + .selected, + isTrue, + ); + + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.run:11:4:output')), + ) + .selected, + isTrue, + ); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.pipeline:7')), + ) + .selected, + isTrue, + ); + + await tester.tap(fieldFinder); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + expect(opened?['target'], 'run:11:4:output'); + }); + + testWidgets( + 'selection resets when result data changes and typing remains active', + (tester) async { + useNarrowSurface(tester); + final changed = hit( + kind: 'pipeline', + target: 'pipeline:7', + payload: { + 'pipeline': { + 'id': 7, + 'name': 'Updated release', + 'version': 4, + 'stages': 5, + }, + }, + ); + final gateway = ImmediateGateway( + (query, _) => + response(query == 'updated' ? [changed, field] : [pipeline, field]), + ); + await tester.pumpWidget( + MaterialApp( + home: SearchScreen(gateway: gateway, onOpen: (_) {}), + ), + ); + + await search(tester, 'release'); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.pump(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.run:11:4:output')), + ) + .selected, + isTrue, + ); + + await tester.enterText( + find.byKey(const ValueKey('search.field')), + 'updated', + ); + await tester.pump(const Duration(milliseconds: 300)); + await tester.pumpAndSettle(); + expect( + tester + .widget( + find.byKey(const ValueKey('search.result.pipeline:7')), + ) + .selected, + isTrue, + ); + expect( + tester + .widget(find.byKey(const ValueKey('search.field'))) + .controller! + .text, + 'updated', + ); + }, + ); + + testWidgets( + 'Enter submits with no results and targetless rows have unique keys', + (tester) async { + useNarrowSurface(tester); + tester.view.physicalSize = const Size(390, 2000); + final gateway = ImmediateGateway( + (query, _) => query == 'empty' + ? response([]) + : response([ + hit(kind: 'future_kind', target: '', payload: const {}), + hit(kind: 'future_kind', target: '', payload: const {}), + ]), + ); + await tester.pumpWidget( + MaterialApp( + home: SearchScreen(gateway: gateway, onOpen: (_) {}), + ), + ); + + await search(tester, 'empty'); + final callsBeforeSubmit = gateway.calls.length; + await tester.tap(find.byKey(const ValueKey('search.field'))); + await tester.testTextInput.receiveAction(TextInputAction.search); + await tester.pumpAndSettle(); + expect(gateway.calls.length, callsBeforeSubmit + 1); + + await search(tester, 'rows'); + expect(find.byKey(const ValueKey('search.result..0')), findsOneWidget); + expect(find.byKey(const ValueKey('search.result..1')), findsOneWidget); + expect(tester.takeException(), isNull); + }, + ); + testWidgets('an unsupported server shows its sentence and retry recovers', ( tester, ) async { diff --git a/app/test/features/settings/keyboard_shortcuts_cheatsheet_test.dart b/app/test/features/settings/keyboard_shortcuts_cheatsheet_test.dart index cddb5df7..a207bd55 100644 --- a/app/test/features/settings/keyboard_shortcuts_cheatsheet_test.dart +++ b/app/test/features/settings/keyboard_shortcuts_cheatsheet_test.dart @@ -30,6 +30,7 @@ const _implemented = [ 'Resize the terminal panel', 'Open a chat list item', 'Resize a list pane', + 'Move through Search results', 'Previous or next artifact or changed file', ]; @@ -130,6 +131,18 @@ void main() { expect(actions, isNot(contains('Move between terminal tabs'))); }); + test('Search documents field-scoped Control navigation', () { + final registry = KeyboardShortcutRegistry( + platform: TargetPlatform.macOS, + web: true, + ); + final entry = _entry(registry, 'Move through Search results'); + expect(entry.group, KeyboardShortcutGroup.search); + expect(entry.keys, 'Ctrl+N, Ctrl+P, Up, Down'); + expect(entry.caveat, contains('Search field')); + expect(entry.caveat, contains('Control rather than Cmd')); + }); + testWidgets('groups stack in one column with their keys right-aligned', ( tester, ) async { From 3164b7c8df2764d3190870b99ab38f4428bd3061 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 13 Nov 2026 15:59:35 +0000 Subject: [PATCH 2/3] Prefer natural voices for read aloud Rank platform voice quality and select the most natural automatic voice while keeping explicit voice choices intact. Surface natural labels, hide novelty voices in the picker, and guide Apple users to download enhanced voices when needed. --- .../speech/flutter_tts_speech_engine.dart | 8 +- .../chat/speech/speech_controller.dart | 7 +- .../features/chat/speech/speech_engine.dart | 84 ++++++++++++++++- .../settings/settings_read_aloud_section.dart | 39 +++++++- .../settings/settings_voice_picker.dart | 60 ++++++++++-- .../chat/speech/fake_speech_engine.dart | 17 +++- .../chat/speech/speech_controller_test.dart | 33 +++++++ .../chat/speech/speech_voice_test.dart | 59 ++++++++++++ .../settings_read_aloud_section_test.dart | 93 +++++++++++++++++-- app/tool/probe_speech_fixture.dart | 59 ++++++++++-- 10 files changed, 424 insertions(+), 35 deletions(-) create mode 100644 app/test/features/chat/speech/speech_voice_test.dart diff --git a/app/lib/features/chat/speech/flutter_tts_speech_engine.dart b/app/lib/features/chat/speech/flutter_tts_speech_engine.dart index 680cba83..15799a06 100644 --- a/app/lib/features/chat/speech/flutter_tts_speech_engine.dart +++ b/app/lib/features/chat/speech/flutter_tts_speech_engine.dart @@ -22,6 +22,12 @@ class FlutterTtsSpeechEngine extends SpeechEngine { TargetPlatform.linux || TargetPlatform.fuchsia => false, }; + @override + bool get supportsVoiceDownloadHint => + !kIsWeb && + (defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS); + /// AVSpeechSynthesizer's normal rate is 0.5 on a 0..1 scale; Android's /// TextToSpeech and the Web Speech API use 1.0 as normal. bool get _appleScale => @@ -106,7 +112,7 @@ class FlutterTtsSpeechEngine extends SpeechEngine { return [ for (final item in raw is List ? raw : const []) if (item is Map && item['name'] != null && item['locale'] != null) - SpeechVoice(name: '${item['name']}', locale: '${item['locale']}'), + SpeechVoice.fromPlatform(item.cast()), ]; } } diff --git a/app/lib/features/chat/speech/speech_controller.dart b/app/lib/features/chat/speech/speech_controller.dart index a60d3d19..893974df 100644 --- a/app/lib/features/chat/speech/speech_controller.dart +++ b/app/lib/features/chat/speech/speech_controller.dart @@ -90,7 +90,12 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver { _set(SpeechState.speaking); return _guard(() async { await engine.setRate(_rate); - await engine.setVoice(voice); + final selected = voice ?? + bestSpeechVoice( + await engine.voices(), + WidgetsBinding.instance.platformDispatcher.locale.languageCode, + ); + await engine.setVoice(selected); await engine.speak(document.text); }); } diff --git a/app/lib/features/chat/speech/speech_engine.dart b/app/lib/features/chat/speech/speech_engine.dart index fddba634..b4508ffb 100644 --- a/app/lib/features/chat/speech/speech_engine.dart +++ b/app/lib/features/chat/speech/speech_engine.dart @@ -3,13 +3,63 @@ import 'package:flutter/foundation.dart'; /// One platform voice. [locale] is a BCP 47 tag such as `en-GB`. @immutable class SpeechVoice { - const SpeechVoice({required this.name, required this.locale}); + const SpeechVoice({ + required this.name, + required this.locale, + this.qualityRank = 0, + this.networkRequired = false, + }); final String name; final String locale; + /// A platform-independent quality score: 0 is unknown/compact and 3 is + /// premium, very high, or a browser's natural voice. + final int qualityRank; + final bool networkRequired; + /// The primary language subtag (`en` for `en-GB`), lower case. String get language => locale.split(RegExp('[-_]')).first.toLowerCase(); + bool get isNatural => qualityRank >= 2; + bool get isNovelty => _noveltyVoice.hasMatch(name); + + /// Converts the maps returned by flutter_tts on Apple, Android, and web. + factory SpeechVoice.fromPlatform(Map raw) { + final name = '${raw['name'] ?? ''}'; + final locale = '${raw['locale'] ?? ''}'; + final quality = '${raw['quality'] ?? ''}'.toLowerCase(); + final identifier = '${raw['identifier'] ?? ''}'.toLowerCase(); + final network = '${raw['network_required'] ?? ''}'.toLowerCase(); + var rank = switch (quality) { + '3' => 3, + '2' => 2, + '1' || '0' => 1, + 'premium' || 'very high' || 'very_high' => 3, + 'enhanced' || 'high' => 2, + 'default' || + 'normal' || + 'standard' || + 'low' || + 'very low' || + 'very_low' => 1, + _ => 0, + }; + if (identifier.contains('premium') || + name.toLowerCase().contains(RegExp(r'\b(natural|online|neural)\b')) || + name.toLowerCase().contains('google ')) { + rank = 3; + } else if (identifier.contains('enhanced')) { + rank = 2; + } else if (identifier.contains('compact')) { + rank = 1; + } + return SpeechVoice( + name: name, + locale: locale, + qualityRank: rank, + networkRequired: network == '1' || network == 'true', + ); + } @override bool operator ==(Object other) => @@ -19,6 +69,32 @@ class SpeechVoice { int get hashCode => Object.hash(name, locale); } +final _noveltyVoice = RegExp( + r'\b(bells|bubbles|zarvox|whisper|boing|cellos|good news|jester|organ|superstar|trinoids)\b', + caseSensitive: false, +); + +/// Picks the best automatic voice without consulting platform APIs. +SpeechVoice? bestSpeechVoice(List voices, String language) { + final matching = voices.where( + (voice) => voice.language == language.toLowerCase(), + ); + if (matching.isEmpty) return null; + final ranked = [...matching]..sort(compareSpeechVoices); + return ranked.first; +} + +int compareSpeechVoices(SpeechVoice a, SpeechVoice b) { + final quality = b.qualityRank.compareTo(a.qualityRank); + if (quality != 0) return quality; + final offline = a.networkRequired ? 1 : 0; + final offlineOther = b.networkRequired ? 1 : 0; + if (offline != offlineOther) return offline.compareTo(offlineOther); + if (a.isNovelty != b.isNovelty) return a.isNovelty ? 1 : -1; + final name = a.name.compareTo(b.name); + return name != 0 ? name : a.locale.compareTo(b.locale); +} + /// The platform text-to-speech service the [SpeechController] drives. /// /// [setRate] takes a multiplier where 1.0 is the platform's normal speed; an @@ -30,6 +106,12 @@ abstract class SpeechEngine { /// Whether this platform can speak at all; the UI hides read aloud if not. bool get supported; + /// Whether this engine can offer the Apple voice-download guidance. + /// + /// This is deliberately false by default: only Apple engines have the + /// matching System Settings destination. + bool get supportsVoiceDownloadHint => false; + VoidCallback? onStart; VoidCallback? onPause; VoidCallback? onResume; diff --git a/app/lib/features/settings/settings_read_aloud_section.dart b/app/lib/features/settings/settings_read_aloud_section.dart index 826ba555..4bd8b022 100644 --- a/app/lib/features/settings/settings_read_aloud_section.dart +++ b/app/lib/features/settings/settings_read_aloud_section.dart @@ -23,11 +23,13 @@ class SettingsReadAloudSection extends StatefulWidget { class _SettingsReadAloudSectionState extends State { SpeechController get _speech => widget.controller ?? speechController; + late final Future> _voices; @override void initState() { super.initState(); unawaited(_speech.restore()); + _voices = _speech.voices(); } Future _pickVoice() async { @@ -65,12 +67,13 @@ class _SettingsReadAloudSectionState extends State { title: const Text('Voice'), subtitle: Text( voice == null - ? 'System default' + ? 'Automatic (most natural)' : '${voice.name} · ${voice.locale}', ), chevron: true, onTap: () => unawaited(_pickVoice()), ), + _voiceDownloadHint(context), SettingsGroupItem( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, @@ -131,4 +134,38 @@ class _SettingsReadAloudSectionState extends State { ); }, ); + + Widget _voiceDownloadHint(BuildContext context) { + return FutureBuilder>( + future: _voices, + builder: (context, snapshot) { + if (!snapshot.hasData || + !needsSpeechVoiceHint( + snapshot.data!, + View.of(context).platformDispatcher.locale.languageCode, + supportsVoiceDownloadHint: + _speech.engine.supportsVoiceDownloadHint, + )) { + return const SizedBox.shrink(); + } + const hint = + 'For a more natural voice, download one in System Settings > ' + 'Accessibility > Spoken Content > System voice > Manage voices.'; + return SettingsGroupItem( + key: const Key('settings-read-aloud-voice-hint'), + child: Tooltip( + message: hint, + child: Text( + hint, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: Theme.of(context).colorScheme.onSurfaceVariant, + ), + ), + ), + ); + }, + ); + } } diff --git a/app/lib/features/settings/settings_voice_picker.dart b/app/lib/features/settings/settings_voice_picker.dart index c714474d..68e29d8a 100644 --- a/app/lib/features/settings/settings_voice_picker.dart +++ b/app/lib/features/settings/settings_voice_picker.dart @@ -4,6 +4,17 @@ import 'package:flutter/material.dart'; import '../chat/speech/speech.dart'; +bool needsSpeechVoiceHint( + List voices, + String language, { + bool supportsVoiceDownloadHint = false, +}) { + if (!supportsVoiceDownloadHint) return false; + return voices + .where((voice) => voice.language == language.toLowerCase()) + .every((voice) => !voice.isNatural); +} + /// The picker's answer: a voice, or null for the system default. typedef SpeechVoiceChoice = ({SpeechVoice? voice}); @@ -22,7 +33,7 @@ Future showSpeechVoicePicker( } /// Voices grouped by language subtag, the [device] language first and the -/// rest by name; voices within a group by name. +/// rest by name; natural voices and offline voices come first within a group. List<({String language, List voices})> groupSpeechVoices( List voices, String device, @@ -41,11 +52,7 @@ List<({String language, List voices})> groupSpeechVoices( for (final language in languages) ( language: language, - voices: groups[language]! - ..sort((a, b) { - final name = a.name.compareTo(b.name); - return name != 0 ? name : a.locale.compareTo(b.locale); - }), + voices: groups[language]!..sort(compareSpeechVoices), ), ]; } @@ -113,7 +120,10 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> { contentPadding: const EdgeInsets.symmetric(vertical: 16), // A list needs a definite width; keep it to a readable dialog measure. content: ConstrainedBox( - constraints: const BoxConstraints(maxWidth: 560), + constraints: BoxConstraints( + maxWidth: 560, + maxHeight: MediaQuery.sizeOf(context).height * .6, + ), child: SizedBox( width: double.maxFinite, child: FutureBuilder>( @@ -122,6 +132,17 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> { if (snapshot.connectionState != ConnectionState.done) { return const Center(child: CircularProgressIndicator()); } + if (snapshot.hasError) { + return Padding( + padding: const EdgeInsets.all(24), + child: Text( + 'Voices could not be loaded. You can cancel and try again.', + style: TextStyle( + color: Theme.of(context).colorScheme.onSurfaceVariant, + ), + ), + ); + } final voices = snapshot.data ?? const []; return _list(context, groupSpeechVoices(voices, device)); }, @@ -154,9 +175,12 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> { Navigator.of(context).pop((voice: voice)); }, child: ListView( - shrinkWrap: true, + primary: false, children: [ - const RadioListTile(value: '', title: Text('System default')), + const RadioListTile( + value: '', + title: Text('Automatic (most natural)'), + ), if (groups.isEmpty) Padding( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8), @@ -181,7 +205,23 @@ class _VoicePickerDialogState extends State<_VoicePickerDialog> { Expanded( child: RadioListTile( value: _keyOf(voice), - title: Text(voice.name), + title: Row( + children: [ + Flexible(child: Text(voice.name)), + if (voice.isNatural) + Padding( + padding: const EdgeInsetsDirectional.only( + start: 8, + ), + child: Text( + 'Natural', + style: theme.textTheme.labelSmall?.copyWith( + color: theme.colorScheme.primary, + ), + ), + ), + ], + ), subtitle: Text(voice.locale), ), ), diff --git a/app/test/features/chat/speech/fake_speech_engine.dart b/app/test/features/chat/speech/fake_speech_engine.dart index 060a4789..6bbe0da5 100644 --- a/app/test/features/chat/speech/fake_speech_engine.dart +++ b/app/test/features/chat/speech/fake_speech_engine.dart @@ -4,16 +4,26 @@ import 'package:pipelines_app/features/chat/speech/speech.dart'; /// A scripted [SpeechEngine]: it records calls and a test fires the platform's /// events. [stop] reports a cancel like every real platform does. class FakeSpeechEngine extends SpeechEngine { - FakeSpeechEngine({this.supported = true, this.available = const []}); + FakeSpeechEngine({ + this.supported = true, + this.available = const [], + this.voiceDownloadHint = false, + this.voiceFailure = false, + }); @override final bool supported; final List available; + final bool voiceDownloadHint; + final bool voiceFailure; final calls = []; final spoken = []; double? rate; SpeechVoice? voice; + @override + bool get supportsVoiceDownloadHint => voiceDownloadHint; + @override Future speak(String text) async { calls.add('speak'); @@ -36,7 +46,10 @@ class FakeSpeechEngine extends SpeechEngine { Future setVoice(SpeechVoice? voice) async => this.voice = voice; @override - Future> voices() async => available; + Future> voices() async { + if (voiceFailure) throw StateError('voice inventory failed'); + return available; + } /// Reports [word] (its first occurrence at or after [from]) being spoken. void speakWord(String word, {int from = 0}) { diff --git a/app/test/features/chat/speech/speech_controller_test.dart b/app/test/features/chat/speech/speech_controller_test.dart index 164b885a..f71d9d55 100644 --- a/app/test/features/chat/speech/speech_controller_test.dart +++ b/app/test/features/chat/speech/speech_controller_test.dart @@ -165,4 +165,37 @@ void main() { expect(speech.rate, defaultSpeechRate); expect(store.saved, (rate: defaultSpeechRate, voice: null)); }); + + testWidgets('an explicitly selected voice does not require voice inventory', ( + tester, + ) async { + engine = FakeSpeechEngine(voiceFailure: true); + reader(); + const voice = SpeechVoice(name: 'Daniel', locale: 'en-GB'); + + // The saved selection is passed directly to the engine, even if querying + // the platform voice inventory is unavailable. + await speech.setVoice(voice); + await speech.read('a', ['Hello']); + + expect(engine.voice, voice); + expect(engine.spoken, ['Hello']); + }); + + testWidgets( + 'automatic voice selects the most natural device-language voice', + (tester) async { + engine = FakeSpeechEngine( + available: const [ + SpeechVoice(name: 'Compact', locale: 'en-US', qualityRank: 1), + SpeechVoice(name: 'Enhanced', locale: 'en-US', qualityRank: 2), + SpeechVoice(name: 'French Natural', locale: 'fr-FR', qualityRank: 3), + ], + ); + reader(); + await speech.read('a', ['Hello']); + expect(engine.voice?.name, 'Enhanced'); + expect(speech.voice, isNull); + }, + ); } diff --git a/app/test/features/chat/speech/speech_voice_test.dart b/app/test/features/chat/speech/speech_voice_test.dart new file mode 100644 index 00000000..c04203c3 --- /dev/null +++ b/app/test/features/chat/speech/speech_voice_test.dart @@ -0,0 +1,59 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/speech/speech.dart'; + +void main() { + test('parses Apple quality and identifier without changing identity', () { + final voice = SpeechVoice.fromPlatform({ + 'name': 'Samantha', + 'locale': 'en-US', + 'quality': 'premium', + 'identifier': 'com.apple.voice.premium.en-US.Samantha', + }); + + expect(voice.qualityRank, 3); + expect(voice.isNatural, isTrue); + expect(voice, const SpeechVoice(name: 'Samantha', locale: 'en-US')); + }); + + test('parses Android quality and prefers offline on a quality tie', () { + final online = SpeechVoice.fromPlatform({ + 'name': 'English online', + 'locale': 'en-US', + 'quality': 'very high', + 'network_required': '1', + }); + final offline = SpeechVoice.fromPlatform({ + 'name': 'English local', + 'locale': 'en-US', + 'quality': 'very high', + 'network_required': '0', + }); + + expect(bestSpeechVoice([online, offline], 'en'), offline); + }); + + test('recognises natural web voice names', () { + final voice = SpeechVoice.fromPlatform({ + 'name': 'Microsoft Aria Online (Natural)', + 'locale': 'en-US', + }); + + expect(voice.qualityRank, 3); + expect(voice.isNatural, isTrue); + }); + + test('picker ranking buries compact novelty voices', () { + final natural = const SpeechVoice( + name: 'Samantha Enhanced', + locale: 'en-US', + qualityRank: 2, + ); + final novelty = const SpeechVoice( + name: 'Zarvox', + locale: 'en-US', + qualityRank: 1, + ); + + expect(compareSpeechVoices(natural, novelty), lessThan(0)); + }); +} diff --git a/app/test/features/settings/settings_read_aloud_section_test.dart b/app/test/features/settings/settings_read_aloud_section_test.dart index 7537bd67..9b0edf25 100644 --- a/app/test/features/settings/settings_read_aloud_section_test.dart +++ b/app/test/features/settings/settings_read_aloud_section_test.dart @@ -7,7 +7,11 @@ import 'package:pipelines_app/features/settings/settings_voice_picker.dart'; import '../chat/speech/fake_speech_engine.dart'; const _daniel = SpeechVoice(name: 'Daniel', locale: 'en-GB'); -const _samantha = SpeechVoice(name: 'Samantha', locale: 'en-US'); +const _samantha = SpeechVoice( + name: 'Samantha Enhanced', + locale: 'en-US', + qualityRank: 2, +); const _thomas = SpeechVoice(name: 'Thomas', locale: 'fr-FR'); const _anna = SpeechVoice(name: 'Anna', locale: 'de-DE'); @@ -21,9 +25,16 @@ late FakeSpeechEngine _engine; /// A fresh reader over [store], as after an app reload, and its section. Future _open( WidgetTester tester, - MemorySpeechPreferences store, -) async { - _engine = FakeSpeechEngine(available: [_samantha, _thomas, _anna, _daniel]); + MemorySpeechPreferences store, { + List? available, + bool voiceDownloadHint = false, + bool voiceFailure = false, +}) async { + _engine = FakeSpeechEngine( + available: available ?? [_samantha, _thomas, _anna, _daniel], + voiceDownloadHint: voiceDownloadHint, + voiceFailure: voiceFailure, + ); final speech = SpeechController(engine: _engine, store: store); addTearDown(speech.dispose); await tester.pumpWidget( @@ -47,10 +58,11 @@ void main() { addTearDown(tester.platformDispatcher.clearLocaleTestValue); final store = MemorySpeechPreferences(); await _open(tester, store); - expect(find.text('System default'), findsOneWidget); + expect(find.text('Automatic (most natural)'), findsOneWidget); await tester.tap(_voiceRow); await tester.pumpAndSettle(); + expect(find.text('Natural'), findsOneWidget); // Grouped by language, the device's language first. final french = tester.getTopLeft(find.text('French')).dy; final english = tester.getTopLeft(find.text('English')).dy; @@ -59,7 +71,7 @@ void main() { expect(english, lessThan(german)); expect( tester.getTopLeft(find.text('Daniel')).dy, - lessThan(tester.getTopLeft(find.text('Samantha')).dy), + greaterThan(tester.getTopLeft(find.text('Samantha Enhanced')).dy), ); await tester.tap(find.text('Daniel')); @@ -108,7 +120,7 @@ void main() { expect(speech.voice, isNull); expect(speech.rate, defaultSpeechRate); expect(store.saved, (rate: defaultSpeechRate, voice: null)); - expect(find.text('System default'), findsOneWidget); + expect(find.text('Automatic (most natural)'), findsOneWidget); expect(find.text('Speaking rate 1.0×'), findsOneWidget); expect(tester.widget(_reset).onPressed, isNull); }); @@ -130,7 +142,7 @@ void main() { // Each voice in the picker has its own sample; it does not choose it. await tester.tap(_voiceRow); await tester.pumpAndSettle(); - await tester.tap(find.byTooltip('Play sample of Samantha')); + await tester.tap(find.byTooltip('Play sample of Samantha Enhanced')); await tester.pump(); expect(_engine.voice, _samantha); expect(_engine.spoken, hasLength(2)); @@ -140,9 +152,72 @@ void main() { expect(speech.state, SpeechState.idle, reason: 'closing stops the sample'); }); + testWidgets( + 'shows the Apple download hint only for a non-natural inventory', + (tester) async { + tester.platformDispatcher.localeTestValue = const Locale('en', 'US'); + addTearDown(tester.platformDispatcher.clearLocaleTestValue); + final store = MemorySpeechPreferences(); + await _open( + tester, + store, + available: const [ + SpeechVoice( + name: 'Samantha Compact', + locale: 'en-US', + qualityRank: 1, + ), + ], + voiceDownloadHint: true, + ); + expect( + find.textContaining('download one in System Settings'), + findsOneWidget, + ); + final hint = tester.widget( + find.descendant( + of: find.byKey(const Key('settings-read-aloud-voice-hint')), + matching: find.byType(Text), + ), + ); + expect(hint.maxLines, 1); + expect( + tester + .getTopLeft(find.byKey(const Key('settings-read-aloud-voice-hint'))) + .dy, + greaterThan(tester.getTopLeft(_voiceRow).dy), + ); + expect( + tester.getTopLeft(find.text('Speaking rate 1.0×')).dy, + greaterThan( + tester + .getTopLeft( + find.byKey(const Key('settings-read-aloud-voice-hint')), + ) + .dy, + ), + ); + }, + ); + + testWidgets('failed voice inventory is safe and dismissible', (tester) async { + final speech = await _open( + tester, + MemorySpeechPreferences(), + voiceFailure: true, + ); + await tester.tap(_voiceRow); + await tester.pumpAndSettle(); + expect(find.textContaining('Voices could not be loaded'), findsOneWidget); + expect(find.text('This device offers no other voices.'), findsNothing); + await tester.tap(find.text('Cancel')); + await tester.pumpAndSettle(); + expect(speech.state, SpeechState.idle); + }); + test('voices group by language, device language first', () { final groups = groupSpeechVoices([_samantha, _anna, _daniel], 'de'); expect(groups.map((group) => group.language), ['de', 'en']); - expect(groups.last.voices, [_daniel, _samantha]); + expect(groups.last.voices, [_samantha, _daniel]); }); } diff --git a/app/tool/probe_speech_fixture.dart b/app/tool/probe_speech_fixture.dart index 3e1283a0..fcd9c476 100644 --- a/app/tool/probe_speech_fixture.dart +++ b/app/tool/probe_speech_fixture.dart @@ -9,15 +9,31 @@ import 'package:pipelines_app/features/chat/speech/speech.dart'; void installSpeechFixture(Map params) { if (params['speech'] != 'fake') return; final step = int.tryParse(params['speechStep'] ?? '') ?? 600; + final hint = params['speechHint'] == '1'; + final empty = params['speechEmpty'] == '1'; + final failure = params['speechFailure'] == '1'; debugSpeechController = SpeechController( - engine: _ScriptedSpeechEngine(Duration(milliseconds: step)), + engine: _ScriptedSpeechEngine( + Duration(milliseconds: step), + appleHint: hint, + empty: empty, + failure: failure, + ), ); } class _ScriptedSpeechEngine extends SpeechEngine { - _ScriptedSpeechEngine(this.step); + _ScriptedSpeechEngine( + this.step, { + this.appleHint = false, + this.empty = false, + this.failure = false, + }); final Duration step; + final bool appleHint; + final bool empty; + final bool failure; Timer? _timer; String _text = ''; List _words = const []; @@ -27,6 +43,9 @@ class _ScriptedSpeechEngine extends SpeechEngine { @override bool get supported => true; + @override + bool get supportsVoiceDownloadHint => appleHint; + @override Future speak(String text) async { if (_paused && text == _text) { @@ -81,12 +100,32 @@ class _ScriptedSpeechEngine extends SpeechEngine { Future setVoice(SpeechVoice? voice) async {} @override - Future> voices() async => const [ - SpeechVoice(name: 'Samantha', locale: 'en-US'), - SpeechVoice(name: 'Daniel', locale: 'en-GB'), - SpeechVoice(name: 'Karen', locale: 'en-AU'), - SpeechVoice(name: 'Thomas', locale: 'fr-FR'), - SpeechVoice(name: 'Anna', locale: 'de-DE'), - SpeechVoice(name: 'Kyoko', locale: 'ja-JP'), - ]; + Future> voices() async { + if (failure) throw StateError('fixture voice inventory failed'); + if (empty) return const []; + return [ + const SpeechVoice( + name: 'Samantha Compact', + locale: 'en-US', + qualityRank: 1, + ), + if (!appleHint) + const SpeechVoice( + name: 'Samantha Enhanced', + locale: 'en-US', + qualityRank: 2, + ), + if (!appleHint) + const SpeechVoice( + name: 'Microsoft Aria Online (Natural)', + locale: 'en-US', + qualityRank: 3, + ), + const SpeechVoice(name: 'Bells', locale: 'en-US', qualityRank: 1), + const SpeechVoice(name: 'Daniel', locale: 'en-GB', qualityRank: 1), + const SpeechVoice(name: 'Thomas', locale: 'fr-FR', qualityRank: 1), + const SpeechVoice(name: 'Anna', locale: 'de-DE', qualityRank: 1), + const SpeechVoice(name: 'Kyoko', locale: 'ja-JP', qualityRank: 1), + ]; + } } From c3c2c1bbc9adcb06c09ac53dd572948890a868e3 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 13 Nov 2026 16:02:31 +0000 Subject: [PATCH 3/3] Open Chat with Cmd+K as well as Ctrl+K Cmd is the natural modifier on a Mac. Like Option+K, it also opens Chat from the terminal, where Ctrl+K stays kill-line. --- .../settings/keyboard_shortcuts_registry.dart | 2 +- app/lib/shell/adaptive_shell.dart | 6 +- app/lib/shell/shell_chat_shortcut.dart | 25 +++--- .../keyboard_shortcuts_help_test.dart | 2 +- app/test/shell/shell_chat_shortcut_test.dart | 88 ++++++++++++------- 5 files changed, 75 insertions(+), 48 deletions(-) diff --git a/app/lib/features/settings/keyboard_shortcuts_registry.dart b/app/lib/features/settings/keyboard_shortcuts_registry.dart index 7e0aa75a..6dc477c8 100644 --- a/app/lib/features/settings/keyboard_shortcuts_registry.dart +++ b/app/lib/features/settings/keyboard_shortcuts_registry.dart @@ -97,7 +97,7 @@ class KeyboardShortcutRegistry { keys: shellChatShortcutLabels(platform).join(', '), caveat: web ? 'Shows the open chat and focuses its message input. The browser may reserve Ctrl+K.' - : 'Shows the open chat and focuses its message input. In the terminal Ctrl+K stays kill-line; Alt+K still opens Chat.', + : 'Shows the open chat and focuses its message input. In the terminal Ctrl+K stays kill-line; ${_apple ? 'Option+K and Cmd+K still open' : 'Alt+K still opens'} Chat.', ), const KeyboardShortcutEntry( group: KeyboardShortcutGroup.navigation, diff --git a/app/lib/shell/adaptive_shell.dart b/app/lib/shell/adaptive_shell.dart index b3ed85c2..24e48822 100644 --- a/app/lib/shell/adaptive_shell.dart +++ b/app/lib/shell/adaptive_shell.dart @@ -416,9 +416,11 @@ class _AdaptiveShellState extends State { return true; } // Ctrl+K is the terminal's kill-line while the terminal has the keyboard; - // Alt+K still opens Chat there. + // Alt+K and Cmd+K still open Chat there. if (shellChatShortcut(event, keyboard) && - (keyboard.isAltPressed || !primaryFocusIsTypingSurface())) { + (keyboard.isAltPressed || + keyboard.isMetaPressed || + !primaryFocusIsTypingSurface())) { if (event is KeyDownEvent) _openChat(); return true; } diff --git a/app/lib/shell/shell_chat_shortcut.dart b/app/lib/shell/shell_chat_shortcut.dart index 52cb1de4..4dabcee9 100644 --- a/app/lib/shell/shell_chat_shortcut.dart +++ b/app/lib/shell/shell_chat_shortcut.dart @@ -2,28 +2,29 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; /// Whether [event] opens the Chat destination from any destination: Ctrl+K, -/// or Alt+K (Option+K on Apple platforms). Alt+K is recognised by key position -/// too, because Option+K yields a dead-key symbol on a Mac. Shift, Cmd and -/// chords with both Ctrl and Alt are not the shortcut. Repeats of a held chord +/// Cmd+K (Meta+K), or Alt+K (Option+K on Apple platforms). Alt+K is +/// recognised by key position too, because Option+K yields a dead-key symbol +/// on a Mac. Shift and chords of more than one of Ctrl, Cmd and Alt are not +/// the shortcut. Repeats of a held chord /// match as well, so the caller can swallow them; only its first press acts. bool shellChatShortcut(KeyEvent event, HardwareKeyboard keyboard) { - if (event is KeyUpEvent || - keyboard.isShiftPressed || - keyboard.isMetaPressed) { - return false; - } - final control = keyboard.isControlPressed; + if (event is KeyUpEvent || keyboard.isShiftPressed) return false; final alt = keyboard.isAltPressed; - if (control == alt) return false; + final held = [ + keyboard.isControlPressed, + keyboard.isMetaPressed, + alt, + ].where((pressed) => pressed).length; + if (held != 1) return false; if (event.logicalKey == LogicalKeyboardKey.keyK) return true; return alt && event.physicalKey == PhysicalKeyboardKey.keyK; } /// The chords that open Chat as they read on this platform, for the help /// page. Alt (Option on Apple platforms) leads, as it does for the floating -/// chats; Ctrl+K is the other chord everywhere. +/// chats; Cmd+K follows on Apple platforms and Ctrl+K works everywhere. List shellChatShortcutLabels([TargetPlatform? platform]) { final target = platform ?? defaultTargetPlatform; final apple = target == TargetPlatform.macOS || target == TargetPlatform.iOS; - return [apple ? 'Option+K' : 'Alt+K', 'Ctrl+K']; + return apple ? ['Option+K', 'Cmd+K', 'Ctrl+K'] : ['Alt+K', 'Ctrl+K']; } diff --git a/app/test/features/settings/keyboard_shortcuts_help_test.dart b/app/test/features/settings/keyboard_shortcuts_help_test.dart index 4cfc6412..e7a0f6fb 100644 --- a/app/test/features/settings/keyboard_shortcuts_help_test.dart +++ b/app/test/features/settings/keyboard_shortcuts_help_test.dart @@ -33,7 +33,7 @@ void main() { web: false, ).entries.firstWhere((entry) => entry.action == 'Open Chat'); - expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Ctrl+K'); + expect(openChat(TargetPlatform.macOS).keys, 'Option+K, Cmd+K, Ctrl+K'); final linux = openChat(TargetPlatform.linux); expect(linux.keys, 'Alt+K, Ctrl+K'); expect(linux.caveat, contains('kill-line')); diff --git a/app/test/shell/shell_chat_shortcut_test.dart b/app/test/shell/shell_chat_shortcut_test.dart index 69a873d4..0130efbd 100644 --- a/app/test/shell/shell_chat_shortcut_test.dart +++ b/app/test/shell/shell_chat_shortcut_test.dart @@ -13,37 +13,48 @@ const _k = KeyDownEvent( ); void main() { - test('Ctrl+K and Alt+K open Chat; other modifiers and plain K do not', () { - bool chord(Set held, [KeyEvent event = _k]) => - shellChatShortcut(event, _FakeKeyboard(held)); - expect(chord({}), isFalse); - expect(chord({LogicalKeyboardKey.controlLeft}), isTrue); - expect(chord({LogicalKeyboardKey.altLeft}), isTrue); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}), - isFalse, - ); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}), - isFalse, - ); - expect(chord({LogicalKeyboardKey.metaLeft}), isFalse); - expect( - chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}), - isFalse, - ); - expect( - shellChatShortcut( - const KeyUpEvent( - physicalKey: PhysicalKeyboardKey.keyK, - logicalKey: LogicalKeyboardKey.keyK, - timeStamp: Duration.zero, + test( + 'Ctrl+K, Cmd+K and Alt+K open Chat; other modifiers and plain K do not', + () { + bool chord(Set held, [KeyEvent event = _k]) => + shellChatShortcut(event, _FakeKeyboard(held)); + expect(chord({}), isFalse); + expect(chord({LogicalKeyboardKey.controlLeft}), isTrue); + expect(chord({LogicalKeyboardKey.altLeft}), isTrue); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.altLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.shiftLeft}), + isFalse, + ); + expect(chord({LogicalKeyboardKey.metaLeft}), isTrue); + expect( + chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.shiftLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.altLeft}), + isFalse, + ); + expect( + chord({LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.metaLeft}), + isFalse, + ); + expect( + shellChatShortcut( + const KeyUpEvent( + physicalKey: PhysicalKeyboardKey.keyK, + logicalKey: LogicalKeyboardKey.keyK, + timeStamp: Duration.zero, + ), + _FakeKeyboard({LogicalKeyboardKey.controlLeft}), ), - _FakeKeyboard({LogicalKeyboardKey.controlLeft}), - ), - isFalse, - ); - }); + isFalse, + ); + }, + ); test('Option+K matches by key position though it types a symbol', () { // On a Mac, Option+K produces ˚: the logical key is not K. @@ -65,11 +76,12 @@ void main() { expect(shellChatShortcutLabels(TargetPlatform.linux), ['Alt+K', 'Ctrl+K']); expect(shellChatShortcutLabels(TargetPlatform.macOS), [ 'Option+K', + 'Cmd+K', 'Ctrl+K', ]); }); - testWidgets('Ctrl+K and Alt+K show Chat from another destination', ( + testWidgets('Ctrl+K, Alt+K and Cmd+K show Chat from another destination', ( tester, ) async { var opened = 0; @@ -104,6 +116,12 @@ void main() { expect(find.text('chat body'), isOnstage); // Already in Chat, the chord still hands the keyboard to the composer. expect(opened, 2); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyK); + await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); + await tester.pump(); + expect(opened, 3); expect(tester.takeException(), isNull); }); @@ -130,7 +148,7 @@ void main() { }); testWidgets( - 'terminal keeps Ctrl+K for kill-line but Alt+K still opens Chat', + 'terminal keeps Ctrl+K for kill-line but Alt+K and Cmd+K still open Chat', (tester) async { final terminal = FocusNode(); addTearDown(terminal.dispose); @@ -168,6 +186,12 @@ void main() { await tester.pump(); expect(find.text('chat body'), isOnstage); expect(opened, 1); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyK); + await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft); + await tester.pump(); + expect(opened, 2); expect(tester.takeException(), isNull); }, );