Skip to content
Merged
Changes from all commits
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
30 changes: 24 additions & 6 deletions pscommander/Services/MenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Management.Automation;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.Windows;

Expand All @@ -25,9 +24,6 @@ public MenuService(PowerShellService powerShellService, CommanderEventProvider c

public void UpdateToolbar(ToolbarIcon icon)
{
var assemblyLocation = Assembly.GetEntryAssembly().Location;
var fileInfo = new FileInfo(assemblyLocation);

_taskbarIcon.Dispatcher.Invoke(() => {

if (string.IsNullOrEmpty(icon.Text))
Expand Down Expand Up @@ -83,7 +79,7 @@ public void UpdateToolbar(ToolbarIcon icon)

AddMenuItem(_taskbarIcon.ContextMenu.Items, new MenuItem {
Text = "Edit Config",
Action = ScriptBlock.Create($". '{Path.Combine(fileInfo.DirectoryName, "PSScriptPad.exe")}' -c '{path}'")
Action = CreateEditConfigAction(path)
});
}

Expand All @@ -99,14 +95,36 @@ public void UpdateToolbar(ToolbarIcon icon)
});
}

private static ScriptBlock CreateEditConfigAction(string path)
{
var escapedPath = EscapePowerShellSingleQuotedString(path);
var script = $@"
$configPath = '{escapedPath}'
$code = Get-Command -Name code.cmd, code.exe, code -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1

if ($code) {{
Start-Process -FilePath $code.Source -ArgumentList @('-n', $configPath)
}}
else {{
Start-Process -FilePath notepad.exe -ArgumentList @($configPath)
}}
";

return ScriptBlock.Create(script);
}

private static string EscapePowerShellSingleQuotedString(string value)
{
return value.Replace("'", "''");
}

private static Icon LoadDefaultIcon()
{
var resourceInfo = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/icon.ico"));
if (resourceInfo == null)
{
throw new FileNotFoundException("The default tray icon resource could not be found.", "Resources/icon.ico");
}

using var icon = new Icon(resourceInfo.Stream);
return (Icon)icon.Clone();
}
Expand Down
Loading