From f631c1c28d918c1920e825914eab5714f05e18e4 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Thu, 20 Feb 2025 19:35:04 +0800 Subject: [PATCH] refact: Remote ID editor, only select text on focus (#10854) Signed-off-by: fufesou --- flutter/lib/common/widgets/autocomplete.dart | 15 ++++++++----- .../lib/desktop/pages/connection_page.dart | 22 ++++++++----------- flutter/lib/mobile/pages/connection_page.dart | 19 ++++++++-------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/flutter/lib/common/widgets/autocomplete.dart b/flutter/lib/common/widgets/autocomplete.dart index 1ea6d47be..ec64cca18 100644 --- a/flutter/lib/common/widgets/autocomplete.dart +++ b/flutter/lib/common/widgets/autocomplete.dart @@ -8,13 +8,16 @@ import 'package:flutter_hbb/common/widgets/peer_card.dart'; class AllPeersLoader { List peers = []; - bool isPeersLoading = false; - bool isPeersLoaded = false; + bool _isPeersLoading = false; + bool _isPeersLoaded = false; final String _listenerKey = 'AllPeersLoader'; late void Function(VoidCallback) setState; + bool get needLoad => !_isPeersLoaded && !_isPeersLoading; + bool get isPeersLoaded => _isPeersLoaded; + AllPeersLoader(); void init(void Function(VoidCallback) setState) { @@ -33,10 +36,10 @@ class AllPeersLoader { } Future getAllPeers() async { - if (isPeersLoaded || isPeersLoading) { + if (!needLoad) { return; } - isPeersLoading = true; + _isPeersLoading = true; if (gFFI.recentPeersModel.peers.isEmpty) { bind.mainLoadRecentPeers(); @@ -96,8 +99,8 @@ class AllPeersLoader { peers = parsedPeers; setState(() { - isPeersLoading = false; - isPeersLoaded = true; + _isPeersLoading = false; + _isPeersLoaded = true; }); } } diff --git a/flutter/lib/desktop/pages/connection_page.dart b/flutter/lib/desktop/pages/connection_page.dart index 2dc387067..0f83bfb1f 100644 --- a/flutter/lib/desktop/pages/connection_page.dart +++ b/flutter/lib/desktop/pages/connection_page.dart @@ -282,8 +282,15 @@ class _ConnectionPageState extends State void onFocusChanged() { _idInputFocused.value = _idFocusNode.hasFocus; - if (_idFocusNode.hasFocus && !_allPeersLoader.isPeersLoading) { - _allPeersLoader.getAllPeers(); + if (_idFocusNode.hasFocus) { + if (_allPeersLoader.needLoad) { + _allPeersLoader.getAllPeers(); + } + + final textLength = _idEditingController.value.text.length; + // Select all to facilitate removing text, just following the behavior of address input of chrome. + _idEditingController.selection = + TextSelection(baseOffset: 0, extentOffset: textLength); } } @@ -390,17 +397,6 @@ class _ConnectionPageState extends State ) { fieldTextEditingController.text = _idController.text; Get.put(fieldTextEditingController); - - // The listener will be added multiple times when the widget is rebuilt. - // We may need to use the `RawAutocomplete` to get the focus node. - - // Temporarily remove Selection because Selection can cause users to accidentally delete previously entered content during input. - // final textLength = - // fieldTextEditingController.value.text.length; - // // Select all to facilitate removing text, just following the behavior of address input of chrome. - // fieldTextEditingController.selection = - // TextSelection(baseOffset: 0, extentOffset: textLength); - return Obx(() => TextField( autocorrect: false, enableSuggestions: false, diff --git a/flutter/lib/mobile/pages/connection_page.dart b/flutter/lib/mobile/pages/connection_page.dart index e550200bf..1e8f4528e 100644 --- a/flutter/lib/mobile/pages/connection_page.dart +++ b/flutter/lib/mobile/pages/connection_page.dart @@ -104,8 +104,15 @@ class _ConnectionPageState extends State { void onFocusChanged() { _idEmpty.value = _idEditingController.text.isEmpty; - if (_idFocusNode.hasFocus && !_allPeersLoader.isPeersLoading) { - _allPeersLoader.getAllPeers(); + if (_idFocusNode.hasFocus) { + if (_allPeersLoader.needLoad) { + _allPeersLoader.getAllPeers(); + } + + final textLength = _idEditingController.value.text.length; + // Select all to facilitate removing text, just following the behavior of address input of chrome. + _idEditingController.selection = + TextSelection(baseOffset: 0, extentOffset: textLength); } } @@ -210,14 +217,6 @@ class _ConnectionPageState extends State { fieldTextEditingController.text = _idController.text; Get.put( fieldTextEditingController); - - // Temporarily remove Selection because Selection can cause users to accidentally delete previously entered content during input. - // final textLength = - // fieldTextEditingController.value.text.length; - // // select all to facilitate removing text, just following the behavior of address input of chrome - // fieldTextEditingController.selection = TextSelection( - // baseOffset: 0, extentOffset: textLength); - return AutoSizeTextField( controller: fieldTextEditingController, focusNode: fieldFocusNode,