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

improved ability to save and load app window size and location #191

Merged
merged 1 commit into from
May 3, 2024
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
53 changes: 11 additions & 42 deletions src/LessMsi.Gui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ internal class MainForm : Form, IMainFormView
private ToolStripMenuItem searchFileToolStripMenuItem;
readonly static string[] AllowedDragDropExtensions = new[] { ".msi", ".msp" };

#region Form size and location members
private const string c_WindowWidthAttribute = "WindowWidth";
private const string c_WindowHeightAttribute = "WindowHeight";

private const string c_XPositionAttribute = "X_Position";
private const string c_YPositionAttribute = "Y_Position";
#endregion

public MainForm(string defaultInputFile)
{
InitializeComponent();
Expand Down Expand Up @@ -1088,46 +1080,23 @@ private void cboStream_SelectedValueChanged(object sender, EventArgs e)

private void MainForm_Load(object sender, EventArgs e)
{
int windowWidth = getIntValueFromConfiguration(c_WindowWidthAttribute, MinimumSize.Width);
int windowHeight = getIntValueFromConfiguration(c_WindowHeightAttribute, MinimumSize.Height);
Size lastRecordedAppSize = Settings.Default.LastRecordedAppSize;
Point lastRecordedAppLocation = Settings.Default.LastRecordedAppLocation;

int xPosition = getIntValueFromConfiguration(c_XPositionAttribute, 0);
int yPosition = getIntValueFromConfiguration(c_YPositionAttribute, 0);
Size = new Size
(
Math.Max(MinimumSize.Width, lastRecordedAppSize.Width),
Math.Max(MinimumSize.Height, lastRecordedAppSize.Height)
);

Size = new Size(windowWidth, windowHeight);
Location = new Point(xPosition, yPosition);
Location = lastRecordedAppLocation;
}

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
updateConfiguration(c_WindowWidthAttribute, Size.Width.ToString());
updateConfiguration(c_WindowHeightAttribute, Size.Height.ToString());

updateConfiguration(c_XPositionAttribute, Location.X.ToString());
updateConfiguration(c_YPositionAttribute, Location.Y.ToString());
}
#endregion

#region Form size and location methods
private int getIntValueFromConfiguration(string i_SettingName, int i_MinValue)
{
int intValue = 0;
string rawValue = ConfigurationManager.AppSettings.Get(i_SettingName);

int.TryParse(rawValue, out intValue);
intValue = Math.Max(intValue, i_MinValue);

return intValue;
}

private void updateConfiguration(string i_SettingName, string i_SettingValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

config.AppSettings.Settings.Remove(i_SettingName);
config.AppSettings.Settings.Add(i_SettingName, i_SettingValue);

config.Save(ConfigurationSaveMode.Minimal);
Settings.Default.LastRecordedAppSize = Size;
Settings.Default.LastRecordedAppLocation = Location;
Settings.Default.Save();
}
#endregion

Expand Down
28 changes: 26 additions & 2 deletions src/LessMsi.Gui/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/LessMsi.Gui/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
<Setting Name="RecentFiles" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="LastRecordedAppSize" Type="System.Drawing.Size" Scope="User">
<Value Profile="(Default)">0, 0</Value>
</Setting>
<Setting Name="LastRecordedAppLocation" Type="System.Drawing.Point" Scope="User">
<Value Profile="(Default)">10, 10</Value>
</Setting>
</Settings>
</SettingsFile>
22 changes: 15 additions & 7 deletions src/LessMsi.Gui/app.config
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="LessMsi.Gui.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0"/>
<supportedRuntime version="v4.5"/>
</startup>
<appSettings>
<add key="WindowWidth" value="0"/>
<add key="WindowHeight" value="0"/>

<add key="X_Position" value="10"/>
<add key="Y_Position" value="10"/>
</appSettings>
<userSettings>
<LessMsi.Gui.Properties.Settings>
<setting name="LastRecordedAppSize" serializeAs="String">
<value>0, 0</value>
</setting>
<setting name="LastRecordedAppLocation" serializeAs="String">
<value>10, 10</value>
</setting>
</LessMsi.Gui.Properties.Settings>
</userSettings>
</configuration>