Skip to content

Commit 3823bd3

Browse files
committed
*actually* fixed double-firing of click handlers
1 parent c4a89d7 commit 3823bd3

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

RetailCoder.VBE/UI/Command/MenuItems/CodePaneRefactorRenameCommandMenuItem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Windows.Input;
21
using Rubberduck.Parsing.VBA;
32
using Rubberduck.UI.Command.MenuItems.ParentMenus;
43

RetailCoder.VBE/UI/Command/MenuItems/ParentMenus/ParentMenuItemBase.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
151151
child.ApplyIcon();
152152

153153
child.BeginsGroup = item.BeginGroup;
154-
child.Tag = item.GetType().FullName;
154+
child.Tag = Item.Parent.Name + "::" + Item.Tag + "::" + item.GetType().Name;
155155
child.Caption = item.Caption.Invoke();
156156
var command = item.Command; // todo: add 'ShortcutText' to a new 'interface CommandBase : System.Windows.Input.CommandBase'
157157
child.ShortcutText = command != null
@@ -162,21 +162,14 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
162162
return child;
163163
}
164164

165-
// note: HAAAAACK!!!
166-
private static int? _lastHashCode;
167-
168165
private void child_Click(object sender, CommandBarButtonClickEventArgs e)
169166
{
170-
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag) as ICommandMenuItem;
171-
if (item == null || (_lastHashCode.HasValue && e.Control.Target.GetHashCode() != _lastHashCode.Value))
167+
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => e.Control.Tag.EndsWith(menu.GetType().Name)) as ICommandMenuItem;
168+
if (item == null)
172169
{
173170
return;
174171
}
175172

176-
// without this hack, handler runs once for each menu item that's hooked up to the command.
177-
// hash code is different on every frakkin' click. go figure. I've had it, this is the fix.
178-
_lastHashCode = e.Control.Target.GetHashCode();
179-
180173
Logger.Debug("({0}) Executing click handler for menu item '{1}', hash code {2}", GetHashCode(), e.Control.Caption, e.Control.Target.GetHashCode());
181174
item.Command.Execute(null);
182175
}

Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarButton.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public event EventHandler<CommandBarButtonClickEventArgs> Click
3232
{
3333
((Microsoft.Office.Core.CommandBarButton)Target).Click += Target_Click;
3434
_clickHandler += value;
35+
System.Diagnostics.Debug.WriteLine("Added handler for: {0} '{1}' (tag: {2}, hashcode:{3})", Parent.Name, Target.Caption, Tag, Target.GetHashCode());
3536
}
3637
remove
3738
{
@@ -44,6 +45,7 @@ public event EventHandler<CommandBarButtonClickEventArgs> Click
4445
// he's gone, dave.
4546
}
4647
_clickHandler -= value;
48+
System.Diagnostics.Debug.WriteLine("Removed handler for: {0} '{1}' (tag: {2}, hashcode:{3})", Parent.GetType().Name, Target.Caption, Tag, Target.GetHashCode());
4749
}
4850
}
4951

@@ -54,9 +56,11 @@ private void Target_Click(Microsoft.Office.Core.CommandBarButton ctrl, ref bool
5456
{
5557
return;
5658
}
57-
59+
60+
System.Diagnostics.Debug.Assert(handler.GetInvocationList().Length == 1, "Multicast delegate is registered more than once.");
61+
5862
//note: event is fired for every parent the command exists under. not sure why.
59-
//System.Diagnostics.Debug.WriteLine("Target_Click: {0} '{1}' (tag: {2}, hashcode:{3})", Parent.Name, Target.Caption, Tag, Target.GetHashCode());
63+
System.Diagnostics.Debug.WriteLine("Executing handler for: {0} '{1}' (tag: {2}, hashcode:{3})", Parent.GetType().Name, Target.Caption, Tag, Target.GetHashCode());
6064

6165
var button = new CommandBarButton(ctrl);
6266
var args = new CommandBarButtonClickEventArgs(button);

0 commit comments

Comments
 (0)