Skip to content

Commit

Permalink
fix: maximized window optimization (AppFlowy-IO#5622)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin authored Jun 25, 2024
1 parent f59b8a2 commit c996c9c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
7 changes: 7 additions & 0 deletions frontend/appflowy_flutter/lib/core/config/kv_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class KVKeys {
/// {'dx': 10.0, 'dy': 10.0}
static const String windowPosition = 'windowPosition';

/// The key for saving the window status
///
/// The value is a json string with the following format:
/// { 'windowMaximized': true }
///
static const String windowMaximized = 'windowMaximized';

static const String kDocumentAppearanceFontSize =
'kDocumentAppearanceFontSize';
static const String kDocumentAppearanceFontFamily =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@ class WindowSizeManager {
'${scaleFactor.clamp(minScaleFactor, maxScaleFactor)}',
);
}

/// Set the window maximized status
Future<void> setWindowMaximized(bool isMaximized) async {
await getIt<KeyValueStorage>()
.set(KVKeys.windowMaximized, isMaximized.toString());
}

/// Get the window maximized status
Future<bool> getWindowMaximized() async {
return await getIt<KeyValueStorage>().getWithFormat<bool>(
KVKeys.windowMaximized,
(v) => bool.tryParse(v) ?? false,
) ??
false;
}
}
30 changes: 19 additions & 11 deletions frontend/appflowy_flutter/lib/startup/tasks/windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ import 'package:scaled_app/scaled_app.dart';
import 'package:window_manager/window_manager.dart';

class InitAppWindowTask extends LaunchTask with WindowListener {
InitAppWindowTask({
this.title = 'AppFlowy',
});
InitAppWindowTask({this.title = 'AppFlowy'});

final String title;

final windowSizeManager = WindowSizeManager();

@override
Expand Down Expand Up @@ -48,7 +45,7 @@ class InitAppWindowTask extends LaunchTask with WindowListener {
final position = await windowSizeManager.getPosition();

if (PlatformExtension.isWindows) {
doWhenWindowReady(() {
doWhenWindowReady(() async {
appWindow.minSize = windowOptions.minimumSize;
appWindow.maxSize = windowOptions.maximumSize;
appWindow.size = windowSize;
Expand All @@ -58,6 +55,13 @@ class InitAppWindowTask extends LaunchTask with WindowListener {
}

appWindow.show();

/// on Windows we maximize the window if it was previously closed
/// from a maximized state.
final isMaximized = await windowSizeManager.getWindowMaximized();
if (isMaximized) {
appWindow.maximize();
}
});
} else {
await windowManager.waitUntilReadyToShow(windowOptions, () async {
Expand All @@ -78,16 +82,20 @@ class InitAppWindowTask extends LaunchTask with WindowListener {
}

@override
Future<void> onWindowResize() async {
super.onWindowResize();
Future<void> onWindowMaximize() async {
super.onWindowMaximize();
await windowSizeManager.setWindowMaximized(true);
}

final currentWindowSize = await windowManager.getSize();
return windowSizeManager.setSize(currentWindowSize);
@override
Future<void> onWindowUnmaximize() async {
super.onWindowUnmaximize();
await windowSizeManager.setWindowMaximized(false);
}

@override
void onWindowMaximize() async {
super.onWindowMaximize();
Future<void> onWindowResize() async {
super.onWindowResize();

final currentWindowSize = await windowManager.getSize();
return windowSizeManager.setSize(currentWindowSize);
Expand Down

0 comments on commit c996c9c

Please sign in to comment.