Skip to content

Commit

Permalink
Fixed windows appearing off screen when primary monitor is zoomed but…
Browse files Browse the repository at this point in the history
… FRB is on 2nd monitor

fixes #1380
  • Loading branch information
vchelaru committed Mar 7, 2024
1 parent c87d809 commit b0a819f
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions FRBDK/Glue/Glue/Extensions/WpfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void MoveToCursor(this Window window, PresentationSource source =
{
window.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;

if(double.IsNaN(window.Width) || double.IsNaN(window.Height))
if (double.IsNaN(window.Width) || double.IsNaN(window.Height))
try { window.UpdateLayout(); } catch { } //Can this throw exception? I don't know...

double width = window.Width;
Expand Down Expand Up @@ -46,22 +46,25 @@ public static void MoveToCursor(this Window window, PresentationSource source =
window.Left = System.Math.Max(0, mousePositionX - width / 2);
window.Top = mousePositionY - height / 2;

window.ShiftWindowOntoScreen();
window.ShiftWindowOntoScreen(source);
}

public static void MoveToMainWindowCenterAndSize(this Window window, System.Windows.Forms.Form MainWindow, float WidthAmount = 0.75f, float HeightAmount = 0.75f)
{
const float MinSize = 400f;

var mw = MainWindow;
if(mw != null) {
if (mw != null)
{
window.Width = mw.Width * WidthAmount;
window.Height = mw.Height * HeightAmount;
window.Left = mw.Left + ((mw.Width - window.Width) / 2);
window.Top = mw.Top + ((mw.Height - window.Height) / 4);
if(window.Width < MinSize) window.Width = MinSize;
if(window.Height < MinSize) window.Height = MinSize;
} else {
if (window.Width < MinSize) window.Width = MinSize;
if (window.Height < MinSize) window.Height = MinSize;
}
else
{
window.Width = MinSize;
window.Height = MinSize;
}
Expand All @@ -74,9 +77,9 @@ public static void MoveToMainWindowCenterAndSize(this Window window, System.Wind
/// - Shift the window onto the visible screen.
/// - Shift the window away from overlapping the task bar.
/// </summary>
public static void ShiftWindowOntoScreen(this Window window)
public static void ShiftWindowOntoScreen(this Window window, PresentationSource source = null)
{
if(double.IsNaN(window.Height))
if (double.IsNaN(window.Height))
try { window.UpdateLayout(); } catch { } //Can this throw exception? I don't know...

var heightToUse = window.Height;
Expand All @@ -86,6 +89,26 @@ public static void ShiftWindowOntoScreen(this Window window)
}
var screen = Screen.FromPoint(Cursor.Position);
var bounds = screen.WorkingArea;

if (
// we have zoom info...
source != null &&
// screen isn't the primary screen...
screen != Screen.PrimaryScreen &&
// zoom isn't 1
source.CompositionTarget.TransformToDevice.M11 != 1
)
{
// There's a bug in winforms (or I just don't understand it)
// where the 2nd screen's values are multiplied by the transformation matrix. Let's divide
bounds = new Rectangle(
(int)(bounds.X / source.CompositionTarget.TransformToDevice.M11),
(int)(bounds.Y / source.CompositionTarget.TransformToDevice.M22),
(int)(bounds.Width / source.CompositionTarget.TransformToDevice.M11),
(int)(bounds.Height / source.CompositionTarget.TransformToDevice.M22)
);
}

// Note that "window.BringIntoView()" does not work.
if (window.Top < bounds.Top)
{
Expand Down

0 comments on commit b0a819f

Please sign in to comment.