This commit is contained in:
Dain Nilsson 2022-02-24 14:50:48 +01:00
commit 14fc58a52d
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
2 changed files with 29 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import 'dart:ui';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
import '../oath/state.dart'; import '../oath/state.dart';
@ -15,6 +16,8 @@ import 'qr_scanner.dart';
import 'state.dart'; import 'state.dart';
final _log = Logger('desktop.init'); final _log = Logger('desktop.init');
const String _keyWidth = 'DESKTOP_WINDOW_WIDTH';
const String _keyHeight = 'DESKTOP_WINDOW_HEIGHT';
initializeLogging() { initializeLogging() {
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
@ -26,15 +29,33 @@ initializeLogging() {
_log.info('Logging initialized, outputting to stderr'); _log.info('Logging initialized, outputting to stderr');
} }
Future<List<Override>> 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<List<Override>> initializeAndGetOverrides(
SharedPreferences prefs) async {
await windowManager.ensureInitialized(); 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 { unawaited(windowManager.waitUntilReadyToShow().then((_) async {
await windowManager.setSize(const Size(400, 720));
await windowManager.setMinimumSize(const Size(270, 0)); await windowManager.setMinimumSize(const Size(270, 0));
await windowManager.show(); // 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. // Either use the _YKMAN_EXE environment variable, or look relative to executable.

View File

@ -31,15 +31,16 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
var prefs = await SharedPreferences.getInstance();
List<Override> overrides = [ List<Override> overrides = [
prefProvider.overrideWithValue(await SharedPreferences.getInstance()), prefProvider.overrideWithValue(prefs),
]; ];
Widget page; Widget page;
try { try {
// Platform specific initialization // Platform specific initialization
if (isDesktop) { if (isDesktop) {
_log.config('Initializing desktop platform.'); _log.config('Initializing desktop platform.');
overrides.addAll(await desktop.initializeAndGetOverrides()); overrides.addAll(await desktop.initializeAndGetOverrides(prefs));
} }
page = const MainPage(); page = const MainPage();
} catch (e) { } catch (e) {