Migrate settings from settings.toml to PluginAdditionalOptions

This commit is contained in:
Victor (Yu Chieh) Lin 2023-10-04 10:13:24 +08:00
parent 77cbf007e8
commit 2796144b78
11 changed files with 185 additions and 68 deletions

View File

@ -19,13 +19,14 @@ namespace Community.PowerToys.Run.Plugin.Everything
private readonly string[] _appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
private bool _swapCopy;
private int[] _options;
internal void UpdateCopy(bool swapCopy)
private string _options;
internal void Update(Settings s)
{
_swapCopy = swapCopy;
_swapCopy = s.Copy;
_options = s.Context;
}
public ContextMenuLoader(PluginInitContext context, int[] options)
public ContextMenuLoader(PluginInitContext context, string options)
{
_context = context;
_options = options;
@ -37,11 +38,11 @@ namespace Community.PowerToys.Run.Plugin.Everything
if (selectedResult.ContextData is SearchResult record)
{
bool isFile = record.File, runAs = CanFileBeRunAsAdmin(record.Path);
foreach (int o in _options)
foreach (char o in _options)
{
switch (o)
{
case 0:
case '0':
// Open folder
if (isFile)
{
@ -68,7 +69,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
break;
case 1:
case '1':
// Run as Adsmin
if (runAs)
{
@ -97,7 +98,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
break;
case 2:
case '2':
// Run as User
if (runAs)
{
@ -126,7 +127,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
break;
case 3:
case '3':
// Copy File/Folder
contextMenus.Add(new ContextMenuResult
{
@ -155,7 +156,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
});
break;
case 4:
case '4':
// Copy Path
contextMenus.Add(new ContextMenuResult
{
@ -184,7 +185,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
});
break;
case 5:
case '5':
// Open in Shell
contextMenus.Add(new ContextMenuResult
{
@ -218,6 +219,8 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
});
break;
default:
break;
}
}
}

View File

@ -14,8 +14,15 @@ namespace Community.PowerToys.Run.Plugin.Everything
internal Everything(Settings setting)
{
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
Everything_SetSort((Sort)setting.Sort);
UpdateSettings(setting);
}
internal void UpdateSettings(Settings setting)
{
Everything_SetSort(setting.Sort);
Everything_SetMax(setting.Max);
Everything_SetMatchPath(setting.MatchPath);
Everything_SetRegex(setting.RegEx);
}
internal IEnumerable<Result> Query(string query, Settings setting)

View File

@ -4,7 +4,7 @@ using System.Text;
namespace Community.PowerToys.Run.Plugin.Everything.Interop
{
internal sealed class NativeMethods
public sealed class NativeMethods
{
#region FlagsEnums
[Flags]
@ -28,9 +28,10 @@ namespace Community.PowerToys.Run.Plugin.Everything.Interop
HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000,
}
internal enum Sort
public enum Sort
{
NAME_ASCENDING = 1,
NULL = 0, // because combobox starts from 0, but everything starts from 1
NAME_ASCENDING,
NAME_DESCENDING,
PATH_ASCENDING,
PATH_DESCENDING,
@ -107,13 +108,13 @@ namespace Community.PowerToys.Run.Plugin.Everything.Interop
}
#endregion
internal const string dllName = "Everything64.dll";
public const string dllName = "Everything64.dll";
[DllImport(dllName)]
internal static extern uint Everything_GetNumResults();
[DllImport(dllName, CharSet = CharSet.Unicode)]
internal static extern void Everything_GetResultFullPathName(uint nIndex, StringBuilder lpString, uint nMaxCount);
[DllImport(dllName, CharSet = CharSet.Unicode)]
public static extern uint Everything_IncRunCountFromFileName(string lpFileName);
internal static extern uint Everything_IncRunCountFromFileName(string lpFileName);
[DllImport(dllName)]
internal static extern bool Everything_IsFolderResult(uint index);
[DllImport(dllName)]

58
Main.cs
View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using ManagedCommon;
@ -10,17 +11,19 @@ using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Common;
using Wox.Plugin.Logger;
using static Community.PowerToys.Run.Plugin.Everything.Interop.NativeMethods;
namespace Community.PowerToys.Run.Plugin.Everything
{
public class Main : IPlugin, IDisposable, IDelayedExecutionPlugin, IContextMenu, ISettingProvider, IPluginI18n
public class Main : IPlugin, IDisposable, IDelayedExecutionPlugin, IContextMenu, ISettingProvider, IPluginI18n, ISavable
{
private readonly Settings _setting;
private readonly PluginJsonStorage<Settings> _storage;
private IContextMenu _contextMenuLoader;
private PluginInitContext _context;
private bool _disposed;
private Settings _setting;
private Everything _everything;
public string Name => Resources.plugin_name;
@ -29,6 +32,31 @@ namespace Community.PowerToys.Run.Plugin.Everything
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = nameof(Settings.Context),
DisplayLabel = Resources.Context,
DisplayDescription = Resources.Context_Description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = _setting.Context,
},
new PluginAdditionalOption()
{
Key = nameof(Settings.Sort),
DisplayLabel = Resources.Sort,
DisplayDescription = Resources.Sort_Description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
ComboBoxOptions = Enum.GetNames(typeof(Sort)).ToList(),
ComboBoxValue = (int)_setting.Sort,
},
new PluginAdditionalOption()
{
Key = nameof(Settings.Max),
DisplayLabel = Resources.Max,
DisplayDescription = Resources.Max_Description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Numberbox,
NumberValue = _setting.Max,
},
new PluginAdditionalOption()
{
Key = nameof(Settings.Copy),
@ -73,13 +101,20 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
};
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_setting = _storage.Load();
}
public void Init(PluginInitContext context)
{
_setting.Getfilters();
if (_setting.Updates)
Task.Run(() => new Update(Assembly.GetExecutingAssembly().GetName().Version));
_context = context;
_contextMenuLoader = new ContextMenuLoader(context, _setting.Options);
((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_setting.Copy);
_contextMenuLoader = new ContextMenuLoader(context, _setting.Context);
((ContextMenuLoader)_contextMenuLoader).Update(_setting);
_everything = new Everything(_setting);
}
@ -133,7 +168,9 @@ namespace Community.PowerToys.Run.Plugin.Everything
{
if (settings != null && settings.AdditionalOptions != null)
{
_setting ??= new Settings();
_setting.Sort = (Sort)(settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Sort))?.ComboBoxValue ?? 13);
_setting.Max = (uint)(settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Max))?.NumberValue ?? 20);
_setting.Context = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Context))?.TextValue ?? "012345";
_setting.RegEx = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.RegEx))?.Value ?? false;
_setting.Preview = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Preview))?.Value ?? true;
_setting.MatchPath = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.MatchPath))?.Value ?? false;
@ -141,13 +178,18 @@ namespace Community.PowerToys.Run.Plugin.Everything
_setting.QueryText = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.QueryText))?.Value ?? false;
_setting.Updates = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Updates))?.Value ?? true;
if (_contextMenuLoader != null) ((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_setting.Copy);
if (_contextMenuLoader != null) ((ContextMenuLoader)_contextMenuLoader).Update(_setting);
if (_contextMenuLoader != null) _everything.UpdateSettings(_setting);
Everything_SetRegex(_setting.RegEx);
Everything_SetMatchPath(_setting.MatchPath);
Save();
}
}
public void Save()
{
_storage.Save();
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)

View File

@ -69,6 +69,25 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Context Menu.
/// </summary>
public static string Context {
get {
return ResourceManager.GetString("Context", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Configure context menu options and order.
///0_Open folder, 1_Run as Admin, 2_Run as User, 3_Copy, 4_Copy Path, 5_Open in Console.
/// </summary>
public static string Context_Description {
get {
return ResourceManager.GetString("Context_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy .
/// </summary>
@ -150,6 +169,24 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Max.
/// </summary>
public static string Max {
get {
return ResourceManager.GetString("Max", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The maximum amount of results to display in the results..
/// </summary>
public static string Max_Description {
get {
return ResourceManager.GetString("Max_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Open containing folder (Ctrl+Shift+E).
/// </summary>
@ -258,6 +295,24 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Sort.
/// </summary>
public static string Sort {
get {
return ResourceManager.GetString("Sort", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Set result sorting method..
/// </summary>
public static string Sort_Description {
get {
return ResourceManager.GetString("Sort_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy.
/// </summary>

View File

@ -120,6 +120,13 @@
<data name="clipboard_failed" xml:space="preserve">
<value>Fail to set text in clipboard</value>
</data>
<data name="Context" xml:space="preserve">
<value>Context Menu</value>
</data>
<data name="Context_Description" xml:space="preserve">
<value>Configure context menu options and order.
0_Open folder, 1_Run as Admin, 2_Run as User, 3_Copy, 4_Copy Path, 5_Open in Console</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>Copy </value>
</data>
@ -147,6 +154,12 @@
<data name="Match_path_Description" xml:space="preserve">
<value>Search path in additional to file/folder name.</value>
</data>
<data name="Max" xml:space="preserve">
<value>Max</value>
</data>
<data name="Max_Description" xml:space="preserve">
<value>The maximum amount of results to display in the results.</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
<value>Open containing folder (Ctrl+Shift+E)</value>
</data>
@ -183,6 +196,12 @@
<data name="run_as_user" xml:space="preserve">
<value>Run as different user (Ctrl+Shift+U)</value>
</data>
<data name="Sort" xml:space="preserve">
<value>Sort</value>
</data>
<data name="Sort_Description" xml:space="preserve">
<value>Set result sorting method.</value>
</data>
<data name="SwapCopy" xml:space="preserve">
<value>Copy</value>
</data>

View File

@ -120,6 +120,10 @@
<data name="clipboard_failed" xml:space="preserve">
<value>无法设置剪贴板中的文本</value>
</data>
<data name="Context_Description" xml:space="preserve">
<value>配置选单选项与顺序
0_打开所在文件夹、1_以管理员身分运行、2_以不同的使用者身分运行、3_复制、4_复制路径、5_在控制台中开启</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>复制</value>
</data>
@ -138,6 +142,9 @@
<data name="Match_path_Description" xml:space="preserve">
<value>匹配路径</value>
</data>
<data name="Max_Description" xml:space="preserve">
<value>结果中显示的最大结果数</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
<value>打开所在文件夹 (Ctrl+Shift+E)</value>
</data>
@ -165,6 +172,9 @@
<data name="run_as_user" xml:space="preserve">
<value>以不同的使用者身份运行(Ctrl+Shift+U)</value>
</data>
<data name="Sort_Description" xml:space="preserve">
<value>设定结果排序方法</value>
</data>
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 复制档案Ctrl+Alt+C 复制路径</value>
</data>

View File

@ -120,6 +120,10 @@
<data name="clipboard_failed" xml:space="preserve">
<value>複製到剪貼簿失敗</value>
</data>
<data name="Context_Description" xml:space="preserve">
<value>配置選單選項與順序
0_開啟資料夾、1_以管理員身分執行、2_以其他使用者身分執行、3_複製、4_複製路徑、5_在命令提示字元中開啟</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>複製</value>
</data>
@ -138,6 +142,9 @@
<data name="Match_path_Description" xml:space="preserve">
<value>也收尋路徑</value>
</data>
<data name="Max_Description" xml:space="preserve">
<value>結果中顯示的最大結果數</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
<value>開啟檔案位置 (Ctrl+Shift+E)</value>
</data>
@ -163,7 +170,10 @@
<value>以系統管理員身分執行 (Ctrl+Shift+Enter)</value>
</data>
<data name="run_as_user" xml:space="preserve">
<value>以不同的使用者身份執行 (Ctrl+Shift+U)</value>
<value>以其他使用者身份執行 (Ctrl+Shift+U)</value>
</data>
<data name="Sort_Description" xml:space="preserve">
<value>設定結果排序方法</value>
</data>
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 複製檔案Ctrl+Alt+C 複製路徑</value>

View File

@ -2,12 +2,16 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace Community.PowerToys.Run.Plugin.Everything
{
internal sealed class Settings
public class Settings
{
// Settings from PTR settings
public Interop.NativeMethods.Sort Sort { get; set; } = Interop.NativeMethods.Sort.DATE_MODIFIED_DESCENDING;
public uint Max { get; set; } = 20;
public string Context { get; set; } = "012345";
internal bool Copy { get; set; }
internal bool MatchPath { get; set; }
internal bool Preview { get; set; }
@ -15,12 +19,9 @@ namespace Community.PowerToys.Run.Plugin.Everything
internal bool RegEx { get; set; }
internal bool Updates { get; set; } = true;
// Settings from settings.toml
internal uint Max { get; } = 20;
internal int Sort { get; } = 14;
internal int[] Options { get; } = new int[] { 0, 1, 2, 3, 4, 5 };
// Get Filters from settings.toml
internal Dictionary<string, string> Filters { get; } = new Dictionary<string, string>();
internal Settings()
internal void Getfilters()
{
string[] strArr;
try { strArr = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.toml")); }
@ -31,24 +32,9 @@ namespace Community.PowerToys.Run.Plugin.Everything
if (str.Length == 0 || str[0] == '#') continue;
string[] kv = str.Split('=', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (kv.Length != 2) continue;
switch (kv[0])
{
case "max":
try { Max = uint.Parse(kv[1], culture.NumberFormat); }
catch { }
break;
case "sort":
try { Sort = int.Parse(kv[1], culture.NumberFormat); }
catch { }
break;
case "options":
Options = Array.ConvertAll(kv[1].Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), int.Parse);
break;
default:
if (kv[0].Contains(':'))
Filters.TryAdd(kv[0].Split(':')[0].ToLowerInvariant(), kv[1]);
break;
}
if (kv[0].Contains(':'))
Filters.TryAdd(kv[0].Split(':')[0].ToLowerInvariant(), kv[1]);
}
}
}

View File

@ -1,10 +1,10 @@
{
"ID": "A86867E2D932459CBD77D176373DD657",
"ActionKeyword": "@",
"ActionKeyword": "`",
"IsGlobal": true,
"Name": "Everything",
"Author": "Yu Chieh (Victor) Lin",
"Version": "0.73.0",
"Version": "0.74.0",
"Language": "csharp",
"Website": "https://github.com/Lin-ycv/EverythingPowerToys",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.Everything.dll",

View File

@ -1,19 +1,3 @@
# This is the settings file to override the behaviour for "Everything for PowerToys"
# to override a setting, uncomment that line and change the value
# ie: "# max = 20" -> "max = 10"
# Restart of powertoys is needed if values are changed
# Set max amount of results to return [default: 20]
# max = 20
# Set result sorting method [default: 14]
# https://www.voidtools.com/support/everything/sdk/everything_getsort/
# sort = 14
# Configure context menu options and order[default: 0;1;2;3;4;5]
# 0:Open folder; 1:Run as Admin; 2:Run as User; 3:Copy; 4:Copy Path; 5:Open in Console
# options = 0;1;3;4;5
# Search filters, filters result with extension type, seperated by ;
Audio: = aac;ac3;aif;aifc;aiff;amr;ape;au;cda;dts;fla;flac;it;m1a;m2a;m3u;m4a;m4b;m4p;mid;midi;mka;mod;mp2;mp3;mpa;ogg;opus;ra;rmi;spc;rmi;snd;umx;voc;wav;weba;wma;xm
Zip: = 7z;ace;arj;bz2;cab;gz;gzip;jar;r00;r01;r02;r03;r04;r05;r06;r07;r08;r09;r10;r11;r12;r13;r14;r15;r16;r17;r18;r19;r20;r21;r22;r23;r24;r25;r26;r27;r28;r29;rar;tar;tgz;z;zip