Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Preserve IME input in Auth type-to-search

Neeraj Gupta0b5640493724
diff --git a/mobile/apps/auth/changes/desktop-type-to-search-ime.md b/mobile/apps/auth/changes/desktop-type-to-search-ime.md
new file mode 100644
index 0000000000..f6840e91a2
--- /dev/null
+++ b/mobile/apps/auth/changes/desktop-type-to-search-ime.md
@@ -0,0 +1 @@
+- Added type-to-search on desktop for faster code lookup (@StDensity).
diff --git a/mobile/apps/auth/lib/ui/home/widgets/home_search_field.dart b/mobile/apps/auth/lib/ui/home/widgets/home_search_field.dart
new file mode 100644
index 0000000000..8810634f3c
--- /dev/null
+++ b/mobile/apps/auth/lib/ui/home/widgets/home_search_field.dart
@@ -0,0 +1,38 @@
+import 'package:flutter/material.dart';
+
+/// Focuses the persistent search field and leaves the triggering key event for
+/// the platform text input system to process.
+bool activateHomeSearchFromKeyEvent({
+ required VoidCallback showSearch,
+ required FocusNode focusNode,
+}) {
+ showSearch();
+ focusNode.requestFocus();
+ FocusManager.instance.applyFocusChangesIfNeeded();
+ return false;
+}
+
+/// Keeps the search input attached and programmatically focusable while hidden.
+class PersistentSearchField extends StatelessWidget {
+ const PersistentSearchField({
+ super.key,
+ required this.visible,
+ required this.child,
+ });
+
+ final bool visible;
+ final Widget child;
+
+ @override
+ Widget build(BuildContext context) {
+ return FocusTraversalGroup(
+ descendantsAreTraversable: visible,
+ child: Visibility(
+ visible: visible,
+ maintainState: true,
+ maintainFocusability: true,
+ child: child,
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/auth/lib/ui/home_page.dart b/mobile/apps/auth/lib/ui/home_page.dart
index e8323561ba..d0ba76a9b1 100644
--- a/mobile/apps/auth/lib/ui/home_page.dart
+++ b/mobile/apps/auth/lib/ui/home_page.dart
@@ -35,6 +35,7 @@ import 'package:ente_auth/ui/home/home_empty_state.dart';
import 'package:ente_auth/ui/home/shortcuts.dart';
import 'package:ente_auth/ui/home/speed_dial_label_widget.dart';
import 'package:ente_auth/ui/home/widgets/auth_logo_widget.dart';
+import 'package:ente_auth/ui/home/widgets/home_search_field.dart';
import 'package:ente_auth/ui/reorder_codes_page.dart';
import 'package:ente_auth/ui/scanner_page.dart';
import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';
@@ -1068,7 +1069,8 @@ class _HomePageState extends State<HomePage> {
return true;
}
- // On desktop, typing a printable character opens search with that input.
+ // On desktop, typing a printable character opens search. Keep the key
+ // unhandled so the text input system can preserve IME composition.
if (PlatformDetector.isDesktop() &&
!isHomeSearchFocused &&
!isMetaKeyPressed &&
@@ -1078,26 +1080,19 @@ class _HomePageState extends State<HomePage> {
!isFocusedCopyShortcut &&
event.character != null &&
event.character!.trim().isNotEmpty) {
- final String searchText = _showSearchBox
- ? _textController.text + event.character!
- : event.character!;
- setState(() {
- _showSearchBox = true;
- _searchText = searchText;
- _textController.value = TextEditingValue(
- text: searchText,
- selection: TextSelection.collapsed(offset: searchText.length),
- );
- });
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (!mounted) return;
- searchBoxFocusNode.requestFocus();
- });
- return true;
+ return activateHomeSearchFromKeyEvent(
+ showSearch: () {
+ setState(() {
+ _showSearchBox = true;
+ });
+ },
+ focusNode: searchBoxFocusNode,
+ );
}
// Only use Escape for the HomePage search UI.
if (event.logicalKey == LogicalKeyboardKey.escape && _showSearchBox) {
+ searchBoxFocusNode.unfocus();
setState(() {
_textController.clear();
_searchText = "";
@@ -1505,6 +1500,36 @@ class _HomePageState extends State<HomePage> {
) {
final colorScheme = getEnteColorScheme(context);
final iconColor = colorScheme.textBase;
+ final searchField = AndroidTextInputAutofocus(
+ enabled: _autoFocusSearch && _showSearchBox,
+ focusNode: searchBoxFocusNode,
+ child: TextField(
+ autocorrect: false,
+ enableSuggestions: false,
+ autofocus: _autoFocusSearch && _showSearchBox && !Platform.isAndroid,
+ selectAllOnFocus: false,
+ controller: _textController,
+ onChanged: (val) {
+ _searchText = val;
+ _applyFilteringAndRefresh();
+ },
+ onSubmitted: (_) {
+ if (_filteredCodes.isNotEmpty) {
+ // Move focus to the first item in the grid
+ _firstItemFocusNode.requestFocus();
+ }
+ },
+ decoration: InputDecoration(
+ hintText: l10n.searchHint,
+ border: InputBorder.none,
+ focusedBorder: InputBorder.none,
+ ),
+ focusNode: searchBoxFocusNode,
+ ),
+ );
+ final nonDesktopTitle = _showSearchBox
+ ? searchField
+ : const AuthLogoWidget(height: 18);
return AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
@@ -1521,35 +1546,18 @@ class _HomePageState extends State<HomePage> {
scaffoldKey.currentState?.openDrawer();
},
),
- title: !_showSearchBox
- ? const AuthLogoWidget(height: 18)
- : AndroidTextInputAutofocus(
- enabled: _autoFocusSearch,
- focusNode: searchBoxFocusNode,
- child: TextField(
- autocorrect: false,
- enableSuggestions: false,
- autofocus: _autoFocusSearch && !Platform.isAndroid,
- selectAllOnFocus: false,
- controller: _textController,
- onChanged: (val) {
- _searchText = val;
- _applyFilteringAndRefresh();
- },
- onSubmitted: (_) {
- if (_filteredCodes.isNotEmpty) {
- // Move focus to the first item in the grid
- _firstItemFocusNode.requestFocus();
- }
- },
- decoration: InputDecoration(
- hintText: l10n.searchHint,
- border: InputBorder.none,
- focusedBorder: InputBorder.none,
+ title: isDesktop
+ ? Stack(
+ alignment: Alignment.center,
+ children: [
+ if (!_showSearchBox) const AuthLogoWidget(height: 18),
+ PersistentSearchField(
+ visible: _showSearchBox,
+ child: searchField,
),
- focusNode: searchBoxFocusNode,
- ),
- ),
+ ],
+ )
+ : nonDesktopTitle,
centerTitle: true,
actions: <Widget>[
if (isDesktop)
@@ -1600,6 +1608,7 @@ class _HomePageState extends State<HomePage> {
setState(() {
_showSearchBox = !_showSearchBox;
if (!_showSearchBox) {
+ searchBoxFocusNode.unfocus();
_textController.clear();
_searchText = "";
} else {
diff --git a/mobile/apps/auth/test/ui/home/widgets/home_search_field_test.dart b/mobile/apps/auth/test/ui/home/widgets/home_search_field_test.dart
new file mode 100644
index 0000000000..21a63d5bf0
--- /dev/null
+++ b/mobile/apps/auth/test/ui/home/widgets/home_search_field_test.dart
@@ -0,0 +1,59 @@
+import 'package:ente_auth/ui/home/widgets/home_search_field.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ testWidgets('activates search before handing the first key to the IME', (
+ tester,
+ ) async {
+ final controller = TextEditingController();
+ final focusNode = FocusNode();
+ addTearDown(controller.dispose);
+ addTearDown(focusNode.dispose);
+
+ var visible = false;
+ late StateSetter setHarnessState;
+ await tester.pumpWidget(
+ MaterialApp(
+ home: StatefulBuilder(
+ builder: (context, setState) {
+ setHarnessState = setState;
+ return Material(
+ child: PersistentSearchField(
+ visible: visible,
+ child: TextField(controller: controller, focusNode: focusNode),
+ ),
+ );
+ },
+ ),
+ ),
+ );
+
+ expect(find.byType(TextField), findsNothing);
+ expect(tester.testTextInput.hasAnyClients, isFalse);
+
+ final handled = activateHomeSearchFromKeyEvent(
+ showSearch: () {
+ setHarnessState(() {
+ visible = true;
+ });
+ },
+ focusNode: focusNode,
+ );
+
+ expect(handled, isFalse);
+ expect(focusNode.hasFocus, isTrue);
+ expect(tester.testTextInput.hasAnyClients, isTrue);
+
+ await tester.pump();
+ const composingValue = TextEditingValue(
+ text: 'に',
+ selection: TextSelection.collapsed(offset: 1),
+ composing: TextRange(start: 0, end: 1),
+ );
+ tester.testTextInput.updateEditingValue(composingValue);
+ await tester.pump();
+
+ expect(controller.value, composingValue);
+ });
+}