feat: Implement window size settings saving (#2648)

This commit addresses the feature request to save the window size settings.
The application will now remember the user's preferred window size, allowing
for a more personalized and seamless user experience.

Fixes: #2544

Co-authored-by: Akarsh Jain <akarsh.jain.790@gmail.com>
This commit is contained in:
Lucas.Xu 2023-05-30 11:06:23 +08:00 committed by GitHub
parent 65a910291b
commit dac4bd88e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 3 deletions

View File

@ -12,4 +12,10 @@ class KVKeys {
/// - local
/// - supabase
static const String loginType = '$prefix.login_type';
/// The key for saving the window size
///
/// The value is a json string with the following format:
/// {'height': 600.0, 'width': 800.0}
static const String windowSize = 'windowSize';
}

View File

@ -0,0 +1,36 @@
import 'dart:convert';
import 'dart:math';
import 'dart:ui';
import 'package:appflowy/core/config/kv.dart';
import 'package:appflowy/core/config/kv_keys.dart';
import 'package:appflowy/startup/startup.dart';
class WindowSizeManager {
static const double minWindowHeight = 400.0;
static const double minWindowWidth = 600.0;
Future<void> saveSize(Size size) async {
final windowSize = {
'height': max(size.height, minWindowHeight),
'width': max(size.width, minWindowWidth),
};
await getIt<KeyValueStorage>().set(
KVKeys.windowSize,
jsonEncode(windowSize),
);
}
Future<Size> getSize() async {
const defaultWindowSize = '{"height": 600.0, "width": 800.0}';
final windowSize = await getIt<KeyValueStorage>().get(KVKeys.windowSize);
final size = json.decode(
windowSize.fold(
(l) => defaultWindowSize,
(r) => r,
),
);
return Size(size['width']!, size['height']!);
}
}

View File

@ -2,10 +2,12 @@ import 'dart:ui';
import 'package:appflowy/core/helpers/helpers.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/startup/tasks/app_window_size_manager.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';
class InitAppWindowTask extends LaunchTask {
class InitAppWindowTask extends LaunchTask with WindowListener {
const InitAppWindowTask({
this.minimumSize = const Size(800, 600),
this.title = 'AppFlowy',
@ -22,9 +24,17 @@ class InitAppWindowTask extends LaunchTask {
}
await windowManager.ensureInitialized();
windowManager.addListener(this);
WindowOptions windowOptions = WindowOptions(
minimumSize: minimumSize,
final windowSize = await WindowSizeManager().getSize();
final windowOptions = WindowOptions(
size: windowSize,
minimumSize: const Size(
WindowSizeManager.minWindowWidth,
WindowSizeManager.minWindowHeight,
),
center: true,
title: title,
);
@ -33,4 +43,10 @@ class InitAppWindowTask extends LaunchTask {
await windowManager.focus();
});
}
@override
Future<void> onWindowResize() async {
final currentWindowSize = await windowManager.getSize();
WindowSizeManager().saveSize(currentWindowSize);
}
}