Skip to content

Commit

Permalink
[gui] refactor deriving of window size
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Dec 12, 2024
1 parent faff142 commit 2b21fb0
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/client/gui/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:async/async.dart';
import 'package:basics/int_basics.dart';
import 'package:flutter/material.dart';
Expand All @@ -17,9 +15,7 @@ import 'extensions.dart';
import 'help.dart';
import 'logger.dart';
import 'notifications.dart';
import 'platform/linux.dart';
import 'platform/platform.dart';
import 'platform/windows.dart';
import 'providers.dart';
import 'settings/hotkey.dart';
import 'settings/settings.dart';
Expand All @@ -41,13 +37,11 @@ void main() async {

final sharedPreferences = await SharedPreferences.getInstance();
final screenSize = await getCurrentScreenSize();
final lastWindowSize = getLastWindowSize(sharedPreferences, screenSize);

await windowManager.ensureInitialized();
final windowOptions = WindowOptions(
center: true,
minimumSize: const Size(750, 450),
size: lastWindowSize ?? computeDefaultWindowSize(screenSize),
size: deriveWindowSize(sharedPreferences, screenSize),
title: 'Multipass',
);

Expand Down Expand Up @@ -239,15 +233,13 @@ Future<Size?> getCurrentScreenSize() async {
if (screen == null) throw Exception('Screen instance is null');

final scaleFactor = screen.scaleFactor;
var size = screen.visibleFrame.size;

// Adjust size based on the platform
if (mpPlatform is LinuxPlatform || mpPlatform is WindowsPlatform) {
size = Size(size.width * scaleFactor, size.height * scaleFactor);
}
final visibleFrameSize = screen.visibleFrame.size;
final size = mpPlatform.multiplyScreenScaleFactor
? visibleFrameSize * scaleFactor
: visibleFrameSize;

logger.d(
'Got Screen{frame: ${screen.frame.s()}, scaleFactor: $scaleFactor, visibleFrame: ${screen.visibleFrame.s()}, adjustedSize: ${size.s()}}',
'Got Screen{frame: ${screen.frame.s()}, scaleFactor: $scaleFactor, visibleFrame: ${screen.visibleFrame.s()}, scaledSize: ${size.s()}}',
);

return size;
Expand All @@ -257,17 +249,16 @@ Future<Size?> getCurrentScreenSize() async {
}
}

Size? getLastWindowSize(SharedPreferences sharedPreferences, Size? screenSize) {
Size deriveWindowSize(SharedPreferences sharedPreferences, Size? screenSize) {
final lastWindowWidth = sharedPreferences.getDouble(windowWidthKey);
final lastWindowHeight = sharedPreferences.getDouble(windowHeightKey);
final size = lastWindowWidth != null && lastWindowHeight != null
? Size(lastWindowWidth, lastWindowHeight)
: null;
logger.d('Got last window size: ${size?.s()}');
if (size == null) return null;
final clampedSize = size.width >= screenSize!.width || size.height >= screenSize.height
? computeDefaultWindowSize(screenSize)
: size;
final clampedSize = size != null && screenSize != null && size < screenSize
? size
: computeDefaultWindowSize(screenSize);
logger.d('Using clamped window size: ${clampedSize.s()}');
return clampedSize;
}
Expand Down

0 comments on commit 2b21fb0

Please sign in to comment.