Adjustments for customization

removed debug option, replaced with update checking option
allow for configuration of context menu options
This commit is contained in:
Lin Yu-Chieh (Victor) 2022-12-11 16:23:48 +08:00
parent e1324baa16
commit ef6154bce0
9 changed files with 218 additions and 210 deletions

View File

@ -18,14 +18,16 @@ namespace Community.PowerToys.Run.Plugin.Everything
private readonly string[] _appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
private bool _swapCopy;
private string[] _options;
internal void UpdateCopy(bool swapCopy)
{
_swapCopy = swapCopy;
}
public ContextMenuLoader(PluginInitContext context)
public ContextMenuLoader(PluginInitContext context, string[] options)
{
_context = context;
_options = options;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
@ -33,164 +35,195 @@ namespace Community.PowerToys.Run.Plugin.Everything
var contextMenus = new List<ContextMenuResult>();
if (selectedResult.ContextData is SearchResult record)
{
bool isFile = record.File;
if (isFile)
bool isFile = record.File, runAs = CanFileBeRunAsAdmin(record.Path);
foreach (string o in _options)
{
contextMenus.Add(CreateOpenContainingFolderResult(record));
}
// Test to check if File can be Run as admin, if yes, we add a 'run as admin' context menu item
if (CanFileBeRunAsAdmin(record.Path))
{
contextMenus.Add(CreateRunAsAdminContextMenu(record));
contextMenus.Add(CreateRunAsUserContextMenu(record));
}
// https://learn.microsoft.com/en-us/windows/apps/design/style/segoe-ui-symbol-font
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.copy_file + (_swapCopy ? Properties.Resources.copy_shortcut : Properties.Resources.copy_shortcutAlt),
Glyph = "\xE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control : ModifierKeys.Control | ModifierKeys.Alt,
Action = (context) =>
switch (o)
{
try
{
Clipboard.SetData(DataFormats.FileDrop, new string[] { record.Path });
return true;
}
catch (Exception e)
{
var message = Properties.Resources.clipboard_failed;
Log.Exception(message, e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.copy_path + (_swapCopy ? Properties.Resources.copy_shortcutAlt : Properties.Resources.copy_shortcut),
Glyph = "\xE71B",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control | ModifierKeys.Alt : ModifierKeys.Control,
Action = (context) =>
{
try
{
Clipboard.SetText(record.Path);
return true;
}
catch (Exception e)
{
var message = Properties.Resources.clipboard_failed;
Log.Exception(message, e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.open_in_console,
Glyph = "\xE756",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = (context) =>
{
try
{
case "0":
// Open folder
if (isFile)
{
Helper.OpenInConsole(Path.GetDirectoryName(record.Path));
}
else
{
Helper.OpenInConsole(record.Path);
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.open_containing_folder,
Glyph = "\xE838",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.E,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
if (!Helper.OpenInShell("explorer.exe", $"/select,\"{record.Path}\""))
{
var message = $"{Properties.Resources.folder_open_failed} {Path.GetDirectoryName(record.Path)}";
_context.API.ShowMsg(message);
return false;
}
return true;
},
});
}
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, GetType());
return false;
}
},
});
break;
case "1":
// Run as Adsmin
if (runAs)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.run_as_admin,
Glyph = "\xE7EF",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsAdmin(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as admin, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
},
});
}
break;
case "2":
// Run as User
if (runAs)
{
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.run_as_user,
Glyph = "\xE7EE",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsUser(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as different user, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
},
});
}
break;
case "3":
// Copy File/Folder
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.copy_file + (_swapCopy ? Properties.Resources.copy_shortcut : Properties.Resources.copy_shortcutAlt),
Glyph = "\xE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control : ModifierKeys.Control | ModifierKeys.Alt,
Action = (context) =>
{
try
{
Clipboard.SetData(DataFormats.FileDrop, new string[] { record.Path });
return true;
}
catch (Exception e)
{
var message = Properties.Resources.clipboard_failed;
Log.Exception(message, e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
break;
case "4":
// Copy Path
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.copy_path + (_swapCopy ? Properties.Resources.copy_shortcutAlt : Properties.Resources.copy_shortcut),
Glyph = "\xE71B",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control | ModifierKeys.Alt : ModifierKeys.Control,
Action = (context) =>
{
try
{
Clipboard.SetText(record.Path);
return true;
}
catch (Exception e)
{
var message = Properties.Resources.clipboard_failed;
Log.Exception(message, e, GetType());
_context.API.ShowMsg(message);
return false;
}
},
});
break;
case "5":
// Open in Shell
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.open_in_console,
Glyph = "\xE756",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = (context) =>
{
try
{
if (isFile)
{
Helper.OpenInConsole(Path.GetDirectoryName(record.Path));
}
else
{
Helper.OpenInConsole(record.Path);
}
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, GetType());
return false;
}
},
});
break;
}
}
}
return contextMenus;
}
// Function to add the context menu item to run as admin
private static ContextMenuResult CreateRunAsAdminContextMenu(SearchResult record)
{
return new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.run_as_admin,
Glyph = "\xE7EF",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsAdmin(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as admin, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
},
};
}
// Function to add the context menu item to run as admin
private static ContextMenuResult CreateRunAsUserContextMenu(SearchResult record)
{
return new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.run_as_user,
Glyph = "\xE7EE",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsUser(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as different user, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
},
};
}
// Function to test if the file can be run as admin
private bool CanFileBeRunAsAdmin(string path)
{
string fileExtension = Path.GetExtension(path);
@ -205,29 +238,5 @@ namespace Community.PowerToys.Run.Plugin.Everything
return false;
}
private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record)
{
return new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.open_containing_folder,
Glyph = "\xE838",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.E,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
if (!Helper.OpenInShell("explorer.exe", $"/select,\"{record.Path}\""))
{
var message = $"{Properties.Resources.folder_open_failed} {Path.GetDirectoryName(record.Path)}";
_context.API.ShowMsg(message);
return false;
}
return true;
},
};
}
}
}

View File

@ -7,7 +7,6 @@
using System.Text;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Wox.Plugin;
using Wox.Plugin.Logger;
using static Interop.NativeMethods;
internal class Everything
@ -50,11 +49,6 @@
uint resultCount = Everything_GetNumResults();
if (setting.Debug)
{
Log.Info(query + " => " + resultCount, typeof(Everything), "EverythingSearch.ResultCount", string.Empty, 217);
}
for (uint i = 0; i < resultCount; i++)
{
StringBuilder buffer = new StringBuilder(260);
@ -65,17 +59,10 @@
string path = isFolder ? fullPath : Path.GetDirectoryName(fullPath);
string ext = Path.GetExtension(fullPath.Replace(".lnk", string.Empty));
if (setting.Debug)
{
Log.Info(i + " : " + ext, typeof(Everything), "EverythingSearch.Result", string.Empty, 229);
}
var r = new Result()
{
Title = name,
ToolTipData = setting.Debug ?
new ToolTipData(orgqry, query) :
new ToolTipData("Name : " + name, fullPath),
ToolTipData = new ToolTipData(name, fullPath),
SubTitle = Resources.plugin_name + ": " + fullPath,
IcoPath = isFolder ? "Images/folder.png" : (setting.Preview ?

14
Main.cs
View File

@ -65,21 +65,21 @@
},
new PluginAdditionalOption()
{
Key = nameof(Settings.Debug),
DisplayLabel = "Log debug data",
Key = nameof(Settings.Updates),
DisplayLabel = Resources.Updates,
DisplayDescription = $"v{Assembly.GetExecutingAssembly().GetName().Version}",
Value = false,
Value = true,
},
};
public void Init(PluginInitContext context)
{
Task.Run(() => new Update(Assembly.GetExecutingAssembly().GetName().Version));
if (_setting.Updates)
Task.Run(() => new Update(Assembly.GetExecutingAssembly().GetName().Version));
_context = context;
_contextMenuLoader = new ContextMenuLoader(context);
_contextMenuLoader = new ContextMenuLoader(context, _setting.Options);
((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_setting.Copy);
_everything = new Everything(_setting);
if (_setting.SkipUpdate) return;
}
public List<Result> Query(Query query)
@ -138,7 +138,7 @@
_setting.MatchPath = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.MatchPath))?.Value ?? false;
_setting.Copy = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Copy))?.Value ?? false;
_setting.QueryText = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.QueryText))?.Value ?? false;
_setting.Debug = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Debug))?.Value ?? true;
_setting.Updates = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Updates))?.Value ?? true;
if (_contextMenuLoader != null) ((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_setting.Copy);

View File

@ -275,5 +275,14 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
return ResourceManager.GetString("SwapCopy_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Check for updates.
/// </summary>
public static string Updates {
get {
return ResourceManager.GetString("Updates", resourceCulture);
}
}
}
}

View File

@ -189,4 +189,7 @@
<data name="SwapCopy_Description" xml:space="preserve">
<value>Set Ctrl+C to copy file/folder, Ctrl+Alt+C to copy path.</value>
</data>
<data name="Updates" xml:space="preserve">
<value>Check for updates</value>
</data>
</root>

View File

@ -168,4 +168,7 @@
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 复制档案Ctrl+Alt+C 复制路径</value>
</data>
<data name="Updates" xml:space="preserve">
<value>检查是否有更新</value>
</data>
</root>

View File

@ -168,4 +168,7 @@
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 複製檔案Ctrl+Alt+C 複製路徑</value>
</data>
<data name="Updates" xml:space="preserve">
<value>啟用時檢查是否有更新</value>
</data>
</root>

View File

@ -2,9 +2,6 @@
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Community.PowerToys.Run.Plugin.Everything.Interop;
using Wox.Plugin.Logger;
internal class Settings
{
@ -14,13 +11,13 @@
internal bool Preview { get; set; } = false;
internal bool QueryText { get; set; } = false;
internal bool RegEx { get; set; } = false;
internal bool Debug { get; set; } = false;
internal bool Updates { get; set; } = true;
// Settings from settings.toml
internal uint Max { get; set; } = 20;
internal int Sort { get; set; } = 14;
internal Dictionary<string, string> Filters { get; set; } = new Dictionary<string, string>();
internal bool SkipUpdate { get; set; } = false;
internal uint Max { get; } = 20;
internal int Sort { get; } = 14;
internal string[] Options { get; }
internal Dictionary<string, string> Filters { get; } = new Dictionary<string, string>();
internal Settings()
{
string[] strArr;
@ -42,8 +39,8 @@
try { Sort = int.Parse(kv[1], culture.NumberFormat); }
catch { }
break;
case "skip":
if (kv[1][0] == 'Y') SkipUpdate = true;
case "options":
Options = kv[1].Split(';', System.StringSplitOptions.RemoveEmptyEntries | System.StringSplitOptions.TrimEntries);
break;
default:
if (kv[0].Contains(':'))
@ -52,11 +49,7 @@
}
}
if (Debug)
{
string msg = $"Max: {Max}\nSort: {Sort}\nFilters: {string.Join("\n - ", Filters.Select(x => { return x.Key + "_" + x.Value; }))}";
Log.Info(msg, typeof(NativeMethods));
}
Options ??= new string[] { "0", "1", "2", "3", "4", "5" };
}
}
}

View File

@ -10,8 +10,9 @@
# https://www.voidtools.com/support/everything/sdk/everything_getsort/
# sort = 14
# Skip update check at start [Y/N default: N]
# skip = N
# 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