Skip to content

Commit 6f7e774

Browse files
authored
Merge pull request #1 from fotijr/window-size
Persist window location and size
2 parents 8117f56 + 8a9400f commit 6f7e774

6 files changed

Lines changed: 205 additions & 2 deletions

File tree

ListFiles.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:vs_shell="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.11.0"
5-
Title="Quick Open File" Height="300" Width="675" WindowStartupLocation="CenterOwner"
5+
Title="Quick Open File" WindowStartupLocation="CenterOwner"
66
MinWidth="200" MinHeight="100" KeyDown="Window_KeyDown"
77
DataContext="{Binding RelativeSource={RelativeSource Self}}" ShowInTaskbar="False"
8-
PreviewKeyDown="Window_PreviewKeyDown">
8+
PreviewKeyDown="Window_PreviewKeyDown"
9+
SourceInitialized="Window_SourceInitialized"
10+
Closing="Window_Closing">
911

1012
<Window.Resources>
1113
<Style TargetType="ListBox">

ListFiles.xaml.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public ListFiles(IEnumerable<ProjectItemWrapper> inItems)
4444
}
4545
}
4646

47+
protected void Window_SourceInitialized(object sender, System.EventArgs e)
48+
{
49+
LoadWindowSettings();
50+
}
51+
4752
private void FilterProjectItems(object sender, FilterEventArgs e)
4853
{
4954
e.Accepted = true;
@@ -137,6 +142,11 @@ private void OpenSelectedFiles(bool bInSolutionExplorer)
137142
Close();
138143
}
139144

145+
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
146+
{
147+
SaveWindowSettings();
148+
}
149+
140150
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
141151
{
142152
if (lstFiles.Items.Count > 0)
@@ -270,5 +280,49 @@ private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
270280
}
271281
}
272282
}
283+
284+
/// <summary>
285+
/// Restores the window's previous size and position
286+
/// </summary>
287+
private void LoadWindowSettings()
288+
{
289+
try
290+
{
291+
var bottomBound = System.Windows.Forms.Screen.AllScreens.Max(s => s.Bounds.Bottom);
292+
var rightBound = System.Windows.Forms.Screen.AllScreens.Max(s => s.Bounds.Right);
293+
294+
// only apply left setting if it is visible on current screen(s)
295+
if (Properties.Settings.Default.Left < rightBound)
296+
{
297+
this.Left = Properties.Settings.Default.Left;
298+
}
299+
300+
// only apply top setting if it is visible on current screen(s)
301+
if (Properties.Settings.Default.Top < bottomBound)
302+
{
303+
this.Top = Properties.Settings.Default.Top;
304+
}
305+
306+
this.Width = Properties.Settings.Default.Width;
307+
this.Height = Properties.Settings.Default.Height;
308+
this.WindowState = (WindowState)Properties.Settings.Default.WindowState;
309+
}
310+
catch (System.Exception ex)
311+
{
312+
// swallow exception if settings fail to load
313+
}
314+
}
315+
316+
/// <summary>
317+
/// Saves the window's current size and position
318+
/// </summary>
319+
private void SaveWindowSettings()
320+
{
321+
Properties.Settings.Default.Width = RestoreBounds.Width;
322+
Properties.Settings.Default.Height = RestoreBounds.Height;
323+
Properties.Settings.Default.Top = RestoreBounds.Top;
324+
Properties.Settings.Default.Left = RestoreBounds.Left;
325+
Properties.Settings.Default.WindowState = (int)this.WindowState;
326+
}
273327
}
274328
}

OpenFileInSolution.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
</UpgradeBackupLocation>
1111
<OldToolsVersion>12.0</OldToolsVersion>
1212
</PropertyGroup>
13+
<PropertyGroup>
14+
<StartupObject />
15+
</PropertyGroup>
1316
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
1417
<PropertyGroup>
1518
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -130,6 +133,11 @@
130133
<Compile Include="ListFiles.xaml.cs">
131134
<DependentUpon>ListFiles.xaml</DependentUpon>
132135
</Compile>
136+
<Compile Include="Properties\Settings.Designer.cs">
137+
<AutoGen>True</AutoGen>
138+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
139+
<DependentUpon>Settings.settings</DependentUpon>
140+
</Compile>
133141
<Compile Include="Resources.Designer.cs">
134142
<AutoGen>True</AutoGen>
135143
<DesignTime>True</DesignTime>
@@ -155,7 +163,12 @@
155163
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
156164
<IncludeInVSIX>true</IncludeInVSIX>
157165
</Content>
166+
<None Include="app.config" />
158167
<None Include="packages.config" />
168+
<None Include="Properties\Settings.settings">
169+
<Generator>SettingsSingleFileGenerator</Generator>
170+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
171+
</None>
159172
<None Include="source.extension.vsixmanifest">
160173
<SubType>Designer</SubType>
161174
</None>

Properties/Settings.Designer.cs

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Settings.settings

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PerniciousGames.OpenFileInSolution.Properties" GeneratedClassName="Settings">
3+
<Profiles />
4+
<Settings>
5+
<Setting Name="Height" Type="System.Double" Scope="User">
6+
<Value Profile="(Default)">300</Value>
7+
</Setting>
8+
<Setting Name="Width" Type="System.Double" Scope="User">
9+
<Value Profile="(Default)">675</Value>
10+
</Setting>
11+
<Setting Name="Left" Type="System.Double" Scope="User">
12+
<Value Profile="(Default)">0</Value>
13+
</Setting>
14+
<Setting Name="Top" Type="System.Double" Scope="User">
15+
<Value Profile="(Default)">0</Value>
16+
</Setting>
17+
<Setting Name="WindowState" Type="System.Int32" Scope="User">
18+
<Value Profile="(Default)">0</Value>
19+
</Setting>
20+
</Settings>
21+
</SettingsFile>

app.config

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="PerniciousGames.OpenFileInSolution.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
8+
<userSettings>
9+
<PerniciousGames.OpenFileInSolution.Properties.Settings>
10+
<setting name="Height" serializeAs="String">
11+
<value>300</value>
12+
</setting>
13+
<setting name="Width" serializeAs="String">
14+
<value>675</value>
15+
</setting>
16+
<setting name="Left" serializeAs="String">
17+
<value>0</value>
18+
</setting>
19+
<setting name="Top" serializeAs="String">
20+
<value>0</value>
21+
</setting>
22+
<setting name="WindowState" serializeAs="String">
23+
<value>0</value>
24+
</setting>
25+
</PerniciousGames.OpenFileInSolution.Properties.Settings>
26+
</userSettings>
27+
</configuration>

0 commit comments

Comments
 (0)