Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: contextMenu items for Run.Plugin.VSCodeWorkspaces #36517

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;

using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.Properties;
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper;
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper;
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper;

using Wox.Infrastructure;
using Wox.Plugin;

namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces
{
public class Main : IPlugin, IPluginI18n
public class Main : IPlugin, IPluginI18n, IContextMenu
{
private PluginInitContext _context;

Expand Down Expand Up @@ -174,6 +177,101 @@ public void Init(PluginInitContext 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>
{
CreateContextMenuResult(
title: $"{Resources.CopyPath} (Ctrl+C)",
glyph: "\xE8C8", // Copy
acceleratorKey: Key.C,
acceleratorModifiers: ModifierKeys.Control,
action: () => CopyToClipboard(realPath)
),
CreateContextMenuResult(
title: $"{Resources.OpenInExplorer} (Ctrl+Shift+F)",
glyph: "\xEC50", // File Explorer
acceleratorKey: Key.F,
acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift,
action: () => OpenInExplorer(realPath)
)
CreateContextMenuResult(
title: $"{Resources.OpenInConsole} (Ctrl+Shift+C)",
glyph: "\xE756", // Command Prompt
acceleratorKey: Key.C,
acceleratorModifiers: ModifierKeys.Control | ModifierKeys.Shift,
action: () => OpenInConsole(realPath)
),
};
}

private ContextMenuResult CreateContextMenuResult(string title, string glyph, Key acceleratorKey, ModifierKeys acceleratorModifiers, Func<bool> action)
{
return new ContextMenuResult
{
PluginName = Name,
Title = title,
Glyph = glyph,
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
AcceleratorKey = acceleratorKey,
AcceleratorModifiers = acceleratorModifiers,
Action = context => action()
};
}

private bool CopyToClipboard(string path)
{
try
{
Clipboard.SetText(path);
return true;
}
catch (Exception)
{
ShowErrorMessage("Can't copy to clipboard");
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add logging using Wox.Plugin.Logger.Log.Exception().

(Same on the other places.)

This comment was marked as outdated.

}
}

private bool OpenInConsole(string path)
{
try
{
Helper.OpenInConsole(path);
return true;
}
catch (Exception)
{
ShowErrorMessage($"Unable to open the specified path in the console: {path}");
return false;
}
}

private bool OpenInExplorer(string path)
{
if (!Helper.OpenInShell("explorer.exe", $"\"{path}\""))
{
ShowErrorMessage($"Failed to open folder in Explorer at path: {path}");
return false;
}

return true;
}

private void ShowErrorMessage(string message)
{
var pluginName = $"Plugin: {_context.CurrentPluginMetadata.Name}";
_context.API.ShowMsg(pluginName, message, string.Empty);
}


public string GetTranslatedPluginTitle()
{
return Resources.PluginTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,13 @@
<value>Project Folder</value>
<comment>It refers to the Visual Studio Code Project Folders</comment>
</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>
Loading