Fixed: window size and location was not saved when closing a maximized or minimized window.

This commit is contained in:
MaxED 2016-11-22 12:07:39 +00:00 committed by spherallic
parent 79531796e6
commit 2cda8bd933

View file

@ -35,8 +35,11 @@ namespace CodeImp.DoomBuilder.Windows
public class DelayedForm : Form public class DelayedForm : Form
{ {
// Variables // Variables
private Timer formshowtimer;
protected readonly string configname; //mxd protected readonly string configname; //mxd
//mxd. Stored window size and location. Tracks location and size of FormWindowState.Normal window
private Size windowsize = Size.Empty;
private Point windowlocation = Point.Empty;
// Constructor // Constructor
protected DelayedForm() protected DelayedForm()
@ -48,23 +51,39 @@ namespace CodeImp.DoomBuilder.Windows
this.KeyUp += OnKeyUp; this.KeyUp += OnKeyUp;
} }
//mxd //mxd. Only when running (this.DesignMode doesn't seem to cut it here,
configname = this.GetType().Name.ToLowerInvariant(); // probably because not this, but a child class is in design mode...)
if(LicenseManager.UsageMode != LicenseUsageMode.Designtime)
// Create a timer that we need to show the form {
formshowtimer = new Timer { Interval = 1 }; configname = this.GetType().Name.ToLowerInvariant();
formshowtimer.Tick += formshowtimer_Tick; General.Actions.BindMethods(this);
}
} }
// When form is shown //mxd
protected override void OnShown(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
//mxd // Let the base class know
base.OnLoad(e);
if(this.DesignMode) return; if(this.DesignMode) return;
//mxd. Restore location and size // Restore location and size
this.SuspendLayout(); this.SuspendLayout();
// Restore windowstate
if(this.MaximizeBox)
{
this.WindowState = (FormWindowState)General.Settings.ReadSetting("windows." + configname + ".windowstate", (int)FormWindowState.Normal);
}
// Form size matters?
if(this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
this.Size = new Size(General.Settings.ReadSetting("windows." + configname + ".sizewidth", this.Size.Width),
General.Settings.ReadSetting("windows." + configname + ".sizeheight", this.Size.Height));
}
// Restore location // Restore location
Point validlocation = Point.Empty; Point validlocation = Point.Empty;
Point location = new Point(General.Settings.ReadSetting("windows." + configname + ".positionx", int.MaxValue), Point location = new Point(General.Settings.ReadSetting("windows." + configname + ".positionx", int.MaxValue),
@ -75,10 +94,7 @@ namespace CodeImp.DoomBuilder.Windows
// Location withing screen bounds? // Location withing screen bounds?
Rectangle bounds = new Rectangle(location, this.Size); Rectangle bounds = new Rectangle(location, this.Size);
bounds.Inflate(16, 16); // Add some safety padding bounds.Inflate(16, 16); // Add some safety padding
if(SystemInformation.VirtualScreen.IntersectsWith(bounds)) if(SystemInformation.VirtualScreen.IntersectsWith(bounds)) validlocation = location;
{
validlocation = location;
}
} }
if(validlocation == Point.Empty && !(this is MainForm)) if(validlocation == Point.Empty && !(this is MainForm))
@ -99,31 +115,10 @@ namespace CodeImp.DoomBuilder.Windows
this.Location = validlocation; this.Location = validlocation;
} }
// Restore windowstate // Show the form if needed
if(this.MaximizeBox) if(this.Opacity < 1.0) this.Opacity = 1.0;
{
this.WindowState = (FormWindowState)General.Settings.ReadSetting("windows." + configname + ".windowstate", (int)FormWindowState.Normal);
}
// Form size matters?
if(this.WindowState == FormWindowState.Normal
&& (this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow))
{
this.Size = new Size(General.Settings.ReadSetting("windows." + configname + ".sizewidth", this.Size.Width),
General.Settings.ReadSetting("windows." + configname + ".sizeheight", this.Size.Height));
}
this.ResumeLayout(); this.ResumeLayout();
//mxd end
// Let the base class know
base.OnShown(e);
// Start the timer to show the form
formshowtimer.Enabled = true;
// Bind any methods (mxd)
if(!DesignMode) General.Actions.BindMethods(this);
} }
//mxd. When form is closing //mxd. When form is closing
@ -149,27 +144,36 @@ namespace CodeImp.DoomBuilder.Windows
// Form size matters? // Form size matters?
if(this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow) if(this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{ {
General.Settings.WriteSetting("windows." + configname + ".sizewidth", this.Size.Width); Size size = ((windowsize.IsEmpty && this.WindowState == FormWindowState.Normal) ? this.Size : windowsize); // Prefer stored size if it was set
General.Settings.WriteSetting("windows." + configname + ".sizeheight", this.Size.Height); if(!size.IsEmpty)
{
General.Settings.WriteSetting("windows." + configname + ".sizewidth", size.Width);
General.Settings.WriteSetting("windows." + configname + ".sizeheight", size.Height);
}
} }
// Save location // Save location
General.Settings.WriteSetting("windows." + configname + ".positionx", this.Location.X); Point location = ((windowlocation.IsEmpty && this.WindowState == FormWindowState.Normal) ? this.Location : windowlocation); // Prefer stored location if it was set
General.Settings.WriteSetting("windows." + configname + ".positiony", this.Location.Y); if(!location.IsEmpty)
{
General.Settings.WriteSetting("windows." + configname + ".positionx", location.X);
General.Settings.WriteSetting("windows." + configname + ".positiony", location.Y);
}
} }
// When the form is to be shown //mxd. Also triggered when the window is dragged.
private void formshowtimer_Tick(object sender, EventArgs e) protected override void OnResizeEnd(EventArgs e)
{ {
// Get rid of the timer // Store location and size when window is not minimized or maximized
formshowtimer.Dispose(); if(this.WindowState == FormWindowState.Normal)
formshowtimer = null;
if(!this.IsDisposed)
{ {
// Make the form visible // Form size matters?
this.Opacity = 1.0; if(this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow)
windowsize = this.Size;
windowlocation = this.Location;
} }
base.OnResizeEnd(e);
} }
//mxd. Special handling to call "save screenshot" actions from any form, //mxd. Special handling to call "save screenshot" actions from any form,