From 0b9a6a280e6f49ace0de8e5bfec2947dcfe00411 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Fri, 21 Feb 2025 10:41:57 +0800 Subject: [PATCH] fix: remote id, update text and reserve selection (#10867) Signed-off-by: fufesou --- flutter/lib/common.dart | 15 +++++++++++++++ flutter/lib/desktop/pages/connection_page.dart | 5 +++-- flutter/lib/mobile/pages/connection_page.dart | 6 +++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 2d4084851..777924c66 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -3705,3 +3705,18 @@ Widget workaroundWindowBorder(BuildContext context, Widget child) { ], ); } + +void updateTextAndPreserveSelection(TextEditingController controller, String text) { + final preSelectionStart = controller.selection.start; + final preSelectionEnd = controller.selection.end; + // Only care about select all for now. + final isSelected = preSelectionEnd > preSelectionStart; + + // Set text will make the selection invalid. + controller.text = text; + + if (isSelected) { + controller.selection = TextSelection( + baseOffset: 0, extentOffset: controller.value.text.length); + } +} diff --git a/flutter/lib/desktop/pages/connection_page.dart b/flutter/lib/desktop/pages/connection_page.dart index 0f83bfb1f..dd9e84041 100644 --- a/flutter/lib/desktop/pages/connection_page.dart +++ b/flutter/lib/desktop/pages/connection_page.dart @@ -225,6 +225,7 @@ class _ConnectionPageState extends State } }); } + Get.put(_idEditingController); Get.put(_idController); windowManager.addListener(this); } @@ -395,8 +396,8 @@ class _ConnectionPageState extends State FocusNode fieldFocusNode, VoidCallback onFieldSubmitted, ) { - fieldTextEditingController.text = _idController.text; - Get.put(fieldTextEditingController); + updateTextAndPreserveSelection( + fieldTextEditingController, _idController.text); 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 1e8f4528e..07aaaef8c 100644 --- a/flutter/lib/mobile/pages/connection_page.dart +++ b/flutter/lib/mobile/pages/connection_page.dart @@ -74,6 +74,7 @@ class _ConnectionPageState extends State { } }); } + Get.put(_idEditingController); } @override @@ -214,9 +215,8 @@ class _ConnectionPageState extends State { TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) { - fieldTextEditingController.text = _idController.text; - Get.put( - fieldTextEditingController); + updateTextAndPreserveSelection( + fieldTextEditingController, _idController.text); return AutoSizeTextField( controller: fieldTextEditingController, focusNode: fieldFocusNode,