-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandManager.cs
More file actions
179 lines (148 loc) · 5.16 KB
/
Copy pathCommandManager.cs
File metadata and controls
179 lines (148 loc) · 5.16 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace CnSharp.VisualStudio.Extensions.Commands
{
public class CommandManager
{
private readonly ICommandBarAccessor _commandBarAccessor;
private readonly Plugin _plugin;
public CommandManager(Plugin plugin)
{
if (Host.Plugins.Any(m => m.Id == plugin.Id))
{
throw new InvalidOperationException($"plugin {plugin.Id} already exists.");
}
CommandConfig = plugin.CommandConfig;
_plugin = plugin;
plugin.CommandManager = this;
_commandBarAccessor = new CommandBarAccessor();
Host.Plugins.Add(plugin);
}
public CommandConfig CommandConfig { get; }
public virtual void Load()
{
CommandConfig.Buttons.ForEach(m =>
{
m.Plugin = _plugin;
_commandBarAccessor.AddControl(m);
});
CommandConfig.Menus.ForEach(m =>
{
m.Plugin = _plugin;
_commandBarAccessor.AddControl(m);
});
CommandConfig.ContextMenus.ForEach(m =>
{
m.Plugin = _plugin;
_commandBarAccessor.AddControl(m);
});
}
public void Execute(string commandName)
{
CommandControl control = FindCommandControl(commandName);
if (control != null)
{
control.Plugin = _plugin;
control.Execute();
}
}
private CommandControl FindCommandControl(string commandName)
{
var menu = FindCommandMenu(CommandConfig.Menus, commandName);
if (menu != null)
{
return menu;
}
var button = FindCommandMenu(CommandConfig.Buttons, commandName);
if (button != null)
{
return button;
}
menu = FindCommandMenu(CommandConfig.ContextMenus, commandName);
return menu;
}
private CommandMenu FindCommandMenu(IEnumerable<CommandMenu> menus, string commandName)
{
foreach (var m in menus)
{
if (string.Compare(m.Id, commandName, StringComparison.OrdinalIgnoreCase) == 0)
return m;
if (m.SubMenus.Count > 0)
{
var subMenu = FindCommandMenu(m.SubMenus, commandName);
if (subMenu != null)
return subMenu;
}
}
return null;
}
public void Reset(Type type)
{
var control = FindCommandControl(type);
if (control == null)
return;
_commandBarAccessor.ResetControl(control);
}
public void Reset(string commandName)
{
var control = FindCommandControl(commandName);
if (control == null)
return;
_commandBarAccessor.ResetControl(control);
}
public void ApplyDependencies(DependentItems items, bool enabled)
{
var ids = new List<string>();
ids.AddRange(GetMatchedMenus(CommandConfig.Menus, items));
ids.AddRange(GetMatchedMenus(CommandConfig.Buttons, items));
ids.AddRange(GetMatchedMenus(CommandConfig.ContextMenus, items));
_commandBarAccessor.EnableControls(ids, enabled);
}
private IEnumerable<string> GetMatchedMenus(IEnumerable<CommandMenu> menus, DependentItems items)
{
var menuIds = new List<string>();
foreach (var menu in menus)
{
if (menu.SubMenus?.Any() == true)
{
menuIds.AddRange(GetMatchedMenus(menu.SubMenus, items));
}
if (menu.DependentItems.HasFlag(items))
menuIds.Add(menu.Id);
}
return menuIds;
}
public CommandControl FindCommandControl(Type type)
{
string typeName = type.FullName;
var menu = FindCommandMenuByClassName(CommandConfig.Menus, typeName);
if (menu != null)
{
return menu;
}
var button = FindCommandMenuByClassName(CommandConfig.Buttons, typeName);
if (button != null)
{
return button;
}
menu = FindCommandMenuByClassName(CommandConfig.ContextMenus, typeName);
return menu;
}
private CommandMenu FindCommandMenuByClassName(IEnumerable<CommandMenu> menus, string typeName)
{
foreach (var m in menus)
{
if (string.Compare(m.ClassName, typeName, StringComparison.OrdinalIgnoreCase) == 0)
return m;
if (m.SubMenus.Count > 0)
return FindCommandMenu(m.SubMenus, typeName);
}
return null;
}
public virtual void Disconnect()
{
_commandBarAccessor.Delete();
}
}
}