1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include <_types/_uint32_t.h>
11 #include "mozilla/dom/Document.h"
12 #include "mozilla/dom/ScriptSettings.h"
13 #include "mozilla/EventDispatcher.h"
14 #include "mozilla/MouseEvents.h"
16 #include "MOZMenuOpeningCoordinator.h"
17 #include "nsMenuItemX.h"
18 #include "nsMenuUtilsX.h"
19 #include "nsMenuItemIconX.h"
21 #include "nsObjCExceptions.h"
23 #include "nsComputedDOMStyle.h"
24 #include "nsThreadUtils.h"
25 #include "nsToolkit.h"
26 #include "nsCocoaUtils.h"
30 #include "nsReadableUtils.h"
31 #include "nsUnicharUtils.h"
32 #include "nsGkAtoms.h"
34 #include "nsBaseWidget.h"
36 #include "nsIContent.h"
37 #include "nsIDocumentObserver.h"
38 #include "nsIComponentManager.h"
39 #include "nsIRollupListener.h"
40 #include "nsIServiceManager.h"
41 #include "nsXULPopupManager.h"
43 using namespace mozilla;
44 using namespace mozilla::dom;
46 static bool gConstructingMenu = false;
47 static bool gMenuMethodsSwizzled = false;
49 int32_t nsMenuX::sIndexingMenuLevel = 0;
51 // TODO: It is unclear whether this is still needed.
52 static void SwizzleDynamicIndexingMethods() {
53 if (gMenuMethodsSwizzled) {
57 nsToolkit::SwizzleMethods([NSMenu class], @selector(_addItem:toTable:),
58 @selector(nsMenuX_NSMenu_addItem:toTable:), true);
59 nsToolkit::SwizzleMethods([NSMenu class], @selector(_removeItem:fromTable:),
60 @selector(nsMenuX_NSMenu_removeItem:fromTable:),
62 // On SnowLeopard the Shortcut framework (which contains the
63 // SCTGRLIndex class) is loaded on demand, whenever the user first opens
64 // a menu (which normally hasn't happened yet). So we need to load it
66 dlopen("/System/Library/PrivateFrameworks/Shortcut.framework/Shortcut",
68 Class SCTGRLIndexClass = ::NSClassFromString(@"SCTGRLIndex");
69 nsToolkit::SwizzleMethods(
70 SCTGRLIndexClass, @selector(indexMenuBarDynamically),
71 @selector(nsMenuX_SCTGRLIndex_indexMenuBarDynamically));
73 Class NSServicesMenuUpdaterClass =
74 ::NSClassFromString(@"_NSServicesMenuUpdater");
75 nsToolkit::SwizzleMethods(
76 NSServicesMenuUpdaterClass,
77 @selector(populateMenu:withServiceEntries:forDisplay:),
78 @selector(nsMenuX_populateMenu:withServiceEntries:forDisplay:));
80 gMenuMethodsSwizzled = true;
87 nsMenuX::nsMenuX(nsMenuParentX* aParent, nsMenuGroupOwnerX* aMenuGroupOwner,
89 : mContent(aContent), mParent(aParent), mMenuGroupOwner(aMenuGroupOwner) {
90 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
92 MOZ_COUNT_CTOR(nsMenuX);
94 SwizzleDynamicIndexingMethods();
96 mMenuDelegate = [[MenuDelegate alloc] initWithGeckoMenu:this];
98 if (!nsMenuBarX::sNativeEventTarget) {
99 nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
102 bool shouldShowServices = false;
103 if (mContent->IsElement()) {
104 mContent->AsElement()->GetAttr(nsGkAtoms::label, mLabel);
107 mContent->AsElement()->HasAttr(nsGkAtoms::showservicesmenu);
109 mNativeMenu = CreateMenuWithGeckoString(mLabel, shouldShowServices);
111 // register this menu to be notified when changes are made to our content
113 NS_ASSERTION(mMenuGroupOwner, "No menu owner given, must have one");
114 mMenuGroupOwner->RegisterForContentChanges(mContent, this);
116 mVisible = !nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
118 NSString* newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
119 mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString
122 mNativeMenuItem.submenu = mNativeMenu;
124 SetEnabled(!mContent->IsElement() ||
125 !mContent->AsElement()->AttrValueIs(
126 kNameSpaceID_None, nsGkAtoms::disabled, nsGkAtoms::_true,
129 // We call RebuildMenu here because keyboard commands are dependent upon
130 // native menu items being created. If we only call RebuildMenu when a menu
131 // is actually selected, then we can't access keyboard commands until the
132 // menu gets selected, which is bad.
135 if (IsXULWindowMenu(mContent)) {
136 // Let the OS know that this is our Window menu.
137 NSApp.windowsMenu = mNativeMenu;
140 mIcon = MakeUnique<nsMenuItemIconX>(this);
146 NS_OBJC_END_TRY_ABORT_BLOCK;
149 nsMenuX::~nsMenuX() {
150 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
152 // Make sure a pending popupshown event isn't dropped.
153 FlushMenuOpenedRunnable();
156 [mNativeMenu cancelTracking];
157 MOZMenuOpeningCoordinator.needToUnwindForMenuClosing = YES;
160 // Make sure pending popuphiding/popuphidden events aren't dropped.
161 FlushMenuClosedRunnable();
163 OnHighlightedItemChanged(Nothing());
166 mNativeMenu.delegate = nil;
167 [mNativeMenu release];
168 [mMenuDelegate release];
169 // autorelease the native menu item so that anything else happening to this
170 // object happens before the native menu item actually dies
171 [mNativeMenuItem autorelease];
173 DetachFromGroupOwnerRecursive();
175 MOZ_COUNT_DTOR(nsMenuX);
177 NS_OBJC_END_TRY_ABORT_BLOCK;
180 void nsMenuX::DetachFromGroupOwnerRecursive() {
181 if (!mMenuGroupOwner) {
182 // Don't recurse if this subtree is already detached.
183 // This avoids repeated recursion during the destruction of nested nsMenuX
184 // structures. Our invariant is: If we are detached, all of our contents are
189 if (mMenuGroupOwner && mContent) {
190 mMenuGroupOwner->UnregisterForContentChanges(mContent);
192 mMenuGroupOwner = nullptr;
194 // Also detach all our children.
195 for (auto& child : mMenuChildren) {
197 [](const RefPtr<nsMenuX>& aMenu) {
198 aMenu->DetachFromGroupOwnerRecursive();
200 [](const RefPtr<nsMenuItemX>& aMenuItem) {
201 aMenuItem->DetachFromGroupOwner();
206 void nsMenuX::OnMenuWillOpen(dom::Element* aPopupElement) {
207 RefPtr<nsMenuX> kungFuDeathGrip(this);
209 mObserver->OnMenuWillOpen(aPopupElement);
213 void nsMenuX::OnMenuDidOpen(dom::Element* aPopupElement) {
214 RefPtr<nsMenuX> kungFuDeathGrip(this);
216 mObserver->OnMenuDidOpen(aPopupElement);
220 void nsMenuX::OnMenuWillActivateItem(dom::Element* aPopupElement,
221 dom::Element* aMenuItemElement) {
222 RefPtr<nsMenuX> kungFuDeathGrip(this);
224 mObserver->OnMenuWillActivateItem(aPopupElement, aMenuItemElement);
228 void nsMenuX::OnMenuClosed(dom::Element* aPopupElement) {
229 RefPtr<nsMenuX> kungFuDeathGrip(this);
231 mObserver->OnMenuClosed(aPopupElement);
235 void nsMenuX::AddMenuChild(MenuChild&& aChild) {
236 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
238 WillInsertChild(aChild);
239 mMenuChildren.AppendElement(aChild);
241 bool isVisible = aChild.match(
242 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->IsVisible(); },
243 [](const RefPtr<nsMenuItemX>& aMenuItem) {
244 return aMenuItem->IsVisible();
246 NSMenuItem* nativeItem = aChild.match(
247 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->NativeNSMenuItem(); },
248 [](const RefPtr<nsMenuItemX>& aMenuItem) {
249 return aMenuItem->NativeNSMenuItem();
253 RemovePlaceholderIfPresent();
254 [mNativeMenu addItem:nativeItem];
255 ++mVisibleItemsCount;
258 NS_OBJC_END_TRY_ABORT_BLOCK;
261 void nsMenuX::InsertMenuChild(MenuChild&& aChild) {
262 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
264 WillInsertChild(aChild);
265 size_t insertionIndex = FindInsertionIndex(aChild);
266 mMenuChildren.InsertElementAt(insertionIndex, aChild);
268 bool isVisible = aChild.match(
269 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->IsVisible(); },
270 [](const RefPtr<nsMenuItemX>& aMenuItem) {
271 return aMenuItem->IsVisible();
274 MenuChildChangedVisibility(aChild, true);
277 NS_OBJC_END_TRY_ABORT_BLOCK;
280 void nsMenuX::RemoveMenuChild(const MenuChild& aChild) {
281 bool isVisible = aChild.match(
282 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->IsVisible(); },
283 [](const RefPtr<nsMenuItemX>& aMenuItem) {
284 return aMenuItem->IsVisible();
287 MenuChildChangedVisibility(aChild, false);
290 WillRemoveChild(aChild);
291 mMenuChildren.RemoveElement(aChild);
294 size_t nsMenuX::FindInsertionIndex(const MenuChild& aChild) {
295 nsCOMPtr<nsIContent> menuPopup = GetMenuPopupContent();
296 MOZ_RELEASE_ASSERT(menuPopup);
298 RefPtr<nsIContent> insertedContent = aChild.match(
299 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->Content(); },
300 [](const RefPtr<nsMenuItemX>& aMenuItem) {
301 return aMenuItem->Content();
304 MOZ_RELEASE_ASSERT(insertedContent->GetParent() == menuPopup);
306 // Iterate over menuPopup's children (insertedContent's siblings) until we
307 // encounter insertedContent. At the same time, keep track of the index in
310 for (nsIContent* child = menuPopup->GetFirstChild();
311 child && index < mMenuChildren.Length();
312 child = child->GetNextSibling()) {
313 if (child == insertedContent) {
317 RefPtr<nsIContent> contentAtIndex = mMenuChildren[index].match(
318 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->Content(); },
319 [](const RefPtr<nsMenuItemX>& aMenuItem) {
320 return aMenuItem->Content();
322 if (child == contentAtIndex) {
330 // Includes all items, including hidden/collapsed ones
331 uint32_t nsMenuX::GetItemCount() { return mMenuChildren.Length(); }
333 // Includes all items, including hidden/collapsed ones
334 mozilla::Maybe<nsMenuX::MenuChild> nsMenuX::GetItemAt(uint32_t aPos) {
335 if (aPos >= (uint32_t)mMenuChildren.Length()) {
339 return Some(mMenuChildren[aPos]);
342 // Only includes visible items
343 nsresult nsMenuX::GetVisibleItemCount(uint32_t& aCount) {
344 aCount = mVisibleItemsCount;
348 // Only includes visible items. Note that this is provides O(N) access
349 // If you need to iterate or search, consider using GetItemAt and doing your own
351 Maybe<nsMenuX::MenuChild> nsMenuX::GetVisibleItemAt(uint32_t aPos) {
352 uint32_t count = mMenuChildren.Length();
353 if (aPos >= mVisibleItemsCount || aPos >= count) {
357 // If there are no invisible items, can provide direct access
358 if (mVisibleItemsCount == count) {
359 return GetItemAt(aPos);
362 // Otherwise, traverse the array until we find the the item we're looking for.
363 uint32_t visibleNodeIndex = 0;
364 for (uint32_t i = 0; i < count; i++) {
365 MenuChild item = *GetItemAt(i);
366 RefPtr<nsIContent> content = item.match(
367 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->Content(); },
368 [](const RefPtr<nsMenuItemX>& aMenuItem) {
369 return aMenuItem->Content();
371 if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(content)) {
372 if (aPos == visibleNodeIndex) {
373 // we found the visible node we're looking for, return it
383 Maybe<nsMenuX::MenuChild> nsMenuX::GetItemForElement(
384 Element* aMenuChildElement) {
385 for (auto& child : mMenuChildren) {
386 RefPtr<nsIContent> content = child.match(
387 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->Content(); },
388 [](const RefPtr<nsMenuItemX>& aMenuItem) {
389 return aMenuItem->Content();
391 if (content == aMenuChildElement) {
398 nsresult nsMenuX::RemoveAll() {
399 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
401 [mNativeMenu removeAllItems];
403 for (auto& child : mMenuChildren) {
404 WillRemoveChild(child);
407 mMenuChildren.Clear();
408 mVisibleItemsCount = 0;
412 NS_OBJC_END_TRY_ABORT_BLOCK;
415 void nsMenuX::WillInsertChild(const MenuChild& aChild) {
416 if (aChild.is<RefPtr<nsMenuX>>()) {
417 aChild.as<RefPtr<nsMenuX>>()->SetObserver(this);
421 void nsMenuX::WillRemoveChild(const MenuChild& aChild) {
423 [](const RefPtr<nsMenuX>& aMenu) {
424 aMenu->DetachFromGroupOwnerRecursive();
425 aMenu->DetachFromParent();
426 aMenu->SetObserver(nullptr);
428 [](const RefPtr<nsMenuItemX>& aMenuItem) {
429 aMenuItem->DetachFromGroupOwner();
430 aMenuItem->DetachFromParent();
434 void nsMenuX::MenuOpened() {
439 // Make sure we fire any pending popupshown / popuphiding / popuphidden events
441 FlushMenuOpenedRunnable();
442 FlushMenuClosedRunnable();
444 if (!mDidFirePopupshowingAndIsApprovedToOpen) {
445 // Fire popupshowing now.
446 bool approvedToOpen = OnOpen();
447 if (!approvedToOpen) {
448 // We can only stop menus from opening which we open ourselves. We cannot
449 // stop menubar root menus or menu submenus from opening. For context
450 // menus, we can call OnOpen() before we ask the system to open the menu.
451 NS_WARNING("The popupshowing event had preventDefault() called on it, "
452 "but in MenuOpened() it "
453 "is too late to stop the menu from opening.");
459 // Reset mDidFirePopupshowingAndIsApprovedToOpen for the next menu opening.
460 mDidFirePopupshowingAndIsApprovedToOpen = false;
463 OnHighlightedItemChanged(Nothing());
468 // Fire the popupshown event in MenuOpenedAsync.
469 // MenuOpened() is called during menuWillOpen, and if cancelTracking is called
470 // now, menuDidClose will not be called. The runnable object must not hold a
471 // strong reference to the nsMenuX, so that there is no reference cycle.
472 class MenuOpenedAsyncRunnable final : public mozilla::CancelableRunnable {
474 explicit MenuOpenedAsyncRunnable(nsMenuX* aMenu)
475 : CancelableRunnable("MenuOpenedAsyncRunnable"), mMenu(aMenu) {}
477 // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230, bug 1535398)
478 MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult Run() override {
479 if (RefPtr<nsMenuX> menu = mMenu) {
480 menu->MenuOpenedAsync();
485 nsresult Cancel() override {
491 nsMenuX* mMenu; // weak, cleared by Cancel() and Run()
493 mPendingAsyncMenuOpenRunnable = new MenuOpenedAsyncRunnable(this);
494 NS_DispatchToCurrentThread(mPendingAsyncMenuOpenRunnable);
497 void nsMenuX::FlushMenuOpenedRunnable() {
498 if (mPendingAsyncMenuOpenRunnable) {
503 void nsMenuX::MenuOpenedAsync() {
504 if (mPendingAsyncMenuOpenRunnable) {
505 mPendingAsyncMenuOpenRunnable->Cancel();
506 mPendingAsyncMenuOpenRunnable = nullptr;
509 mIsOpenForGecko = true;
512 if (mContent->IsElement()) {
513 mContent->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::open,
517 RefPtr<nsIContent> popupContent = GetMenuPopupContent();
519 // Notify our observer.
520 if (mObserver && popupContent) {
521 mObserver->OnMenuDidOpen(popupContent->AsElement());
525 nsEventStatus status = nsEventStatus_eIgnore;
526 WidgetMouseEvent event(true, eXULPopupShown, nullptr,
527 WidgetMouseEvent::eReal);
528 RefPtr<nsIContent> dispatchTo = popupContent ? popupContent : mContent;
529 EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
532 void nsMenuX::MenuClosed() {
537 // Make sure we fire any pending popupshown events first.
538 FlushMenuOpenedRunnable();
540 // If any of our submenus were opened programmatically, make sure they get
542 for (auto& child : mMenuChildren) {
543 if (child.is<RefPtr<nsMenuX>>()) {
544 child.as<RefPtr<nsMenuX>>()->MenuClosed();
550 // Do the rest of the MenuClosed work in MenuClosedAsync.
551 // MenuClosed() is called from -[NSMenuDelegate menuDidClose:]. If a menuitem
552 // was clicked, menuDidClose is called *before* menuItemHit for the clicked
553 // menu item is called. This runnable will be canceled if ~nsMenuX runs before
554 // the runnable. The runnable object must not hold a strong reference to the
555 // nsMenuX, so that there is no reference cycle.
556 class MenuClosedAsyncRunnable final : public mozilla::CancelableRunnable {
558 explicit MenuClosedAsyncRunnable(nsMenuX* aMenu)
559 : CancelableRunnable("MenuClosedAsyncRunnable"), mMenu(aMenu) {}
561 // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230, bug 1535398)
562 MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult Run() override {
563 if (RefPtr<nsMenuX> menu = mMenu) {
564 menu->MenuClosedAsync();
569 nsresult Cancel() override {
575 nsMenuX* mMenu; // weak, cleared by Cancel() and Run()
578 mPendingAsyncMenuCloseRunnable = new MenuClosedAsyncRunnable(this);
580 NS_DispatchToCurrentThread(mPendingAsyncMenuCloseRunnable);
583 void nsMenuX::FlushMenuClosedRunnable() {
584 // If any of our submenus have a pending menu closed runnable, make sure those
586 for (auto& child : mMenuChildren) {
587 if (child.is<RefPtr<nsMenuX>>()) {
588 child.as<RefPtr<nsMenuX>>()->FlushMenuClosedRunnable();
592 if (mPendingAsyncMenuCloseRunnable) {
597 void nsMenuX::MenuClosedAsync() {
598 if (mPendingAsyncMenuCloseRunnable) {
599 mPendingAsyncMenuCloseRunnable->Cancel();
600 mPendingAsyncMenuCloseRunnable = nullptr;
603 // If we have pending command events, run those first.
604 nsTArray<PendingCommandEvent> events = std::move(mPendingCommandEvents);
605 for (auto& event : events) {
606 event.mMenuItem->DoCommand(event.mModifiers, event.mButton);
609 // Make sure no item is highlighted.
610 OnHighlightedItemChanged(Nothing());
612 nsCOMPtr<nsIContent> popupContent = GetMenuPopupContent();
613 nsCOMPtr<nsIContent> dispatchTo = popupContent ? popupContent : mContent;
615 nsEventStatus status = nsEventStatus_eIgnore;
616 WidgetMouseEvent popupHiding(true, eXULPopupHiding, nullptr,
617 WidgetMouseEvent::eReal);
618 EventDispatcher::Dispatch(dispatchTo, nullptr, &popupHiding, nullptr,
621 mIsOpenForGecko = false;
623 if (mContent->IsElement()) {
624 mContent->AsElement()->UnsetAttr(kNameSpaceID_None, nsGkAtoms::open, true);
627 WidgetMouseEvent popupHidden(true, eXULPopupHidden, nullptr,
628 WidgetMouseEvent::eReal);
629 EventDispatcher::Dispatch(dispatchTo, nullptr, &popupHidden, nullptr,
632 // Notify our observer.
633 if (mObserver && popupContent) {
634 mObserver->OnMenuClosed(popupContent->AsElement());
638 void nsMenuX::ActivateItemAfterClosing(RefPtr<nsMenuItemX>&& aItem,
639 NSEventModifierFlags aModifiers,
641 if (mIsOpenForGecko) {
642 // Queue the event into mPendingCommandEvents. We will call aItem->DoCommand
643 // in MenuClosedAsync(). We rely on the assumption that MenuClosedAsync will
645 mPendingCommandEvents.AppendElement(
646 PendingCommandEvent{std::move(aItem), aModifiers, aButton});
648 // The menu item was activated outside of a regular open / activate / close
649 // sequence. This happens in multiple cases:
650 // - When a menu item is activated by a keyboard shortcut while all windows
652 // (otherwise those shortcuts go through Gecko's manual keyboard
654 // - When a menu item in the Dock menu is clicked
655 // - During native menu tests
657 // Run the command synchronously.
658 aItem->DoCommand(aModifiers, aButton);
662 bool nsMenuX::Close() {
663 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
665 if (mDidFirePopupshowingAndIsApprovedToOpen && !mIsOpen) {
666 // Close is being called right after this menu was opened, but before
667 // MenuOpened() had a chance to run. Call it here so that we can go through
668 // the entire popupshown -> popuphiding -> popuphidden sequence. Some
669 // callers expect to get a popuphidden event even if they close the popup
670 // before it was fully open.
674 FlushMenuOpenedRunnable();
676 bool wasOpen = mIsOpenForGecko;
680 // We usually don't get here during normal Firefox usage: If the user closes
681 // the menu by clicking an item, or by clicking outside the menu, or by
682 // pressing escape, then the menu gets closed by macOS, and not by a call to
683 // nsMenuX::Close(). If we do get here, it's usually because we're running
684 // an automated test. Close the menu without the fade-out animation so that
685 // we don't unnecessarily slow down the automated tests.
686 [mNativeMenu cancelTrackingWithoutAnimation];
687 MOZMenuOpeningCoordinator.needToUnwindForMenuClosing = YES;
689 // Handle closing synchronously.
693 FlushMenuClosedRunnable();
697 NS_OBJC_END_TRY_ABORT_BLOCK;
700 void nsMenuX::OnHighlightedItemChanged(
701 const Maybe<uint32_t>& aNewHighlightedIndex) {
702 if (mHighlightedItemIndex == aNewHighlightedIndex) {
706 if (mHighlightedItemIndex) {
707 Maybe<nsMenuX::MenuChild> target = GetVisibleItemAt(*mHighlightedItemIndex);
708 if (target && target->is<RefPtr<nsMenuItemX>>()) {
709 bool handlerCalledPreventDefault; // but we don't actually care
710 target->as<RefPtr<nsMenuItemX>>()->DispatchDOMEvent(
711 u"DOMMenuItemInactive"_ns, &handlerCalledPreventDefault);
714 if (aNewHighlightedIndex) {
715 Maybe<nsMenuX::MenuChild> target = GetVisibleItemAt(*aNewHighlightedIndex);
716 if (target && target->is<RefPtr<nsMenuItemX>>()) {
717 bool handlerCalledPreventDefault; // but we don't actually care
718 target->as<RefPtr<nsMenuItemX>>()->DispatchDOMEvent(
719 u"DOMMenuItemActive"_ns, &handlerCalledPreventDefault);
722 mHighlightedItemIndex = aNewHighlightedIndex;
725 void nsMenuX::OnWillActivateItem(NSMenuItem* aItem) {
726 if (!mIsOpenForGecko) {
730 if (mMenuGroupOwner && mObserver) {
732 mMenuGroupOwner->GetMenuItemForCommandID(uint32_t(aItem.tag));
733 if (item && item->Content()->IsElement()) {
734 RefPtr<dom::Element> itemElement = item->Content()->AsElement();
735 if (nsCOMPtr<nsIContent> popupContent = GetMenuPopupContent()) {
736 mObserver->OnMenuWillActivateItem(popupContent->AsElement(),
744 static NSUserInterfaceLayoutDirection DirectionForElement(
745 dom::Element* aElement) {
746 // Get the direction from the computed style so that inheritance into submenus
747 // is respected. aElement may not have a frame.
748 RefPtr<const ComputedStyle> sc =
749 nsComputedDOMStyle::GetComputedStyle(aElement);
751 return NSApp.userInterfaceLayoutDirection;
754 switch (sc->StyleVisibility()->mDirection) {
755 case StyleDirection::Ltr:
756 return NSUserInterfaceLayoutDirectionLeftToRight;
757 case StyleDirection::Rtl:
758 return NSUserInterfaceLayoutDirectionRightToLeft;
762 void nsMenuX::RebuildMenu() {
763 MOZ_RELEASE_ASSERT(mNeedsRebuild);
764 gConstructingMenu = true;
766 // Retrieve our menupopup.
767 nsCOMPtr<nsIContent> menuPopup = GetMenuPopupContent();
769 gConstructingMenu = false;
773 if (menuPopup->IsElement()) {
774 mNativeMenu.userInterfaceLayoutDirection =
775 DirectionForElement(menuPopup->AsElement());
778 // Iterate over the kids
779 for (nsIContent* child = menuPopup->GetFirstChild(); child;
780 child = child->GetNextSibling()) {
781 if (Maybe<MenuChild> menuChild = CreateMenuChild(child)) {
782 AddMenuChild(std::move(*menuChild));
784 } // for each menu item
786 InsertPlaceholderIfNeeded();
788 gConstructingMenu = false;
789 mNeedsRebuild = false;
792 void nsMenuX::InsertPlaceholderIfNeeded() {
793 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
795 if ([mNativeMenu numberOfItems] == 0) {
796 MOZ_RELEASE_ASSERT(mVisibleItemsCount == 0);
797 NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:@""
802 [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 150, 1)] autorelease];
803 [mNativeMenu addItem:item];
807 NS_OBJC_END_TRY_ABORT_BLOCK;
810 void nsMenuX::RemovePlaceholderIfPresent() {
811 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
813 if (mVisibleItemsCount == 0 && [mNativeMenu numberOfItems] == 1) {
814 // Remove the placeholder.
815 [mNativeMenu removeItemAtIndex:0];
818 NS_OBJC_END_TRY_ABORT_BLOCK;
821 void nsMenuX::SetRebuild(bool aNeedsRebuild) {
822 if (!gConstructingMenu) {
823 mNeedsRebuild = aNeedsRebuild;
824 if (mParent && mParent->AsMenuBar()) {
825 mParent->AsMenuBar()->SetNeedsRebuild();
830 nsresult nsMenuX::SetEnabled(bool aIsEnabled) {
831 if (aIsEnabled != mIsEnabled) {
832 // we always want to rebuild when this changes
833 mIsEnabled = aIsEnabled;
834 mNativeMenuItem.enabled = mIsEnabled;
839 nsresult nsMenuX::GetEnabled(bool* aIsEnabled) {
840 NS_ENSURE_ARG_POINTER(aIsEnabled);
841 *aIsEnabled = mIsEnabled;
845 GeckoNSMenu* nsMenuX::CreateMenuWithGeckoString(nsString& aMenuTitle,
846 bool aShowServices) {
847 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
849 NSString* title = [NSString stringWithCharacters:(UniChar*)aMenuTitle.get()
850 length:aMenuTitle.Length()];
851 GeckoNSMenu* myMenu = [[GeckoNSMenu alloc] initWithTitle:title];
852 myMenu.delegate = mMenuDelegate;
854 // We don't want this menu to auto-enable menu items because then Cocoa
855 // overrides our decisions and things get incorrectly enabled/disabled.
856 myMenu.autoenablesItems = NO;
858 // Only show "Services", "Autofill" and similar entries provided by macOS
859 // if our caller wants them:
860 myMenu.allowsContextMenuPlugIns = aShowServices;
862 // we used to install Carbon event handlers here, but since NSMenu* doesn't
863 // create its underlying MenuRef until just before display, we delay until
864 // that happens. Now we install the event handlers when Cocoa notifies
865 // us that a menu is about to display - see the Cocoa MenuDelegate class.
869 NS_OBJC_END_TRY_ABORT_BLOCK;
872 Maybe<nsMenuX::MenuChild> nsMenuX::CreateMenuChild(nsIContent* aContent) {
873 if (aContent->IsAnyOfXULElements(nsGkAtoms::menuitem,
874 nsGkAtoms::menuseparator)) {
875 return Some(MenuChild(CreateMenuItem(aContent)));
877 if (aContent->IsXULElement(nsGkAtoms::menu)) {
879 MenuChild(MakeRefPtr<nsMenuX>(this, mMenuGroupOwner, aContent)));
884 RefPtr<nsMenuItemX> nsMenuX::CreateMenuItem(nsIContent* aMenuItemContent) {
885 MOZ_RELEASE_ASSERT(aMenuItemContent);
887 nsAutoString menuitemName;
888 if (aMenuItemContent->IsElement()) {
889 aMenuItemContent->AsElement()->GetAttr(nsGkAtoms::label, menuitemName);
892 EMenuItemType itemType = eRegularMenuItemType;
893 if (aMenuItemContent->IsXULElement(nsGkAtoms::menuseparator)) {
894 itemType = eSeparatorMenuItemType;
895 } else if (aMenuItemContent->IsElement()) {
896 static Element::AttrValuesArray strings[] = {nsGkAtoms::checkbox,
897 nsGkAtoms::radio, nullptr};
898 switch (aMenuItemContent->AsElement()->FindAttrValueIn(
899 kNameSpaceID_None, nsGkAtoms::type, strings, eCaseMatters)) {
901 itemType = eCheckboxMenuItemType;
904 itemType = eRadioMenuItemType;
909 return MakeRefPtr<nsMenuItemX>(this, menuitemName, itemType, mMenuGroupOwner,
913 // This menu is about to open. Returns false if the handler wants to stop the
914 // opening of the menu.
915 bool nsMenuX::OnOpen() {
916 if (mDidFirePopupshowingAndIsApprovedToOpen) {
921 NS_WARNING("nsMenuX::OnOpen() called while the menu is already considered "
926 RefPtr<nsIContent> popupContent = GetMenuPopupContent();
928 if (mObserver && popupContent) {
929 mObserver->OnMenuWillOpen(popupContent->AsElement());
932 nsEventStatus status = nsEventStatus_eIgnore;
933 WidgetMouseEvent event(true, eXULPopupShowing, nullptr,
934 WidgetMouseEvent::eReal);
937 RefPtr<nsIContent> dispatchTo = popupContent ? popupContent : mContent;
938 rv = EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
939 if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault) {
943 DidFirePopupShowing();
948 void nsMenuX::DidFirePopupShowing() {
949 mDidFirePopupshowingAndIsApprovedToOpen = true;
951 // If the open is going to succeed we need to walk our menu items, checking to
952 // see if any of them have a command attribute. If so, several attributes
953 // must potentially be updated.
955 nsCOMPtr<nsIContent> popupContent = GetMenuPopupContent();
960 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
962 pm->UpdateMenuItems(popupContent->AsElement());
966 // Find the |menupopup| child in the |popup| representing this menu. It should
967 // be one of a very few children so we won't be iterating over a bazillion menu
968 // items to find it (so the strcmp won't kill us).
969 already_AddRefed<nsIContent> nsMenuX::GetMenuPopupContent() {
970 // Check to see if we are a "menupopup" node (if we are a native menu).
971 if (mContent->IsXULElement(nsGkAtoms::menupopup)) {
972 return do_AddRef(mContent);
975 // Otherwise check our child nodes.
977 for (RefPtr<nsIContent> child = mContent->GetFirstChild(); child;
978 child = child->GetNextSibling()) {
979 if (child->IsXULElement(nsGkAtoms::menupopup)) {
980 return child.forget();
987 bool nsMenuX::IsXULHelpMenu(nsIContent* aMenuContent) {
989 if (aMenuContent && aMenuContent->IsElement()) {
991 aMenuContent->AsElement()->GetAttr(nsGkAtoms::id, id);
992 if (id.Equals(u"helpMenu"_ns)) {
999 bool nsMenuX::IsXULWindowMenu(nsIContent* aMenuContent) {
1000 bool retval = false;
1001 if (aMenuContent && aMenuContent->IsElement()) {
1003 aMenuContent->AsElement()->GetAttr(nsGkAtoms::id, id);
1004 if (id.Equals(u"windowMenu"_ns)) {
1015 void nsMenuX::ObserveAttributeChanged(dom::Document* aDocument,
1016 nsIContent* aContent,
1017 nsAtom* aAttribute) {
1018 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
1020 // ignore the |open| attribute, which is by far the most common
1021 if (gConstructingMenu || (aAttribute == nsGkAtoms::open)) {
1025 if (aAttribute == nsGkAtoms::disabled) {
1026 SetEnabled(!mContent->AsElement()->AttrValueIs(
1027 kNameSpaceID_None, nsGkAtoms::disabled, nsGkAtoms::_true,
1029 } else if (aAttribute == nsGkAtoms::label) {
1030 mContent->AsElement()->GetAttr(nsGkAtoms::label, mLabel);
1031 NSString* newCocoaLabelString =
1032 nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
1033 mNativeMenu.title = newCocoaLabelString;
1034 mNativeMenuItem.title = newCocoaLabelString;
1035 } else if (aAttribute == nsGkAtoms::hidden ||
1036 aAttribute == nsGkAtoms::collapsed) {
1039 bool newVisible = !nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
1041 // don't do anything if the state is correct already
1042 if (newVisible == mVisible) {
1046 mVisible = newVisible;
1048 RefPtr<nsMenuX> self = this;
1049 mParent->MenuChildChangedVisibility(MenuChild(self), newVisible);
1054 } else if (aAttribute == nsGkAtoms::image) {
1058 NS_OBJC_END_TRY_ABORT_BLOCK;
1061 void nsMenuX::ObserveContentRemoved(dom::Document* aDocument,
1062 nsIContent* aContainer, nsIContent* aChild,
1063 nsIContent* aPreviousSibling) {
1064 if (gConstructingMenu) {
1069 mMenuGroupOwner->UnregisterForContentChanges(aChild);
1072 // We will update the menu contents the next time the menu is opened.
1076 // The menu is currently open. Remove the child from mMenuChildren and from
1078 nsCOMPtr<nsIContent> popupContent = GetMenuPopupContent();
1079 if (popupContent && aContainer == popupContent && aChild->IsElement()) {
1080 if (Maybe<MenuChild> child = GetItemForElement(aChild->AsElement())) {
1081 RemoveMenuChild(*child);
1086 void nsMenuX::ObserveContentInserted(dom::Document* aDocument,
1087 nsIContent* aContainer,
1088 nsIContent* aChild) {
1089 if (gConstructingMenu) {
1096 // We will update the menu contents the next time the menu is opened.
1100 // The menu is currently open. Insert the child into mMenuChildren and into
1102 nsCOMPtr<nsIContent> popupContent = GetMenuPopupContent();
1103 if (popupContent && aContainer == popupContent) {
1104 if (Maybe<MenuChild> child = CreateMenuChild(aChild)) {
1105 InsertMenuChild(std::move(*child));
1110 void nsMenuX::SetupIcon() {
1111 mIcon->SetupIcon(mContent);
1112 mNativeMenuItem.image = mIcon->GetIconImage();
1115 void nsMenuX::IconUpdated() {
1116 mNativeMenuItem.image = mIcon->GetIconImage();
1117 if (mIconListener) {
1118 mIconListener->IconUpdated();
1122 void nsMenuX::MenuChildChangedVisibility(const MenuChild& aChild,
1124 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
1126 NSMenuItem* nativeItem = aChild.match(
1127 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->NativeNSMenuItem(); },
1128 [](const RefPtr<nsMenuItemX>& aMenuItem) {
1129 return aMenuItem->NativeNSMenuItem();
1134 "The native item should not be in a menu while it is hidden");
1135 RemovePlaceholderIfPresent();
1136 NSInteger insertionPoint = CalculateNativeInsertionPoint(aChild);
1137 [mNativeMenu insertItem:nativeItem atIndex:insertionPoint];
1138 mVisibleItemsCount++;
1141 [mNativeMenu indexOfItem:nativeItem] != -1,
1142 "The native item should be in this menu while it is visible");
1143 [mNativeMenu removeItem:nativeItem];
1144 mVisibleItemsCount--;
1145 InsertPlaceholderIfNeeded();
1148 NS_OBJC_END_TRY_ABORT_BLOCK;
1151 NSInteger nsMenuX::CalculateNativeInsertionPoint(const MenuChild& aChild) {
1152 NSInteger insertionPoint = 0;
1153 for (auto& currItem : mMenuChildren) {
1154 // Using GetItemAt instead of GetVisibleItemAt to avoid O(N^2)
1155 if (currItem == aChild) {
1156 return insertionPoint;
1158 NSMenuItem* nativeItem = currItem.match(
1159 [](const RefPtr<nsMenuX>& aMenu) { return aMenu->NativeNSMenuItem(); },
1160 [](const RefPtr<nsMenuItemX>& aMenuItem) {
1161 return aMenuItem->NativeNSMenuItem();
1163 // Only count visible items.
1164 if (nativeItem.menu) {
1168 return insertionPoint;
1171 void nsMenuX::Dump(uint32_t aIndent) const {
1173 "%*s - menu [%p] %-16s <%s>", aIndent * 2, "", this,
1174 mLabel.IsEmpty() ? "(empty label)" : NS_ConvertUTF16toUTF8(mLabel).get(),
1175 NS_ConvertUTF16toUTF8(mContent->NodeName()).get());
1176 if (mNeedsRebuild) {
1177 printf(" [NeedsRebuild]");
1183 printf(" [Visible]");
1186 printf(" [IsEnabled]");
1188 printf(" (%d visible items)", int(mVisibleItemsCount));
1190 for (const auto& subitem : mMenuChildren) {
1192 [=](const RefPtr<nsMenuX>& aMenu) { aMenu->Dump(aIndent + 1); },
1193 [=](const RefPtr<nsMenuItemX>& aMenuItem) {
1194 aMenuItem->Dump(aIndent + 1);
1200 // MenuDelegate Objective-C class, used to set up Carbon events
1203 @implementation MenuDelegate
1205 - (id)initWithGeckoMenu:(nsMenuX*)geckoMenu {
1206 if ((self = [super init])) {
1207 NS_ASSERTION(geckoMenu, "Cannot initialize native menu delegate with NULL "
1208 "gecko menu! Will crash!");
1209 mGeckoMenu = geckoMenu;
1210 mBlocksToRunWhenOpen = [[NSMutableArray alloc] init];
1216 [mBlocksToRunWhenOpen release];
1220 - (void)runBlockWhenOpen:(void (^)())block {
1221 [mBlocksToRunWhenOpen addObject:[[block copy] autorelease]];
1224 - (void)menu:(NSMenu*)aMenu willHighlightItem:(NSMenuItem*)aItem {
1225 if (!aMenu || !mGeckoMenu) {
1229 Maybe<uint32_t> index =
1230 aItem ? Some(static_cast<uint32_t>([aMenu indexOfItem:aItem]))
1232 mGeckoMenu->OnHighlightedItemChanged(index);
1235 - (void)menuWillOpen:(NSMenu*)menu {
1236 for (void (^block)() in mBlocksToRunWhenOpen) {
1239 [mBlocksToRunWhenOpen removeAllObjects];
1245 // Don't do anything while the OS is (re)indexing our menus (on Leopard and
1246 // higher). This stops the Help menu from being able to search in our
1247 // menus, but it also resolves many other problems.
1248 if (nsMenuX::sIndexingMenuLevel > 0) {
1252 // Hold a strong reference to mGeckoMenu while calling its methods.
1253 RefPtr<nsMenuX> geckoMenu = mGeckoMenu;
1254 geckoMenu->MenuOpened();
1257 - (void)menuDidClose:(NSMenu*)menu {
1262 // Don't do anything while the OS is (re)indexing our menus (on Leopard and
1263 // higher). This stops the Help menu from being able to search in our
1264 // menus, but it also resolves many other problems.
1265 if (nsMenuX::sIndexingMenuLevel > 0) {
1269 // Hold a strong reference to mGeckoMenu while calling its methods.
1270 RefPtr<nsMenuX> geckoMenu = mGeckoMenu;
1271 geckoMenu->MenuClosed();
1274 // This is called after menuDidClose:.
1275 - (void)menu:(NSMenu*)aMenu willActivateItem:(NSMenuItem*)aItem {
1280 // Hold a strong reference to mGeckoMenu while calling its methods.
1281 RefPtr<nsMenuX> geckoMenu = mGeckoMenu;
1282 geckoMenu->OnWillActivateItem(aItem);
1287 // OS X Leopard (at least as of 10.5.2) has an obscure bug triggered by some
1288 // behavior that's present in Mozilla.org browsers but not (as best I can
1289 // tell) in Apple products like Safari. (It's not yet clear exactly what this
1292 // The bug is that sometimes you crash on quit in nsMenuX::RemoveAll(), on a
1293 // call to [NSMenu removeItemAtIndex:]. The crash is caused by trying to
1294 // access a deleted NSMenuItem object (sometimes (perhaps always?) by trying
1295 // to send it a _setChangedFlags: message). Though this object was deleted
1296 // some time ago, it remains registered as a potential target for a particular
1297 // key equivalent. So when [NSMenu removeItemAtIndex:] removes the current
1298 // target for that same key equivalent, the OS tries to "activate" the
1301 // The underlying reason appears to be that NSMenu's _addItem:toTable: and
1302 // _removeItem:fromTable: methods (which are used to keep a hashtable of
1303 // registered key equivalents) don't properly "retain" and "release"
1304 // NSMenuItem objects as they are added to and removed from the hashtable.
1306 // Our (hackish) workaround is to shadow the OS's hashtable with another
1307 // hastable of our own (gShadowKeyEquivDB), and use it to "retain" and
1308 // "release" NSMenuItem objects as needed. This resolves bmo bugs 422287 and
1309 // 423669. When (if) Apple fixes this bug, we can remove this workaround.
1311 static NSMutableDictionary* gShadowKeyEquivDB = nil;
1313 // Class for values in gShadowKeyEquivDB.
1315 @interface KeyEquivDBItem : NSObject {
1317 NSMutableSet* mTables;
1320 - (id)initWithItem:(NSMenuItem*)aItem table:(NSMapTable*)aTable;
1321 - (BOOL)hasTable:(NSMapTable*)aTable;
1322 - (int)addTable:(NSMapTable*)aTable;
1323 - (int)removeTable:(NSMapTable*)aTable;
1327 @implementation KeyEquivDBItem
1329 - (id)initWithItem:(NSMenuItem*)aItem table:(NSMapTable*)aTable {
1330 if (!gShadowKeyEquivDB) {
1331 gShadowKeyEquivDB = [[NSMutableDictionary alloc] init];
1333 self = [super init];
1334 if (aItem && aTable) {
1335 mTables = [[NSMutableSet alloc] init];
1336 mItem = [aItem retain];
1337 [mTables addObject:[NSValue valueWithPointer:aTable]];
1355 - (BOOL)hasTable:(NSMapTable*)aTable {
1356 return [mTables member:[NSValue valueWithPointer:aTable]] ? YES : NO;
1359 // Does nothing if aTable (its index value) is already present in mTables.
1360 - (int)addTable:(NSMapTable*)aTable {
1362 [mTables addObject:[NSValue valueWithPointer:aTable]];
1364 return [mTables count];
1367 - (int)removeTable:(NSMapTable*)aTable {
1369 NSValue* objectToRemove =
1370 [mTables member:[NSValue valueWithPointer:aTable]];
1371 if (objectToRemove) {
1372 [mTables removeObject:objectToRemove];
1375 return [mTables count];
1380 @interface NSMenu (MethodSwizzling)
1381 + (void)nsMenuX_NSMenu_addItem:(NSMenuItem*)aItem toTable:(NSMapTable*)aTable;
1382 + (void)nsMenuX_NSMenu_removeItem:(NSMenuItem*)aItem
1383 fromTable:(NSMapTable*)aTable;
1386 @implementation NSMenu (MethodSwizzling)
1388 + (void)nsMenuX_NSMenu_addItem:(NSMenuItem*)aItem toTable:(NSMapTable*)aTable {
1389 if (aItem && aTable) {
1390 NSValue* key = [NSValue valueWithPointer:aItem];
1391 KeyEquivDBItem* shadowItem = [gShadowKeyEquivDB objectForKey:key];
1393 [shadowItem addTable:aTable];
1395 shadowItem = [[KeyEquivDBItem alloc] initWithItem:aItem table:aTable];
1396 [gShadowKeyEquivDB setObject:shadowItem forKey:key];
1397 // Release after [NSMutableDictionary setObject:forKey:] retains it (so
1398 // that it will get dealloced when removeObjectForKey: is called).
1399 [shadowItem release];
1403 [self nsMenuX_NSMenu_addItem:aItem toTable:aTable];
1406 + (void)nsMenuX_NSMenu_removeItem:(NSMenuItem*)aItem
1407 fromTable:(NSMapTable*)aTable {
1408 [self nsMenuX_NSMenu_removeItem:aItem fromTable:aTable];
1410 if (aItem && aTable) {
1411 NSValue* key = [NSValue valueWithPointer:aItem];
1412 KeyEquivDBItem* shadowItem = [gShadowKeyEquivDB objectForKey:key];
1413 if (shadowItem && [shadowItem hasTable:aTable]) {
1414 if (![shadowItem removeTable:aTable]) {
1415 [gShadowKeyEquivDB removeObjectForKey:key];
1423 // This class is needed to keep track of when the OS is (re)indexing all of
1424 // our menus. This appears to only happen on Leopard and higher, and can
1425 // be triggered by opening the Help menu. Some operations are unsafe while
1426 // this is happening -- notably the calls to [[NSImage alloc]
1427 // initWithSize:imageRect.size] and [newImage lockFocus] in nsMenuItemIconX::
1428 // OnStopFrame(). But we don't yet have a complete list, and Apple doesn't
1429 // yet have any documentation on this subject. (Apple also doesn't yet have
1430 // any documented way to find the information we seek here.) The "original"
1431 // of this class (the one whose indexMenuBarDynamically method we hook) is
1432 // defined in the Shortcut framework in /System/Library/PrivateFrameworks.
1433 @interface NSObject (SCTGRLIndexMethodSwizzling)
1434 - (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically;
1437 @implementation NSObject (SCTGRLIndexMethodSwizzling)
1439 - (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically {
1440 // This method appears to be called (once) whenever the OS (re)indexes our
1441 // menus. sIndexingMenuLevel is a int32_t just in case it might be
1442 // reentered. As it's running, it spawns calls to two undocumented
1443 // HIToolbox methods (_SimulateMenuOpening() and _SimulateMenuClosed()),
1444 // which "simulate" the opening and closing of our menus without actually
1446 ++nsMenuX::sIndexingMenuLevel;
1447 [self nsMenuX_SCTGRLIndex_indexMenuBarDynamically];
1448 --nsMenuX::sIndexingMenuLevel;
1453 @interface NSObject (NSServicesMenuUpdaterSwizzling)
1454 - (void)nsMenuX_populateMenu:(NSMenu*)aMenu
1455 withServiceEntries:(NSArray*)aServices
1456 forDisplay:(BOOL)aForDisplay;
1459 @interface _NSServiceEntry : NSObject
1460 - (NSString*)bundleIdentifier;
1463 @implementation NSObject (NSServicesMenuUpdaterSwizzling)
1465 - (void)nsMenuX_populateMenu:(NSMenu*)aMenu
1466 withServiceEntries:(NSArray*)aServices
1467 forDisplay:(BOOL)aForDisplay {
1468 NSMutableArray* filteredServices = [NSMutableArray array];
1470 // We need to filter some services, such as "Search with Google", since this
1471 // service is duplicating functionality already exposed by our "Search Google
1472 // for..." context menu entry and because it opens in Safari, which can cause
1473 // confusion for users.
1474 for (_NSServiceEntry* service in aServices) {
1475 NSString* bundleId = [service bundleIdentifier];
1476 NSString* msg = [service valueForKey:@"message"];
1477 bool shouldSkip = ([bundleId isEqualToString:@"com.apple.Safari"]) ||
1478 ([bundleId isEqualToString:@"com.apple.systemuiserver"] &&
1479 [msg isEqualToString:@"openURL"]);
1481 [filteredServices addObject:service];
1485 [self nsMenuX_populateMenu:aMenu
1486 withServiceEntries:filteredServices
1487 forDisplay:aForDisplay];