feat: add listener to windows resize

This commit is contained in:
Prateek Sunal 2024-03-23 18:44:21 +05:30
parent 9e6e91fe7e
commit 1f091e33df
3 changed files with 50 additions and 3 deletions

View file

@ -12,11 +12,13 @@ import 'package:ente_auth/locale.dart';
import "package:ente_auth/onboarding/view/onboarding_page.dart";
import 'package:ente_auth/services/update_service.dart';
import 'package:ente_auth/services/user_service.dart';
import 'package:ente_auth/services/window_listener_service.dart';
import 'package:ente_auth/ui/home_page.dart';
import 'package:ente_auth/ui/settings/app_update_dialog.dart';
import 'package:flutter/foundation.dart';
import "package:flutter/material.dart";
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:window_manager/window_manager.dart';
class App extends StatefulWidget {
final Locale locale;
@ -31,7 +33,7 @@ class App extends StatefulWidget {
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
class _AppState extends State<App> with WindowListener {
late StreamSubscription<SignedOutEvent> _signedOutEvent;
late StreamSubscription<SignedInEvent> _signedInEvent;
Locale? locale;
@ -43,6 +45,7 @@ class _AppState extends State<App> {
@override
void initState() {
windowManager.addListener(this);
_signedOutEvent = Bus.instance.on<SignedOutEvent>().listen((event) {
if (mounted) {
setState(() {});
@ -76,6 +79,7 @@ class _AppState extends State<App> {
@override
void dispose() {
super.dispose();
windowManager.removeListener(this);
_signedOutEvent.cancel();
_signedInEvent.cancel();
}
@ -134,4 +138,9 @@ class _AppState extends State<App> {
: const OnboardingPage(),
};
}
@override
void onWindowResize() {
WindowListenerService.instance.onWindowResize().ignore();
}
}

View file

@ -16,6 +16,7 @@ import 'package:ente_auth/services/preference_service.dart';
import 'package:ente_auth/services/update_service.dart';
import 'package:ente_auth/services/user_remote_flag_service.dart';
import 'package:ente_auth/services/user_service.dart';
import 'package:ente_auth/services/window_listener_service.dart';
import 'package:ente_auth/store/code_store.dart';
import 'package:ente_auth/ui/tools/app_lock.dart';
import 'package:ente_auth/ui/tools/lock_screen.dart';
@ -39,8 +40,9 @@ void main() async {
if (PlatformUtil.isDesktop()) {
await windowManager.ensureInitialized();
WindowOptions windowOptions = const WindowOptions(
size: Size(450, 800),
await WindowListenerService.instance.init();
WindowOptions windowOptions = WindowOptions(
size: WindowListenerService.instance.getWindowSize(),
);
await windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();

View file

@ -0,0 +1,36 @@
import 'dart:async';
import 'dart:ui';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
class WindowListenerService {
late SharedPreferences _preferences;
WindowListenerService._privateConstructor();
static final WindowListenerService instance =
WindowListenerService._privateConstructor();
Future<void> init() async {
_preferences = await SharedPreferences.getInstance();
}
Size getWindowSize() {
final double windowWidth = _preferences.getDouble('windowWidth') ?? 450.0;
final double windowHeight = _preferences.getDouble('windowHeight') ?? 800.0;
return Size(windowWidth, windowHeight);
}
Future<void> onWindowResize() async {
// Save the window size to shared preferences
await _preferences.setDouble(
'windowWidth',
(await windowManager.getSize()).width,
);
await _preferences.setDouble(
'windowHeight',
(await windowManager.getSize()).height,
);
}
}