Skip to content

Commit e474a63

Browse files
committed
feat: 记忆窗口大小
1 parent 309b8bf commit e474a63

File tree

3 files changed

+124
-10
lines changed

3 files changed

+124
-10
lines changed

simple_live_app/lib/main.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import 'package:simple_live_app/services/history_service.dart';
3434
import 'package:simple_live_app/services/local_storage_service.dart';
3535
import 'package:simple_live_app/services/migration_service.dart';
3636
import 'package:simple_live_app/services/sync_service.dart';
37+
import 'package:simple_live_app/services/window_service.dart';
3738
import 'package:simple_live_app/widgets/status/app_loadding_widget.dart';
3839
import 'package:simple_live_core/simple_live_core.dart';
3940
import 'package:window_manager/window_manager.dart';
4041

4142
void main() async {
4243
WidgetsFlutterBinding.ensureInitialized();
4344
await MigrationService.migrateData();
44-
await initWindow();
4545
MediaKit.ensureInitialized();
4646
await Hive.initFlutter(
4747
(!Platform.isAndroid && !Platform.isIOS)
@@ -50,6 +50,7 @@ void main() async {
5050
);
5151
//初始化服务
5252
await initServices();
53+
await initWindow();
5354

5455
MigrationService.migrateDataByVersion();
5556
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
@@ -77,15 +78,9 @@ Future initWindow() async {
7778
}
7879
exit(0);
7980
}
80-
WindowOptions windowOptions = const WindowOptions(
81-
minimumSize: Size(280, 280),
82-
center: true,
83-
title: "Simple Live",
84-
);
85-
windowManager.waitUntilReadyToShow(windowOptions, () async {
86-
await windowManager.show();
87-
await windowManager.focus();
88-
});
81+
82+
WindowService.instance.init();
83+
8984
}
9085

9186
Future initServices() async {
@@ -112,6 +107,8 @@ Future initServices() async {
112107

113108
Get.put(HistoryService());
114109

110+
Get.put(WindowService());
111+
115112
initCoreLog();
116113
}
117114

simple_live_app/lib/services/local_storage_service.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ class LocalStorageService extends GetxService {
165165
/// WebDAV_最后一次备份时间
166166
static const String kWebDAVLastRecoverTime = "kWebDAVLastRecoverTime";
167167

168+
/// windows窗口size
169+
static const String kWindowX = "WindowX";
170+
171+
static const String kWindowY = "WindowY";
172+
173+
static const String kWindowWidth = "WindowWidth";
174+
175+
static const String kWindowHeight = "WindowHeight";
176+
168177
/// 数据库版本
169178
static const String kHiveDbVer = "kHiveDbVer";
170179

@@ -200,4 +209,8 @@ class LocalStorageService extends GetxService {
200209
Log.d("Remove LocalStorage:$key");
201210
return await settingsBox.delete(key);
202211
}
212+
213+
Future flush() async {
214+
return await settingsBox.flush();
215+
}
203216
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'dart:ui';
2+
3+
import 'package:get/get.dart';
4+
import 'package:simple_live_app/services/local_storage_service.dart';
5+
import 'package:window_manager/window_manager.dart';
6+
7+
class WindowService extends GetxService implements WindowListener {
8+
static WindowService get instance => Get.find<WindowService>();
9+
10+
WindowService() {
11+
windowManager.addListener(this);
12+
}
13+
14+
Future<void> init() async {
15+
await resize();
16+
WindowOptions windowOptions = WindowOptions(
17+
minimumSize: Size(280, 280),
18+
center: false,
19+
title: "Simple Live",
20+
);
21+
windowManager.waitUntilReadyToShow(windowOptions, () async {
22+
await windowManager.show();
23+
await windowManager.focus();
24+
});
25+
}
26+
27+
Future<void> resize() async {
28+
// 初始分辨率默认 1920×1080
29+
final width = LocalStorageService.instance
30+
.getValue(LocalStorageService.kWindowWidth, 1280.0);
31+
final height = LocalStorageService.instance
32+
.getValue(LocalStorageService.kWindowHeight, 720.0);
33+
final x = LocalStorageService.instance
34+
.getValue(LocalStorageService.kWindowX, 320.0);
35+
final y = LocalStorageService.instance
36+
.getValue(LocalStorageService.kWindowY, 180.0);
37+
windowManager.setBounds(Rect.fromLTWH(x, y, width, height));
38+
}
39+
40+
@override
41+
void onWindowBlur() {}
42+
43+
@override
44+
void onWindowClose() {}
45+
46+
@override
47+
void onWindowDocked() {}
48+
49+
@override
50+
void onWindowEnterFullScreen() {}
51+
52+
@override
53+
void onWindowEvent(String eventName) {}
54+
55+
@override
56+
void onWindowFocus() {}
57+
58+
@override
59+
void onWindowLeaveFullScreen() {}
60+
61+
@override
62+
void onWindowMaximize() {}
63+
64+
@override
65+
void onWindowMinimize() {}
66+
67+
@override
68+
Future<void> onWindowMove() async {}
69+
70+
@override
71+
Future<void> onWindowMoved() async {
72+
final bounds = await windowManager.getBounds();
73+
_saveBounds(bounds);
74+
}
75+
76+
@override
77+
Future<void> onWindowResize() async {}
78+
79+
@override
80+
Future<void> onWindowResized() async {
81+
final bounds = await windowManager.getBounds();
82+
_saveBounds(bounds);
83+
}
84+
85+
@override
86+
void onWindowRestore() {}
87+
88+
@override
89+
void onWindowUndocked() {}
90+
91+
@override
92+
void onWindowUnmaximize() {}
93+
94+
void _saveBounds(Rect bounds) {
95+
LocalStorageService.instance
96+
.setValue(LocalStorageService.kWindowX, bounds.left);
97+
LocalStorageService.instance
98+
.setValue(LocalStorageService.kWindowY, bounds.top);
99+
LocalStorageService.instance
100+
.setValue(LocalStorageService.kWindowWidth, bounds.width);
101+
LocalStorageService.instance
102+
.setValue(LocalStorageService.kWindowHeight, bounds.height);
103+
}
104+
}

0 commit comments

Comments
 (0)