From f398376e383d059dcbce37ab4e04c5262da7309b Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 6 Nov 2026 18:36:53 +0000 Subject: [PATCH] Keep Cupertino web search editable across clear and revisit --- app/lib/features/search/search_screen.dart | 39 +++++- app/lib/ui/input/app_search_field.dart | 15 +++ .../ui/input/cupertino_web_search_field.dart | 115 ++++++++++++++++++ app/notes/chat-search-keyboard-shortcuts.md | 8 ++ .../cupertino_web_search_field_test.dart | 89 ++++++++++++++ 5 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 app/lib/ui/input/cupertino_web_search_field.dart create mode 100644 app/test/ui/input/cupertino_web_search_field_test.dart diff --git a/app/lib/features/search/search_screen.dart b/app/lib/features/search/search_screen.dart index 438ea3c4..eea613fe 100644 --- a/app/lib/features/search/search_screen.dart +++ b/app/lib/features/search/search_screen.dart @@ -82,9 +82,46 @@ class _SearchScreenState extends State { super.dispose(); } + bool get _activeSearch { + final scope = ShellBarScope.maybeOf(context); + return scope == null || + scope.registry.focus == null || + scope.registry.focus == scope.destination; + } + + void _focusSearch() { + final before = FocusManager.instance.primaryFocus; + // The shell moves Search's field into its bar after changing tabs. End an + // old browser edit only after that layout, then hand focus to the mounted + // field. Ordinary query changes never reset focus or the input connection. + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted || !_activeSearch || !_focus.canRequestFocus) return; + final current = FocusManager.instance.primaryFocus; + if (current != null && + current != before && + current != _focus && + current is! FocusScopeNode) { + return; + } + _focus.unfocus(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted || !_activeSearch || !_focus.canRequestFocus) return; + final current = FocusManager.instance.primaryFocus; + if (current == null || + current == _focus || + current == before || + current is FocusScopeNode) { + _focus.requestFocus(); + } + }); + WidgetsBinding.instance.ensureVisualUpdate(); + }); + WidgetsBinding.instance.ensureVisualUpdate(); + } + @override Widget build(BuildContext context) => - ShellSearchShortcut(onFocus: _focus.requestFocus, child: _build(context)); + ShellSearchShortcut(onFocus: _focusSearch, child: _build(context)); Widget _build(BuildContext context) { // Follow the actual host app class: Cupertino only under an explicit diff --git a/app/lib/ui/input/app_search_field.dart b/app/lib/ui/input/app_search_field.dart index e2c16ee6..1c6f3c12 100644 --- a/app/lib/ui/input/app_search_field.dart +++ b/app/lib/ui/input/app_search_field.dart @@ -1,7 +1,9 @@ import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; +import 'cupertino_web_search_field.dart'; /// The app's one search field per platform. /// @@ -57,6 +59,19 @@ class AppSearchField extends StatelessWidget { @override Widget build(BuildContext context) { if (cupertino ?? usesCupertinoHost(context)) { + if (kIsWeb) { + return CupertinoWebSearchField( + controller: controller, + placeholder: placeholder, + fieldKey: fieldKey, + focusNode: focusNode, + autofocus: autofocus, + inBar: inBar, + onChanged: onChanged, + onSubmitted: onSubmitted, + onCleared: _clear, + ); + } return CupertinoSearchTextField( key: fieldKey, controller: controller, diff --git a/app/lib/ui/input/cupertino_web_search_field.dart b/app/lib/ui/input/cupertino_web_search_field.dart new file mode 100644 index 00000000..740033a0 --- /dev/null +++ b/app/lib/ui/input/cupertino_web_search_field.dart @@ -0,0 +1,115 @@ +import 'package:flutter/cupertino.dart'; + +/// Keeps the web input's semantics stable when its clear action appears. +/// Nesting the clear button in CupertinoSearchTextField changes its merged +/// editable node into a disabled child node on web. Native sibling controls +/// retain both an enabled input and an independently accessible clear action. +class CupertinoWebSearchField extends StatefulWidget { + const CupertinoWebSearchField({ + super.key, + required this.controller, + required this.placeholder, + required this.onCleared, + this.fieldKey, + this.focusNode, + this.autofocus = false, + this.inBar = false, + this.onChanged, + this.onSubmitted, + }); + + final TextEditingController controller; + final String placeholder; + final VoidCallback onCleared; + final Key? fieldKey; + final FocusNode? focusNode; + final bool autofocus; + final bool inBar; + final ValueChanged? onChanged; + final ValueChanged? onSubmitted; + + @override + State createState() => + _CupertinoWebSearchFieldState(); +} + +class _CupertinoWebSearchFieldState extends State { + FocusNode? _localFocus; + FocusNode get _focus => widget.focusNode ?? (_localFocus ??= FocusNode()); + + void _clear() { + final focus = _focus; + // The browser can keep the old editing session focused on the semantic + // clear button after it is removed. This explicit action ends that edit, + // then hands focus back after layout; ordinary query changes never refocus. + focus.unfocus(); + widget.onCleared(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted || !identical(focus, _focus)) return; + final current = FocusManager.instance.primaryFocus; + if (current == null || current == focus || current is FocusScopeNode) { + focus.requestFocus(); + } + }); + WidgetsBinding.instance.ensureVisualUpdate(); + } + + @override + void dispose() { + _localFocus?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => DecoratedBox( + decoration: BoxDecoration( + color: widget.inBar + ? const Color(0x00000000) + : CupertinoColors.tertiarySystemFill.resolveFrom(context), + borderRadius: BorderRadius.circular(9), + ), + child: ConstrainedBox( + constraints: const BoxConstraints( + minHeight: kMinInteractiveDimensionCupertino, + ), + child: TextFieldTapRegion( + child: Row( + children: [ + Expanded( + child: CupertinoSearchTextField( + key: widget.fieldKey, + controller: widget.controller, + focusNode: _focus, + autofocus: widget.autofocus, + placeholder: widget.placeholder, + backgroundColor: const Color(0x00000000), + suffixMode: OverlayVisibilityMode.never, + onChanged: widget.onChanged, + onSubmitted: widget.onSubmitted, + ), + ), + ValueListenableBuilder( + valueListenable: widget.controller, + builder: (context, value, _) => value.text.isEmpty + ? const SizedBox.shrink() + : CupertinoButton( + padding: const EdgeInsets.symmetric(horizontal: 8), + onPressed: _clear, + child: Icon( + CupertinoIcons.clear_thick_circled, + color: CupertinoColors.secondaryLabel.resolveFrom( + context, + ), + size: 20, + semanticLabel: CupertinoLocalizations.of( + context, + ).clearButtonLabel, + ), + ), + ), + ], + ), + ), + ), + ); +} diff --git a/app/notes/chat-search-keyboard-shortcuts.md b/app/notes/chat-search-keyboard-shortcuts.md index 6a2d5343..1d88110d 100644 --- a/app/notes/chat-search-keyboard-shortcuts.md +++ b/app/notes/chat-search-keyboard-shortcuts.md @@ -5,3 +5,11 @@ Ctrl+/ opens the persistent Search tab and focuses its existing field. On the ac Ctrl+1…9 and macOS Command+1…9 still choose conversation rows, in the visible list order. Pipelines and Runs no longer receive digit shortcuts. Ctrl+Tab and Ctrl+Shift+Tab no longer switch destinations; workspace pane navigation is handled separately. Extra Shift/Alt or combined Ctrl+Command modifiers pass through for both Search and digit selection. No socket or backend changes. Checks: bounded analysis clean. 188 shell/Search tests passed through the memory watchdog (peak1713MB,14s). Tests cover exact modifiers on macOS/Windows, actual SearchScreen focus from a composer, repeat/revisit retaining query, Android-theme/macOS-host mismatch, chat-only digits, unchanged destination/history for removed chords, and normal Tab traversal. Browser interaction for the full split workspace is recorded by the parent integration task; this lane has no visual layout change. + +## Cupertino web focus recovery + +Real Mac Chrome raw-key captures found that the original native Cupertino search field accepted only the first character with accessibility semantics enabled: when its nested clear action appeared, the generated editable DOM node changed into a disabled child. The EditableText widget and FocusNode identities remained stable, so a widget-focus assertion alone missed the browser input failure. Generic outer semantics boundaries and an always-visible nested native suffix did not solve it. + +The web Cupertino path now composes the native CupertinoSearchTextField without an inner suffix and a native sibling CupertinoButton for Clear, inside the same TextFieldTapRegion. Editable semantics stay enabled and keep their identity while the independent clear action appears. A standard Cupertino minimum interactive height keeps out-of-bar geometry constant across empty/text/clear. The helper owns a FocusNode only when the caller supplies none; native non-web Cupertino and Material paths are unchanged. Clear explicitly hands focus back after its button disappears. The Search shortcut likewise ends an old native edit after the bar is laid out, then focuses the mounted field on the following frame, guarded by active destination, mount, focus ownership and canRequestFocus. These handoffs happen only for explicit user actions; query updates never refocus or restart editing. + +Final checks:191 shell/Search/shared-input tests passed (peak1512MB,18s), including stable enabled editable semantics, unchanged empty/text/clear geometry, externally supplied/internal FocusNode clear handling, and Cupertino shortcut focus/query retention. Bounded analysis is clean. Fresh full-app fixture build and real Chrome captures with no diagnostic probe code passed individual raw letter presses for `release`, Clear then raw `x`, revisit Chat then Command+/ preserving `x`, and raw `y`,`z` editing to `yz`. Native single-line select-all-on-focus behavior intentionally replaces the retained query when typing begins. All three final PNGs were inspected; kit-owned processes exited. Evidence: `/private/tmp/search-focus-native-final/{01-typed,02-clear-retyped,03-revisited}.png`, report.json. No capture `type` fallback/insertText was used for these acceptance assertions. diff --git a/app/test/ui/input/cupertino_web_search_field_test.dart b/app/test/ui/input/cupertino_web_search_field_test.dart new file mode 100644 index 00000000..e669297a --- /dev/null +++ b/app/test/ui/input/cupertino_web_search_field_test.dart @@ -0,0 +1,89 @@ +import 'dart:ui' show Tristate; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/input/cupertino_web_search_field.dart'; + +void main() { + for (final provided in [false, true]) { + testWidgets( + 'native clear preserves semantics and focus (provided node: $provided)', + (tester) async { + final semantics = tester.ensureSemantics(); + + try { + final text = TextEditingController(); + final focus = FocusNode(); + addTearDown(text.dispose); + addTearDown(focus.dispose); + var clears = 0; + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + child: Center( + child: CupertinoWebSearchField( + controller: text, + placeholder: 'Search', + focusNode: provided ? focus : null, + onCleared: () { + clears++; + text.clear(); + }, + ), + ), + ), + ), + ); + final field = find.byType(EditableText); + final effective = tester.widget(field).focusNode; + effective.requestFocus(); + await tester.pumpAndSettle(); + final height = tester + .getSize(find.byType(CupertinoWebSearchField)) + .height; + final before = tester.getSemantics(field); + final identity = before.id; + expect( + before.getSemanticsData().flagsCollection.isEnabled, + Tristate.isTrue, + ); + await tester.enterText( + find.byType(CupertinoSearchTextField), + 'release', + ); + await tester.pumpAndSettle(); + final after = tester.getSemantics(field); + expect(after.id, identity); + expect( + tester.getSize(find.byType(CupertinoWebSearchField)).height, + height, + ); + expect( + after.getSemanticsData().flagsCollection.isEnabled, + Tristate.isTrue, + ); + expect(effective.hasPrimaryFocus, isTrue); + await tester.tap(find.byType(CupertinoButton)); + await tester.pumpAndSettle(); + expect(text.text, isEmpty); + expect(clears, 1); + expect( + tester.getSize(find.byType(CupertinoWebSearchField)).height, + height, + ); + expect(effective.hasPrimaryFocus, isTrue); + expect(tester.getSemantics(field).id, identity); + await tester.enterText( + find.byType(CupertinoSearchTextField), + 'again', + ); + await tester.pumpAndSettle(); + expect(text.text, 'again'); + expect(effective.hasPrimaryFocus, isTrue); + } finally { + semantics.dispose(); + } + }, + ); + } +}