EverythingPowerToys/Everything.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2023-01-07 02:20:22 +08:00
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Wox.Plugin;
using static Community.PowerToys.Run.Plugin.Everything.Interop.NativeMethods;
2022-12-11 02:20:09 +08:00
2023-01-07 02:20:22 +08:00
namespace Community.PowerToys.Run.Plugin.Everything
{
2023-03-30 00:22:49 +08:00
internal sealed class Everything
2022-12-11 02:20:09 +08:00
{
internal Everything(Settings setting)
{
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
UpdateSettings(setting);
}
internal void UpdateSettings(Settings setting)
{
Everything_SetSort(setting.Sort);
2022-12-11 02:20:09 +08:00
Everything_SetMax(setting.Max);
Everything_SetMatchPath(setting.MatchPath);
Everything_SetRegex(setting.RegEx);
2022-12-11 02:20:09 +08:00
}
internal IEnumerable<Result> Query(string query, Settings setting)
{
string orgqry = query;
if (orgqry.Contains('\"') && !setting.MatchPath)
{
Everything_SetMatchPath(true);
}
if (orgqry.Contains(':'))
{
string[] nqry = query.Split(':');
2023-03-30 00:22:49 +08:00
if (setting.Filters.TryGetValue(nqry[0].ToLowerInvariant(), out string value))
query = nqry[1].Trim() + " ext:" + value;
2022-12-11 02:20:09 +08:00
}
_ = Everything_SetSearchW(query);
if (!Everything_QueryW(true))
{
throw new Win32Exception("Unable to Query");
}
if (orgqry.Contains('\"') && !setting.MatchPath)
{
Everything_SetMatchPath(false);
}
uint resultCount = Everything_GetNumResults();
for (uint i = 0; i < resultCount; i++)
{
StringBuilder buffer = new StringBuilder(260);
Everything_GetResultFullPathName(i, buffer, 260);
string fullPath = buffer.ToString();
string name = Path.GetFileName(fullPath);
bool isFolder = Everything_IsFolderResult(i);
string path = isFolder ? fullPath : Path.GetDirectoryName(fullPath);
string ext = Path.GetExtension(fullPath.Replace(".lnk", string.Empty));
var r = new Result()
{
Title = name,
ToolTipData = new ToolTipData(name, fullPath),
2022-12-11 02:20:09 +08:00
SubTitle = Resources.plugin_name + ": " + fullPath,
IcoPath = isFolder ? "Images/folder.png" : (setting.Preview ?
fullPath : (SearchHelper.IconLoader.Icon(ext) ?? "Images/file.png")),
ContextData = new SearchResult()
{
Path = fullPath,
Title = name,
File = !isFolder,
},
Action = e =>
{
using var process = new Process();
process.StartInfo.FileName = fullPath;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
_ = Everything_IncRunCountFromFileName(fullPath);
2022-12-11 02:20:09 +08:00
return true;
}
catch (Win32Exception)
{
return false;
}
},
QueryTextDisplay = setting.QueryText ? (isFolder ? path : name) : orgqry,
};
yield return r;
}
}
}
}