Files
PowerToys/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/SearchController.cs

153 lines
4.8 KiB
C#
Raw Normal View History

2020-08-17 10:00:56 -07:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/
using System;
using System.Collections.Generic;
Adding FxCop to Microsoft.Plugin.WindowWalker (#6260) * Adding FxCop to Microsoft.Plugin.WindowWalker * Delete WindowResult.cs -- Fix for CA1812 WindowResult is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it static (Shared in Visual Basic). * Fix for CA1806 UpdateOpenWindowsList calls EnumWindows but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. * Fix for: CA1066 Type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT should implement IEquatable<T> because it overrides Equals * Fix for: CA1052 Type 'FuzzyMatching' is a static holder type but is neither static nor NotInheritable * Suppress for CA1069 - These values are defined in https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles. CA1069 The enum member 'WS_EX_LTRREADING' has the same constant value '0' as member 'WS_EX_LEFT' CA1069 The enum member 'WS_EX_RIGHTSCROLLBAR' has the same constant value '0' as member 'WS_EX_LEFT' * Supress CA1069 Code Description CA1069 The enum member 'SWP_NOREPOSITION' has the same constant value '512' as member 'SWP_NOOWNERZORDER' CA1069 The enum member 'SWP_FRAMECHANGED' has the same constant value '32' as member 'SWP_DRAWFRAME' * Suprress CA1069 for ShowWindow values. See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow CA1069 The enum member 'ShowMaximized' has the same constant value '3' as member 'Maximize' * Fix code formatting error * Fix for CA2235: Making POINT serializable CA2235 Field MinPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable CA2235 Field MaxPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable * Fix CA2235 Making RECT serializable CA2235 Field NormalPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT which is not serializable * Fixes for CA2101 Specify marshaling for P/Invoke string arguments. * Fixes for CA2007 Consider calling ConfigureAwait on the awaited task * Fixes for the following (CA1822 / CA1801): CA1822 Member 'OnOpenWindowsUpdate' does not access instance data and can be marked as static Code Description CA1801 Parameter value of method add_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. CA1801 Parameter value of method remove_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. * Fix: CA1710 Rename OpenWindowsUpdateHandler to end in 'EventHandler' * Fix CA1822 Member 'GetProcessIDFromWindowHandle' does not access instance data and can be marked as static * Fix CA1062 In externally visible method 'List<int> FuzzyMatching.FindBestFuzzyMatch(string text, string searchText)', validate parameter 'searchText' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. Code Description CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. * Supressing warning for CA1814: Prefer jagged arrays over multidimensional however this might be something to consider if needing to optimize the window walker search. * Fix: CA1062 In externally visible method 'List<List<int>> FuzzyMatching.GetAllMatchIndexes(bool[,] matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fix for CA1062 In externally visible method 'int FuzzyMatching.CalculateScoreForMatches(List<int> matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1806 Calls x... but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. Using discard for methods that return void, and checking the hresult before returning parameters. * Fix for CA1820 Test for empty strings using 'string.Length' property or 'string.IsNullOrEmpty' method instead of an Equality check * Supress CA1031 Modify 'get_WindowIcon' to catch a more specific allowed exception type, or rethrow the exception * Code Description CA1062 In externally visible method 'List<Result> Main.Query(Query query)', validate parameter 'query' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes For CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'SearchController.SearchText.set' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.ProcessName.get' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.SwitchToWindow()' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. Replace this call in 'Window.ToString()' with a call to 'string.ToUpper(CultureInfo)'. CA1307 The behavior of 'string.Equals(string?)' could vary based on the current user's locale settings. Replace this call in 'Microsoft.Plugin.WindowWalker.Components.Window.SwitchToWindow()' with a call to 'string.Equals(string?, System.StringComparison)'. * Fix: CA1710 Rename SearchResultUpdateHandler to end in 'EventHandler' * Fix CA1060 Move pinvokes to native methods class * Fix: CS0067 The event 'OpenWindows.OnOpenWindowsUpdateEventHandler' is never used 1) Remove SearchController::OpenWindowsUpdateHandler(object sender, SearchResultUpdateEventArgs e) as it wasn't being called and was redundant with Update Search Text. 2) In Main.cs calling UpdateOpenWindowsList before UpdateSearchText so that the latest enumerated windows will be called. 3) Removing unused OnOpenWindowsUpdateEventHandler and related code. * Revert "Fixes for CA2101 Specify marshaling for P/Invoke string arguments." This reverts commit b3dfe07915dc37618881d348130a7b3c0cd5c59d. * Fixing CA2101 by turning off best fit mapping for methods that require ANSI marshalling. See: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2101?view=vs-2019 * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetWindowText(hwnd, titleBuffer, sizeOfTitle); * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetClassName(Hwnd, windowClassName, windowClassName.MaxCapacity); * Removing unused window code. This was done instead of validating fxcop changes in WindowIcon. * Fixing typos in Window.cs (charachter -> character)
2020-09-10 09:44:22 -07:00
using System.Globalization;
2020-08-17 10:00:56 -07:00
using System.Linq;
namespace Microsoft.Plugin.WindowWalker.Components
{
/// <summary>
/// Responsible for searching and finding matches for the strings provided.
/// Essentially the UI independent model of the application
/// </summary>
internal class SearchController
{
/// <summary>
/// the current search text
/// </summary>
private string searchText;
/// <summary>
/// Open window search results
/// </summary>
2020-08-17 10:00:56 -07:00
private List<SearchResult> searchMatches;
/// <summary>
/// Singleton pattern
/// </summary>
private static SearchController instance;
/// <summary>
/// Gets or sets the current search text
/// </summary>
public string SearchText
{
get
{
return searchText;
}
set
{
// Using CurrentCulture since this is user facing
Adding FxCop to Microsoft.Plugin.WindowWalker (#6260) * Adding FxCop to Microsoft.Plugin.WindowWalker * Delete WindowResult.cs -- Fix for CA1812 WindowResult is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it static (Shared in Visual Basic). * Fix for CA1806 UpdateOpenWindowsList calls EnumWindows but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. * Fix for: CA1066 Type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT should implement IEquatable<T> because it overrides Equals * Fix for: CA1052 Type 'FuzzyMatching' is a static holder type but is neither static nor NotInheritable * Suppress for CA1069 - These values are defined in https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles. CA1069 The enum member 'WS_EX_LTRREADING' has the same constant value '0' as member 'WS_EX_LEFT' CA1069 The enum member 'WS_EX_RIGHTSCROLLBAR' has the same constant value '0' as member 'WS_EX_LEFT' * Supress CA1069 Code Description CA1069 The enum member 'SWP_NOREPOSITION' has the same constant value '512' as member 'SWP_NOOWNERZORDER' CA1069 The enum member 'SWP_FRAMECHANGED' has the same constant value '32' as member 'SWP_DRAWFRAME' * Suprress CA1069 for ShowWindow values. See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow CA1069 The enum member 'ShowMaximized' has the same constant value '3' as member 'Maximize' * Fix code formatting error * Fix for CA2235: Making POINT serializable CA2235 Field MinPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable CA2235 Field MaxPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable * Fix CA2235 Making RECT serializable CA2235 Field NormalPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT which is not serializable * Fixes for CA2101 Specify marshaling for P/Invoke string arguments. * Fixes for CA2007 Consider calling ConfigureAwait on the awaited task * Fixes for the following (CA1822 / CA1801): CA1822 Member 'OnOpenWindowsUpdate' does not access instance data and can be marked as static Code Description CA1801 Parameter value of method add_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. CA1801 Parameter value of method remove_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. * Fix: CA1710 Rename OpenWindowsUpdateHandler to end in 'EventHandler' * Fix CA1822 Member 'GetProcessIDFromWindowHandle' does not access instance data and can be marked as static * Fix CA1062 In externally visible method 'List<int> FuzzyMatching.FindBestFuzzyMatch(string text, string searchText)', validate parameter 'searchText' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. Code Description CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. * Supressing warning for CA1814: Prefer jagged arrays over multidimensional however this might be something to consider if needing to optimize the window walker search. * Fix: CA1062 In externally visible method 'List<List<int>> FuzzyMatching.GetAllMatchIndexes(bool[,] matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fix for CA1062 In externally visible method 'int FuzzyMatching.CalculateScoreForMatches(List<int> matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1806 Calls x... but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. Using discard for methods that return void, and checking the hresult before returning parameters. * Fix for CA1820 Test for empty strings using 'string.Length' property or 'string.IsNullOrEmpty' method instead of an Equality check * Supress CA1031 Modify 'get_WindowIcon' to catch a more specific allowed exception type, or rethrow the exception * Code Description CA1062 In externally visible method 'List<Result> Main.Query(Query query)', validate parameter 'query' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes For CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'SearchController.SearchText.set' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.ProcessName.get' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.SwitchToWindow()' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. Replace this call in 'Window.ToString()' with a call to 'string.ToUpper(CultureInfo)'. CA1307 The behavior of 'string.Equals(string?)' could vary based on the current user's locale settings. Replace this call in 'Microsoft.Plugin.WindowWalker.Components.Window.SwitchToWindow()' with a call to 'string.Equals(string?, System.StringComparison)'. * Fix: CA1710 Rename SearchResultUpdateHandler to end in 'EventHandler' * Fix CA1060 Move pinvokes to native methods class * Fix: CS0067 The event 'OpenWindows.OnOpenWindowsUpdateEventHandler' is never used 1) Remove SearchController::OpenWindowsUpdateHandler(object sender, SearchResultUpdateEventArgs e) as it wasn't being called and was redundant with Update Search Text. 2) In Main.cs calling UpdateOpenWindowsList before UpdateSearchText so that the latest enumerated windows will be called. 3) Removing unused OnOpenWindowsUpdateEventHandler and related code. * Revert "Fixes for CA2101 Specify marshaling for P/Invoke string arguments." This reverts commit b3dfe07915dc37618881d348130a7b3c0cd5c59d. * Fixing CA2101 by turning off best fit mapping for methods that require ANSI marshalling. See: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2101?view=vs-2019 * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetWindowText(hwnd, titleBuffer, sizeOfTitle); * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetClassName(Hwnd, windowClassName, windowClassName.MaxCapacity); * Removing unused window code. This was done instead of validating fxcop changes in WindowIcon. * Fixing typos in Window.cs (charachter -> character)
2020-09-10 09:44:22 -07:00
searchText = value.ToLower(CultureInfo.CurrentCulture).Trim();
2020-08-17 10:00:56 -07:00
}
}
/// <summary>
/// Gets the open window search results
/// </summary>
public List<SearchResult> SearchMatches
{
get { return new List<SearchResult>(searchMatches).OrderByDescending(x => x.Score).ToList(); }
}
/// <summary>
/// Gets singleton Pattern
/// </summary>
public static SearchController Instance
{
get
{
if (instance == null)
{
instance = new SearchController();
}
return instance;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SearchController"/> class.
/// Initializes the search controller object
/// </summary>
private SearchController()
{
searchText = string.Empty;
}
/// <summary>
/// Event handler for when the search text has been updated
/// </summary>
public void UpdateSearchText(string searchText)
2020-08-17 10:00:56 -07:00
{
SearchText = searchText;
SyncOpenWindowsWithModel();
2020-08-17 10:00:56 -07:00
}
/// <summary>
/// Syncs the open windows with the OpenWindows Model
/// </summary>
public void SyncOpenWindowsWithModel()
2020-08-17 10:00:56 -07:00
{
System.Diagnostics.Debug.Print("Syncing WindowSearch result with OpenWindows Model");
List<Window> snapshotOfOpenWindows = OpenWindows.Instance.Windows;
Adding FxCop to Microsoft.Plugin.WindowWalker (#6260) * Adding FxCop to Microsoft.Plugin.WindowWalker * Delete WindowResult.cs -- Fix for CA1812 WindowResult is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it static (Shared in Visual Basic). * Fix for CA1806 UpdateOpenWindowsList calls EnumWindows but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. * Fix for: CA1066 Type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT should implement IEquatable<T> because it overrides Equals * Fix for: CA1052 Type 'FuzzyMatching' is a static holder type but is neither static nor NotInheritable * Suppress for CA1069 - These values are defined in https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles. CA1069 The enum member 'WS_EX_LTRREADING' has the same constant value '0' as member 'WS_EX_LEFT' CA1069 The enum member 'WS_EX_RIGHTSCROLLBAR' has the same constant value '0' as member 'WS_EX_LEFT' * Supress CA1069 Code Description CA1069 The enum member 'SWP_NOREPOSITION' has the same constant value '512' as member 'SWP_NOOWNERZORDER' CA1069 The enum member 'SWP_FRAMECHANGED' has the same constant value '32' as member 'SWP_DRAWFRAME' * Suprress CA1069 for ShowWindow values. See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow CA1069 The enum member 'ShowMaximized' has the same constant value '3' as member 'Maximize' * Fix code formatting error * Fix for CA2235: Making POINT serializable CA2235 Field MinPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable CA2235 Field MaxPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.POINT which is not serializable * Fix CA2235 Making RECT serializable CA2235 Field NormalPosition is a member of type WINDOWPLACEMENT which is serializable but is of type Microsoft.Plugin.WindowWalker.Components.InteropAndHelpers.RECT which is not serializable * Fixes for CA2101 Specify marshaling for P/Invoke string arguments. * Fixes for CA2007 Consider calling ConfigureAwait on the awaited task * Fixes for the following (CA1822 / CA1801): CA1822 Member 'OnOpenWindowsUpdate' does not access instance data and can be marked as static Code Description CA1801 Parameter value of method add_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. CA1801 Parameter value of method remove_OnOpenWindowsUpdate is never used. Remove the parameter or use it in the method body. * Fix: CA1710 Rename OpenWindowsUpdateHandler to end in 'EventHandler' * Fix CA1822 Member 'GetProcessIDFromWindowHandle' does not access instance data and can be marked as static * Fix CA1062 In externally visible method 'List<int> FuzzyMatching.FindBestFuzzyMatch(string text, string searchText)', validate parameter 'searchText' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. Code Description CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'FuzzyMatching.FindBestFuzzyMatch(string, string)' with a call to 'string.ToLower(CultureInfo)'. * Supressing warning for CA1814: Prefer jagged arrays over multidimensional however this might be something to consider if needing to optimize the window walker search. * Fix: CA1062 In externally visible method 'List<List<int>> FuzzyMatching.GetAllMatchIndexes(bool[,] matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fix for CA1062 In externally visible method 'int FuzzyMatching.CalculateScoreForMatches(List<int> matches)', validate parameter 'matches' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes for CA1806 Calls x... but does not use the HRESULT or error code that the method returns. This could lead to unexpected behavior in error conditions or low-resource situations. Use the result in a conditional statement, assign the result to a variable, or pass it as an argument to another method. Using discard for methods that return void, and checking the hresult before returning parameters. * Fix for CA1820 Test for empty strings using 'string.Length' property or 'string.IsNullOrEmpty' method instead of an Equality check * Supress CA1031 Modify 'get_WindowIcon' to catch a more specific allowed exception type, or rethrow the exception * Code Description CA1062 In externally visible method 'List<Result> Main.Query(Query query)', validate parameter 'query' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. * Fixes For CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'SearchController.SearchText.set' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.ProcessName.get' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'Window.SwitchToWindow()' with a call to 'string.ToLower(CultureInfo)'. CA1304 The behavior of 'string.ToUpper()' could vary based on the current user's locale settings. Replace this call in 'Window.ToString()' with a call to 'string.ToUpper(CultureInfo)'. CA1307 The behavior of 'string.Equals(string?)' could vary based on the current user's locale settings. Replace this call in 'Microsoft.Plugin.WindowWalker.Components.Window.SwitchToWindow()' with a call to 'string.Equals(string?, System.StringComparison)'. * Fix: CA1710 Rename SearchResultUpdateHandler to end in 'EventHandler' * Fix CA1060 Move pinvokes to native methods class * Fix: CS0067 The event 'OpenWindows.OnOpenWindowsUpdateEventHandler' is never used 1) Remove SearchController::OpenWindowsUpdateHandler(object sender, SearchResultUpdateEventArgs e) as it wasn't being called and was redundant with Update Search Text. 2) In Main.cs calling UpdateOpenWindowsList before UpdateSearchText so that the latest enumerated windows will be called. 3) Removing unused OnOpenWindowsUpdateEventHandler and related code. * Revert "Fixes for CA2101 Specify marshaling for P/Invoke string arguments." This reverts commit b3dfe07915dc37618881d348130a7b3c0cd5c59d. * Fixing CA2101 by turning off best fit mapping for methods that require ANSI marshalling. See: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2101?view=vs-2019 * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetWindowText(hwnd, titleBuffer, sizeOfTitle); * Previous fix for CA1806 misunderstood int result as hresult. The actual return value is number of characters written. NativeMethods.GetClassName(Hwnd, windowClassName, windowClassName.MaxCapacity); * Removing unused window code. This was done instead of validating fxcop changes in WindowIcon. * Fixing typos in Window.cs (charachter -> character)
2020-09-10 09:44:22 -07:00
if (string.IsNullOrWhiteSpace(SearchText))
2020-08-17 10:00:56 -07:00
{
searchMatches = new List<SearchResult>();
}
else
{
searchMatches = FuzzySearchOpenWindows(snapshotOfOpenWindows);
2020-08-17 10:00:56 -07:00
}
}
/// <summary>
/// Search method that matches the title of windows with the user search text
/// </summary>
/// <param name="openWindows">what windows are open</param>
/// <returns>Returns search results</returns>
private List<SearchResult> FuzzySearchOpenWindows(List<Window> openWindows)
{
List<SearchResult> result = new List<SearchResult>();
List<SearchString> searchStrings = new List<SearchString>();
searchStrings.Add(new SearchString(searchText, SearchResult.SearchType.Fuzzy));
foreach (var searchString in searchStrings)
{
foreach (var window in openWindows)
{
var titleMatch = FuzzyMatching.FindBestFuzzyMatch(window.Title, searchString.SearchText);
var processMatch = FuzzyMatching.FindBestFuzzyMatch(window.ProcessInfo.Name, searchString.SearchText);
2020-08-17 10:00:56 -07:00
if ((titleMatch.Count != 0 || processMatch.Count != 0) &&
window.Title.Length != 0)
{
var temp = new SearchResult(window, titleMatch, processMatch, searchString.SearchType);
result.Add(temp);
}
}
}
System.Diagnostics.Debug.Print("Found " + result.Count + " windows that match the search text");
return result;
}
/// <summary>
/// Event args for a window list update event
/// </summary>
public class SearchResultUpdateEventArgs : EventArgs
{
}
}
}