|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | 4 | using System.Reflection; |
| 5 | +using Microsoft.Office.Core; |
5 | 6 | using Microsoft.Vbe.Interop; |
6 | 7 | using Ninject; |
7 | 8 | using Ninject.Extensions.Conventions; |
|
12 | 13 | using Rubberduck.Settings; |
13 | 14 | using Rubberduck.UI; |
14 | 15 | using Rubberduck.UI.CodeInspections; |
| 16 | +using Rubberduck.UI.Commands; |
15 | 17 | using Rubberduck.VBEditor.VBEHost; |
16 | 18 |
|
17 | 19 | namespace Rubberduck.Root |
@@ -46,23 +48,55 @@ public override void Load() |
46 | 48 | Assembly.GetAssembly(typeof(IRubberduckParser)) |
47 | 49 | }; |
48 | 50 |
|
49 | | - BindMenuTypes(); |
50 | | - BindToolbarTypes(); |
| 51 | + BindRubberduckMenu(); |
51 | 52 |
|
52 | 53 | ApplyConfigurationConvention(assemblies); |
53 | 54 | ApplyDefaultInterfacesConvention(assemblies); |
54 | 55 | ApplyAbstractFactoryConvention(assemblies); |
55 | 56 | } |
56 | 57 |
|
57 | | - private void BindMenuTypes() |
| 58 | + private void BindRubberduckMenu() |
58 | 59 | { |
59 | | - _kernel.Bind<IMenu>().To<RubberduckMenu>(); // todo: confirm RubberduckMenuFactory is actually needed |
60 | | - _kernel.Bind<IMenu>().To<FormContextMenu>().WhenTargetHas<FormContextMenuAttribute>(); |
| 60 | + const int windowMenuId = 30009; |
| 61 | + var menuBarControls = _vbe.CommandBars[1].Controls; |
| 62 | + var beforeIndex = FindMenuInsertionIndex(menuBarControls, windowMenuId); |
| 63 | + |
| 64 | + _kernel.Bind(t => t.FromThisAssembly() |
| 65 | + .SelectAllClasses() |
| 66 | + .InNamespaceOf<ICommand>() |
| 67 | + .EndingWith("CommandMenuItem") |
| 68 | + .BindToSelf()); |
| 69 | + |
| 70 | + //_kernel.Bind(t => t.FromThisAssembly() |
| 71 | + // .SelectAllClasses() |
| 72 | + // .InNamespaceOf<ICommand>() |
| 73 | + // .EndingWith("Command") |
| 74 | + // .Where(type => type.GetInterfaces().Contains(typeof (ICommand))) |
| 75 | + // .BindAllInterfaces() |
| 76 | + // .Configure(binding => binding |
| 77 | + // .When(request => request.Service == typeof(ICommand) |
| 78 | + // && request.Target.Member.DeclaringType.Name.StartsWith("????")))); |
| 79 | + |
| 80 | + _kernel.Bind<ICommand>().To<AboutCommand>().WhenInjectedExactlyInto<AboutCommandMenuItem>(); |
| 81 | + _kernel.Bind<ICommand>().To<OptionsCommand>().WhenInjectedExactlyInto<OptionsCommandMenuItem>(); |
| 82 | + _kernel.Bind<ICommand>().To<CodeExplorerCommand>().WhenInjectedExactlyInto<CodeExplorerCommandMenuItem>(); |
| 83 | + |
| 84 | + _kernel.Bind<RubberduckParentMenu>().ToSelf() |
| 85 | + .WithConstructorArgument("parent", menuBarControls) |
| 86 | + .WithConstructorArgument("beforeIndex", beforeIndex); |
61 | 87 | } |
62 | 88 |
|
63 | | - private void BindToolbarTypes() |
| 89 | + private int FindMenuInsertionIndex(CommandBarControls controls, int beforeId) |
64 | 90 | { |
65 | | - _kernel.Bind<IToolbar>().To<CodeInspectionsToolbar>().WhenTargetHas<CodeInspectionsToolbarAttribute>(); |
| 91 | + for (var i = 1; i <= controls.Count; i++) |
| 92 | + { |
| 93 | + if (controls[i].BuiltIn && controls[i].Id == beforeId) |
| 94 | + { |
| 95 | + return i; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return controls.Count; |
66 | 100 | } |
67 | 101 |
|
68 | 102 | private void ApplyDefaultInterfacesConvention(IEnumerable<Assembly> assemblies) |
|
0 commit comments