From 20a39f02926f889b55ca5cace2eba0fc91f07acd Mon Sep 17 00:00:00 2001 From: Noo6 <72285529+No06@users.noreply.github.com> Date: Wed, 5 Feb 2025 20:59:04 +0800 Subject: [PATCH] feat: record window position (#692) --- lib/data/store/setting.dart | 13 ++++++++++--- lib/main.dart | 23 ++++++++++++++--------- pubspec.yaml | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/data/store/setting.dart b/lib/data/store/setting.dart index 81f65c59..4eb03ffa 100644 --- a/lib/data/store/setting.dart +++ b/lib/data/store/setting.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:fl_lib/fl_lib.dart'; import 'package:server_box/data/model/app/menu/server_func.dart'; import 'package:server_box/data/model/app/server_detail_card.dart'; @@ -223,9 +225,14 @@ class SettingStore extends HiveStore { late final betaTest = propertyDefault('betaTest', false); - /// If it's empty, skip change window size. - /// Format: {width}x{height} - late final windowSize = propertyDefault('windowSize', ''); + /// For desktop only. + /// Record the position and size of the window. + late final windowState = property( + 'windowState', + fromStr: (jsonStr) => + WindowState.fromJson(jsonDecode(jsonStr) as Map), + toStr: (state) => state == null ? null : jsonEncode(state.toJson()), + ); late final introVer = propertyDefault('introVer', 0); diff --git a/lib/main.dart b/lib/main.dart index 5cd2e146..d300573d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -53,15 +53,7 @@ Future _initApp() async { await Paths.init(BuildData.name, bakName: 'srvbox_bak.json'); await _initData(); _setupDebug(); - - final windowSize = Stores.setting.windowSize; - final hideTitleBar = Stores.setting.hideTitleBar.fetch(); - await SystemUIs.initDesktopWindow( - hideTitleBar: hideTitleBar, - size: windowSize.fetch().toSize(), - listener: WindowSizeListener(windowSize), - ); - + _initWindow(); FontUtils.loadFrom(Stores.setting.fontPath.fetch()); _doPlatformRelated(); @@ -130,3 +122,16 @@ Future _doVersionRelated() async { Stores.setting.lastVer.put(newVer); } } + +Future _initWindow() async { + if (!isDesktop) return; + final windowStateProp = Stores.setting.windowState; + final windowState = windowStateProp.fetch(); + final hideTitleBar = Stores.setting.hideTitleBar.fetch(); + await SystemUIs.initDesktopWindow( + hideTitleBar: hideTitleBar, + size: windowState?.size, + position: windowState?.position, + listener: WindowStateListener(windowStateProp), + ); +} diff --git a/pubspec.yaml b/pubspec.yaml index 14c65eca..5c6c1d58 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -63,7 +63,7 @@ dependencies: fl_lib: git: url: https://github.com/lppcg/fl_lib - ref: v1.0.236 + ref: v1.0.243 flutter_distributor: ^0.4.5 dependency_overrides: