Skip to content

Commit

Permalink
improved auto resize
Browse files Browse the repository at this point in the history
  • Loading branch information
nini22P committed Dec 14, 2024
1 parent c62e408 commit abd500f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 64 deletions.
1 change: 0 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"add_webdav_storage": "Add WebDAV Storage",
"edit_webdav_storage": "Edit WebDAV Storage",
"auto_resize": "Auto Resize Window Proportion",
"center_on_resize": "Center On Resize",
"always_on_top_on": "Always On Top: On",
"always_on_top_off": "Always On Top: Off"
}
1 change: 0 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"add_webdav_storage": "添加 WebDAV 存储",
"edit_webdav_storage": "编辑 WebDAV 存储",
"auto_resize": "自动调整窗口比例",
"center_on_resize": "调整窗口时移动到屏幕中心",
"always_on_top_on": "窗口置顶: 开",
"always_on_top_off": "窗口置顶: 关"
}
6 changes: 0 additions & 6 deletions lib/models/store/app_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class AppState {
bool autoCheckUpdates;
String language;
bool autoResize;
bool centerOnResize;

AppState({
this.autoPlay = false,
Expand All @@ -20,7 +19,6 @@ class AppState {
this.autoCheckUpdates = true,
this.language = 'auto',
this.autoResize = true,
this.centerOnResize = false,
});

AppState copyWith({
Expand All @@ -33,7 +31,6 @@ class AppState {
bool? autoCheckUpdates,
String? language,
bool? autoResize,
bool? centerOnResize,
}) =>
AppState(
autoPlay: autoPlay ?? this.autoPlay,
Expand All @@ -45,7 +42,6 @@ class AppState {
autoCheckUpdates: autoCheckUpdates ?? this.autoCheckUpdates,
language: language ?? this.language,
autoResize: autoResize ?? this.autoResize,
centerOnResize: centerOnResize ?? this.centerOnResize,
);

Map<String, dynamic> toJson() {
Expand All @@ -59,7 +55,6 @@ class AppState {
'autoCheckUpdates': autoCheckUpdates,
'language': language,
'autoResize': autoResize,
'centerOnResize': centerOnResize,
};
}

Expand All @@ -74,7 +69,6 @@ class AppState {
autoCheckUpdates: json['autoCheckUpdates'] ?? true,
language: json['language'] ?? 'auto',
autoResize: json['autoResize'] ?? true,
centerOnResize: json['centerOnResize'] ?? false,
);
}
}
14 changes: 0 additions & 14 deletions lib/pages/settings/play.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class Play extends HookWidget {

final autoResize =
useAppStore().select(context, (state) => state.autoResize);
final centerOnResize =
useAppStore().select(context, (state) => state.centerOnResize);

return SingleChildScrollView(
child: Column(
Expand All @@ -35,18 +33,6 @@ class Play extends HookWidget {
),
),
),
Visibility(
visible: isDesktop,
child: ListTile(
leading: const Icon(Icons.center_focus_strong_rounded),
title: Text(t.center_on_resize),
onTap: () => useAppStore().toggleCenterOnResize(),
trailing: Checkbox(
value: centerOnResize,
onChanged: (_) => useAppStore().toggleCenterOnResize(),
),
),
),
],
),
);
Expand Down
5 changes: 0 additions & 5 deletions lib/store/use_app_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class AppStore extends PersistentStore<AppState> {
save(state);
}

Future<void> toggleCenterOnResize() async {
set(state.copyWith(centerOnResize: !state.centerOnResize));
save(state);
}

@override
Future<AppState?> load() async {
try {
Expand Down
86 changes: 49 additions & 37 deletions lib/utils/resize_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,77 @@ Future<void> resizeWindow(double? videoAspectRatio) async {
return;
}

final autoResize = useAppStore().state.autoResize;
bool autoResize = useAppStore().state.autoResize;

if (!autoResize) return;

final centerOnResize = useAppStore().state.centerOnResize;

windowManager.setAspectRatio(videoAspectRatio);

final windowSize = await windowManager.getSize();
final windowAspectRatio = windowSize.aspectRatio;
Rect bounds = await windowManager.getBounds();
Size windowSize = bounds.size;
double windowAspectRatio = windowSize.aspectRatio;

if (windowAspectRatio.toStringAsFixed(2) ==
videoAspectRatio.toStringAsFixed(2)) return;

final screen = await getCurrentScreen();
Screen? screen = await getCurrentScreen();

if (screen == null) return;

final screenWidth = screen.frame.size.width;
final screenHeight = screen.frame.size.height;
final screenAspectRatio = screen.frame.size.aspectRatio;

if (screenAspectRatio > videoAspectRatio) {
final height = screenHeight * 0.8 / screen.scaleFactor;
final width = height * videoAspectRatio;

logger('Window resize: width: $width, height: $height');

final size = Size(width, height);
double screenWidth = screen.frame.size.width;
double screenHeight = screen.frame.size.height;
double screenAspectRatio = screen.frame.size.aspectRatio;

resize(size, centerOnResize);
} else {
final width = screenWidth * 0.8 / screen.scaleFactor;
final height = width / videoAspectRatio;

logger('Window resize: width: $width, height: $height');
Size size = Size(windowSize.height * videoAspectRatio, windowSize.height);

final size = Size(width, height);
if (size.width < screenWidth / screen.scaleFactor &&
size.height < screenHeight / screen.scaleFactor) {
logger('Window resize: $size');

resize(size, centerOnResize);
}
}

Future<void> resize(Size size, bool? center) async {
if (center ?? false) {
Offset newPosition = await calcWindowPosition(
size,
Alignment.center,
);
windowManager.setBounds(
null,
position: newPosition,
position: Offset(
bounds.left < 0
? 0
: screenWidth / screen.scaleFactor - bounds.left < size.width
? screenWidth / screen.scaleFactor - size.width
: bounds.left,
bounds.top),
size: size,
animate: true,
);
} else {
windowManager.setSize(
Offset position = await calcWindowPosition(
size,
animate: true,
Alignment.center,
);

if (screenAspectRatio > videoAspectRatio) {
double height = screenHeight * 0.9 / screen.scaleFactor;
double width = height * videoAspectRatio;
Size size = Size(width, height);

logger('Window resize: $size');

windowManager.setBounds(
null,
position: position,
size: size,
animate: true,
);
} else {
double width = screenWidth * 0.9 / screen.scaleFactor;
double height = width / videoAspectRatio;
Size size = Size(width, height);

logger('Window resize: $size');

windowManager.setBounds(
null,
position: position,
size: size,
animate: true,
);
}
}
}

0 comments on commit abd500f

Please sign in to comment.