mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 12:57:24 +00:00
Restore window placement on startup
This commit is contained in:
21
lib/utils/loop.dart
Normal file
21
lib/utils/loop.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'dart:async';
|
||||
|
||||
class Loop {
|
||||
static final List<void Function()> _callbacks = [];
|
||||
|
||||
static void start() {
|
||||
Timer.periodic(const Duration(milliseconds: 100), (timer) {
|
||||
for(var func in _callbacks) {
|
||||
func.call();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void register(void Function() func) {
|
||||
_callbacks.add(func);
|
||||
}
|
||||
|
||||
static void remove(void Function() func) {
|
||||
_callbacks.remove(func);
|
||||
}
|
||||
}
|
64
lib/utils/window.dart
Normal file
64
lib/utils/window.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:ui';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:pixes/foundation/app.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
class WindowPlacement {
|
||||
final Rect rect;
|
||||
|
||||
final bool isMaximized;
|
||||
|
||||
const WindowPlacement(this.rect, this.isMaximized);
|
||||
|
||||
Future<void> applyToWindow() async {
|
||||
await windowManager.setBounds(rect);
|
||||
|
||||
if (isMaximized) {
|
||||
await windowManager.maximize();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> writeToFile() async {
|
||||
var file = File("${App.dataPath}/window_placement");
|
||||
await file.writeAsString(jsonEncode({
|
||||
'width': rect.width,
|
||||
'height': rect.height,
|
||||
'x': rect.topLeft.dx,
|
||||
'y': rect.topLeft.dy,
|
||||
'isMaximized': isMaximized
|
||||
}));
|
||||
}
|
||||
|
||||
static Future<WindowPlacement> loadFromFile() async {
|
||||
var file = File("${App.dataPath}/window_placement");
|
||||
if (!file.existsSync()) {
|
||||
return defaultPlacement;
|
||||
}
|
||||
var json = jsonDecode(await file.readAsString());
|
||||
var rect =
|
||||
Rect.fromLTWH(json['x'], json['y'], json['width'], json['height']);
|
||||
return WindowPlacement(rect, json['isMaximized']);
|
||||
}
|
||||
|
||||
static Future<WindowPlacement> get current async {
|
||||
var rect = await windowManager.getBounds();
|
||||
var isMaximized = await windowManager.isMaximized();
|
||||
return WindowPlacement(rect, isMaximized);
|
||||
}
|
||||
|
||||
static const defaultPlacement =
|
||||
WindowPlacement(Rect.fromLTWH(10, 10, 900, 600), false);
|
||||
|
||||
static WindowPlacement cache = defaultPlacement;
|
||||
|
||||
static void loop() async {
|
||||
var placement = await WindowPlacement.current;
|
||||
if (placement.rect != cache.rect ||
|
||||
placement.isMaximized != cache.isMaximized) {
|
||||
cache = placement;
|
||||
await placement.writeToFile();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user