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;
|
2021-12-06 20:27:10 +01:00
|
|
|
|
using System.IO;
|
2022-02-10 23:44:19 +02:00
|
|
|
|
using Wox.Plugin.Common.Win32;
|
2020-08-17 10:00:56 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Plugin.WindowWalker.Components
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Class that represents the state of the desktops windows
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class OpenWindows
|
|
|
|
|
|
{
|
2021-12-06 20:27:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// PowerLauncher main executable
|
|
|
|
|
|
/// </summary>
|
2022-03-08 17:47:10 +01:00
|
|
|
|
private static readonly string _powerLauncherExe = Path.GetFileName(Environment.ProcessPath);
|
2021-12-06 20:27:10 +01:00
|
|
|
|
|
2020-08-17 10:00:56 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// List of all the open windows
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly List<Window> windows = new List<Window>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An instance of the class OpenWindows
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static OpenWindows instance;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the list of all open windows
|
|
|
|
|
|
/// </summary>
|
2022-03-07 12:45:29 +01:00
|
|
|
|
internal List<Window> Windows
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
get { return new List<Window>(windows); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets an instance property of this class that makes sure that
|
|
|
|
|
|
/// the first instance gets created and that all the requests
|
|
|
|
|
|
/// end up at that one instance
|
|
|
|
|
|
/// </summary>
|
2022-03-07 12:45:29 +01:00
|
|
|
|
internal static OpenWindows Instance
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (instance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
instance = new OpenWindows();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="OpenWindows"/> class.
|
|
|
|
|
|
/// Private constructor to make sure there is never
|
|
|
|
|
|
/// more than one instance of this class
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private OpenWindows()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Updates the list of open windows
|
|
|
|
|
|
/// </summary>
|
2022-03-07 12:45:29 +01:00
|
|
|
|
internal void UpdateOpenWindowsList()
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
windows.Clear();
|
2022-02-10 23:44:19 +02:00
|
|
|
|
EnumWindowsProc callbackptr = new EnumWindowsProc(WindowEnumerationCallBack);
|
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
|
|
|
|
_ = NativeMethods.EnumWindows(callbackptr, 0);
|
2020-08-17 10:00:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Call back method for window enumeration
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hwnd">The handle to the current window being enumerated</param>
|
|
|
|
|
|
/// <param name="lParam">Value being passed from the caller (we don't use this but might come in handy
|
|
|
|
|
|
/// in the future</param>
|
|
|
|
|
|
/// <returns>true to make sure to continue enumeration</returns>
|
2022-03-07 12:45:29 +01:00
|
|
|
|
internal bool WindowEnumerationCallBack(IntPtr hwnd, IntPtr lParam)
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
Window newWindow = new Window(hwnd);
|
|
|
|
|
|
|
2021-07-28 14:15:47 +03:00
|
|
|
|
if (newWindow.IsWindow && newWindow.Visible && newWindow.IsOwner &&
|
2020-08-17 10:00:56 -07:00
|
|
|
|
(!newWindow.IsToolWindow || newWindow.IsAppWindow) && !newWindow.TaskListDeleted &&
|
2022-03-14 16:40:27 +01:00
|
|
|
|
(newWindow.Desktop.IsVisible || !WindowWalkerSettings.Instance.ResultsFromVisibleDesktopOnly || Main.VirtualDesktopHelperInstance.GetDesktopCount() < 2) &&
|
2022-03-07 12:45:29 +01:00
|
|
|
|
newWindow.ClassName != "Windows.UI.Core.CoreWindow" && newWindow.Process.Name != _powerLauncherExe)
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
2022-03-07 12:45:29 +01:00
|
|
|
|
// To hide (not add) preloaded uwp app windows that are invisible to the user and other cloaked windows, we check the cloak state. (Issue #13637.)
|
|
|
|
|
|
// (If user asking to see cloaked uwp app windows again we can add an optional plugin setting in the future.)
|
|
|
|
|
|
if (!newWindow.IsCloaked || newWindow.GetWindowCloakState() == Window.WindowCloakState.OtherDesktop)
|
|
|
|
|
|
{
|
|
|
|
|
|
windows.Add(newWindow);
|
|
|
|
|
|
}
|
2020-08-17 10:00:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|