resolves #34 - Copy file with Ctrl+C

split option text into label and description
label always english, description can be translated
slightly adjust debug data for better privacy
This commit is contained in:
Lin Yu-Chieh (Victor) 2022-12-02 00:31:07 +08:00
parent f0372aa625
commit fab3b63123
7 changed files with 227 additions and 67 deletions

View File

@ -19,14 +19,20 @@ namespace Community.PowerToys.Run.Plugin.Everything
{
internal class ContextMenuLoader : IContextMenu
{
private readonly PluginInitContext context;
private readonly PluginInitContext _context;
// Extensions for adding run as admin context menu item for applications
private readonly string[] appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
private readonly string[] _appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
private bool _swapCopy;
internal void UpdateCopy(bool swapCopy)
{
_swapCopy = swapCopy;
}
public ContextMenuLoader(PluginInitContext context)
{
this.context = context;
_context = context;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
@ -34,27 +40,54 @@ namespace Community.PowerToys.Run.Plugin.Everything
var contextMenus = new List<ContextMenuResult>();
if (selectedResult.ContextData is SearchResult record)
{
bool isFile = record.File; //Path.HasExtension(record.Path);
bool isFile = record.File;
if (isFile)
{
contextMenus.Add(this.CreateOpenContainingFolderResult(record));
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 (this.CanFileBeRunAsAdmin(record.Path))
if (CanFileBeRunAsAdmin(record.Path))
{
contextMenus.Add(CreateRunAsAdminContextMenu(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_path,
Title = _swapCopy ? Properties.Resources.copy_file : Properties.Resources.copy_file.Replace("Ctrl", "Ctrl+Alt"),
Glyph = "\xE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = ModifierKeys.Control,
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;
}
},
});
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = _swapCopy ? Properties.Resources.copy_path.Replace("Ctrl", "Ctrl+Alt") : Properties.Resources.copy_path,
Glyph = "\xE71B",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.C,
AcceleratorModifiers = _swapCopy ? ModifierKeys.Control | ModifierKeys.Alt : ModifierKeys.Control,
Action = (context) =>
{
@ -66,9 +99,9 @@ namespace Community.PowerToys.Run.Plugin.Everything
catch (Exception e)
{
var message = Properties.Resources.clipboard_failed;
Log.Exception(message, e, this.GetType());
Log.Exception(message, e, GetType());
this.context.API.ShowMsg(message);
_context.API.ShowMsg(message);
return false;
}
},
@ -99,7 +132,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
catch (Exception e)
{
Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, this.GetType());
Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, GetType());
return false;
}
},
@ -140,7 +173,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
private bool CanFileBeRunAsAdmin(string path)
{
string fileExtension = Path.GetExtension(path);
foreach (string extension in this.appExtensions)
foreach (string extension in _appExtensions)
{
// Using OrdinalIgnoreCase since this is internal
if (extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase))
@ -167,7 +200,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
if (!Helper.OpenInShell("explorer.exe", $"/select,\"{record.Path}\""))
{
var message = $"{Properties.Resources.folder_open_failed} {Path.GetDirectoryName(record.Path)}";
this.context.API.ShowMsg(message);
_context.API.ShowMsg(message);
return false;
}

83
Main.cs
View File

@ -27,12 +27,19 @@ namespace Community.PowerToys.Run.Plugin.Everything
private const string RegEx = nameof(RegEx);
private const string NoPreview = nameof(NoPreview);
private const string MatchPath = nameof(MatchPath);
private bool regEx;
private bool preview;
private bool matchPath;
private const string SwapCopy = nameof(SwapCopy);
private const string QueryTextDisplay = nameof(QueryTextDisplay);
private bool _regEx;
private bool _preview;
private bool _matchPath;
private bool _swapCopy;
private bool _queryTextDisplay;
private IContextMenu _contextMenuLoader;
private PluginInitContext _context;
private bool _disposed;
private const string Debug = nameof(Debug);
private bool debug;
private bool _debug;
public string Name => Resources.plugin_name;
@ -44,18 +51,35 @@ namespace Community.PowerToys.Run.Plugin.Everything
{
Key = MatchPath,
DisplayLabel = Resources.Match_path,
DisplayDescription = Resources.Match_path_Description,
Value = false,
},
new PluginAdditionalOption()
{
Key = NoPreview,
DisplayLabel = Resources.Preview,
DisplayDescription = Resources.Preview_Description,
Value = false,
},
new PluginAdditionalOption()
{
Key = RegEx,
DisplayLabel = Resources.RegEx,
DisplayDescription = Resources.RegEx_Description,
Value = false,
},
new PluginAdditionalOption()
{
Key = SwapCopy,
DisplayLabel = Resources.SwapCopy,
DisplayDescription = Resources.SwapCopy_Description,
Value = false,
},
new PluginAdditionalOption()
{
Key = QueryTextDisplay,
DisplayLabel = Resources.QueryText,
DisplayDescription = Resources.QueryText_Description,
Value = false,
},
new PluginAdditionalOption()
@ -66,15 +90,12 @@ namespace Community.PowerToys.Run.Plugin.Everything
},
};
private IContextMenu contextMenuLoader;
private PluginInitContext context;
private bool disposed;
public void Init(PluginInitContext context)
{
this.context = context;
this.contextMenuLoader = new ContextMenuLoader(context);
EverythingSetup(debug);
_context = context;
_contextMenuLoader = new ContextMenuLoader(context);
((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_swapCopy);
EverythingSetup(_debug);
}
public List<Result> Query(Query query)
@ -92,7 +113,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
try
{
results.AddRange(EverythingSearch(searchQuery, this.preview, this.matchPath, debug));
results.AddRange(EverythingSearch(searchQuery, _preview, _matchPath, _queryTextDisplay, _debug));
}
catch (System.ComponentModel.Win32Exception)
{
@ -107,7 +128,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
catch (Exception e)
{
Log.Exception("Everything Exception", e, this.GetType());
Log.Exception("Everything Exception", e, GetType());
}
}
@ -116,7 +137,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
return this.contextMenuLoader.LoadContextMenus(selectedResult);
return _contextMenuLoader.LoadContextMenus(selectedResult);
}
public Control CreateSettingPanel()
@ -126,44 +147,38 @@ namespace Community.PowerToys.Run.Plugin.Everything
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var regX = false;
var nopreview = false;
var debuging = false;
var searchpath = false;
if (settings != null && settings.AdditionalOptions != null)
{
regX = settings.AdditionalOptions.FirstOrDefault(x => x.Key == RegEx)?.Value ?? false;
nopreview = settings.AdditionalOptions.FirstOrDefault(x => x.Key == NoPreview)?.Value ?? false;
debuging = settings.AdditionalOptions.FirstOrDefault(x => x.Key == Debug)?.Value ?? true;
searchpath = settings.AdditionalOptions.FirstOrDefault(x => x.Key == MatchPath)?.Value ?? false;
_regEx = settings.AdditionalOptions.FirstOrDefault(x => x.Key == RegEx)?.Value ?? false;
_preview = settings.AdditionalOptions.FirstOrDefault(x => x.Key == NoPreview)?.Value ?? false;
_matchPath = settings.AdditionalOptions.FirstOrDefault(x => x.Key == MatchPath)?.Value ?? false;
_swapCopy = settings.AdditionalOptions.FirstOrDefault(x => x.Key == SwapCopy)?.Value ?? false;
_queryTextDisplay = settings.AdditionalOptions.FirstOrDefault(x => x.Key == QueryTextDisplay)?.Value ?? false;
_debug = settings.AdditionalOptions.FirstOrDefault(x => x.Key == Debug)?.Value ?? true;
if (_contextMenuLoader != null) ((ContextMenuLoader)_contextMenuLoader).UpdateCopy(_swapCopy);
Everything_SetRegex(_regEx);
Everything_SetMatchPath(_matchPath);
}
this.regEx = regX;
Everything_SetRegex(this.regEx);
this.preview = nopreview;
this.debug = debuging;
this.matchPath = searchpath;
Everything_SetMatchPath(this.matchPath);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
if (!_disposed)
{
if (disposing)
{
}
this.disposed = true;
_disposed = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

View File

@ -193,7 +193,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
}
public static IEnumerable<Result> EverythingSearch(string qry, bool preview, bool matchpath, bool debug)
public static IEnumerable<Result> EverythingSearch(string qry, bool preview, bool matchpath, bool updateQuery, bool debug)
{
string orgqry = qry;
Everything_SetMax(max);
@ -242,7 +242,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
if (debug)
{
Log.Info(i + " : " + name + " = " + fullPath, typeof(NativeMethods), "EverythingSearch.Result", string.Empty, 229);
Log.Info(i + " : " + ext, typeof(NativeMethods), "EverythingSearch.Result", string.Empty, 229);
}
var r = new Result()
@ -279,9 +279,7 @@ namespace Community.PowerToys.Run.Plugin.Everything
}
},
// Changing query text will cause new query, changing results, not desirable
// QueryTextDisplay = isFolder ? path : name,
QueryTextDisplay = orgqry,
QueryTextDisplay = updateQuery ? (isFolder ? path : name) : orgqry,
};
yield return r;
}

View File

@ -69,6 +69,15 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Copy (Ctrl+C).
/// </summary>
public static string copy_file {
get {
return ResourceManager.GetString("copy_file", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy path (Ctrl+C).
/// </summary>
@ -106,7 +115,7 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
/// <summary>
/// Looks up a localized string similar to Path - Search path in additional to file/folder name..
/// Looks up a localized string similar to Match Path.
/// </summary>
public static string Match_path {
get {
@ -114,6 +123,15 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Search path in additional to file/folder name..
/// </summary>
public static string Match_path_Description {
get {
return ResourceManager.GetString("Match_path_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Open containing folder (Ctrl+Shift+E).
/// </summary>
@ -151,7 +169,7 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
/// <summary>
/// Looks up a localized string similar to Preview - Preview file content as icon, may cause freezing if file is not local..
/// Looks up a localized string similar to Preview.
/// </summary>
public static string Preview {
get {
@ -160,7 +178,34 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
/// <summary>
/// Looks up a localized string similar to RegEx - Enable regular expression in search..
/// Looks up a localized string similar to Preview file content as icon, may cause freezing if file is not local..
/// </summary>
public static string Preview_Description {
get {
return ResourceManager.GetString("Preview_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Query Text.
/// </summary>
public static string QueryText {
get {
return ResourceManager.GetString("QueryText", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Selected result can update query text. Helps Display additional info, but may change display results..
/// </summary>
public static string QueryText_Description {
get {
return ResourceManager.GetString("QueryText_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to RegEx.
/// </summary>
public static string RegEx {
get {
@ -168,6 +213,15 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Enable regular expression in search..
/// </summary>
public static string RegEx_Description {
get {
return ResourceManager.GetString("RegEx_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Run as administrator (Ctrl+Shift+Enter).
/// </summary>
@ -176,5 +230,23 @@ namespace Community.PowerToys.Run.Plugin.Everything.Properties {
return ResourceManager.GetString("run_as_admin", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy.
/// </summary>
public static string SwapCopy {
get {
return ResourceManager.GetString("SwapCopy", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Set Ctrl+C to copy file/folder, Ctrl+Alt+C to copy path..
/// </summary>
public static string SwapCopy_Description {
get {
return ResourceManager.GetString("SwapCopy_Description", resourceCulture);
}
}
}
}

View File

@ -120,6 +120,9 @@
<data name="clipboard_failed" xml:space="preserve">
<value>Fail to set text in clipboard</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>Copy (Ctrl+C)</value>
</data>
<data name="copy_path" xml:space="preserve">
<value>Copy path (Ctrl+C)</value>
</data>
@ -133,7 +136,10 @@
<value>Fail to open folder at</value>
</data>
<data name="Match_path" xml:space="preserve">
<value>Path - Search path in additional to file/folder name.</value>
<value>Match Path</value>
</data>
<data name="Match_path_Description" xml:space="preserve">
<value>Search path in additional to file/folder name.</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
<value>Open containing folder (Ctrl+Shift+E)</value>
@ -148,12 +154,30 @@
<value>Everything</value>
</data>
<data name="Preview" xml:space="preserve">
<value>Preview - Preview file content as icon, may cause freezing if file is not local.</value>
<value>Preview</value>
</data>
<data name="Preview_Description" xml:space="preserve">
<value>Preview file content as icon, may cause freezing if file is not local.</value>
</data>
<data name="QueryText" xml:space="preserve">
<value>Query Text</value>
</data>
<data name="QueryText_Description" xml:space="preserve">
<value>Selected result can update query text. Helps Display additional info, but may change display results.</value>
</data>
<data name="RegEx" xml:space="preserve">
<value>RegEx - Enable regular expression in search.</value>
<value>RegEx</value>
</data>
<data name="RegEx_Description" xml:space="preserve">
<value>Enable regular expression in search.</value>
</data>
<data name="run_as_admin" xml:space="preserve">
<value>Run as administrator (Ctrl+Shift+Enter)</value>
</data>
<data name="SwapCopy" xml:space="preserve">
<value>Copy</value>
</data>
<data name="SwapCopy_Description" xml:space="preserve">
<value>Set Ctrl+C to copy file/folder, Ctrl+Alt+C to copy path.</value>
</data>
</root>

View File

@ -120,6 +120,9 @@
<data name="clipboard_failed" xml:space="preserve">
<value>无法设置剪贴板中的文本</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>复制 (Ctrl+C)</value>
</data>
<data name="copy_path" xml:space="preserve">
<value>复制路径 (Ctrl+C)</value>
</data>
@ -132,7 +135,7 @@
<data name="folder_open_failed" xml:space="preserve">
<value>无法打开所在文件夹</value>
</data>
<data name="Match_path" xml:space="preserve">
<data name="Match_path_Description" xml:space="preserve">
<value>匹配路径</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
@ -147,13 +150,19 @@
<data name="plugin_name" xml:space="preserve">
<value>Everything</value>
</data>
<data name="Preview" xml:space="preserve">
<value>预览 - 以图标形式预览文件内容,如果文件不是本地文件,可能会导致冻结</value>
<data name="Preview_Description" xml:space="preserve">
<value>以图标形式预览文件内容,如果文件不是本地文件,可能会导致冻结</value>
</data>
<data name="RegEx" xml:space="preserve">
<data name="QueryText_Description" xml:space="preserve">
<value>选取结果时更新搜寻词,获得更多资讯,但可能会变更搜寻结果</value>
</data>
<data name="RegEx_Description" xml:space="preserve">
<value>RegEx 正则表达式搜索</value>
</data>
<data name="run_as_admin" xml:space="preserve">
<value>以管理员身份运行 (Ctrl+Shift+Enter)</value>
</data>
</root>
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 复制档案Ctrl+Alt+C 复制路径</value>
</data>
</root>

View File

@ -120,6 +120,9 @@
<data name="clipboard_failed" xml:space="preserve">
<value>複製到剪貼簿失敗</value>
</data>
<data name="copy_file" xml:space="preserve">
<value>複製 (Ctrl+C)</value>
</data>
<data name="copy_path" xml:space="preserve">
<value>複製路徑 (Ctrl+C)</value>
</data>
@ -132,7 +135,7 @@
<data name="folder_open_failed" xml:space="preserve">
<value>無法啟動資料夾</value>
</data>
<data name="Match_path" xml:space="preserve">
<data name="Match_path_Description" xml:space="preserve">
<value>也收尋路徑</value>
</data>
<data name="open_containing_folder" xml:space="preserve">
@ -147,13 +150,19 @@
<data name="plugin_name" xml:space="preserve">
<value>Everything 搜尋</value>
</data>
<data name="Preview" xml:space="preserve">
<value>預覽 - 將檔案內容顯示為預覽圖示, 非本機的檔案可能會造成停頓.</value>
<data name="Preview_Description" xml:space="preserve">
<value>將檔案內容顯示為預覽圖示,預覽雲端的檔案可能會造成停頓</value>
</data>
<data name="RegEx" xml:space="preserve">
<data name="QueryText_Description" xml:space="preserve">
<value>選取結果時更新搜尋詞,獲得更多資訊,但可能會變更搜尋結果</value>
</data>
<data name="RegEx_Description" xml:space="preserve">
<value>RegEx 正規表示式搜尋</value>
</data>
<data name="run_as_admin" xml:space="preserve">
<value>以系統管理員身分執行 (Ctrl+Shift+Enter)</value>
</data>
<data name="SwapCopy_Description" xml:space="preserve">
<value>使用 Ctrl+C 複製檔案Ctrl+Alt+C 複製路徑</value>
</data>
</root>