-
Notifications
You must be signed in to change notification settings - Fork 3
/
IconProvider.cs
71 lines (59 loc) · 2.57 KB
/
IconProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections.Concurrent;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Flow.Launcher.Plugin.VisualStudio
{
public class IconProvider
{
public const string DefaultIcon = ImageFolderName + "\\icon.png";
public const string Remove = ImageFolderName +"\\delete.png";
public const string Folder = ImageFolderName +"\\folder.png";
private const string ImageFolderName = "Images";
private const string VSIconsFolderName = "VSIcons";
private readonly ConcurrentDictionary<string, string> vsIcons;
private readonly string vsIconsDirectoryPath;
public string Windows { get; init; }
public string Notification { get; init; }
public IconProvider(PluginInitContext context)
{
vsIcons = new ConcurrentDictionary<string, string>();
Windows = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, ImageFolderName, "windows.png");
Notification = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, ImageFolderName, "notification.png");
vsIconsDirectoryPath = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, ImageFolderName, VSIconsFolderName);
Directory.CreateDirectory(vsIconsDirectoryPath);
foreach (var iconPath in Directory.EnumerateFiles(vsIconsDirectoryPath))
{
vsIcons.TryAdd(Path.GetFileNameWithoutExtension(iconPath), iconPath);
}
}
public void ReloadIcons()
{
vsIcons.Clear();
foreach (var iconPath in Directory.EnumerateFiles(vsIconsDirectoryPath))
{
vsIcons.TryAdd(Path.GetFileNameWithoutExtension(iconPath), iconPath);
}
}
public string GetIconPath(VisualStudioInstance vs)
{
if (!vsIcons.TryGetValue(vs.InstanceId, out string iconPath))
{
try
{
var icon = Icon.ExtractAssociatedIcon(vs.ExePath);
var bitmap = icon.ToBitmap();
iconPath = Path.Combine(vsIconsDirectoryPath, $"{vs.InstanceId}.png");
using var fileStream = new FileStream(iconPath, FileMode.CreateNew);
bitmap.Save(fileStream, ImageFormat.Png);
vsIcons.TryAdd(vs.InstanceId, iconPath);
}
catch (System.Exception)
{
iconPath = DefaultIcon;
}
}
return iconPath;
}
}
}