Skip to content
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

Save restore position when minimized #2725

Merged
merged 2 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 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 Expand Up @@ -470,7 +483,7 @@ protected override void OnFormClosing(FormClosingEventArgs e)
}

// Copy window location to app settings
configuration.WindowLoc = Location;
configuration.WindowLoc = WindowState == FormWindowState.Normal ? Location : RestoreBounds.Location;

// Copy window size to app settings if not maximized
configuration.WindowSize = WindowState == FormWindowState.Normal ? Size : RestoreBounds.Size;
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