Skip to content

Commit

Permalink
feat: persist app window size and position (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint authored Jan 11, 2024
1 parent 68bfcd7 commit f85b967
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,27 @@ void main(List<String> args) async {

if (!Platform.isAndroid) {
await windowManager.ensureInitialized();
WindowOptions windowOptions = const WindowOptions(
size: Size(1280, 720),
minimumSize: Size(600, 500),
final sizeArr = MiruStorage.getSetting(SettingKey.windowSize).split(",");
final size = Size(double.parse(sizeArr[0]), double.parse(sizeArr[1]));
WindowOptions windowOptions = WindowOptions(
size: size,
center: true,
minimumSize: const Size(600, 500),
skipTaskbar: false,
titleBarStyle: TitleBarStyle.hidden,
);
windowManager.waitUntilReadyToShow(windowOptions, () async {
final position = MiruStorage.getSetting(SettingKey.windowPosition);
if (position != null) {
final offsetArr = position.split(",");
final offset = Offset(
double.parse(offsetArr[0]),
double.parse(offsetArr[1]),
);
await windowManager.setPosition(
offset,
);
}
await windowManager.show();
await windowManager.focus();
});
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/miru_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MiruStorage {
await _initSetting(SettingKey.arrowLeft, -2.0);
await _initSetting(SettingKey.arrowRight, 2.0);
await _initSetting(SettingKey.readingMode, "standard");
await _initSetting(SettingKey.windowSize, "1280,720");
}

static _initSetting(String key, dynamic value) async {
Expand Down Expand Up @@ -151,4 +152,6 @@ class SettingKey {
static String arrowLeft = 'Arrowleft';
static String arrowRight = 'Arrowright';
static String readingMode = 'ReadingMode';
static String windowSize = 'WindowsSize';
static String windowPosition = 'WindowsPosition';
}
29 changes: 28 additions & 1 deletion lib/views/pages/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DesktopMainPage extends StatefulWidget {
State<DesktopMainPage> createState() => _DesktopMainPageState();
}

class _DesktopMainPageState extends State<DesktopMainPage> {
class _DesktopMainPageState extends State<DesktopMainPage> with WindowListener {
late MainController c;

@override
Expand All @@ -39,9 +39,16 @@ class _DesktopMainPageState extends State<DesktopMainPage> {
if (MiruStorage.getSetting(SettingKey.autoCheckUpdate)) {
ApplicationUtils.checkUpdate(context);
}
windowManager.addListener(this);
super.initState();
}

@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}

Widget _title() {
return const DragToMoveArea(
child: Align(
Expand Down Expand Up @@ -147,6 +154,26 @@ class _DesktopMainPageState extends State<DesktopMainPage> {
),
);
}

@override
void onWindowResize() {
WindowManager.instance.getSize().then((value) {
MiruStorage.setSetting(
SettingKey.windowSize,
"${value.width},${value.height}",
);
});
}

@override
void onWindowMove() {
WindowManager.instance.getPosition().then((value) {
MiruStorage.setSetting(
SettingKey.windowPosition,
"${value.dx},${value.dy}",
);
});
}
}

class AndroidMainPage extends fluent.StatefulWidget {
Expand Down

0 comments on commit f85b967

Please sign in to comment.