fix: last window location calculation

This commit is contained in:
Kingtous 2022-11-09 15:14:11 +08:00
parent 2af6441495
commit 200d8dc0f5
6 changed files with 38 additions and 27 deletions

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'package:back_button_interceptor/back_button_interceptor.dart';
@ -1036,6 +1037,7 @@ Future<void> saveWindowPosition(WindowType type, {int? windowId}) async {
final isMaximized = await wc.isMaximized();
final pos = LastWindowPosition(
sz.width, sz.height, position.dx, position.dy, isMaximized);
print("saving frame: ${windowId}: ${pos.width}/${pos.height}, offset:${pos.offsetWidth}/${pos.offsetHeight}");
await Get.find<SharedPreferences>()
.setString(kWindowPrefix + type.name, pos.toString());
break;
@ -1081,7 +1083,7 @@ Future<Size> _adjustRestoreMainWindowSize(double? width, double? height) async {
restoreWidth = maxWidth;
}
if (restoreHeight > maxHeight) {
restoreWidth = maxHeight;
restoreHeight = maxHeight;
}
return Size(restoreWidth, restoreHeight);
}
@ -1092,11 +1094,11 @@ Future<Offset?> _adjustRestoreMainWindowOffset(
if (left == null || top == null) {
await windowManager.center();
} else {
double windowLeft = left;
double windowTop = top;
double windowLeft = max(0.0, left);
double windowTop = max(0.0, top);
double frameLeft = 0;
double frameTop = 0;
double frameLeft = double.infinity;
double frameTop = double.infinity;
double frameRight = ((isDesktop || isWebDesktop)
? kDesktopMaxDisplayWidth
: kMobileMaxDisplayWidth)
@ -1107,12 +1109,11 @@ Future<Offset?> _adjustRestoreMainWindowOffset(
.toDouble();
if (isDesktop || isWebDesktop) {
final screen = (await window_size.getWindowInfo()).screen;
if (screen != null) {
frameLeft = screen.visibleFrame.left;
frameTop = screen.visibleFrame.top;
frameRight = screen.visibleFrame.right;
frameBottom = screen.visibleFrame.bottom;
for(final screen in await window_size.getScreenList()) {
frameLeft = min(screen.visibleFrame.left, frameLeft);
frameTop = min(screen.visibleFrame.top, frameTop);
frameRight = max(screen.visibleFrame.right, frameRight);
frameBottom = max(screen.visibleFrame.bottom, frameBottom);
}
}
@ -1174,6 +1175,7 @@ Future<bool> restoreWindowPosition(WindowType type, {int? windowId}) async {
await _adjustRestoreMainWindowSize(lpos.width, lpos.height);
final offset = await _adjustRestoreMainWindowOffset(
lpos.offsetWidth, lpos.offsetHeight);
print("restore lpos: ${size.width}/${size.height}, offset:${offset?.dx}/${offset?.dy}");
if (offset == null) {
await wc.center();
} else {

View File

@ -96,8 +96,7 @@ class _FileManagerTabPageState extends State<FileManagerTabPage> {
void onRemoveId(String id) {
if (tabController.state.value.tabs.isEmpty) {
WindowController.fromWindowId(windowId()).hide();
rustDeskWinManager.call(WindowType.Main, kWindowEventHide, {"id": windowId()});
WindowController.fromWindowId(windowId()).close();
}
}

View File

@ -107,8 +107,7 @@ class _PortForwardTabPageState extends State<PortForwardTabPage> {
void onRemoveId(String id) {
if (tabController.state.value.tabs.isEmpty) {
WindowController.fromWindowId(windowId()).hide();
rustDeskWinManager.call(WindowType.Main, kWindowEventHide, {"id": windowId()});
WindowController.fromWindowId(windowId()).close();
}
}

View File

@ -97,9 +97,6 @@ class _ConnectionTabPageState extends State<ConnectionTabPage> {
}
_update_remote_count();
});
Future.delayed(Duration.zero, () {
restoreWindowPosition(WindowType.RemoteDesktop, windowId: windowId());
});
}
@override
@ -321,10 +318,9 @@ class _ConnectionTabPageState extends State<ConnectionTabPage> {
);
}
void onRemoveId(String id) {
void onRemoveId(String id) async {
if (tabController.state.value.tabs.isEmpty) {
WindowController.fromWindowId(windowId()).hide();
rustDeskWinManager.call(WindowType.Main, kWindowEventHide, {"id": windowId()});
await WindowController.fromWindowId(windowId()).close();
}
ConnectionTypeState.delete(id);
_update_remote_count();

View File

@ -500,15 +500,19 @@ class WindowActionPanelState extends State<WindowActionPanel>
@override
void onWindowClose() async {
print("onWindowClose");
// hide window on close
if (widget.isMainWindow) {
await windowManager.hide();
rustDeskWinManager.unregisterActiveWindow(0);
} else {
widget.onClose?.call();
WindowController.fromWindowId(windowId!).hide();
final frame = await WindowController.fromWindowId(windowId!).getFrame();
await WindowController.fromWindowId(windowId!).hide();
rustDeskWinManager
.call(WindowType.Main, kWindowEventHide, {"id": windowId!});
final frame2 = await WindowController.fromWindowId(windowId!).getFrame();
print("${frame} ---hide--> ${frame2}");
}
super.onWindowClose();
}
@ -555,12 +559,9 @@ class WindowActionPanelState extends State<WindowActionPanel>
// note: the main window can be restored by tray icon
Future.delayed(Duration.zero, () async {
if (widget.isMainWindow) {
await windowManager.hide();
rustDeskWinManager.unregisterActiveWindow(0);
await windowManager.close();
} else {
await WindowController.fromWindowId(windowId!).hide();
rustDeskWinManager.call(
WindowType.Main, kWindowEventHide, {"id": windowId!});
await WindowController.fromWindowId(windowId!).close();
}
});
}

View File

@ -168,6 +168,20 @@ void runMultiWindow(
widget,
MyTheme.currentThemeMode(),
);
switch (appType) {
case kAppTypeDesktopRemote:
await restoreWindowPosition(WindowType.RemoteDesktop, windowId: windowId!);
break;
case kAppTypeDesktopFileTransfer:
await restoreWindowPosition(WindowType.FileTransfer, windowId: windowId!);
break;
case kAppTypeDesktopPortForward:
await restoreWindowPosition(WindowType.PortForward, windowId: windowId!);
break;
default:
// no such appType
exit(0);
}
}
void runConnectionManagerScreen() async {