Reduced extra operations and checks

This commit is contained in:
Victor Lin 2022-01-23 16:38:20 +08:00
parent c47a8a5b0a
commit 0c05b5f363
8 changed files with 79 additions and 191 deletions

View File

@ -7,12 +7,17 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Community.PowerToys.Run.Plugin.Everything</RootNamespace>
<AssemblyName>Community.PowerToys.Run.Plugin.Everything</AssemblyName>
<Version>$(Version).0</Version>
<Version>0.0.2.1</Version>
<useWPF>true</useWPF>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Platforms>x64</Platforms>
<NeutralLanguage>en-US</NeutralLanguage>
<Authors>Yu Chieh (Victor) Lin</Authors>
<Company />
<Product />
<AssemblyVersion>0.0.2.1</AssemblyVersion>
<FileVersion>0.0.2.1</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -98,6 +103,9 @@
<None Update="Everything64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Everything64nolock.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Images\Everything.dark.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

Binary file not shown.

BIN
Everything64_Official.dll Normal file

Binary file not shown.

38
Main.cs
View File

@ -25,8 +25,10 @@ namespace Community.PowerToys.Run.Plugin.Everything
public class Main : IPlugin, IDisposable, IDelayedExecutionPlugin, IContextMenu, ISettingProvider
{
private const string Wait = nameof(Wait);
private const string Top = nameof(Top);
private readonly string reservedStringPattern = @"^[\/\\\$\%]+$|^.*[<>].*$";
private bool _wait;
private bool _top;
public string Name => Resources.plugin_name;
@ -34,6 +36,12 @@ namespace Community.PowerToys.Run.Plugin.Everything
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = Top,
DisplayLabel = Resources.Top,
Value = true,
},
new PluginAdditionalOption()
{
Key = Wait,
@ -42,12 +50,18 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
};
private string IconPath { get; set; }
private IContextMenu _contextMenuLoader;
private PluginInitContext _context;
private bool disposed;
private string _warningIconPath;
private static string _warningIconPath;
internal static string WarningIcon
{
get
{
return _warningIconPath;
}
}
public void Init(PluginInitContext context)
{
@ -55,6 +69,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
_contextMenuLoader = new ContextMenuLoader(context);
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(_context.API.GetCurrentTheme());
EverythingSetup();
}
public List<Result> Query(Query query)
@ -78,7 +93,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
{
try
{
results.AddRange(EverythingSearch(searchQuery, _wait));
results.AddRange(EverythingSearch(searchQuery, _wait, _top));
}
catch (OperationCanceledException)
{
@ -87,21 +102,23 @@ namespace Community.PowerToys.Run.Plugin.Everything
Title = Resources.timeout,
SubTitle = Resources.enable_wait,
IcoPath = _warningIconPath,
Score = int.MaxValue,
});
}
catch (EntryPointNotFoundException)
catch (System.ComponentModel.Win32Exception)
{
results.Add(new Result()
{
Title = Resources.Everything_not_running,
SubTitle = Resources.Everything_ini,
IcoPath = _warningIconPath,
QueryTextDisplay = Resources.Everything_url,
QueryTextDisplay = '.' + Resources.plugin_name,
Score = int.MaxValue,
});
}
catch (Exception e)
{
Log.Exception("Something failed", e, GetType());
Log.Exception("Everything Exception", e, GetType());
}
}
}
@ -114,16 +131,14 @@ namespace Community.PowerToys.Run.Plugin.Everything
UpdateIconPath(newTheme);
}
private void UpdateIconPath(Theme theme)
private static void UpdateIconPath(Theme theme)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
IconPath = "Images/Everything.light.png";
_warningIconPath = "Images/Warning.light.png";
}
else
{
IconPath = "Images/Everything.dark.png";
_warningIconPath = "Images/Warning.dark.png";
}
}
@ -141,11 +156,14 @@ namespace Community.PowerToys.Run.Plugin.Everything
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var wait = false;
var top = true;
if (settings != null && settings.AdditionalOptions != null)
{
wait = settings.AdditionalOptions.FirstOrDefault(x => x.Key == Wait)?.Value ?? false;
top = settings.AdditionalOptions.FirstOrDefault(x => x.Key == Top)?.Value ?? true;
}
_top = top;
_wait = wait;
}

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@ -12,24 +13,13 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Wox.Plugin;
namespace Community.PowerToys.Run.Plugin.Everything
{
internal static class NativeMethods
{
internal enum ErrorCode
{
OK,
ERROR_MEMORY,
ERROR_IPC,
ERROR_REGISTERCLASSEX,
ERROR_CREATEWINDOW,
ERROR_CREATETHREAD,
ERROR_INVALIDINDEX,
ERROR_INVALIDCALL,
}
internal enum Request
{
FILE_NAME = 0x00000001,
@ -80,167 +70,35 @@ namespace Community.PowerToys.Run.Plugin.Everything
DATE_RUN_DESCENDING,
}
internal const int TARGET_MACHINE_X86 = 1;
internal const int TARGET_MACHINE_X64 = 2;
internal const int TARGET_MACHINE_ARM = 3;
private const string dllName = "Everything64.dll";
internal const string dllName = "Everything64.dll"; // Included dll is a modified file without locking, if this creates issues, replace with official dll
#pragma warning disable SA1516 // Elements should be separated by blank line
[DllImport(dllName)]
public static extern void Everything_CleanUp();
[DllImport(dllName)]
public static extern uint Everything_DeleteRunHistory();
[DllImport(dllName)]
public static extern uint Everything_Exit();
[DllImport(dllName)]
public static extern uint Everything_GetBuildNumber();
[DllImport(dllName)]
public static extern uint Everything_GetLastError();
[DllImport(dllName)]
public static extern uint Everything_GetMajorVersion();
[DllImport(dllName)]
public static extern bool Everything_GetMatchCase();
[DllImport(dllName)]
public static extern bool Everything_GetMatchPath();
[DllImport(dllName)]
public static extern bool Everything_GetMatchWholeWord();
[DllImport(dllName)]
public static extern uint Everything_GetMax();
[DllImport(dllName)]
public static extern uint Everything_GetMinorVersion();
[DllImport(dllName)]
public static extern uint Everything_GetNumFileResults();
[DllImport(dllName)]
public static extern uint Everything_GetNumFolderResults();
[DllImport(dllName)]
public static extern uint Everything_GetNumResults();
[DllImport(dllName)]
public static extern uint Everything_GetOffset();
[DllImport(dllName)]
public static extern bool Everything_GetRegex();
[DllImport(dllName)]
public static extern uint Everything_GetReplyID();
[DllImport(dllName)]
public static extern uint Everything_GetRequestFlags();
[DllImport(dllName)]
public static extern uint Everything_GetResultAttributes(uint nIndex);
[DllImport(dllName)]
public static extern bool Everything_GetResultDateAccessed(uint nIndex, out long lpFileTime);
[DllImport(dllName)]
public static extern bool Everything_GetResultDateCreated(uint nIndex, out long lpFileTime);
[DllImport(dllName)]
public static extern bool Everything_GetResultDateModified(uint nIndex, out long lpFileTime);
[DllImport(dllName)]
public static extern bool Everything_GetResultDateRecentlyChanged(uint nIndex, out long lpFileTime);
[DllImport(dllName)]
public static extern bool Everything_GetResultDateRun(uint nIndex, out long lpFileTime);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultExtension(uint nIndex);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultFileListFileName(uint nIndex);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultFileName(uint nIndex);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern void Everything_GetResultFullPathName(uint nIndex, StringBuilder lpString, uint nMaxCount);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultHighlightedFileName(uint nIndex);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultHighlightedFullPathAndFileName(uint nIndex);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern IntPtr Everything_GetResultHighlightedPath(uint nIndex);
[DllImport(dllName)]
public static extern uint Everything_GetResultListRequestFlags();
[DllImport(dllName)]
public static extern uint Everything_GetResultListSort();
[DllImport(dllName)]
public static extern IntPtr Everything_GetResultPath(uint nIndex);
[DllImport(dllName)]
public static extern uint Everything_GetResultRunCount(uint nIndex);
[DllImport(dllName)]
public static extern bool Everything_GetResultSize(uint nIndex, out long lpFileSize);
[DllImport(dllName)]
public static extern uint Everything_GetRevision();
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern uint Everything_GetRunCountFromFileName(string lpFileName);
[DllImport(dllName)]
public static extern IntPtr Everything_GetSearchW();
[DllImport(dllName)]
public static extern uint Everything_GetSort();
[DllImport(dllName)]
public static extern uint Everything_GetTotFileResults();
[DllImport(dllName)]
public static extern uint Everything_GetTotFolderResults();
[DllImport(dllName)]
public static extern uint Everything_GetTotResults();
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern uint Everything_IncRunCountFromFileName(string lpFileName);
[DllImport(dllName)]
public static extern bool Everything_IsAdmin();
[DllImport(dllName)]
public static extern bool Everything_IsAppData();
[DllImport(dllName)]
public static extern bool Everything_IsDBLoaded();
[DllImport(dllName)]
public static extern bool Everything_IsFastSort(uint sortType);
[DllImport(dllName)]
public static extern bool Everything_IsFileInfoIndexed(uint fileInfoType);
[DllImport(dllName)]
public static extern bool Everything_IsFileResult(uint nIndex);
[DllImport(dllName)]
public static extern bool Everything_IsFolderResult(uint nIndex);
[DllImport(dllName)]
public static extern bool Everything_IsQueryReply(uint message, uint wParam, long lParam, uint nId);
[DllImport(dllName)]
public static extern bool Everything_IsVolumeResult(uint nIndex);
[DllImport(dllName)]
public static extern bool Everything_QueryW(bool bWait);
[DllImport(dllName)]
public static extern bool Everything_RebuildDB();
[DllImport(dllName)]
public static extern void Everything_Reset();
[DllImport(dllName)]
public static extern bool Everything_SaveDB();
[DllImport(dllName)]
public static extern bool Everything_SaveRunHistory();
[DllImport(dllName)]
public static extern void Everything_SetMatchCase(bool bEnable);
[DllImport(dllName)]
public static extern void Everything_SetMatchPath(bool bEnable);
[DllImport(dllName)]
public static extern void Everything_SetMatchWholeWord(bool bEnable);
[DllImport(dllName)]
public static extern void Everything_SetMax(uint dwMax);
[DllImport(dllName)]
public static extern void Everything_SetOffset(uint dwOffset);
[DllImport(dllName)]
public static extern void Everything_SetRegex(bool bEnable);
[DllImport(dllName)]
public static extern void Everything_SetReplyID(uint nId);
[DllImport(dllName)]
public static extern void Everything_SetRequestFlags(Request RequestFlags);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern bool Everything_SetRunCountFromFileName(string lpFileName, uint dwRunCount);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern uint Everything_SetSearchW(string lpSearchString);
[DllImport(dllName)]
public static extern void Everything_SetSort(Sort SortType);
[DllImport(dllName)]
public static extern void Everything_SortResultsByPath();
[DllImport(dllName)]
public static extern bool Everything_UpdateAllFolderIndexes();
private const int max = 20;
private static CancellationTokenSource source;
#pragma warning disable SA1503 // Braces should not be omitted
public static IEnumerable<Result> EverythingSearch(string qry, bool wait)
public static void EverythingSetup()
{
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
Everything_SetSort(Sort.DATE_MODIFIED_DESCENDING);
Everything_SetMax(max);
}
public static IEnumerable<Result> EverythingSearch(string qry, bool wait, bool top)
{
source?.Cancel();
source = new CancellationTokenSource();
@ -249,23 +107,16 @@ namespace Community.PowerToys.Run.Plugin.Everything
_ = Everything_SetSearchW(qry);
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
Everything_SetSort(Sort.DATE_MODIFIED_DESCENDING);
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
Everything_SetMax(max);
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
if (!Everything_QueryW(true))
{
throw new EntryPointNotFoundException();
throw new Win32Exception("Unable to Query");
}
uint resultCount = Everything_GetNumResults();
if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
for (uint i = 0; i < resultCount; i++)
{
if (token.IsCancellationRequested) break;
StringBuilder sb = new StringBuilder(260);
Everything_GetResultFullPathName(i, sb, 260);
string fullPath = sb.ToString();
@ -277,13 +128,12 @@ namespace Community.PowerToys.Run.Plugin.Everything
else
path = Path.GetDirectoryName(fullPath);
yield return new Result()
var r = new Result()
{
Title = name,
ToolTipData = new ToolTipData("Name : " + name, "Path : " + path),
SubTitle = Properties.Resources.plugin_name + ": " + fullPath,
SubTitle = Resources.plugin_name + ": " + fullPath,
IcoPath = fullPath,
Score = (int)(max - i),
ContextData = new SearchResult()
{
Path = fullPath,
@ -302,7 +152,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
process.Start();
return true;
}
catch (System.ComponentModel.Win32Exception)
catch (Win32Exception)
{
return false;
}
@ -310,7 +160,19 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
QueryTextDisplay = isFolder ? path : name,
};
if (token.IsCancellationRequested) yield break/*token.ThrowIfCancellationRequested()*/;
if (top) r.Score = (int)(max - i);
yield return r;
}
if (token.IsCancellationRequested)
{
yield return new Result()
{
Title = Resources.timeout,
SubTitle = Resources.enable_wait,
IcoPath = Main.WarningIcon,
Score = int.MaxValue,
};
}
}
#pragma warning restore SA1503 // Braces should not be omitted

View File

@ -105,15 +105,6 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to https://www.voidtools.com/downloads/.
/// </summary>
public static string Everything_url {
get {
return ResourceManager.GetString("Everything_url", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Fail to open folder at.
/// </summary>
@ -177,6 +168,15 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Insert result at the top of the list..
/// </summary>
public static string Top {
get {
return ResourceManager.GetString("Top", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Wait - Wait longer for the query to finish..
/// </summary>

View File

@ -132,9 +132,6 @@
<data name="Everything_not_running" xml:space="preserve">
<value>Everything is not running</value>
</data>
<data name="Everything_url" xml:space="preserve">
<value>https://www.voidtools.com/downloads/</value>
</data>
<data name="folder_open_failed" xml:space="preserve">
<value>Fail to open folder at</value>
</data>
@ -156,6 +153,9 @@
<data name="timeout" xml:space="preserve">
<value>Timed out before finishing the query</value>
</data>
<data name="Top" xml:space="preserve">
<value>Insert result at the top of the list.</value>
</data>
<data name="Wait" xml:space="preserve">
<value>Wait - Wait longer for the query to finish.</value>
</data>

View File

@ -4,7 +4,7 @@
"IsGlobal": true,
"Name": "Everything",
"Author": "Yu Chieh (Victor) Lin",
"Version": "0.2.0",
"Version": "0.2.1",
"Language": "csharp",
"Website": "https://aka.ms/powertoys",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.Everything.dll",