-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathPluginsMenuHelper.cs
More file actions
92 lines (79 loc) · 2.73 KB
/
Copy pathPluginsMenuHelper.cs
File metadata and controls
92 lines (79 loc) · 2.73 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
using System;
using System.Collections.Generic;
using System.Linq;
using MW5.Plugins;
using MW5.Plugins.Concrete;
using MW5.Plugins.Events;
using MW5.Plugins.Interfaces;
using MW5.Plugins.Services;
namespace MW5.Menu
{
internal static class PluginsMenuHelper
{
private static IPluginManager _manager;
private static IAppContext _context;
internal static void Init(IAppContext context, IPluginManager pluginManager)
{
if (context == null || pluginManager == null)
{
throw new NullReferenceException("Failed to initialize plugins.");
}
_context = context;
_manager = pluginManager;
var menuItem = context.Menu.PluginsMenu;
menuItem.DropDownOpening += MenuDropDownOpening;
}
private static void ClearPlugins()
{
var list = new List<IMenuItem>();
var menuItem = _context.Menu.PluginsMenu;
foreach (var item in menuItem.SubItems)
{
if (!item.Skip && !item.HasKey)
{
list.Add(item);
}
}
foreach (var item in list)
{
menuItem.SubItems.Remove(item);
}
}
private static void MenuDropDownOpening(object sender, EventArgs e)
{
ClearPlugins();
var menuItem = _context.Menu.PluginsMenu;
var configureItem = _context.Menu.FindItem(MenuKeys.PluginsConfigure, PluginIdentity.Default);
menuItem.SubItems.InsertBefore = configureItem;
foreach (var p in _manager.CustomPlugins)
{
var item = menuItem.SubItems.AddButton(p.Identity.Name, PluginIdentity.Default);
item.Tag = p.Identity;
item.ItemClicked += OnItemClick;
item.Checked = _manager.PluginActive(p.Identity);
}
menuItem.Update();
}
private static void OnItemClick(object sender, MenuItemEventArgs e)
{
var item = sender as IMenuItem;
if (item != null)
{
var identity = item.Tag as PluginIdentity;
if (identity != null)
{
if (item.Checked)
{
_manager.UnloadPlugin(identity, _context);
}
else
{
_manager.LoadPlugin(identity, Enumerable.Empty<string>().ToDictionary(s => s), _context);
}
item.Checked = !item.Checked;
_context.View.Update();
}
}
}
}
}