-
-
Notifications
You must be signed in to change notification settings - Fork 449
Fix Vertical Window Positioning with Multiple Monitors #3537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -711,8 +711,26 @@ void InitializePositionInner() | |||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Top = _settings.WindowTop; | ||||||||||||||||||||||||||||||||
var previousScreenWidth = _settings.PreviousScreenWidth; | ||||||||||||||||||||||||||||||||
var previousScreenHeight = _settings.PreviousScreenHeight; | ||||||||||||||||||||||||||||||||
GetDpi(out var previousDpiX, out var previousDpiY); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
_settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth; | ||||||||||||||||||||||||||||||||
_settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight; | ||||||||||||||||||||||||||||||||
GetDpi(out var currentDpiX, out var currentDpiY); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (previousScreenWidth != 0 && previousScreenHeight != 0 && | ||||||||||||||||||||||||||||||||
previousDpiX != 0 && previousDpiY != 0 && | ||||||||||||||||||||||||||||||||
(previousScreenWidth != SystemParameters.VirtualScreenWidth || | ||||||||||||||||||||||||||||||||
previousScreenHeight != SystemParameters.VirtualScreenHeight || | ||||||||||||||||||||||||||||||||
previousDpiX != currentDpiX || previousDpiY != currentDpiY)) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
AdjustPositionForResolutionChange(); | ||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Left = _settings.WindowLeft; | ||||||||||||||||||||||||||||||||
Top = _settings.WindowTop; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
|
@@ -725,27 +743,73 @@ void InitializePositionInner() | |||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||
case SearchWindowAligns.CenterTop: | ||||||||||||||||||||||||||||||||
Left = HorizonCenter(screen); | ||||||||||||||||||||||||||||||||
Top = 10; | ||||||||||||||||||||||||||||||||
Top = VerticalTop(screen); | ||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||
case SearchWindowAligns.LeftTop: | ||||||||||||||||||||||||||||||||
Left = HorizonLeft(screen); | ||||||||||||||||||||||||||||||||
Top = 10; | ||||||||||||||||||||||||||||||||
Top = VerticalTop(screen); | ||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||
case SearchWindowAligns.RightTop: | ||||||||||||||||||||||||||||||||
Left = HorizonRight(screen); | ||||||||||||||||||||||||||||||||
Top = 10; | ||||||||||||||||||||||||||||||||
Top = VerticalTop(screen); | ||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||
case SearchWindowAligns.Custom: | ||||||||||||||||||||||||||||||||
Left = Win32Helper.TransformPixelsToDIP(this, | ||||||||||||||||||||||||||||||||
screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; | ||||||||||||||||||||||||||||||||
Top = Win32Helper.TransformPixelsToDIP(this, 0, | ||||||||||||||||||||||||||||||||
screen.WorkingArea.Y + _settings.CustomWindowTop).Y; | ||||||||||||||||||||||||||||||||
var customLeft = Win32Helper.TransformPixelsToDIP(this, | ||||||||||||||||||||||||||||||||
screen.WorkingArea.X + _settings.CustomWindowLeft, 0); | ||||||||||||||||||||||||||||||||
var customTop = Win32Helper.TransformPixelsToDIP(this, 0, | ||||||||||||||||||||||||||||||||
screen.WorkingArea.Y + _settings.CustomWindowTop); | ||||||||||||||||||||||||||||||||
Left = customLeft.X; | ||||||||||||||||||||||||||||||||
Top = customTop.Y; | ||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
private void AdjustPositionForResolutionChange() | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
var screenWidth = SystemParameters.VirtualScreenWidth; | ||||||||||||||||||||||||||||||||
var screenHeight = SystemParameters.VirtualScreenHeight; | ||||||||||||||||||||||||||||||||
GetDpi(out var currentDpiX, out var currentDpiY); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var previousLeft = _settings.WindowLeft; | ||||||||||||||||||||||||||||||||
var previousTop = _settings.WindowTop; | ||||||||||||||||||||||||||||||||
GetDpi(out var previousDpiX, out var previousDpiY); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var widthRatio = screenWidth / _settings.PreviousScreenWidth; | ||||||||||||||||||||||||||||||||
var heightRatio = screenHeight / _settings.PreviousScreenHeight; | ||||||||||||||||||||||||||||||||
var dpiXRatio = currentDpiX / previousDpiX; | ||||||||||||||||||||||||||||||||
var dpiYRatio = currentDpiY / previousDpiY; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var newLeft = previousLeft * widthRatio * dpiXRatio; | ||||||||||||||||||||||||||||||||
var newTop = previousTop * heightRatio * dpiYRatio; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Comment on lines
+779
to
+786
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Scaling uses freshly-overwritten values ⇒ ratios are always 1 Inside Either pass the captured -var widthRatio = screenWidth / _settings.PreviousScreenWidth;
-var heightRatio = screenHeight / _settings.PreviousScreenHeight;
-var dpiXRatio = currentDpiX / previousDpiX;
-var dpiYRatio = currentDpiY / previousDpiY;
+var widthRatio = screenWidth / previousScreenWidth;
+var heightRatio = screenHeight / previousScreenHeight;
+var dpiXRatio = currentDpiX / previousDpiX;
+var dpiYRatio = currentDpiY / previousDpiY; Fixing this will finally position the window correctly after display configuration changes. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||
var screenLeft = SystemParameters.VirtualScreenLeft; | ||||||||||||||||||||||||||||||||
var screenTop = SystemParameters.VirtualScreenTop; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
var maxX = screenLeft + screenWidth - ActualWidth; | ||||||||||||||||||||||||||||||||
var maxY = screenTop + screenHeight - ActualHeight; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Left = Math.Max(screenLeft, Math.Min(newLeft, maxX)); | ||||||||||||||||||||||||||||||||
Top = Math.Max(screenTop, Math.Min(newTop, maxY)); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
private void GetDpi(out double dpiX, out double dpiY) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
var source = PresentationSource.FromVisual(this); | ||||||||||||||||||||||||||||||||
if (source != null && source.CompositionTarget != null) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
var matrix = source.CompositionTarget.TransformToDevice; | ||||||||||||||||||||||||||||||||
dpiX = 96 * matrix.M11; | ||||||||||||||||||||||||||||||||
dpiY = 96 * matrix.M22; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
dpiX = 96; | ||||||||||||||||||||||||||||||||
dpiY = 96; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
private Screen SelectedScreen() | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Screen screen; | ||||||||||||||||||||||||||||||||
|
@@ -806,6 +870,13 @@ private double HorizonLeft(Screen screen) | |||||||||||||||||||||||||||||||
return left; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
public double VerticalTop(Screen screen) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
var dip1 = Win32Helper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); | ||||||||||||||||||||||||||||||||
var top = dip1.Y + 10; | ||||||||||||||||||||||||||||||||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
return top; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
#endregion | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
#region Window Animation | ||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -137,18 +137,40 @@ public void UpdatePositionAndState() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (previousTop == null || previousLeft == null || !IsPositionValid(previousTop.Value, previousLeft.Value)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Top = WindowTop(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Left = WindowLeft(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SetWindowPosition(WindowTop(), WindowLeft()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Top = previousTop.Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Left = previousLeft.Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var left = _settings.SettingWindowLeft.Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var top = _settings.SettingWindowTop.Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AdjustWindowPosition(ref top, ref left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SetWindowPosition(top, left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WindowState = _settings.SettingWindowState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private void SetWindowPosition(double top, double left) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Ensure window does not exceed screen boundaries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
top = Math.Max(top, SystemParameters.VirtualScreenTop); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
left = Math.Max(left, SystemParameters.VirtualScreenLeft); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Top = top; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Left = left; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private void AdjustWindowPosition(ref double top, ref double left) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Jack251970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Adjust window position if it exceeds screen boundaries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
top = Math.Max(top, SystemParameters.VirtualScreenTop); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
left = Math.Max(left, SystemParameters.VirtualScreenLeft); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+153
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clamp calculations use width/height instead of absolute screen bounds – may push window off-screen
Current logic allows the window to slip below / to the right of the virtual desktop when monitors start at a negative coordinate (very common with mixed-resolution or vertical layouts). -top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight);
-left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth);
+var maxTop = SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight - ActualHeight;
+var maxLeft = SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth - ActualWidth;
+
+top = Math.Min(top, maxTop);
+left = Math.Min(left, maxLeft); Apply the same fix in 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private static bool IsPositionValid(double top, double left) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var screen in Screen.AllScreens) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous DPI/screen data are never read from settings and therefore always equal current values
previousDpiX/Y
are obtained viaGetDpi
, which returns current DPI, not the saved values.As a result the “resolution/DPI change” branch is never taken and scaling never occurs.
Remember to store the current values after the position logic runs:
Without this, the new properties added to
Settings
are never populated.