Skip to content

Commit

Permalink
fix: resolved an error that would occur upon closing the app due to s…
Browse files Browse the repository at this point in the history
…aving window size and location in app folder (#191)

fixes #190
Thanks @mega5800 !
  • Loading branch information
mega5800 authored May 3, 2024
1 parent 83e57cf commit e8c7b5c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 51 deletions.
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>

0 comments on commit e8c7b5c

Please sign in to comment.