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.
This commit is contained in:
Théo Poizat
2025-12-11 11:13:35 +01:00
committed by Dat H. Pham
parent 2228596c1c
commit d369fcbf5f
4 changed files with 35 additions and 25 deletions
@@ -159,9 +159,7 @@ class ComposerController extends BaseController
final isMarkAsImportant = Rx<bool>(false); final isMarkAsImportant = Rx<bool>(false);
final isContentHeightExceeded = Rx<bool>(false); final isContentHeightExceeded = Rx<bool>(false);
final selectedText = Rxn<String>(); final editorTextSelection = Rxn<EditorTextSelection>();
final hasTextSelection = false.obs;
final textSelectionCoordinates = Rxn<Offset>();
final LocalFilePickerInteractor _localFilePickerInteractor; final LocalFilePickerInteractor _localFilePickerInteractor;
final LocalImagePickerInteractor _localImagePickerInteractor; final LocalImagePickerInteractor _localImagePickerInteractor;
@@ -928,7 +926,7 @@ class ComposerController extends BaseController
} }
void showAIScribeMenuForSelectedText(BuildContext context, {Offset? buttonPosition}) { void showAIScribeMenuForSelectedText(BuildContext context, {Offset? buttonPosition}) {
final selection = selectedText.value; final selection = editorTextSelection.value?.selectedText;
if (selection == null || selection.isEmpty) { if (selection == null || selection.isEmpty) {
return; return;
} }
@@ -944,22 +942,18 @@ class ComposerController extends BaseController
} }
void handleTextSelection(TextSelectionData? textSelectionData) { void handleTextSelection(TextSelectionData? textSelectionData) {
if (textSelectionData != null) { if (textSelectionData != null && textSelectionData.hasSelection) {
hasTextSelection.value = textSelectionData.hasSelection; editorTextSelection.value = EditorTextSelection(
selectedText.value = textSelectionData.selectedText; selectedText: textSelectionData.selectedText,
coordinates: textSelectionData.coordinates != null
if (textSelectionData.coordinates != null) { ? Offset(
textSelectionCoordinates.value = Offset( textSelectionData.coordinates!.x,
textSelectionData.coordinates!.x, textSelectionData.coordinates!.y,
textSelectionData.coordinates!.y, )
); : null,
} else { );
textSelectionCoordinates.value = null;
}
} else { } else {
hasTextSelection.value = false; editorTextSelection.value = null;
selectedText.value = null;
textSelectionCoordinates.value = null;
} }
} }
@@ -561,9 +561,11 @@ class ComposerView extends GetWidget<ComposerController> {
} }
return Obx(() { return Obx(() {
if (controller.hasTextSelection.value && final textSelection = controller.editorTextSelection.value;
controller.textSelectionCoordinates.value != null) { if (textSelection != null &&
final coordinates = controller.textSelectionCoordinates.value!; textSelection.hasSelection &&
textSelection.coordinates != null) {
final coordinates = textSelection.coordinates!;
// Account for the horizontal padding around the editor // Account for the horizontal padding around the editor
const editorHorizontalPadding = 12.0; const editorHorizontalPadding = 12.0;
return PositionedDirectional( return PositionedDirectional(
@@ -963,9 +963,11 @@ class ComposerView extends GetWidget<ComposerController> {
} }
return Obx(() { return Obx(() {
if (controller.hasTextSelection.value && final textSelection = controller.editorTextSelection.value;
controller.textSelectionCoordinates.value != null) { if (textSelection != null &&
final coordinates = controller.textSelectionCoordinates.value!; textSelection.hasSelection &&
textSelection.coordinates != null) {
final coordinates = textSelection.coordinates!;
return PositionedDirectional( return PositionedDirectional(
start: coordinates.dx, start: coordinates.dx,
top: coordinates.dy, top: coordinates.dy,
@@ -69,6 +69,18 @@ class TextSelectionCoordinates {
Offset get position => Offset(x, y); 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<T extends StatefulWidget> on State<T> { mixin TextSelectionMixin<T extends StatefulWidget> on State<T> {
OnTextSelectionChanged? get onSelectionChanged => null; OnTextSelectionChanged? get onSelectionChanged => null;