Skip to content

Commit 0243636

Browse files
committed
Fix #420, #452, #757: windows positioning for monitors with different DPIs
1 parent 8043863 commit 0243636

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

QuickLook/App.config

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</startup>
77
<runtime>
88
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
9-
<AppContextSwitchOverrides value="Switch.System.Windows.DoNotScaleForDpiChanges=false" />
109
<legacyCorruptedStateExceptionsPolicy enabled="true" />
1110
<legacyUnhandledExceptionPolicy enabled="1" />
1211
</runtime>

QuickLook/ViewerWindow.Actions.cs

+38-38
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,11 @@ private void PositionWindow(Size size)
6666
if (WindowState == WindowState.Maximized)
6767
return;
6868

69+
size = new Size(Math.Max(MinWidth, size.Width), Math.Max(MinHeight, size.Height));
70+
6971
var newRect = IsLoaded ? ResizeAndCentreExistingWindow(size) : ResizeAndCentreNewWindow(size);
7072

71-
if (IsLoaded)
72-
{
73-
this.MoveWindow(newRect.Left, newRect.Top, newRect.Width, newRect.Height);
74-
}
75-
else
76-
{
77-
Top = newRect.Top;
78-
Left = newRect.Left;
79-
Width = newRect.Width;
80-
Height = newRect.Height;
81-
}
73+
this.MoveWindow(newRect.Left, newRect.Top, newRect.Width, newRect.Height);
8274
}
8375

8476
private Rect ResizeAndCentreExistingWindow(Size size)
@@ -96,52 +88,60 @@ private Rect ResizeAndCentreExistingWindow(Size size)
9688
// |LB | B |RB |10%
9789
// |---|-----------|---|---
9890

99-
const double limitPercentX = 0.1;
100-
const double limitPercentY = 0.1;
91+
var scale = DpiHelper.GetScaleFactorFromWindow(this);
92+
93+
var limitPercentX = 0.1 * scale.Horizontal;
94+
var limitPercentY = 0.1 * scale.Vertical;
10195

102-
var oldRect = new Rect(Left, Top, Width, Height);
96+
// use absolute pixels for calculation
97+
var pxSize = new Size(scale.Horizontal * size.Width, scale.Vertical * size.Height);
98+
var pxOldRect = this.GetWindowRectInPixel();
10399

104100
// scale to new size, maintain centre
105-
var newRect = Rect.Inflate(oldRect,
106-
(Math.Max(MinWidth, size.Width) - oldRect.Width) / 2,
107-
(Math.Max(MinHeight, size.Height) - oldRect.Height) / 2);
101+
var pxNewRect = Rect.Inflate(pxOldRect,
102+
(pxSize.Width - pxOldRect.Width) / 2,
103+
(pxSize.Height - pxOldRect.Height) / 2);
108104

109-
var desktopRect = WindowHelper.GetDesktopRectFromWindow(this);
105+
var desktopRect = WindowHelper.GetDesktopRectFromWindowInPixel(this);
110106

111107
var leftLimit = desktopRect.Left + desktopRect.Width * limitPercentX;
112108
var rightLimit = desktopRect.Right - desktopRect.Width * limitPercentX;
113109
var topLimit = desktopRect.Top + desktopRect.Height * limitPercentY;
114110
var bottomLimit = desktopRect.Bottom - desktopRect.Height * limitPercentY;
115111

116-
if (oldRect.Left < leftLimit && oldRect.Right < rightLimit) // L
117-
newRect.Location = new Point(Math.Max(oldRect.Left, desktopRect.Left), newRect.Top);
118-
else if (oldRect.Left > leftLimit && oldRect.Right > rightLimit) // R
119-
newRect.Location = new Point(Math.Min(oldRect.Right, desktopRect.Right) - newRect.Width, newRect.Top);
112+
if (pxOldRect.Left < leftLimit && pxOldRect.Right < rightLimit) // L
113+
pxNewRect.Location = new Point(Math.Max(pxOldRect.Left, desktopRect.Left), pxNewRect.Top);
114+
else if (pxOldRect.Left > leftLimit && pxOldRect.Right > rightLimit) // R
115+
pxNewRect.Location = new Point(Math.Min(pxOldRect.Right, desktopRect.Right) - pxNewRect.Width, pxNewRect.Top);
120116
else // C, fix window boundary
121-
newRect.Offset(
122-
Math.Max(0, desktopRect.Left - newRect.Left) + Math.Min(0, desktopRect.Right - newRect.Right), 0);
123-
124-
if (oldRect.Top < topLimit && oldRect.Bottom < bottomLimit) // T
125-
newRect.Location = new Point(newRect.Left, Math.Max(oldRect.Top, desktopRect.Top));
126-
else if (oldRect.Top > topLimit && oldRect.Bottom > bottomLimit) // B
127-
newRect.Location = new Point(newRect.Left,
128-
Math.Min(oldRect.Bottom, desktopRect.Bottom) - newRect.Height);
117+
pxNewRect.Offset(
118+
Math.Max(0, desktopRect.Left - pxNewRect.Left) + Math.Min(0, desktopRect.Right - pxNewRect.Right), 0);
119+
120+
if (pxOldRect.Top < topLimit && pxOldRect.Bottom < bottomLimit) // T
121+
pxNewRect.Location = new Point(pxNewRect.Left, Math.Max(pxOldRect.Top, desktopRect.Top));
122+
else if (pxOldRect.Top > topLimit && pxOldRect.Bottom > bottomLimit) // B
123+
pxNewRect.Location = new Point(pxNewRect.Left,
124+
Math.Min(pxOldRect.Bottom, desktopRect.Bottom) - pxNewRect.Height);
129125
else // C, fix window boundary
130-
newRect.Offset(0,
131-
Math.Max(0, desktopRect.Top - newRect.Top) + Math.Min(0, desktopRect.Bottom - newRect.Bottom));
126+
pxNewRect.Offset(0,
127+
Math.Max(0, desktopRect.Top - pxNewRect.Top) + Math.Min(0, desktopRect.Bottom - pxNewRect.Bottom));
132128

133-
return newRect;
129+
// return absolute location and relative size
130+
return new Rect(pxNewRect.Location, size);
134131
}
135132

136133
private Rect ResizeAndCentreNewWindow(Size size)
137134
{
138-
var desktopRect = WindowHelper.GetCurrentDesktopRect();
135+
var desktopRect = WindowHelper.GetCurrentDesktopRectInPixel();
136+
var scale = DpiHelper.GetCurrentScaleFactor();
137+
var pxSize = new Size(scale.Horizontal * size.Width, scale.Vertical * size.Height);
139138

140-
var newRect = Rect.Inflate(desktopRect,
141-
(Math.Max(MinWidth, size.Width) - desktopRect.Width) / 2,
142-
(Math.Max(MinHeight, size.Height) - desktopRect.Height) / 2);
139+
var pxLocation = new Point(
140+
desktopRect.X + (desktopRect.Width - pxSize.Width) / 2,
141+
desktopRect.Y + (desktopRect.Height - pxSize.Height) / 2);
143142

144-
return newRect;
143+
// return absolute location and relative size
144+
return new Rect(pxLocation, size);
145145
}
146146

147147
internal void UnloadPlugin()

0 commit comments

Comments
 (0)