EverythingPowerToys/Everything.cs

123 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-01-07 02:20:22 +08:00
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 (setting.EnvVar && orgqry.Contains('%'))
{
query = Environment.ExpandEnvironmentVariables(query).Replace(';', '|');
}
2022-12-11 02:20:09 +08:00
if (orgqry.Contains(':'))
{
StringBuilder sb = new();
foreach (var kv in setting.Filters)
{
if (query.Contains(kv.Key, StringComparison.OrdinalIgnoreCase))
{
2024-01-24 18:06:06 +08:00
sb.Append(kv.Value + ' ');
query = query.Replace(kv.Key, string.Empty);
}
}
if (sb.Length > 0)
{
2024-01-24 18:06:06 +08:00
query = query.Trim() + " " + sb.ToString();
}
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(260);
2022-12-11 02:20:09 +08:00
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;
}
}
}
}