Skip to content

Commit

Permalink
resizable window fixes #7, keep recent search history fixes #4, chang…
Browse files Browse the repository at this point in the history
…e search and folder boxes to comboboxes, focus on searchfield at start
  • Loading branch information
unitycoder committed Oct 14, 2018
1 parent f6979b4 commit 0e3bec5
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 30 deletions.
15 changes: 15 additions & 0 deletions FindInFiles/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?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="FindInFiles.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" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<FindInFiles.Properties.Settings>
<setting name="windowWidth" serializeAs="String">
<value>650</value>
</setting>
<setting name="windowHeight" serializeAs="String">
<value>500</value>
</setting>
</FindInFiles.Properties.Settings>
</userSettings>
</configuration>
14 changes: 8 additions & 6 deletions FindInFiles/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
xmlns:local="clr-namespace:FindInFiles"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="FindInFiles.MainWindow"
mc:Ignorable="d"
Title="FindInFiles" Height="392.388" Width="781.747">
Title="FindInFiles" Height="392.388" Width="781.747" Icon="findinfiles.ico">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="29*"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnFind" Content="Find" HorizontalAlignment="Left" Margin="68.255,19,0,0" VerticalAlignment="Top" Width="75" Click="btnFind_Click" Grid.Column="1"/>
<Button x:Name="btnBrowse" Content="..." HorizontalAlignment="Left" Margin="512.255,17,0,0" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click" Grid.Column="1"/>
<TextBox x:Name="txtSearch" HorizontalAlignment="Left" Height="23" Margin="10,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="227" KeyDown="OnKeyDownSearch" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtFolder" HorizontalAlignment="Left" Height="23" Margin="182.255,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="325" Grid.Column="1"/>
<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"/>
<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"/>
<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"/>
<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"/>
<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" />
<ComboBox x:Name="cmbFolder" Grid.Column="1" HorizontalAlignment="Left" Margin="182,28,0,0" VerticalAlignment="Top" Width="325" IsEditable="True" IsTabStop="True" TabIndex="1"/>
<Label Content="Search String" HorizontalAlignment="Left" Margin="6,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.026,0.308" Height="30"/>
<Label Content="Folder" HorizontalAlignment="Left" Margin="177,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.026,0.308" Height="30" Grid.Column="1"/>

</Grid>
</Window>
86 changes: 80 additions & 6 deletions FindInFiles/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// simple Find In Files tool for searching files containting given string
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

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

public MainWindow()
{
Expand All @@ -31,17 +35,75 @@ void Start()
// have any args? first item is exe name, skip that
if (args.Length > 1)
{
txtFolder.Text = args[1];
cmbFolder.Text = args[1];
}

// window size
this.Width = Properties.Settings.Default.windowWidth;
this.Height = Properties.Settings.Default.windowHeight;

// search history
//cmbSearch.Items.Add(Properties.Settings.Default.recentSearches.Cast<string[]>());
cmbSearch.ItemsSource = Properties.Settings.Default.recentSearches;

// focus on searchbox
cmbSearch.Focus();

// get close event, so can save settings
Application.Current.MainWindow.Closing += new CancelEventHandler(OnWindowClose);
}

// force combobox text to be selected at start https://stackoverflow.com/q/31483650/5452781
private void cmbSearch_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmBox = (System.Windows.Controls.ComboBox)sender;
var textBox = (cmBox.Template.FindName("PART_EditableTextBox",
cmBox) as TextBox);
if (textBox != null)
{
textBox.Focus();
textBox.SelectAll();
}
}

void OnWindowClose(object sender, CancelEventArgs e)
{
// save settings
Properties.Settings.Default.windowWidth = (int)this.Width;
Properties.Settings.Default.windowHeight = (int)this.Height;
Properties.Settings.Default.Save();
}

private void btnFind_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text) == false)
Search(cmbSearch.Text, cmbFolder.Text);
}

void AddSearchHistory(string searchString)
{
// handle settings
if (Properties.Settings.Default.recentSearches == null)
{
Properties.Settings.Default.recentSearches = new StringCollection();
}

// remove old items
if (Properties.Settings.Default.recentSearches.Count > maxRecentItems)
{
Properties.Settings.Default.recentSearches.RemoveAt(0);
}

// skip if duplicate
if (Properties.Settings.Default.recentSearches.Contains(searchString) == false)
{
SearchFiles(txtSearch.Text, txtFolder.Text);
Properties.Settings.Default.recentSearches.Add(searchString);
Console.WriteLine("added");
}
Properties.Settings.Default.Save();

// rebuild dropdown
cmbSearch.ItemsSource = null;
cmbSearch.ItemsSource = Properties.Settings.Default.recentSearches;
}

private void btnBrowse_Click(object sender, RoutedEventArgs e)
Expand All @@ -55,10 +117,10 @@ private void OnKeyDownSearch(object sender, KeyEventArgs e)
switch (e.Key)
{
case Key.Escape:
txtSearch.Text = "";
cmbSearch.Text = "";
break;
case Key.Return:
SearchFiles(txtSearch.Text, txtFolder.Text);
Search(cmbSearch.Text, cmbFolder.Text);
break;
default:
break;
Expand All @@ -77,8 +139,14 @@ private void gridResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
myProcess.Start();
}

void SearchFiles(string searchString, string sourceFolder)
// main search method
void Search(string searchString, string sourceFolder)
{
if (string.IsNullOrEmpty(searchString) == true) return;
if (string.IsNullOrEmpty(sourceFolder) == true) return;

AddSearchHistory(cmbSearch.Text);

// validate folder
if (Directory.Exists(sourceFolder) == false)
{
Expand Down Expand Up @@ -117,6 +185,12 @@ void SearchFiles(string searchString, string sourceFolder)
}
gridResults.ItemsSource = results;
}

// recent search item selected
private void cmbSearch_DropDownClosed(object sender, EventArgs e)
{
Search(cmbSearch.Text, cmbFolder.Text);
}
}
}

Expand Down
68 changes: 55 additions & 13 deletions FindInFiles/Properties/Settings.Designer.cs

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

21 changes: 16 additions & 5 deletions FindInFiles/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="FindInFiles.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="windowWidth" Type="System.Int32" Scope="User">
<Value Profile="(Default)">650</Value>
</Setting>
<Setting Name="windowHeight" Type="System.Int32" Scope="User">
<Value Profile="(Default)">500</Value>
</Setting>
<Setting Name="recentSearches" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="recentFolders" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

0 comments on commit 0e3bec5

Please sign in to comment.