From 298157114e3025498e6553cae01d4bad72aa21e2 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Thu, 24 Feb 2022 14:05:30 +0100 Subject: [PATCH 1/2] Store window size in shared preferences. --- lib/desktop/init.dart | 23 +++++++++++++++++++++-- lib/main.dart | 5 +++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/desktop/init.dart b/lib/desktop/init.dart index ddda762a..91308292 100755 --- a/lib/desktop/init.dart +++ b/lib/desktop/init.dart @@ -4,6 +4,7 @@ import 'dart:ui'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:window_manager/window_manager.dart'; import '../oath/state.dart'; @@ -15,6 +16,8 @@ import 'qr_scanner.dart'; import 'state.dart'; final _log = Logger('desktop.init'); +const String _keyWidth = 'DESKTOP_WINDOW_WIDTH'; +const String _keyHeight = 'DESKTOP_WINDOW_HEIGHT'; initializeLogging() { Logger.root.onRecord.listen((record) { @@ -26,15 +29,31 @@ initializeLogging() { _log.info('Logging initialized, outputting to stderr'); } -Future> initializeAndGetOverrides() async { +class _WindowResizeListener extends WindowListener { + final SharedPreferences _prefs; + _WindowResizeListener(this._prefs); + + @override + void onWindowResize() async { + final size = await windowManager.getSize(); + await _prefs.setDouble(_keyWidth, size.width); + await _prefs.setDouble(_keyHeight, size.height); + } +} + +Future> initializeAndGetOverrides( + SharedPreferences prefs) async { await windowManager.ensureInitialized(); // Linux doesn't currently support hiding the window at start currently. // For now, this size should match linux/flutter/my_application.cc to avoid window flicker at startup. unawaited(windowManager.waitUntilReadyToShow().then((_) async { - await windowManager.setSize(const Size(400, 720)); await windowManager.setMinimumSize(const Size(270, 0)); + final width = prefs.getDouble(_keyWidth) ?? 400; + final height = prefs.getDouble(_keyHeight) ?? 720; + await windowManager.setSize(Size(width, height)); await windowManager.show(); + windowManager.addListener(_WindowResizeListener(prefs)); })); // Either use the _YKMAN_EXE environment variable, or look relative to executable. diff --git a/lib/main.dart b/lib/main.dart index 1c867035..63abcf89 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -31,15 +31,16 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); + var prefs = await SharedPreferences.getInstance(); List overrides = [ - prefProvider.overrideWithValue(await SharedPreferences.getInstance()), + prefProvider.overrideWithValue(prefs), ]; Widget page; try { // Platform specific initialization if (isDesktop) { _log.config('Initializing desktop platform.'); - overrides.addAll(await desktop.initializeAndGetOverrides()); + overrides.addAll(await desktop.initializeAndGetOverrides(prefs)); } page = const MainPage(); } catch (e) { From 4efc8d969ed6e53a420c7023f1ebaaed372cdd8a Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Thu, 24 Feb 2022 14:35:26 +0100 Subject: [PATCH 2/2] Don't apply window size on Linux. --- lib/desktop/init.dart | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/desktop/init.dart b/lib/desktop/init.dart index 91308292..e89de27e 100755 --- a/lib/desktop/init.dart +++ b/lib/desktop/init.dart @@ -45,15 +45,17 @@ Future> initializeAndGetOverrides( SharedPreferences prefs) async { await windowManager.ensureInitialized(); - // Linux doesn't currently support hiding the window at start currently. - // For now, this size should match linux/flutter/my_application.cc to avoid window flicker at startup. unawaited(windowManager.waitUntilReadyToShow().then((_) async { await windowManager.setMinimumSize(const Size(270, 0)); - final width = prefs.getDouble(_keyWidth) ?? 400; - final height = prefs.getDouble(_keyHeight) ?? 720; - await windowManager.setSize(Size(width, height)); - await windowManager.show(); - windowManager.addListener(_WindowResizeListener(prefs)); + // Linux doesn't currently support hiding the window at start currently. + // For now, size on Linux is in linux/flutter/my_application.cc to avoid window flicker at startup. + if (!Platform.isLinux) { + final width = prefs.getDouble(_keyWidth) ?? 400; + final height = prefs.getDouble(_keyHeight) ?? 720; + await windowManager.setSize(Size(width, height)); + await windowManager.show(); + windowManager.addListener(_WindowResizeListener(prefs)); + } })); // Either use the _YKMAN_EXE environment variable, or look relative to executable.