From d369fcbf5f50101a1ba38c2237d7f6c7be2c5a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Thu, 11 Dec 2025 11:13:35 +0100 Subject: [PATCH] Simplify observed values about text selection in composer hasTextSelection had no interest. The other two values have been put inside an object to observe only one object. --- .../presentation/composer_controller.dart | 32 ++++++++----------- .../composer/presentation/composer_view.dart | 8 +++-- .../presentation/composer_view_web.dart | 8 +++-- .../widgets/mixins/text_selection_mixin.dart | 12 +++++++ 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/features/composer/presentation/composer_controller.dart b/lib/features/composer/presentation/composer_controller.dart index 7d6036e6c..c277162c3 100644 --- a/lib/features/composer/presentation/composer_controller.dart +++ b/lib/features/composer/presentation/composer_controller.dart @@ -159,9 +159,7 @@ class ComposerController extends BaseController final isMarkAsImportant = Rx(false); final isContentHeightExceeded = Rx(false); - final selectedText = Rxn(); - final hasTextSelection = false.obs; - final textSelectionCoordinates = Rxn(); + final editorTextSelection = Rxn(); final LocalFilePickerInteractor _localFilePickerInteractor; final LocalImagePickerInteractor _localImagePickerInteractor; @@ -928,7 +926,7 @@ class ComposerController extends BaseController } void showAIScribeMenuForSelectedText(BuildContext context, {Offset? buttonPosition}) { - final selection = selectedText.value; + final selection = editorTextSelection.value?.selectedText; if (selection == null || selection.isEmpty) { return; } @@ -944,22 +942,18 @@ class ComposerController extends BaseController } void handleTextSelection(TextSelectionData? textSelectionData) { - if (textSelectionData != null) { - hasTextSelection.value = textSelectionData.hasSelection; - selectedText.value = textSelectionData.selectedText; - - if (textSelectionData.coordinates != null) { - textSelectionCoordinates.value = Offset( - textSelectionData.coordinates!.x, - textSelectionData.coordinates!.y, - ); - } else { - textSelectionCoordinates.value = null; - } + if (textSelectionData != null && textSelectionData.hasSelection) { + editorTextSelection.value = EditorTextSelection( + selectedText: textSelectionData.selectedText, + coordinates: textSelectionData.coordinates != null + ? Offset( + textSelectionData.coordinates!.x, + textSelectionData.coordinates!.y, + ) + : null, + ); } else { - hasTextSelection.value = false; - selectedText.value = null; - textSelectionCoordinates.value = null; + editorTextSelection.value = null; } } diff --git a/lib/features/composer/presentation/composer_view.dart b/lib/features/composer/presentation/composer_view.dart index 6856ab9cb..cd5f0185f 100644 --- a/lib/features/composer/presentation/composer_view.dart +++ b/lib/features/composer/presentation/composer_view.dart @@ -561,9 +561,11 @@ class ComposerView extends GetWidget { } return Obx(() { - if (controller.hasTextSelection.value && - controller.textSelectionCoordinates.value != null) { - final coordinates = controller.textSelectionCoordinates.value!; + final textSelection = controller.editorTextSelection.value; + if (textSelection != null && + textSelection.hasSelection && + textSelection.coordinates != null) { + final coordinates = textSelection.coordinates!; // Account for the horizontal padding around the editor const editorHorizontalPadding = 12.0; return PositionedDirectional( diff --git a/lib/features/composer/presentation/composer_view_web.dart b/lib/features/composer/presentation/composer_view_web.dart index eaf4a60df..2759a1e84 100644 --- a/lib/features/composer/presentation/composer_view_web.dart +++ b/lib/features/composer/presentation/composer_view_web.dart @@ -963,9 +963,11 @@ class ComposerView extends GetWidget { } return Obx(() { - if (controller.hasTextSelection.value && - controller.textSelectionCoordinates.value != null) { - final coordinates = controller.textSelectionCoordinates.value!; + final textSelection = controller.editorTextSelection.value; + if (textSelection != null && + textSelection.hasSelection && + textSelection.coordinates != null) { + final coordinates = textSelection.coordinates!; return PositionedDirectional( start: coordinates.dx, top: coordinates.dy, diff --git a/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart b/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart index 24fa2bb92..3437ac3d2 100644 --- a/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart +++ b/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart @@ -69,6 +69,18 @@ class TextSelectionCoordinates { Offset get position => Offset(x, y); } +class EditorTextSelection { + final String? selectedText; + final Offset? coordinates; + + const EditorTextSelection({ + this.selectedText, + this.coordinates, + }); + + bool get hasSelection => selectedText != null && selectedText!.isNotEmpty; +} + mixin TextSelectionMixin on State { OnTextSelectionChanged? get onSelectionChanged => null;