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 isContentHeightExceeded = Rx<bool>(false);
final selectedText = Rxn<String>();
final hasTextSelection = false.obs;
final textSelectionCoordinates = Rxn<Offset>();
final editorTextSelection = Rxn<EditorTextSelection>();
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;
}
}
@@ -561,9 +561,11 @@ class ComposerView extends GetWidget<ComposerController> {
}
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(
@@ -963,9 +963,11 @@ class ComposerView extends GetWidget<ComposerController> {
}
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,
@@ -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<T extends StatefulWidget> on State<T> {
OnTextSelectionChanged? get onSelectionChanged => null;