Skip to content

Commit

Permalink
Recover usable window position when config has -32000,-32000
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 17, 2019
1 parent 39c07f1 commit 72eea01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
21 changes: 17 additions & 4 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,16 @@ protected override void OnFormClosed(FormClosedEventArgs e)
base.OnFormClosed(e);
}

protected override void OnLoad(EventArgs e)
private void SetStartPosition()
{
if (configuration.WindowLoc.X == -1 && configuration.WindowLoc.Y == -1)
Screen screen = Util.FindScreen(configuration.WindowLoc, configuration.WindowSize);
if (screen == null)
{
// Start at center of screen if we have an invalid location saved in the config
// (such as -32000,-32000, which Windows uses when you're minimized)
StartPosition = FormStartPosition.CenterScreen;
}
else if (configuration.WindowLoc.X == -1 && configuration.WindowLoc.Y == -1)
{
// Center on screen for first launch
StartPosition = FormStartPosition.CenterScreen;
Expand All @@ -319,14 +326,20 @@ protected override void OnLoad(EventArgs e)
// Make sure there's room at the top for the MacOSX menu bar
Location = Util.ClampedLocationWithMargins(
configuration.WindowLoc, configuration.WindowSize,
new Size(0, 30), new Size(0, 0)
new Size(0, 30), new Size(0, 0),
screen
);
}
else
{
// Just make sure it's fully on screen
Location = Util.ClampedLocation(configuration.WindowLoc, configuration.WindowSize);
Location = Util.ClampedLocation(configuration.WindowLoc, configuration.WindowSize, screen);
}
}

protected override void OnLoad(EventArgs e)
{
SetStartPosition();
Size = configuration.WindowSize;
WindowState = configuration.IsWindowMaximised ? FormWindowState.Maximized : FormWindowState.Normal;

Expand Down
51 changes: 31 additions & 20 deletions GUI/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public static bool TryOpenWebPage(string url, IEnumerable<string> prefixes = nul
}
}

/// <summary>
/// Find a screen that the given box overlaps
/// </summary>
/// <param name="location">Upper left corner of box</param>
/// <param name="size">Width and height of box</param>
/// <returns>
/// The first screen that overlaps the box if any, otherwise null
/// </returns>
public static Screen FindScreen(Point location, Size size)
{
var rect = new Rectangle(location, size);
return Screen.AllScreens.FirstOrDefault(sc => sc.WorkingArea.IntersectsWith(rect));
}

/// <summary>
/// Adjust position of a box so it fits entirely on one screen
/// </summary>
Expand All @@ -125,26 +139,23 @@ public static bool TryOpenWebPage(string url, IEnumerable<string> prefixes = nul
/// Original location if already fully on-screen, otherwise
/// a position representing sliding it onto the screen
/// </returns>
public static Point ClampedLocation(Point location, Size size)
public static Point ClampedLocation(Point location, Size size, Screen screen = null)
{
var rect = new Rectangle(location, size);
// Find a screen that the default position overlaps
foreach (Screen screen in Screen.AllScreens)
if (screen == null)
{
if (screen.WorkingArea.IntersectsWith(rect))
{
// Slide the whole rectangle fully onto the screen
if (location.X < screen.WorkingArea.Top)
location.X = screen.WorkingArea.Top;
if (location.Y < screen.WorkingArea.Left)
location.Y = screen.WorkingArea.Left;
if (location.X + size.Width > screen.WorkingArea.Right)
location.X = screen.WorkingArea.Right - size.Width;
if (location.Y + size.Height > screen.WorkingArea.Bottom)
location.Y = screen.WorkingArea.Bottom - size.Height;
// Stop checking screens
break;
}
screen = FindScreen(location, size);
}
if (screen != null)
{
// Slide the whole rectangle fully onto the screen
if (location.X < screen.WorkingArea.Top)
location.X = screen.WorkingArea.Top;
if (location.Y < screen.WorkingArea.Left)
location.Y = screen.WorkingArea.Left;
if (location.X + size.Width > screen.WorkingArea.Right)
location.X = screen.WorkingArea.Right - size.Width;
if (location.Y + size.Height > screen.WorkingArea.Bottom)
location.Y = screen.WorkingArea.Bottom - size.Height;
}
return location;
}
Expand All @@ -160,12 +171,12 @@ public static Point ClampedLocation(Point location, Size size)
/// Original location if already fully on-screen plus margins, otherwise
/// a position representing sliding it onto the screen
/// </returns>
public static Point ClampedLocationWithMargins(Point location, Size size, Size topLeftMargin, Size bottomRightMargin)
public static Point ClampedLocationWithMargins(Point location, Size size, Size topLeftMargin, Size bottomRightMargin, Screen screen = null)
{
// Imagine drawing a larger box around the window, the size of the desired margin.
// We pass that box to ClampedLocation to make sure it fits on screen,
// then place our window at an offset within the box
return ClampedLocation(location - topLeftMargin, size + topLeftMargin + bottomRightMargin) + topLeftMargin;
return ClampedLocation(location - topLeftMargin, size + topLeftMargin + bottomRightMargin, screen) + topLeftMargin;
}

}
Expand Down

0 comments on commit 72eea01

Please sign in to comment.