Skip to content

Commit 534cd22

Browse files
authored
Merge pull request #3534 from rkapka/rkapka-master
Hotkey wiring enhancements
2 parents 827e708 + 537d6a5 commit 534cd22

33 files changed

+964
-308
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Rubberduck.Settings;
2+
using Rubberduck.UI.Command;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace Rubberduck.Common.Hotkeys
8+
{
9+
public class HotkeyFactory
10+
{
11+
private readonly IEnumerable<CommandBase> _commands;
12+
13+
public HotkeyFactory(IEnumerable<CommandBase> commands)
14+
{
15+
_commands = commands;
16+
}
17+
18+
public Hotkey Create(HotkeySetting setting, IntPtr hWndVbe)
19+
{
20+
if (setting == null)
21+
{
22+
return null;
23+
}
24+
25+
var commandToBind = _commands.FirstOrDefault(command => command.GetType().Name == setting.CommandTypeName);
26+
27+
return commandToBind == null ? null : new Hotkey(hWndVbe, setting.ToString(), commandToBind);
28+
}
29+
}
30+
}

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4-
using System.Diagnostics;
54
using System.Linq;
65
using Rubberduck.Common.Hotkeys;
76
using Rubberduck.Settings;
8-
using Rubberduck.UI.Command;
97
using NLog;
108
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
119
using Rubberduck.VBEditor.WindowsApi;
@@ -15,20 +13,15 @@ namespace Rubberduck.Common
1513
public class RubberduckHooks : SubclassingWindow, IRubberduckHooks
1614
{
1715
private readonly IGeneralConfigService _config;
18-
private readonly IEnumerable<CommandBase> _commands;
16+
private readonly HotkeyFactory _hotkeyFactory;
1917
private readonly IList<IAttachable> _hooks = new List<IAttachable>();
2018
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2119

22-
public RubberduckHooks(IVBE vbe, IGeneralConfigService config, IEnumerable<CommandBase> commands)
20+
public RubberduckHooks(IVBE vbe, IGeneralConfigService config, HotkeyFactory hotkeyFactory)
2321
: base((IntPtr)vbe.MainWindow.HWnd, (IntPtr)vbe.MainWindow.HWnd)
2422
{
25-
_commands = commands;
2623
_config = config;
27-
}
28-
29-
private CommandBase Command(RubberduckHotkey hotkey)
30-
{
31-
return _commands.FirstOrDefault(s => s.Hotkey == hotkey);
24+
_hotkeyFactory = hotkeyFactory;
3225
}
3326

3427
public void HookHotkeys()
@@ -39,16 +32,15 @@ public void HookHotkeys()
3932
var config = _config.LoadConfiguration();
4033
var settings = config.UserSettings.HotkeySettings;
4134

42-
foreach (var hotkey in settings.Settings.Where(hotkey => hotkey.IsEnabled))
35+
foreach (var hotkeySetting in settings.Settings.Where(hotkeySetting => hotkeySetting.IsEnabled))
4336
{
44-
if (Enum.TryParse(hotkey.Name, out RubberduckHotkey assigned))
37+
var hotkey = _hotkeyFactory.Create(hotkeySetting, Hwnd);
38+
if (hotkey != null)
4539
{
46-
var command = Command(assigned);
47-
Debug.Assert(command != null);
48-
49-
AddHook(new Hotkey(Hwnd, hotkey.ToString(), command));
40+
AddHook(hotkey);
5041
}
5142
}
43+
5244
Attach();
5345
}
5446

RetailCoder.VBE/Properties/Settings.Designer.cs

Lines changed: 247 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)