Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: resume selection on window focus (#6455)

Mathias Mogensenb36babf75437
diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart
new file mode 100644
index 0000000000..61984eae5c
--- /dev/null
+++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart
@@ -0,0 +1,32 @@
+import 'dart:ui';
+
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:integration_test/integration_test.dart';
+
+import '../../shared/util.dart';
+
+void main() {
+ final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
+
+ group('Editor AppLifeCycle tests', () {
+ testWidgets(
+ 'Selection is added back after pausing AppFlowy',
+ (tester) async {
+ await tester.initializeAppFlowy();
+ await tester.tapAnonymousSignInButton();
+
+ final selection = Selection.single(path: [4], startOffset: 0);
+ await tester.editor.updateSelection(selection);
+
+ binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ expect(tester.editor.getCurrentEditorState().selection, null);
+
+ binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ await tester.pumpAndSettle();
+
+ expect(tester.editor.getCurrentEditorState().selection, selection);
+ },
+ );
+ });
+}
diff --git a/frontend/appflowy_flutter/integration_test/desktop_runner_3.dart b/frontend/appflowy_flutter/integration_test/desktop_runner_3.dart
index 90cbc1e813..1f78a7d598 100644
--- a/frontend/appflowy_flutter/integration_test/desktop_runner_3.dart
+++ b/frontend/appflowy_flutter/integration_test/desktop_runner_3.dart
@@ -4,6 +4,8 @@ import 'desktop/board/board_test_runner.dart' as board_test_runner;
import 'desktop/database/database_row_cover_test.dart'
as database_row_cover_test;
import 'desktop/document/document_title_test.dart' as document_title_test;
+import 'desktop/document/document_app_lifecycle_test.dart'
+ as document_app_lifecycle_test;
import 'desktop/document/document_sub_page_test.dart' as document_sub_page_test;
import 'desktop/grid/grid_filter_and_sort_test.dart'
as grid_filter_and_sort_test_runner;
@@ -11,8 +13,8 @@ import 'desktop/grid/grid_reopen_test.dart' as grid_reopen_test_runner;
import 'desktop/grid/grid_reorder_row_test.dart'
as grid_reorder_row_test_runner;
import 'desktop/grid/grid_edit_row_test.dart' as grid_edit_row_test_runner;
-import 'desktop/settings/settings_runner.dart' as settings_test_runner;
import 'desktop/grid/grid_row_test.dart' as grid_create_row_test_runner;
+import 'desktop/settings/settings_runner.dart' as settings_test_runner;
import 'desktop/sidebar/sidebar_test_runner.dart' as sidebar_test_runner;
import 'desktop/uncategorized/emoji_shortcut_test.dart' as emoji_shortcut_test;
import 'desktop/uncategorized/empty_test.dart' as first_test;
@@ -51,4 +53,5 @@ Future<void> runIntegration3OnDesktop() async {
zoom_in_out_test.main();
document_title_test.main();
document_sub_page_test.main();
+ document_app_lifecycle_test.main();
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
index 972bb6bbc3..0dd62b24da 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
@@ -58,7 +58,8 @@ class AppFlowyEditorPage extends StatefulWidget {
State<AppFlowyEditorPage> createState() => _AppFlowyEditorPageState();
}
-class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
+class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
+ with WidgetsBindingObserver {
late final ScrollController effectiveScrollController;
late final InlineActionsService inlineActionsService = InlineActionsService(
@@ -126,6 +127,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
@override
void initState() {
super.initState();
+ WidgetsBinding.instance.addObserver(this);
if (widget.useViewInfoBloc) {
viewInfoBloc.add(
@@ -163,6 +165,8 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
// customize the dynamic theme color
_customizeBlockComponentBackgroundColorDecorator();
+ widget.editorState.selectionNotifier.addListener(onSelectionChanged);
+
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) {
return;
@@ -181,6 +185,24 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
});
}
+ Selection? previousSelection;
+
+ void onSelectionChanged() {
+ if (widget.editorState.isDisposed || widget.editorState.selection == null) {
+ return;
+ }
+
+ previousSelection = widget.editorState.selection;
+ }
+
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ super.didChangeAppLifecycleState(state);
+ if (state == AppLifecycleState.resumed && !widget.editorState.isDisposed) {
+ widget.editorState.selection = previousSelection;
+ }
+ }
+
@override
void didChangeDependencies() {
final currFocusManager = AFFocusManager.maybeOf(context);
@@ -202,6 +224,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
@override
void dispose() {
+ widget.editorState.selectionNotifier.removeListener(onSelectionChanged);
widget.editorState.service.keyboardService?.unregisterInterceptor(
editorKeyboardInterceptor,
);