Cleanup and NullReferenceException fix (#26956)

This commit is contained in:
Davide Giacometti 2023-06-22 14:28:13 +02:00 committed by GitHub
parent 26bf4193f7
commit 08215a8a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,9 +15,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
{ {
public static class VSCodeInstances public static class VSCodeInstances
{ {
private static List<string> _paths = new List<string>(); private static readonly string _userAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private static string _userAppDataPath = Environment.GetEnvironmentVariable("AppData");
public static List<VSCodeInstance> Instances { get; set; } = new List<VSCodeInstance>(); public static List<VSCodeInstance> Instances { get; set; } = new List<VSCodeInstance>();
@ -44,7 +42,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
int bitmap1Width = bitmap1.Width; int bitmap1Width = bitmap1.Width;
int bitmap1Height = bitmap1.Height; int bitmap1Height = bitmap1.Height;
Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2)); using Bitmap overlayBitmapResized = new Bitmap(overlayBitmap, new System.Drawing.Size(bitmap1Width / 2, bitmap1Height / 2));
float marginLeft = (float)((bitmap1Width * 0.7) - (overlayBitmapResized.Width * 0.5)); float marginLeft = (float)((bitmap1Width * 0.7) - (overlayBitmapResized.Width * 0.5));
float marginTop = (float)((bitmap1Height * 0.7) - (overlayBitmapResized.Height * 0.5)); float marginTop = (float)((bitmap1Height * 0.7) - (overlayBitmapResized.Height * 0.5));
@ -62,33 +60,25 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
// Gets the executablePath and AppData foreach instance of VSCode // Gets the executablePath and AppData foreach instance of VSCode
public static void LoadVSCodeInstances() public static void LoadVSCodeInstances()
{ {
var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); var environmentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User) ?? string.Empty;
environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? ";" : string.Empty) + Environment.GetEnvironmentVariable("PATH"); environmentPath += (environmentPath.Length > 0 && environmentPath.EndsWith(';') ? string.Empty : ";") + Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var paths = environmentPath.Split(";").ToList(); var paths = environmentPath
paths = paths.Distinct().ToList(); .Split(';')
.Distinct()
.Where(x => x.Contains("VS Code", StringComparison.OrdinalIgnoreCase)
|| x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase)
|| x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToArray();
var deletedItems = paths.Except(_paths).Any();
var newItems = _paths.Except(paths).Any();
if (newItems || deletedItems)
{
Instances = new List<VSCodeInstance>();
paths = paths.Where(x =>
x.Contains("VS Code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase) ||
x.Contains("vscode", StringComparison.OrdinalIgnoreCase)).ToList();
foreach (var path in paths) foreach (var path in paths)
{ {
if (Directory.Exists(path)) if (Directory.Exists(path))
{ {
var files = Directory.GetFiles(path); var files = Directory.GetFiles(path)
var iconPath = Path.GetDirectoryName(path); .Where(x => (x.Contains("code", StringComparison.OrdinalIgnoreCase) || x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase))
files = files.Where(x =>
(x.Contains("code", StringComparison.OrdinalIgnoreCase) ||
x.Contains("VSCodium", StringComparison.OrdinalIgnoreCase))
&& !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray(); && !x.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)).ToArray();
var iconPath = Path.GetDirectoryName(path);
if (files.Length > 0) if (files.Length > 0)
{ {
var file = files[0]; var file = files[0];
@ -125,18 +115,23 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
var portableData = Path.Join(iconPath, "data"); var portableData = Path.Join(iconPath, "data");
instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version); instance.AppData = Directory.Exists(portableData) ? Path.Join(portableData, "user-data") : Path.Combine(_userAppDataPath, version);
var iconVSCode = Path.Join(iconPath, $"{version}.exe"); var vsCodeIconPath = Path.Join(iconPath, $"{version}.exe");
var vsCodeIcon = Icon.ExtractAssociatedIcon(vsCodeIconPath);
var bitmapIconVscode = Icon.ExtractAssociatedIcon(iconVSCode).ToBitmap(); if (vsCodeIcon != null)
{
using var vsCodeIconBitmap = vsCodeIcon.ToBitmap();
// workspace // workspace
var folderIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//folder.png"); using var folderIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//folder.png"));
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(folderIcon, bitmapIconVscode)); using var bitmapFolderIcon = BitmapOverlayToCenter(folderIcon, vsCodeIconBitmap);
instance.WorkspaceIconBitMap = Bitmap2BitmapImage(bitmapFolderIcon);
// remote // remote
var monitorIcon = (Bitmap)System.Drawing.Image.FromFile(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "//Images//monitor.png"); using var monitorIcon = (Bitmap)Image.FromFile(Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images//monitor.png"));
using var bitmapMonitorIcon = BitmapOverlayToCenter(monitorIcon, vsCodeIconBitmap);
instance.RemoteIconBitMap = Bitmap2BitmapImage(BitmapOverlayToCenter(monitorIcon, bitmapIconVscode)); instance.RemoteIconBitMap = Bitmap2BitmapImage(bitmapMonitorIcon);
}
Instances.Add(instance); Instances.Add(instance);
} }
@ -146,4 +141,3 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper
} }
} }
} }
}