Parcourir la source

feat: the application remembers being resized (#2907)

Serge Brainin il y a 1 an
Parent
commit
a00dd5498e

+ 6 - 0
frontend/appflowy_flutter/lib/core/config/kv_keys.dart

@@ -19,6 +19,12 @@ class KVKeys {
   ///   {'height': 600.0, 'width': 800.0}
   static const String windowSize = 'windowSize';
 
+  /// The key for saving the window position
+  ///
+  /// The value is a json string with the following format:
+  ///   {'dx': 10.0, 'dy': 10.0}
+  static const String windowPosition = 'windowPosition';
+
   static const String kDocumentAppearanceFontSize =
       'kDocumentAppearanceFontSize';
   static const String kDocumentAppearanceFontFamily =

+ 5 - 4
frontend/appflowy_flutter/lib/core/helpers/target_platform.dart

@@ -1,11 +1,12 @@
-import 'package:flutter/foundation.dart' show TargetPlatform;
+import 'package:flutter/foundation.dart' show TargetPlatform, kIsWeb;
 
 extension TargetPlatformHelper on TargetPlatform {
   /// Convenience function to check if the app is running on a desktop computer.
   ///
   /// Easily check if on desktop by checking `defaultTargetPlatform.isDesktop`.
   bool get isDesktop =>
-      this == TargetPlatform.linux ||
-      this == TargetPlatform.macOS ||
-      this == TargetPlatform.windows;
+      !kIsWeb &&
+      (this == TargetPlatform.linux ||
+          this == TargetPlatform.macOS ||
+          this == TargetPlatform.windows);
 }

+ 25 - 1
frontend/appflowy_flutter/lib/startup/tasks/app_window_size_manager.dart

@@ -13,7 +13,10 @@ class WindowSizeManager {
   static const width = 'width';
   static const height = 'height';
 
-  Future<void> saveSize(Size size) async {
+  static const String dx = 'dx';
+  static const String dy = 'dy';
+
+  Future<void> setSize(Size size) async {
     final windowSize = {
       height: max(size.height, minWindowHeight),
       width: max(size.width, minWindowWidth),
@@ -33,4 +36,25 @@ class WindowSizeManager {
     );
     return Size(size[width]!, size[height]!);
   }
+
+  Future<void> setPosition(Offset offset) async {
+    await getIt<KeyValueStorage>().set(
+      KVKeys.windowPosition,
+      jsonEncode({
+        dx: offset.dx,
+        dy: offset.dy,
+      }),
+    );
+  }
+
+  Future<Offset?> getPosition() async {
+    final position = await getIt<KeyValueStorage>().get(KVKeys.windowPosition);
+    return position.fold(
+      (l) => null,
+      (r) {
+        final offset = json.decode(r);
+        return Offset(offset[dx], offset[dy]);
+      },
+    );
+  }
 }

+ 24 - 1
frontend/appflowy_flutter/lib/startup/tasks/windows.dart

@@ -40,12 +40,35 @@ class InitAppWindowTask extends LaunchTask with WindowListener {
     windowManager.waitUntilReadyToShow(windowOptions, () async {
       await windowManager.show();
       await windowManager.focus();
+
+      final position = await WindowSizeManager().getPosition();
+      if (position != null) {
+        await windowManager.setPosition(position);
+      }
     });
   }
 
   @override
   Future<void> onWindowResize() async {
+    super.onWindowResize();
+
     final currentWindowSize = await windowManager.getSize();
-    WindowSizeManager().saveSize(currentWindowSize);
+    WindowSizeManager().setSize(currentWindowSize);
+  }
+
+  @override
+  void onWindowMaximize() async {
+    super.onWindowMaximize();
+
+    final currentWindowSize = await windowManager.getSize();
+    WindowSizeManager().setSize(currentWindowSize);
+  }
+
+  @override
+  void onWindowMoved() async {
+    super.onWindowMoved();
+
+    final position = await windowManager.getPosition();
+    WindowSizeManager().setPosition(position);
   }
 }