11import 'dart:async' ;
22import 'dart:ui' ;
33
4+ import 'package:shared_preferences/shared_preferences.dart' ;
45import 'package:window_manager/window_manager.dart' ;
56
7+ import '../extensions/shared_preferences_x.dart' ;
68import '../extensions/taget_platform_x.dart' ;
79
810class WindowSizeToSettingsListener implements WindowListener {
9- WindowSizeToSettingsListener ({
10- required Future <void > Function (Size value) onResize,
11- required Future <void > Function (bool value) onMaximize,
12- required Future <void > Function (bool value) onFullscreen,
13- }) : _onResize = onResize,
14- _onMaximize = onMaximize,
15- _onFullscreen = onFullscreen;
16-
17- final Future <void > Function (Size value) _onResize;
18- final Future <void > Function (bool value) _onMaximize;
19- final Future <void > Function (bool value) _onFullscreen;
11+ WindowSizeToSettingsListener ({required SharedPreferences sharedPreferences})
12+ : _sp = sharedPreferences;
13+
14+ final SharedPreferences _sp;
15+
16+ static Future <WindowSizeToSettingsListener > register ({
17+ required SharedPreferences sharedPreferences,
18+ required WindowManager windowManager,
19+ }) async {
20+ final wm = windowManager;
21+ final sp = sharedPreferences;
22+ if (sp.getBool (SPKeys .saveWindowSize) == null ) {
23+ await sp.setBool (SPKeys .saveWindowSize, true );
24+ }
2025
21- Timer ? _debounce;
26+ if (sp.getBool (SPKeys .windowFullscreen) ?? false ) {
27+ await wm.setFullScreen (true );
28+ } else if (sp.getBool (SPKeys .windowMaximized) ?? false ) {
29+ await wm.maximize ();
30+ } else {
31+ final height = sp.getInt (SPKeys .windowHeight) ?? 820 ;
32+ final width = sp.getInt (SPKeys .windowWidth) ?? 950 ;
33+ await wm.setSize (Size (width.toDouble (), height.toDouble ()));
34+ }
35+
36+ final windowSizeToSettingsListener = WindowSizeToSettingsListener (
37+ sharedPreferences: sp,
38+ );
39+ wm.addListener (windowSizeToSettingsListener);
40+
41+ return windowSizeToSettingsListener;
42+ }
2243
2344 @override
2445 void onWindowBlur () {}
@@ -30,7 +51,7 @@ class WindowSizeToSettingsListener implements WindowListener {
3051 void onWindowDocked () {}
3152
3253 @override
33- void onWindowEnterFullScreen () => _onFullscreen ( true );
54+ void onWindowEnterFullScreen () => _sp. setBool ( SPKeys .windowFullscreen, true );
3455
3556 @override
3657 void onWindowEvent (String eventName) {}
@@ -39,10 +60,10 @@ class WindowSizeToSettingsListener implements WindowListener {
3960 void onWindowFocus () {}
4061
4162 @override
42- void onWindowLeaveFullScreen () => _onFullscreen ( false );
63+ void onWindowLeaveFullScreen () => _sp. setBool ( SPKeys .windowFullscreen, false );
4364
4465 @override
45- void onWindowMaximize () => _onMaximize ( true );
66+ void onWindowMaximize () => _sp. setBool ( SPKeys .windowMaximized, true );
4667
4768 @override
4869 void onWindowMinimize () {}
@@ -55,20 +76,34 @@ class WindowSizeToSettingsListener implements WindowListener {
5576
5677 // Note: linux does not have window resized, so we need to use window resize
5778 // and debounce it
79+ Timer ? _debounce;
80+ void dispose () => _debounce? .cancel ();
5881 @override
5982 void onWindowResize () {
6083 if (isLinux) {
6184 if (_debounce? .isActive ?? false ) _debounce? .cancel ();
6285 _debounce = Timer (const Duration (seconds: 5 ), () {
63- WindowManager .instance.getSize ().then (_onResize);
86+ WindowManager .instance.getSize ().then ((v) async {
87+ if (_sp.getBool (SPKeys .saveWindowSize) ?? false ) {
88+ _sp
89+ .setInt (SPKeys .windowHeight, v.height.toInt ())
90+ .then ((_) => _sp.setInt (SPKeys .windowWidth, v.width.toInt ()));
91+ }
92+ });
6493 });
6594 }
6695 }
6796
6897 @override
6998 void onWindowResized () {
7099 if (isMacOS || isWindows) {
71- WindowManager .instance.getSize ().then (_onResize);
100+ WindowManager .instance.getSize ().then ((v) async {
101+ if (_sp.getBool (SPKeys .saveWindowSize) ?? false ) {
102+ _sp
103+ .setInt (SPKeys .windowHeight, v.height.toInt ())
104+ .then ((_) => _sp.setInt (SPKeys .windowWidth, v.width.toInt ()));
105+ }
106+ });
72107 }
73108 }
74109
@@ -79,5 +114,5 @@ class WindowSizeToSettingsListener implements WindowListener {
79114 void onWindowUndocked () {}
80115
81116 @override
82- void onWindowUnmaximize () => _onMaximize ( false );
117+ void onWindowUnmaximize () => _sp. setBool ( SPKeys .windowMaximized, false );
83118}
0 commit comments