Hide keyboard and selection icons when Scribe mobile modal opened

When opening Scribe mobile modal, we now unfocus the composer to hide the keyboard and the selection start and end icons from the OS.

Add new methods to save, restore, and clear text selection in HTML editor. These methods allow preserving and restoring user selection state when opening the Scribe mobile modal so that the "replace" action still works.
This commit is contained in:
Théo Poizat
2026-01-12 11:08:13 +01:00
committed by Dat H. Pham
parent 09fe002d3c
commit 84176cb113
5 changed files with 135 additions and 4 deletions
@@ -71,6 +71,83 @@ extension HandleAiScribeInComposerExtension on ComposerController {
}
}
Future<bool> saveSelection() async {
try {
if (PlatformInfo.isWeb) {
final result = await richTextWebController?.editorController.evaluateJavascriptWeb(
HtmlUtils.saveSelection.name,
hasReturnValue: true,
) ?? false;
return result;
} else {
final result = await richTextMobileTabletController?.htmlEditorApi?.webViewController
.evaluateJavascript(
source: HtmlUtils.saveSelection.script,
) ?? false;
return result;
}
} catch (e) {
logError('$runtimeType::saveSelection:Exception = $e');
return false;
}
}
Future<bool> restoreSelection() async {
try {
if (PlatformInfo.isWeb) {
final result = await richTextWebController?.editorController.evaluateJavascriptWeb(
HtmlUtils.restoreSelection.name,
hasReturnValue: true,
) ?? false;
return result;
} else {
final result = await richTextMobileTabletController?.htmlEditorApi?.webViewController
.evaluateJavascript(
source: HtmlUtils.restoreSelection.script,
) ?? false;
return result;
}
} catch (e) {
logError('$runtimeType::restoreSelection:Exception = $e');
return false;
}
}
Future<void> clearSavedSelection() async {
try {
if (PlatformInfo.isWeb) {
await richTextWebController?.editorController.evaluateJavascriptWeb(
HtmlUtils.clearSavedSelection.name,
hasReturnValue: false,
);
} else {
await richTextMobileTabletController?.htmlEditorApi?.webViewController
.evaluateJavascript(
source: HtmlUtils.clearSavedSelection.script,
);
}
} catch (e) {
logError('$runtimeType::clearSavedSelection:Exception = $e');
}
}
Future<void> unfocusEditor() async {
final editorApi = richTextMobileTabletController?.htmlEditorApi;
if (PlatformInfo.isIOS) {
await editorApi?.unfocus();
} else if (PlatformInfo.isAndroid) {
await editorApi?.hideKeyboard();
await editorApi?.unfocus();
}
}
Future<void> saveAndUnfocusForModal() async {
final saved = await saveSelection();
if (saved) {
await unfocusEditor();
}
}
void clearTextInEditor() {
try {
if (PlatformInfo.isWeb) {
@@ -92,6 +169,11 @@ extension HandleAiScribeInComposerExtension on ComposerController {
// If there is a selection, it will replace the selection, else it will replace everything
Future<void> onReplaceTextCallback(String text) async {
final selection = editorTextSelection.value?.selectedText;
if (PlatformInfo.isMobile) {
await restoreSelection();
}
if (selection == null || selection.isEmpty) {
clearTextInEditor();
}
@@ -103,6 +185,10 @@ extension HandleAiScribeInComposerExtension on ComposerController {
clearFocusRecipients();
clearFocusSubject();
if (PlatformInfo.isMobile) {
await saveAndUnfocusForModal();
}
final fullText = await _getTextOnlyContentInEditor();
await AiScribeModalManager.showAIScribeMenuModal(
@@ -129,6 +215,10 @@ extension HandleAiScribeInComposerExtension on ComposerController {
await onInsertTextCallback(suggestionText);
break;
}
if (PlatformInfo.isMobile) {
await clearSavedSelection();
}
}
void handleTextSelection(TextSelectionData? textSelectionData) {
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:core/utils/platform_info.dart';
import 'package:scribe/scribe/ai/presentation/widgets/overlay/ai_selection_overlay.dart';
import 'package:tmail_ui_user/features/composer/presentation/composer_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/ai_scribe/handle_ai_scribe_in_composer_extension.dart';
@@ -29,8 +30,12 @@ class ComposerAiScribeSelectionOverlay extends StatelessWidget {
});
}
void _clearComposerInputFocus() {
Future<void> _clearComposerInputFocus() async {
controller.clearFocusRecipients();
controller.clearFocusSubject();
if (PlatformInfo.isMobile) {
await controller.saveAndUnfocusForModal();
}
}
}