1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Runtime . InteropServices ;
@@ -38,14 +39,22 @@ public void Localize()
38
39
return ;
39
40
}
40
41
41
- foreach ( var kvp in _items )
42
+ foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null ) )
42
43
{
43
- var item = kvp ;
44
- UiDispatcher . Invoke ( ( ) =>
44
+ try
45
45
{
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
+ }
49
58
}
50
59
}
51
60
@@ -60,7 +69,15 @@ public virtual void Initialize()
60
69
Item . IsVisible = true ;
61
70
foreach ( var item in _items . Keys . OrderBy ( item => item . DisplayOrder ) )
62
71
{
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
+ }
64
81
}
65
82
}
66
83
@@ -92,17 +109,23 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
92
109
93
110
public void EvaluateCanExecute ( RubberduckParserState state )
94
111
{
95
- foreach ( var kvp in _items )
112
+ foreach ( var kvp in _items . Where ( kv => kv . Key != null && kv . Value != null ) )
96
113
{
97
114
var commandItem = kvp . Key ;
98
- if ( commandItem != null && kvp . Value != null )
115
+ var canExecute = false ;
116
+ try
99
117
{
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 ;
106
129
}
107
130
}
108
131
}
@@ -144,21 +167,14 @@ private void RemoveChildren()
144
167
_items . Clear ( ) ;
145
168
}
146
169
147
- // note: HAAAAACK!!!
148
- private static int _lastHashCode ;
149
-
150
170
private void child_Click ( object sender , CommandBarButtonClickEventArgs e )
151
171
{
152
172
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 )
154
174
{
155
175
return ;
156
176
}
157
177
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
-
162
178
Logger . Debug ( "({0}) Executing click handler for commandbar item '{1}', hash code {2}" , GetHashCode ( ) , e . Control . Caption , e . Control . Target . GetHashCode ( ) ) ;
163
179
item . Command . Execute ( null ) ;
164
180
}
0 commit comments