Skip to content

Commit 0e3bec5

Browse files
committed
resizable window fixes #7, keep recent search history fixes #4, change search and folder boxes to comboboxes, focus on searchfield at start
1 parent f6979b4 commit 0e3bec5

File tree

5 files changed

+174
-30
lines changed

5 files changed

+174
-30
lines changed

FindInFiles/App.config

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="FindInFiles.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
38
<startup>
49
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
510
</startup>
11+
<userSettings>
12+
<FindInFiles.Properties.Settings>
13+
<setting name="windowWidth" serializeAs="String">
14+
<value>650</value>
15+
</setting>
16+
<setting name="windowHeight" serializeAs="String">
17+
<value>500</value>
18+
</setting>
19+
</FindInFiles.Properties.Settings>
20+
</userSettings>
621
</configuration>

FindInFiles/MainWindow.xaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
xmlns:local="clr-namespace:FindInFiles"
77
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="FindInFiles.MainWindow"
88
mc:Ignorable="d"
9-
Title="FindInFiles" Height="392.388" Width="781.747">
9+
Title="FindInFiles" Height="392.388" Width="781.747" Icon="findinfiles.ico">
1010
<Grid>
1111
<Grid.ColumnDefinitions>
1212
<ColumnDefinition Width="29*"/>
1313
<ColumnDefinition Width="100*"/>
1414
</Grid.ColumnDefinitions>
15-
<Button x:Name="btnFind" Content="Find" HorizontalAlignment="Left" Margin="68.255,19,0,0" VerticalAlignment="Top" Width="75" Click="btnFind_Click" Grid.Column="1"/>
16-
<Button x:Name="btnBrowse" Content="..." HorizontalAlignment="Left" Margin="512.255,17,0,0" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click" Grid.Column="1"/>
17-
<TextBox x:Name="txtSearch" HorizontalAlignment="Left" Height="23" Margin="10,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="227" KeyDown="OnKeyDownSearch" Grid.ColumnSpan="2"/>
18-
<TextBox x:Name="txtFolder" HorizontalAlignment="Left" Height="23" Margin="182.255,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="325" Grid.Column="1"/>
19-
<DataGrid x:Name="gridResults" HorizontalAlignment="Left" Height="296" Margin="10,55,0,0" VerticalAlignment="Top" Width="754" Grid.ColumnSpan="2" MouseDoubleClick="gridResults_MouseDoubleClick" IsReadOnly="True"/>
15+
<Button x:Name="btnFind" Content="Find" HorizontalAlignment="Left" Margin="68,28,0,0" VerticalAlignment="Top" Width="75" Click="btnFind_Click" Grid.Column="1" Height="22"/>
16+
<Button x:Name="btnBrowse" Content="..." HorizontalAlignment="Left" Margin="512,28,0,0" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click" Grid.Column="1" Height="22" IsEnabled="False"/>
17+
<DataGrid x:Name="gridResults" HorizontalAlignment="Left" Height="294" Margin="11,58,0,0" VerticalAlignment="Top" Width="754" Grid.ColumnSpan="2" MouseDoubleClick="gridResults_MouseDoubleClick" IsReadOnly="True" IsTabStop="True" TabIndex="2"/>
18+
<ComboBox x:Name="cmbSearch" HorizontalAlignment="Left" Margin="10,28,0,0" VerticalAlignment="Top" Width="227" KeyDown="OnKeyDownSearch" Grid.ColumnSpan="2" IsEditable="True" DropDownClosed="cmbSearch_DropDownClosed" IsSynchronizedWithCurrentItem="True" IsTabStop="True" TabIndex="0" Loaded="cmbSearch_Loaded" />
19+
<ComboBox x:Name="cmbFolder" Grid.Column="1" HorizontalAlignment="Left" Margin="182,28,0,0" VerticalAlignment="Top" Width="325" IsEditable="True" IsTabStop="True" TabIndex="1"/>
20+
<Label Content="Search String" HorizontalAlignment="Left" Margin="6,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.026,0.308" Height="30"/>
21+
<Label Content="Folder" HorizontalAlignment="Left" Margin="177,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.026,0.308" Height="30" Grid.Column="1"/>
2022

2123
</Grid>
2224
</Window>

FindInFiles/MainWindow.xaml.cs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// simple Find In Files tool for searching files containting given string
22
using System;
33
using System.Collections.Generic;
4+
using System.Collections.Specialized;
5+
using System.ComponentModel;
46
using System.Diagnostics;
57
using System.IO;
68
using System.Linq;
79
using System.Windows;
10+
using System.Windows.Controls;
811
using System.Windows.Input;
912

1013
namespace FindInFiles
@@ -16,6 +19,7 @@ public partial class MainWindow : Window
1619
{
1720
string[] fileExtensions = new[] { "*.txt", "*.shader", "*.cs", "*.log", "*.js", "*.cging" };
1821
const int previewSnippetLength = 32;
22+
const int maxRecentItems = 32;
1923

2024
public MainWindow()
2125
{
@@ -31,17 +35,75 @@ void Start()
3135
// have any args? first item is exe name, skip that
3236
if (args.Length > 1)
3337
{
34-
txtFolder.Text = args[1];
38+
cmbFolder.Text = args[1];
3539
}
3640

41+
// window size
42+
this.Width = Properties.Settings.Default.windowWidth;
43+
this.Height = Properties.Settings.Default.windowHeight;
44+
45+
// search history
46+
//cmbSearch.Items.Add(Properties.Settings.Default.recentSearches.Cast<string[]>());
47+
cmbSearch.ItemsSource = Properties.Settings.Default.recentSearches;
48+
49+
// focus on searchbox
50+
cmbSearch.Focus();
51+
52+
// get close event, so can save settings
53+
Application.Current.MainWindow.Closing += new CancelEventHandler(OnWindowClose);
54+
}
55+
56+
// force combobox text to be selected at start https://stackoverflow.com/q/31483650/5452781
57+
private void cmbSearch_Loaded(object sender, RoutedEventArgs e)
58+
{
59+
ComboBox cmBox = (System.Windows.Controls.ComboBox)sender;
60+
var textBox = (cmBox.Template.FindName("PART_EditableTextBox",
61+
cmBox) as TextBox);
62+
if (textBox != null)
63+
{
64+
textBox.Focus();
65+
textBox.SelectAll();
66+
}
67+
}
68+
69+
void OnWindowClose(object sender, CancelEventArgs e)
70+
{
71+
// save settings
72+
Properties.Settings.Default.windowWidth = (int)this.Width;
73+
Properties.Settings.Default.windowHeight = (int)this.Height;
74+
Properties.Settings.Default.Save();
3775
}
3876

3977
private void btnFind_Click(object sender, RoutedEventArgs e)
4078
{
41-
if (string.IsNullOrEmpty(txtSearch.Text) == false)
79+
Search(cmbSearch.Text, cmbFolder.Text);
80+
}
81+
82+
void AddSearchHistory(string searchString)
83+
{
84+
// handle settings
85+
if (Properties.Settings.Default.recentSearches == null)
86+
{
87+
Properties.Settings.Default.recentSearches = new StringCollection();
88+
}
89+
90+
// remove old items
91+
if (Properties.Settings.Default.recentSearches.Count > maxRecentItems)
92+
{
93+
Properties.Settings.Default.recentSearches.RemoveAt(0);
94+
}
95+
96+
// skip if duplicate
97+
if (Properties.Settings.Default.recentSearches.Contains(searchString) == false)
4298
{
43-
SearchFiles(txtSearch.Text, txtFolder.Text);
99+
Properties.Settings.Default.recentSearches.Add(searchString);
100+
Console.WriteLine("added");
44101
}
102+
Properties.Settings.Default.Save();
103+
104+
// rebuild dropdown
105+
cmbSearch.ItemsSource = null;
106+
cmbSearch.ItemsSource = Properties.Settings.Default.recentSearches;
45107
}
46108

47109
private void btnBrowse_Click(object sender, RoutedEventArgs e)
@@ -55,10 +117,10 @@ private void OnKeyDownSearch(object sender, KeyEventArgs e)
55117
switch (e.Key)
56118
{
57119
case Key.Escape:
58-
txtSearch.Text = "";
120+
cmbSearch.Text = "";
59121
break;
60122
case Key.Return:
61-
SearchFiles(txtSearch.Text, txtFolder.Text);
123+
Search(cmbSearch.Text, cmbFolder.Text);
62124
break;
63125
default:
64126
break;
@@ -77,8 +139,14 @@ private void gridResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
77139
myProcess.Start();
78140
}
79141

80-
void SearchFiles(string searchString, string sourceFolder)
142+
// main search method
143+
void Search(string searchString, string sourceFolder)
81144
{
145+
if (string.IsNullOrEmpty(searchString) == true) return;
146+
if (string.IsNullOrEmpty(sourceFolder) == true) return;
147+
148+
AddSearchHistory(cmbSearch.Text);
149+
82150
// validate folder
83151
if (Directory.Exists(sourceFolder) == false)
84152
{
@@ -117,6 +185,12 @@ void SearchFiles(string searchString, string sourceFolder)
117185
}
118186
gridResults.ItemsSource = results;
119187
}
188+
189+
// recent search item selected
190+
private void cmbSearch_DropDownClosed(object sender, EventArgs e)
191+
{
192+
Search(cmbSearch.Text, cmbFolder.Text);
193+
}
120194
}
121195
}
122196

FindInFiles/Properties/Settings.Designer.cs

Lines changed: 55 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
3-
<Profiles>
4-
<Profile Name="(Default)" />
5-
</Profiles>
6-
<Settings />
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="FindInFiles.Properties" GeneratedClassName="Settings">
3+
<Profiles />
4+
<Settings>
5+
<Setting Name="windowWidth" Type="System.Int32" Scope="User">
6+
<Value Profile="(Default)">650</Value>
7+
</Setting>
8+
<Setting Name="windowHeight" Type="System.Int32" Scope="User">
9+
<Value Profile="(Default)">500</Value>
10+
</Setting>
11+
<Setting Name="recentSearches" Type="System.Collections.Specialized.StringCollection" Scope="User">
12+
<Value Profile="(Default)" />
13+
</Setting>
14+
<Setting Name="recentFolders" Type="System.Collections.Specialized.StringCollection" Scope="User">
15+
<Value Profile="(Default)" />
16+
</Setting>
17+
</Settings>
718
</SettingsFile>

0 commit comments

Comments
 (0)