[PTRun]Add context buttons for VSCodeWorkspaces plugin (#36517)
This commit is contained in:
Родитель
6ca02f0d3c
Коммит
5e9675eb4f
|
@ -7,16 +7,21 @@ using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties;
|
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties;
|
||||||
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper;
|
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper;
|
||||||
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper;
|
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper;
|
||||||
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper;
|
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper;
|
||||||
|
|
||||||
|
using Wox.Infrastructure;
|
||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
|
using Wox.Plugin.Logger;
|
||||||
|
|
||||||
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
||||||
{
|
{
|
||||||
public class Main : IPlugin, IPluginI18n
|
public class Main : IPlugin, IPluginI18n, IContextMenu
|
||||||
{
|
{
|
||||||
private PluginInitContext _context;
|
private PluginInitContext _context;
|
||||||
|
|
||||||
|
@ -76,11 +81,9 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
||||||
|
|
||||||
hide = true;
|
hide = true;
|
||||||
}
|
}
|
||||||
catch (Win32Exception)
|
catch (Win32Exception ex)
|
||||||
{
|
{
|
||||||
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
|
HandleError("Can't Open this file", ex, showMsg: true);
|
||||||
var msg = "Can't Open this file";
|
|
||||||
_context.API.ShowMsg(name, msg, string.Empty);
|
|
||||||
hide = false;
|
hide = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,11 +127,9 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
||||||
|
|
||||||
hide = true;
|
hide = true;
|
||||||
}
|
}
|
||||||
catch (Win32Exception)
|
catch (Win32Exception ex)
|
||||||
{
|
{
|
||||||
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
|
HandleError("Can't Open this file", ex, showMsg: true);
|
||||||
var msg = "Can't Open this file";
|
|
||||||
_context.API.ShowMsg(name, msg, string.Empty);
|
|
||||||
hide = false;
|
hide = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,23 +143,23 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
||||||
results = results.Where(a => a.Title.Contains(query.Search, StringComparison.InvariantCultureIgnoreCase)).ToList();
|
results = results.Where(a => a.Title.Contains(query.Search, StringComparison.InvariantCultureIgnoreCase)).ToList();
|
||||||
|
|
||||||
results.ForEach(x =>
|
results.ForEach(x =>
|
||||||
{
|
{
|
||||||
if (x.Score == 0)
|
if (x.Score == 0)
|
||||||
{
|
{
|
||||||
x.Score = 100;
|
x.Score = 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
// intersect the title with the query
|
// intersect the title with the query
|
||||||
var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Length);
|
var intersection = Convert.ToInt32(x.Title.ToLowerInvariant().Intersect(query.Search.ToLowerInvariant()).Count() * query.Search.Length);
|
||||||
var differenceWithQuery = Convert.ToInt32((x.Title.Length - intersection) * query.Search.Length * 0.7);
|
var differenceWithQuery = Convert.ToInt32((x.Title.Length - intersection) * query.Search.Length * 0.7);
|
||||||
x.Score = x.Score - differenceWithQuery + intersection;
|
x.Score = x.Score - differenceWithQuery + intersection;
|
||||||
|
|
||||||
// if is a remote machine give it 12 extra points
|
// if is a remote machine give it 12 extra points
|
||||||
if (x.ContextData is VSCodeRemoteMachine)
|
if (x.ContextData is VSCodeRemoteMachine)
|
||||||
{
|
{
|
||||||
x.Score = Convert.ToInt32(x.Score + (intersection * 2));
|
x.Score = Convert.ToInt32(x.Score + (intersection * 2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
results = results.OrderByDescending(x => x.Score).ToList();
|
results = results.OrderByDescending(x => x.Score).ToList();
|
||||||
if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty)
|
if (query.Search == string.Empty || query.Search.Replace(" ", string.Empty) == string.Empty)
|
||||||
|
@ -174,6 +175,108 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
||||||
|
{
|
||||||
|
if (selectedResult?.ContextData is not VSCodeWorkspace workspace)
|
||||||
|
{
|
||||||
|
return new List<ContextMenuResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
string realPath = SystemPath.RealPath(workspace.RelativePath);
|
||||||
|
|
||||||
|
return new List<ContextMenuResult>
|
||||||
|
{
|
||||||
|
new ContextMenuResult
|
||||||
|
{
|
||||||
|
PluginName = Name,
|
||||||
|
Title = $"{Resources.CopyPath} (Ctrl+C)",
|
||||||
|
Glyph = "\xE8C8", // Copy
|
||||||
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
||||||
|
AcceleratorKey = Key.C,
|
||||||
|
AcceleratorModifiers = ModifierKeys.Control,
|
||||||
|
Action = context => CopyToClipboard(realPath),
|
||||||
|
},
|
||||||
|
new ContextMenuResult
|
||||||
|
{
|
||||||
|
PluginName = Name,
|
||||||
|
Title = $"{Resources.OpenInExplorer} (Ctrl+Shift+F)",
|
||||||
|
Glyph = "\xEC50", // File Explorer
|
||||||
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
||||||
|
AcceleratorKey = Key.F,
|
||||||
|
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||||
|
Action = context => OpenInExplorer(realPath),
|
||||||
|
},
|
||||||
|
new ContextMenuResult
|
||||||
|
{
|
||||||
|
PluginName = Name,
|
||||||
|
Title = $"{Resources.OpenInConsole} (Ctrl+Shift+C)",
|
||||||
|
Glyph = "\xE756", // Command Prompt
|
||||||
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
||||||
|
AcceleratorKey = Key.C,
|
||||||
|
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||||
|
Action = context => OpenInConsole(realPath),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CopyToClipboard(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Clipboard.SetText(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
HandleError("Can't copy to clipboard", ex, showMsg: true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool OpenInConsole(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Helper.OpenInConsole(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
HandleError($"Unable to open the specified path in the console: {path}", ex, showMsg: true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool OpenInExplorer(string path)
|
||||||
|
{
|
||||||
|
if (!Helper.OpenInShell("explorer.exe", $"\"{path}\""))
|
||||||
|
{
|
||||||
|
HandleError($"Failed to open folder in Explorer at path: {path}", showMsg: true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleError(string msg, Exception exception = null, bool showMsg = false)
|
||||||
|
{
|
||||||
|
if (exception != null)
|
||||||
|
{
|
||||||
|
Log.Exception(msg, exception, exception.GetType());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Error(msg, typeof(VSCodeWorkspaces.Main));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showMsg)
|
||||||
|
{
|
||||||
|
_context.API.ShowMsg(
|
||||||
|
$"Plugin: {_context.CurrentPluginMetadata.Name}",
|
||||||
|
msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string GetTranslatedPluginTitle()
|
public string GetTranslatedPluginTitle()
|
||||||
{
|
{
|
||||||
return Resources.PluginTitle;
|
return Resources.PluginTitle;
|
||||||
|
|
|
@ -140,5 +140,32 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties {
|
||||||
return ResourceManager.GetString("Workspace", resourceCulture);
|
return ResourceManager.GetString("Workspace", resourceCulture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Copy Path.
|
||||||
|
/// </summary>
|
||||||
|
internal static string CopyPath {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("CopyPath", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Open in console.
|
||||||
|
/// </summary>
|
||||||
|
internal static string OpenInConsole {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("OpenInConsole", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Open in Explorer.
|
||||||
|
/// </summary>
|
||||||
|
internal static string OpenInExplorer {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("OpenInExplorer", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,4 +151,13 @@
|
||||||
<value>Project Folder</value>
|
<value>Project Folder</value>
|
||||||
<comment>It refers to the Visual Studio Code Project Folders</comment>
|
<comment>It refers to the Visual Studio Code Project Folders</comment>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="CopyPath" xml:space="preserve">
|
||||||
|
<value>Copy path</value>
|
||||||
|
</data>
|
||||||
|
<data name="OpenInConsole" xml:space="preserve">
|
||||||
|
<value>Open in console</value>
|
||||||
|
</data>
|
||||||
|
<data name="OpenInExplorer" xml:space="preserve">
|
||||||
|
<value>Open in Explorer</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -4,7 +4,7 @@
|
||||||
"ActionKeyword": "{",
|
"ActionKeyword": "{",
|
||||||
"Name": "VS Code Workspaces",
|
"Name": "VS Code Workspaces",
|
||||||
"Author": "ricardosantos9521",
|
"Author": "ricardosantos9521",
|
||||||
"Version": "1.1.0",
|
"Version": "1.2.0",
|
||||||
"Language": "csharp",
|
"Language": "csharp",
|
||||||
"Website": "https://github.com/ricardosantos9521/PowerToys/",
|
"Website": "https://github.com/ricardosantos9521/PowerToys/",
|
||||||
"ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll",
|
"ExecuteFileName": "Community.PowerToys.Run.Plugin.VSCodeWorkspaces.dll",
|
||||||
|
|
Загрузка…
Ссылка в новой задаче