Skip to content

Commit 910309a

Browse files
committed
Implemented ICommand - removed existing RD menu.
1 parent a14a05b commit 910309a

15 files changed

+328
-286
lines changed

RetailCoder.VBE/App.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,33 @@
66
using Rubberduck.Parsing.VBA;
77
using Rubberduck.Settings;
88
using Rubberduck.UI;
9+
using Rubberduck.UI.Commands;
910
using Rubberduck.UI.ParserErrors;
1011

1112
namespace Rubberduck
1213
{
1314
public class App : IDisposable
1415
{
1516
private readonly IMessageBox _messageBox;
17+
private readonly RubberduckParentMenu _rubberduckMenu;
1618
private readonly IParserErrorsPresenterFactory _parserErrorsPresenterFactory;
1719
private readonly IRubberduckParserFactory _parserFactory;
1820
private readonly IInspectorFactory _inspectorFactory;
1921
private IParserErrorsPresenter _parserErrorsPresenter;
2022
private readonly IGeneralConfigService _configService;
21-
private readonly IRubberduckMenuFactory _menuFactory;
2223

2324
private IRubberduckParser _parser;
24-
private IMenu _menu;
2525
private Configuration _config;
2626

2727
public App(IMessageBox messageBox,
28-
//IMenu integratedUserInterface,
29-
IRubberduckMenuFactory menuFactory,
28+
RubberduckParentMenu rubberduckMenu,
3029
IParserErrorsPresenterFactory parserErrorsPresenterFactory,
3130
IRubberduckParserFactory parserFactory,
3231
IInspectorFactory inspectorFactory,
3332
IGeneralConfigService configService)
3433
{
3534
_messageBox = messageBox;
36-
_menuFactory = menuFactory;
35+
_rubberduckMenu = rubberduckMenu;
3736
_parserErrorsPresenterFactory = parserErrorsPresenterFactory;
3837
_parserFactory = parserFactory;
3938
_inspectorFactory = inspectorFactory;
@@ -44,6 +43,7 @@ public App(IMessageBox messageBox,
4443

4544
public void Startup()
4645
{
46+
_rubberduckMenu.Initialize();
4747
CleanReloadConfig();
4848
}
4949

@@ -85,8 +85,7 @@ private void Setup()
8585

8686
_inspectorFactory.Create();
8787

88-
_menu = _menuFactory.Create();
89-
_menu.Initialize();
88+
_rubberduckMenu.Localize();
9089
}
9190

9291
private void _parser_ParseStarted(object sender, ParseStartedEventArgs e)
@@ -107,19 +106,17 @@ public void Dispose()
107106

108107
protected virtual void Dispose(bool disposing)
109108
{
110-
if (!disposing) { return; }
109+
if (!disposing)
110+
{
111+
return;
112+
}
111113

112114
CleanUp();
115+
_rubberduckMenu.Remove();
113116
}
114117

115118
private void CleanUp()
116119
{
117-
var menu = _menu as IDisposable;
118-
if (menu != null)
119-
{
120-
menu.Dispose();
121-
}
122-
123120
if (_parser != null)
124121
{
125122
_parser.ParseStarted -= _parser_ParseStarted;

RetailCoder.VBE/Root/RubberduckModule.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using Microsoft.Office.Core;
56
using Microsoft.Vbe.Interop;
67
using Ninject;
78
using Ninject.Extensions.Conventions;
@@ -12,6 +13,7 @@
1213
using Rubberduck.Settings;
1314
using Rubberduck.UI;
1415
using Rubberduck.UI.CodeInspections;
16+
using Rubberduck.UI.Commands;
1517
using Rubberduck.VBEditor.VBEHost;
1618

1719
namespace Rubberduck.Root
@@ -46,23 +48,55 @@ public override void Load()
4648
Assembly.GetAssembly(typeof(IRubberduckParser))
4749
};
4850

49-
BindMenuTypes();
50-
BindToolbarTypes();
51+
BindRubberduckMenu();
5152

5253
ApplyConfigurationConvention(assemblies);
5354
ApplyDefaultInterfacesConvention(assemblies);
5455
ApplyAbstractFactoryConvention(assemblies);
5556
}
5657

57-
private void BindMenuTypes()
58+
private void BindRubberduckMenu()
5859
{
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);
6187
}
6288

63-
private void BindToolbarTypes()
89+
private int FindMenuInsertionIndex(CommandBarControls controls, int beforeId)
6490
{
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;
66100
}
67101

68102
private void ApplyDefaultInterfacesConvention(IEnumerable<Assembly> assemblies)

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@
264264
<Compile Include="Refactorings\ReorderParameters\ReorderParametersRefactoring.cs" />
265265
<Compile Include="Root\RubberduckModule.cs" />
266266
<Compile Include="UI\Commands\AboutCommand.cs" />
267-
<Compile Include="UI\Commands\IRubberduckMenuCommand.cs" />
267+
<Compile Include="UI\Commands\ICommand.cs" />
268+
<Compile Include="UI\Commands\ICommandBar.cs" />
269+
<Compile Include="UI\Commands\ICommandMenuItem.cs" />
270+
<Compile Include="UI\Commands\IMenuItem.cs" />
268271
<Compile Include="Settings\DisplayLanguageSetting.cs" />
269272
<Compile Include="Settings\SourceControlConfiguration.cs" />
270273
<Compile Include="Settings\SourceControlConfigurationService.cs" />
@@ -305,12 +308,13 @@
305308
<Compile Include="Inspections\VariableNotAssignedInspection.cs" />
306309
<Compile Include="Inspections\IdentifierNotAssignedInspectionResult.cs" />
307310
<Compile Include="UI\Commands\OptionsCommand.cs" />
311+
<Compile Include="UI\Commands\ParentMenu.cs" />
308312
<Compile Include="UI\Commands\ParentMenuNotFoundException.cs" />
309-
<Compile Include="UI\Commands\RubberduckCommandBase.cs" />
313+
<Compile Include="UI\Commands\RubberduckParentMenu.cs" />
314+
<Compile Include="UI\Commands\CodeExplorerCommand.cs" />
310315
<Compile Include="UI\Commands\ToolbarState.cs" />
311316
<Compile Include="UI\FolderBrowser.cs" />
312317
<Compile Include="UI\IMessageBox.cs" />
313-
<Compile Include="UI\Commands\RubberduckMenuCommand.cs" />
314318
<Compile Include="UI\ParserErrors\ParseErrorListItem.cs" />
315319
<Compile Include="UI\RubberduckUI.de.Designer.cs">
316320
<AutoGen>True</AutoGen>
Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
1+
using System;
2+
using System.Drawing;
13
using System.Linq;
24
using Microsoft.Office.Core;
35
using Microsoft.Vbe.Interop;
46

57
namespace Rubberduck.UI.Commands
68
{
7-
/// <summary>
8-
/// A command that displays the "About" dialog.
9-
/// </summary>
10-
public class AboutCommand : RubberduckCommandBase
9+
public class AboutCommand : ICommand
1110
{
12-
private readonly VBE _vbe;
13-
14-
public AboutCommand(IRubberduckMenuCommand command, VBE vbe)
15-
: base(command)
16-
{
17-
_vbe = vbe;
18-
}
19-
20-
public override void Initialize()
11+
public void Execute()
2112
{
22-
var parent = _vbe.CommandBars[1].Controls.OfType<CommandBarPopup>()
23-
.SingleOrDefault(control => control.Caption == RubberduckUI.RubberduckMenu);
24-
25-
if (parent == null)
13+
using (var window = new _AboutWindow())
2614
{
27-
throw new ParentMenuNotFoundException(RubberduckUI.RubberduckMenu);
15+
window.ShowDialog();
2816
}
29-
30-
Command.AddCommandBarButton(parent.Controls, RubberduckUI.RubberduckMenu_About, true);
3117
}
18+
}
3219

33-
public override void ExecuteAction()
20+
public class AboutCommandMenuItem : CommandMenuItemBase
21+
{
22+
public AboutCommandMenuItem(ICommand command)
23+
: base(command)
3424
{
35-
using (var window = new _AboutWindow())
36-
{
37-
window.ShowDialog();
38-
}
3925
}
26+
27+
public override string Key { get { return "RubberduckMenu_About"; } }
4028
}
4129
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Rubberduck.UI.Commands
2+
{
3+
public interface ICommand
4+
{
5+
void Execute();
6+
}
7+
8+
public interface ICommand<in T> : ICommand
9+
{
10+
void Execute(T parameter);
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace Rubberduck.UI.Commands
4+
{
5+
public interface ICommandBar
6+
{
7+
void Localize();
8+
void AddItem(IMenuItem item, bool? beginGroup = null, int? beforeIndex = null);
9+
bool RemoveItem(IMenuItem item);
10+
bool Remove();
11+
IEnumerable<IMenuItem> Items { get; }
12+
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace Rubberduck.UI.Commands
5+
{
6+
public interface ICommandMenuItem : IMenuItem
7+
{
8+
ICommand Command { get; }
9+
}
10+
11+
public abstract class CommandMenuItemBase : ICommandMenuItem
12+
{
13+
private readonly ICommand _command;
14+
15+
protected CommandMenuItemBase(ICommand command)
16+
{
17+
_command = command;
18+
}
19+
20+
public abstract string Key { get; }
21+
22+
public ICommand Command { get { return _command; } }
23+
public Func<string> Caption { get { return () => RubberduckUI.ResourceManager.GetString(Key, RubberduckUI.Culture); } }
24+
public bool IsParent { get { return false; } }
25+
public Image Image { get { return null; } }
26+
public Image Mask { get { return null; } }
27+
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace Rubberduck.UI.Commands
5+
{
6+
public interface IMenuItem
7+
{
8+
Func<string> Caption { get; }
9+
10+
string Key { get; }
11+
12+
bool IsParent { get; }
13+
Image Image { get; }
14+
Image Mask { get; }
15+
}
16+
}

RetailCoder.VBE/UI/Commands/IRubberduckMenuCommand.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)