From d4a67e528f3eaa495732ca9a8971b354c495f9dd Mon Sep 17 00:00:00 2001 From: Luo Sheng Date: Wed, 7 Jan 2015 21:21:28 -0800 Subject: [PATCH 1/3] Stop observing NSMenuDidChangeItemNotification if the item will be created. --- FuzzyAutocomplete/FuzzyAutocomplete.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/FuzzyAutocomplete/FuzzyAutocomplete.m b/FuzzyAutocomplete/FuzzyAutocomplete.m index 4d0dad6..ea5bf41 100644 --- a/FuzzyAutocomplete/FuzzyAutocomplete.m +++ b/FuzzyAutocomplete/FuzzyAutocomplete.m @@ -71,6 +71,10 @@ + (void)createMenuItem { NSMenuItem * editorMenuItem = [[NSApp mainMenu] itemWithTitle: @"Editor"]; if (editorMenuItem && ![editorMenuItem.submenu itemWithTitle: name]) { + [[NSNotificationCenter defaultCenter] removeObserver: self + name: NSMenuDidChangeItemNotification + object: nil]; + NSMenuItem * fuzzyItem = [[NSMenuItem alloc] initWithTitle: name action: NULL keyEquivalent: @""]; From 06b5c844a05fa22a0b024fd25953f35fc3ab33de Mon Sep 17 00:00:00 2001 From: Luo Sheng Date: Thu, 8 Jan 2015 07:29:02 -0800 Subject: [PATCH 2/3] Re-add NSMenuDidChangeItemNotification observer after menu item is added. --- FuzzyAutocomplete/FuzzyAutocomplete.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/FuzzyAutocomplete/FuzzyAutocomplete.m b/FuzzyAutocomplete/FuzzyAutocomplete.m index ea5bf41..b455173 100644 --- a/FuzzyAutocomplete/FuzzyAutocomplete.m +++ b/FuzzyAutocomplete/FuzzyAutocomplete.m @@ -102,6 +102,10 @@ + (void)createMenuItem { [editorMenuItem.submenu insertItem: fuzzyItem atIndex: menuIndex]; } + [[NSNotificationCenter defaultCenter] addObserver: self + selector: @selector(menuDidChange:) + name: NSMenuDidChangeItemNotification + object: nil]; } } From 8dbda41bb2aef8dcc41617fbb30b7b49dbb9c9ae Mon Sep 17 00:00:00 2001 From: Luo Sheng Date: Thu, 8 Jan 2015 07:39:18 -0800 Subject: [PATCH 3/3] Handle observer changes in `menuDidChange:` method. --- FuzzyAutocomplete/FuzzyAutocomplete.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/FuzzyAutocomplete/FuzzyAutocomplete.m b/FuzzyAutocomplete/FuzzyAutocomplete.m index b455173..6aecc49 100644 --- a/FuzzyAutocomplete/FuzzyAutocomplete.m +++ b/FuzzyAutocomplete/FuzzyAutocomplete.m @@ -62,7 +62,16 @@ + (void) applicationDidFinishLaunching: (NSNotification *) notification { } + (void) menuDidChange: (NSNotification *) notification { + [[NSNotificationCenter defaultCenter] removeObserver: self + name: NSMenuDidChangeItemNotification + object: nil]; + [self createMenuItem]; + + [[NSNotificationCenter defaultCenter] addObserver: self + selector: @selector(menuDidChange:) + name: NSMenuDidChangeItemNotification + object: nil]; } + (void)createMenuItem { @@ -71,9 +80,6 @@ + (void)createMenuItem { NSMenuItem * editorMenuItem = [[NSApp mainMenu] itemWithTitle: @"Editor"]; if (editorMenuItem && ![editorMenuItem.submenu itemWithTitle: name]) { - [[NSNotificationCenter defaultCenter] removeObserver: self - name: NSMenuDidChangeItemNotification - object: nil]; NSMenuItem * fuzzyItem = [[NSMenuItem alloc] initWithTitle: name action: NULL @@ -101,11 +107,6 @@ + (void)createMenuItem { } else { [editorMenuItem.submenu insertItem: fuzzyItem atIndex: menuIndex]; } - - [[NSNotificationCenter defaultCenter] addObserver: self - selector: @selector(menuDidChange:) - name: NSMenuDidChangeItemNotification - object: nil]; } }