-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirTemplateExtension.cs
187 lines (159 loc) · 6.75 KB
/
DirTemplateExtension.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
namespace DirTemplateExtension
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
[COMServerAssociation(AssociationType.DirectoryBackground)]
[COMServerAssociation(AssociationType.Folder)]
public class DirTemplateExtension : SharpContextMenu
{
protected override bool CanShowMenu()
{
var selectedDirCount = SelectedItemPaths.Count();
var selectedFoldersValid = selectedDirCount > 0 &&
SelectedItemPaths.Count(IsSupportedDirectory) == selectedDirCount;
Logger.Debug(
$"CanShowMenu result for FolderPath: ({IsSupportedDirectory(FolderPath)})['{string.Join("; ", FolderPath)}']; result for SelectedItemPaths: ({selectedFoldersValid}) ['{string.Join("; ", SelectedItemPaths)}']");
return IsSupportedDirectory(FolderPath) || selectedFoldersValid;
}
protected override ContextMenuStrip CreateMenu()
{
var menu = new ContextMenuStrip
{
RenderMode = ToolStripRenderMode.System
};
var directory = Configuration.GetTemplateDirectoryInfo();
var itemRootMenu = CreateItemMenu(Resources.ContextMenu_CreateProject,
Resources.ContextMenu_CreateProjectTooltip, Resources.InitProject);
menu.Items.Add(itemRootMenu);
var dirIndex = 1;
foreach (var directoryInfo in directory.EnumerateDirectories())
{
var itemImage = LoadProjectIcon(directoryInfo);
var itemDir = CreateItemMenu(directoryInfo.Name,
string.Format(Resources.ContextMenu_ItemTooltip, directoryInfo.FullName), itemImage);
itemDir.Tag = directoryInfo.FullName;
// Define shortcut keys for first F1-12 keys.
RegisterItemShortcutKey(dirIndex, itemDir);
itemDir.Click += (sender, args) => InitializeDirFromTemplate((string)((ToolStripMenuItem)sender).Tag);
itemRootMenu.DropDownItems.Add(itemDir);
dirIndex++;
}
if (itemRootMenu.DropDownItems.Count > 0)
{
var separatorItem = new ToolStripSeparator();
itemRootMenu.DropDownItems.Add(separatorItem);
}
var templateDirMenuItem = CreateItemMenu(Resources.ContextMenu_OpenTemplateDir);
templateDirMenuItem.Click += (sender, args) => OpenTemplatesDirectory();
itemRootMenu.DropDownItems.Add(templateDirMenuItem);
return menu;
}
private static void RegisterItemShortcutKey(int keyIndex, ToolStripMenuItem itemDir)
{
if (keyIndex >= 13 || !Enum.TryParse($"F{keyIndex}", out Keys parsedKey))
{
return;
}
try
{
itemDir.ShowShortcutKeys = true;
itemDir.ShortcutKeys = Keys.Control | Keys.Alt | Keys.LShiftKey | parsedKey;
itemDir.ShortcutKeyDisplayString = $"Ctrl + LWin + LShift + F{keyIndex}";
}
catch
{
Logger.WriteLog("Unable to register shortcut key: F" + keyIndex);
}
}
private static Image LoadProjectIcon(DirectoryInfo directoryInfo)
{
var projectImagePath = Path.Combine(directoryInfo.FullName, Configuration.ProjectImageFile);
if (!File.Exists(projectImagePath))
{
return null;
}
Image itemImage = null;
try
{
using (var image = Image.FromFile(projectImagePath))
{
itemImage = ImageUtils.ResizeImage(image, 16, 16);
}
}
catch (Exception exception)
{
Logger.WriteLog(
$"Unable to read project \"{directoryInfo.FullName}\" image.\nException: {exception}",
EventLogEntryType.Error);
}
return itemImage;
}
private bool IsSupportedDirectory(string path)
{
return !string.IsNullOrEmpty(path)
&& Directory.Exists(path)
&& !DirectoryUtils.IsSpecialFolder(path)
&& !DirectoryUtils.IsEqualOrChildOf(new DirectoryInfo(path),
Configuration.GetTemplateDirectoryInfo());
}
private static ToolStripMenuItem CreateItemMenu(string text, string tooltip = "", Image image = null)
{
return new ToolStripMenuItem
{
Text = text,
ToolTipText = tooltip,
Image = image,
TextImageRelation = TextImageRelation.ImageBeforeText,
ImageScaling = ToolStripItemImageScaling.None,
Margin = new Padding(15, 0, 0, 0),
TextAlign = ContentAlignment.TopLeft,
ImageAlign = ContentAlignment.MiddleLeft
};
}
private void OpenTemplatesDirectory()
{
var templateDirInfo = Configuration.GetTemplateDirectoryInfo();
try
{
if (!templateDirInfo.Exists)
{
throw new DirectoryNotFoundException("Unable to open template dir");
}
Process.Start(new ProcessStartInfo()
{
FileName = templateDirInfo.FullName,
UseShellExecute = true,
Verb = "open"
});
}
catch (Exception exception)
{
var message = string.Format(Resources.TemplateDir_OpenError, templateDirInfo.FullName);
Logger.WriteLog(message + $"\nException: {exception}", EventLogEntryType.Error);
MessageBox.Show(message, Resources.TemplateDir_OpenErrorBoxTitle, MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void InitializeDirFromTemplate(string templateDir)
{
Logger.Debug(
$"InitializeDirFromTemplate for SelectedItemPaths['{string.Join(", ", SelectedItemPaths)}'] and FolderPath ({FolderPath})");
var dirList = SelectedItemPaths.ToList();
if (!string.IsNullOrEmpty(FolderPath) && !dirList.Contains(FolderPath))
{
dirList.Add(FolderPath);
}
InitializeProjectAction.Build(templateDir, dirList)
.Start();
}
}
}