Skip to content

Commit

Permalink
Fixed: Shortcuts don't occlude actual searches
Browse files Browse the repository at this point in the history
- Searches that have been matched to a shortcut also return the actual
text results (eg. 'i' being a shortcut for 'internet'. we now return
results for both 'internet' AND 'i' instead of just 'internet')
  • Loading branch information
betsegaw committed Sep 7, 2014
1 parent 1ef94d6 commit e27c57e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 31 deletions.
65 changes: 56 additions & 9 deletions Window Walker/Window Walker/Components/WindowSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private async void SyncOpenWindowsWithModelAsync()

foreach(var window in windows)
{
this.searchMatches.Add(new WindowSearchResult(window, new List<int>(), new List<int>()));
this.searchMatches.Add(new WindowSearchResult(window, new List<int>(), new List<int>(), WindowSearchResult.SearchType.Empty));
}
}
else
Expand All @@ -156,20 +156,30 @@ private Task<List<WindowSearchResult>> FuzzySearchOpenWindowsAsync(List<Window>
private List<WindowSearchResult> FuzzySearchOpenWindows(List<Window> openWindows)
{
List<WindowSearchResult> result = new List<WindowSearchResult>();
List<SearchString> searchStrings = new List<SearchString>();

string shortcut = SettingsManager.Instance.GetShortcut(this.SearchText);
string searchString = (shortcut != null) ? shortcut : this.searchText;

foreach(var window in openWindows)
if (shortcut != null)
{
var titleMatch = FuzzyMatching.FindBestFuzzyMatch(window.Title, searchString);
var processMatch = FuzzyMatching.FindBestFuzzyMatch(window.ProcessName, searchString);
searchStrings.Add(new SearchString(shortcut, WindowSearchResult.SearchType.Shortcut));
}

searchStrings.Add(new SearchString(this.searchText, WindowSearchResult.SearchType.Fuzzy));

if ((titleMatch.Count != 0 || processMatch.Count != 0) &&
window.Title.Length != 0)
foreach (var searchString in searchStrings)
{
foreach (var window in openWindows)
{
var temp = new WindowSearchResult(window, titleMatch, processMatch);
result.Add(temp);
var titleMatch = FuzzyMatching.FindBestFuzzyMatch(window.Title, searchString.SearchText);
var processMatch = FuzzyMatching.FindBestFuzzyMatch(window.ProcessName, searchString.SearchText);

if ((titleMatch.Count != 0 || processMatch.Count != 0) &&
window.Title.Length != 0)
{
var temp = new WindowSearchResult(window, titleMatch, processMatch, searchString.SearchType);
result.Add(temp);
}
}
}

Expand All @@ -178,6 +188,43 @@ private List<WindowSearchResult> FuzzySearchOpenWindows(List<Window> openWindows
return result;
}

/// <summary>
/// A class to represent a search string
/// </summary>
/// <remarks>Class was added inorder to be able to attach various context data to
/// a search string</remarks>
class SearchString
{
/// <summary>
/// Where is the search string coming from (is it a shortcut
/// or direct string, etc...)
/// </summary>
public WindowSearchResult.SearchType SearchType
{
get;
private set;
}

/// <summary>
/// The actual text we are searching for
/// </summary>
public string SearchText
{
get;
private set;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="searchText"></param>
/// <param name="searchType"></param>
public SearchString(string searchText, WindowSearchResult.SearchType searchType)
{
this.SearchText = searchText;
this.SearchType = searchType;
}
}
#endregion
}
}
35 changes: 34 additions & 1 deletion Window Walker/Window Walker/Components/WindowSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public List<int> SearchMatchesInProcessName
private set;
}

/// <summary>
/// The type of match (shortcut, fuzzy or nothing)
/// </summary>
public SearchType SearchResultMatchType
{
get;
private set;
}

/// <summary>
/// A score indicating how well this matches what we are looking for
/// </summary>
Expand All @@ -60,11 +69,12 @@ public TextType BestScoreSource
/// <summary>
/// Constructor
/// </summary>
public WindowSearchResult(Window window, List<int> matchesInTitle, List<int> matchesInProcessName)
public WindowSearchResult(Window window, List<int> matchesInTitle, List<int> matchesInProcessName, SearchType matchType)
{
this.ResultWindow = window;
this.SearchMatchesInTitle = matchesInTitle;
this.SearchMatchesInProcessName = matchesInProcessName;
this.SearchResultMatchType = matchType;
this.CalculateScore();
}

Expand Down Expand Up @@ -97,5 +107,28 @@ public enum TextType
ProcessName,
WindowTitle
}

/// <summary>
/// The type of search
/// </summary>
public enum SearchType
{
/// <summary>
/// the search string is empty, which means all open windows are
/// going to be returned
/// </summary>
Empty,

/// <summary>
/// Regular fuzzy match search
/// </summary>
Fuzzy,

/// <summary>
/// The user has entered text that has been matched to a shortcut
/// and the shortcut is now being searched
/// </summary>
Shortcut
}
}
}
52 changes: 31 additions & 21 deletions Window Walker/Window Walker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,41 @@ public void SearchResultUpdateHandler(object sender, WindowWalker.Components.Win
resultsListBox.Items.Clear();
var windowsResult = WindowSearchController.Instance.SearchMatches.Where(x => x.ResultWindow.Hwnd != this.handleToMainWindow);
Dictionary<TextBlock, WindowSearchResult> highlightStack = new Dictionary<TextBlock,WindowSearchResult>();
Dictionary<TextBlock, WindowSearchResult> highlightStack = new Dictionary<TextBlock, WindowSearchResult>();
var windowResultsByType = new List<List<WindowSearchResult>>();
var windowNoSearchTextResults = windowsResult.Where(x => x.SearchResultMatchType == WindowSearchResult.SearchType.Empty).OrderByDescending(x => x.Score);
var windowsShortcutResults = windowsResult.Where(x => x.SearchResultMatchType == WindowSearchResult.SearchType.Shortcut).OrderByDescending(x => x.Score);
var windowsFuzzyResults = windowsResult.Where(x => x.SearchResultMatchType == WindowSearchResult.SearchType.Fuzzy).OrderByDescending(x => x.Score);
windowsResult = windowsResult.OrderByDescending(x => x.Score);
windowResultsByType.Add(windowNoSearchTextResults.ToList());
windowResultsByType.Add(windowsShortcutResults.ToList());
windowResultsByType.Add(windowsFuzzyResults.ToList());
foreach (WindowSearchResult windowResult in windowsResult)
foreach (var windowsResultForType in windowResultsByType)
{
/// Each window is shown in a horizontal stack panel
/// that contains an image object on the left and
/// a textblock with the window title on the right
var tempStackPanel = new StackPanel();
tempStackPanel.Orientation = Orientation.Horizontal;
var image = new Image();
image.Source = windowResult.ResultWindow.WindowIcon;
image.Margin = new Thickness(0,0,5,0);
tempStackPanel.Children.Add(image);
var tempTextBlock = new TextBlockWindow();
tempTextBlock.Window = windowResult.ResultWindow;
tempStackPanel.Children.Add(tempTextBlock);
image.Height = 15;
this.resultsListBox.Items.Add(tempStackPanel);
highlightStack[tempTextBlock] = windowResult;
foreach (WindowSearchResult windowResult in windowsResultForType)
{
/// Each window is shown in a horizontal stack panel
/// that contains an image object on the left and
/// a textblock with the window title on the right
var tempStackPanel = new StackPanel();
tempStackPanel.Orientation = Orientation.Horizontal;
var image = new Image();
image.Source = windowResult.ResultWindow.WindowIcon;
image.Margin = new Thickness(0, 0, 5, 0);
tempStackPanel.Children.Add(image);
var tempTextBlock = new TextBlockWindow();
tempTextBlock.Window = windowResult.ResultWindow;
tempStackPanel.Children.Add(tempTextBlock);
image.Height = 15;
this.resultsListBox.Items.Add(tempStackPanel);
highlightStack[tempTextBlock] = windowResult;
}
}
if (resultsListBox.Items.Count != 0)
{
resultsListBox.SelectedIndex = 0;
Expand Down

0 comments on commit e27c57e

Please sign in to comment.