feat: the application remembers being resized (#2907)

This commit is contained in:
Serge Brainin 2023-07-18 05:45:06 +03:00 committed by GitHub
parent 7dcc7c221f
commit a00dd5498e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 6 deletions

View File

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

View File

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

View File

@ -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]);
},
);
}
}

View File

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