Skip to content

Commit 7af0c3e

Browse files
authored
Merge pull request #3455 from retailcoder/CommandBarIssues
Handle/log exceptions thrown during initialization and localization of commandbar controls.
2 parents e7d4ca0 + 8d09b18 commit 7af0c3e

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.InteropServices;
@@ -38,14 +39,22 @@ public void Localize()
3839
return;
3940
}
4041

41-
foreach (var kvp in _items)
42+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
4243
{
43-
var item = kvp;
44-
UiDispatcher.Invoke(() =>
44+
try
4545
{
46-
item.Value.Caption = item.Key.Caption.Invoke();
47-
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
48-
});
46+
var item = kvp;
47+
UiDispatcher.Invoke(() =>
48+
{
49+
item.Value.Caption = item.Key.Caption.Invoke();
50+
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
51+
});
52+
53+
}
54+
catch (Exception e)
55+
{
56+
Logger.Error(e, $"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
57+
}
4958
}
5059
}
5160

@@ -60,7 +69,15 @@ public virtual void Initialize()
6069
Item.IsVisible = true;
6170
foreach (var item in _items.Keys.OrderBy(item => item.DisplayOrder))
6271
{
63-
_items[item] = InitializeChildControl(item);
72+
try
73+
{
74+
_items[item] = InitializeChildControl(item);
75+
76+
}
77+
catch (Exception e)
78+
{
79+
Logger.Error(e, $"Initialization of the menu item for {item.Command.GetType().Name} threw an exception.");
80+
}
6481
}
6582
}
6683

@@ -92,17 +109,23 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
92109

93110
public void EvaluateCanExecute(RubberduckParserState state)
94111
{
95-
foreach (var kvp in _items)
112+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
96113
{
97114
var commandItem = kvp.Key;
98-
if (commandItem != null && kvp.Value != null)
115+
var canExecute = false;
116+
try
99117
{
100-
var canExecute = commandItem.EvaluateCanExecute(state);
101-
kvp.Value.IsEnabled = canExecute;
102-
if (commandItem.HiddenWhenDisabled)
103-
{
104-
kvp.Value.IsVisible = canExecute;
105-
}
118+
canExecute = commandItem.EvaluateCanExecute(state);
119+
120+
}
121+
catch (Exception e)
122+
{
123+
Logger.Error(e, $"{commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}.EvaluateCanExecute(RubberduckParserState) threw an exception.");
124+
}
125+
kvp.Value.IsEnabled = canExecute;
126+
if (commandItem?.HiddenWhenDisabled ?? false)
127+
{
128+
kvp.Value.IsVisible = canExecute;
106129
}
107130
}
108131
}
@@ -144,21 +167,14 @@ private void RemoveChildren()
144167
_items.Clear();
145168
}
146169

147-
// note: HAAAAACK!!!
148-
private static int _lastHashCode;
149-
150170
private void child_Click(object sender, CommandBarButtonClickEventArgs e)
151171
{
152172
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
153-
if (item == null || e.Control.Target.GetHashCode() == _lastHashCode)
173+
if (item == null)
154174
{
155175
return;
156176
}
157177

158-
// without this hack, handler runs once for each menu item that's hooked up to the command.
159-
// hash code is different on every frakkin' click. go figure. I've had it, this is the fix.
160-
_lastHashCode = e.Control.Target.GetHashCode();
161-
162178
Logger.Debug("({0}) Executing click handler for commandbar item '{1}', hash code {2}", GetHashCode(), e.Control.Caption, e.Control.Target.GetHashCode());
163179
item.Command.Execute(null);
164180
}

0 commit comments

Comments
 (0)