fix: maximized window optimization (#5622)

This commit is contained in:
Mathias Mogensen 2024-06-25 20:07:43 +02:00 committed by GitHub
parent f59b8a2e5c
commit c996c9c28e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 12 deletions

View File

@ -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 =

View File

@ -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;
}
}

View File

@ -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
@ -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;
@ -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 {
@ -78,16 +82,20 @@ class InitAppWindowTask extends LaunchTask with WindowListener {
}
@override
Future<void> onWindowResize() async {
super.onWindowResize();
final currentWindowSize = await windowManager.getSize();
return windowSizeManager.setSize(currentWindowSize);
Future<void> onWindowMaximize() async {
super.onWindowMaximize();
await windowSizeManager.setWindowMaximized(true);
}
@override
void onWindowMaximize() async {
super.onWindowMaximize();
Future<void> onWindowUnmaximize() async {
super.onWindowUnmaximize();
await windowSizeManager.setWindowMaximized(false);
}
@override
Future<void> onWindowResize() async {
super.onWindowResize();
final currentWindowSize = await windowManager.getSize();
return windowSizeManager.setSize(currentWindowSize);