fix: windows, restore windows pos (#8075)
* fix: windows, restore windows pos Signed-off-by: fufesou <shuanglongchen@yeah.net> * fix: restore window pos, a better way to get cur scale factor Signed-off-by: fufesou <shuanglongchen@yeah.net> * chore Signed-off-by: fufesou <shuanglongchen@yeah.net> --------- Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
1dd7cd9384
commit
44d4e13fa7
@ -1500,6 +1500,12 @@ Widget getPlatformImage(String platform, {double size = 50}) {
|
|||||||
return SvgPicture.asset('assets/$platform.svg', height: size, width: size);
|
return SvgPicture.asset('assets/$platform.svg', height: size, width: size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OffsetDevicePixelRatio {
|
||||||
|
Offset offset;
|
||||||
|
final double devicePixelRatio;
|
||||||
|
OffsetDevicePixelRatio(this.offset, this.devicePixelRatio);
|
||||||
|
}
|
||||||
|
|
||||||
class LastWindowPosition {
|
class LastWindowPosition {
|
||||||
double? width;
|
double? width;
|
||||||
double? height;
|
double? height;
|
||||||
@ -1689,8 +1695,15 @@ Future<Size> _adjustRestoreMainWindowSize(double? width, double? height) async {
|
|||||||
return Size(restoreWidth, restoreHeight);
|
return Size(restoreWidth, restoreHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isPointInRect(Offset point, Rect rect) {
|
||||||
|
return point.dx >= rect.left &&
|
||||||
|
point.dx <= rect.right &&
|
||||||
|
point.dy >= rect.top &&
|
||||||
|
point.dy <= rect.bottom;
|
||||||
|
}
|
||||||
|
|
||||||
/// return null means center
|
/// return null means center
|
||||||
Future<Offset?> _adjustRestoreMainWindowOffset(
|
Future<OffsetDevicePixelRatio?> _adjustRestoreMainWindowOffset(
|
||||||
double? left,
|
double? left,
|
||||||
double? top,
|
double? top,
|
||||||
double? width,
|
double? width,
|
||||||
@ -1704,9 +1717,13 @@ Future<Offset?> _adjustRestoreMainWindowOffset(
|
|||||||
double? frameTop;
|
double? frameTop;
|
||||||
double? frameRight;
|
double? frameRight;
|
||||||
double? frameBottom;
|
double? frameBottom;
|
||||||
|
double devicePixelRatio = 1.0;
|
||||||
|
|
||||||
if (isDesktop || isWebDesktop) {
|
if (isDesktop || isWebDesktop) {
|
||||||
for (final screen in await window_size.getScreenList()) {
|
for (final screen in await window_size.getScreenList()) {
|
||||||
|
if (isPointInRect(Offset(left, top), screen.visibleFrame)) {
|
||||||
|
devicePixelRatio = screen.scaleFactor;
|
||||||
|
}
|
||||||
frameLeft = frameLeft == null
|
frameLeft = frameLeft == null
|
||||||
? screen.visibleFrame.left
|
? screen.visibleFrame.left
|
||||||
: min(screen.visibleFrame.left, frameLeft);
|
: min(screen.visibleFrame.left, frameLeft);
|
||||||
@ -1740,7 +1757,7 @@ Future<Offset?> _adjustRestoreMainWindowOffset(
|
|||||||
top < frameTop!) {
|
top < frameTop!) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return Offset(left, top);
|
return OffsetDevicePixelRatio(Offset(left, top), devicePixelRatio);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1805,22 +1822,47 @@ Future<bool> restoreWindowPosition(WindowType type,
|
|||||||
}
|
}
|
||||||
|
|
||||||
final size = await _adjustRestoreMainWindowSize(lpos.width, lpos.height);
|
final size = await _adjustRestoreMainWindowSize(lpos.width, lpos.height);
|
||||||
final offset = await _adjustRestoreMainWindowOffset(
|
final offsetDevicePixelRatio = await _adjustRestoreMainWindowOffset(
|
||||||
lpos.offsetWidth,
|
lpos.offsetWidth,
|
||||||
lpos.offsetHeight,
|
lpos.offsetHeight,
|
||||||
size.width,
|
size.width,
|
||||||
size.height,
|
size.height,
|
||||||
);
|
);
|
||||||
debugPrint(
|
debugPrint(
|
||||||
"restore lpos: ${size.width}/${size.height}, offset:${offset?.dx}/${offset?.dy}");
|
"restore lpos: ${size.width}/${size.height}, offset:${offsetDevicePixelRatio?.offset.dx}/${offsetDevicePixelRatio?.offset.dy}, devicePixelRatio:${offsetDevicePixelRatio?.devicePixelRatio}");
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case WindowType.Main:
|
case WindowType.Main:
|
||||||
|
// https://github.com/rustdesk/rustdesk/issues/8038
|
||||||
|
// `setBounds()` in `window_manager` will use the current devicePixelRatio.
|
||||||
|
// So we need to adjust the offset by the scale factor.
|
||||||
|
// https://github.com/rustdesk-org/window_manager/blob/f19acdb008645366339444a359a45c3257c8b32e/windows/window_manager.cpp#L701
|
||||||
|
if (isWindows) {
|
||||||
|
double? curDevicePixelRatio;
|
||||||
|
Offset curPos = await windowManager.getPosition();
|
||||||
|
for (final screen in await window_size.getScreenList()) {
|
||||||
|
if (isPointInRect(curPos, screen.visibleFrame)) {
|
||||||
|
curDevicePixelRatio = screen.scaleFactor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (curDevicePixelRatio != null &&
|
||||||
|
curDevicePixelRatio != 0 &&
|
||||||
|
offsetDevicePixelRatio != null) {
|
||||||
|
if (offsetDevicePixelRatio.devicePixelRatio != 0) {
|
||||||
|
final scale =
|
||||||
|
offsetDevicePixelRatio.devicePixelRatio / curDevicePixelRatio;
|
||||||
|
offsetDevicePixelRatio.offset =
|
||||||
|
offsetDevicePixelRatio.offset.scale(scale, scale);
|
||||||
|
debugPrint(
|
||||||
|
"restore new offset: ${offsetDevicePixelRatio.offset.dx}/${offsetDevicePixelRatio.offset.dy}, scale:$scale");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
restorePos() async {
|
restorePos() async {
|
||||||
if (offset == null) {
|
if (offsetDevicePixelRatio == null) {
|
||||||
await windowManager.center();
|
await windowManager.center();
|
||||||
} else {
|
} else {
|
||||||
await windowManager.setPosition(offset);
|
await windowManager.setPosition(offsetDevicePixelRatio.offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lpos.isMaximized == true) {
|
if (lpos.isMaximized == true) {
|
||||||
@ -1838,11 +1880,11 @@ Future<bool> restoreWindowPosition(WindowType type,
|
|||||||
default:
|
default:
|
||||||
final wc = WindowController.fromWindowId(windowId!);
|
final wc = WindowController.fromWindowId(windowId!);
|
||||||
restoreFrame() async {
|
restoreFrame() async {
|
||||||
if (offset == null) {
|
if (offsetDevicePixelRatio == null) {
|
||||||
await wc.center();
|
await wc.center();
|
||||||
} else {
|
} else {
|
||||||
final frame =
|
final frame = Rect.fromLTWH(offsetDevicePixelRatio.offset.dx,
|
||||||
Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height);
|
offsetDevicePixelRatio.offset.dy, size.width, size.height);
|
||||||
await wc.setFrame(frame);
|
await wc.setFrame(frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user