diff --git a/widget/cocoa/NativeMenuSupport.h b/widget/cocoa/NativeMenuSupport.h new file mode 100644 index 000000000000..e01516d2431c --- /dev/null +++ b/widget/cocoa/NativeMenuSupport.h @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_widget_NativeMenuSupport_h +#define mozilla_widget_NativeMenuSupport_h + +class nsIWidget; + +namespace mozilla { + +namespace dom { +class Element; +} + +namespace widget { + +class NativeMenuSupport final { + public: + // Given a top-level window widget and a menu bar DOM node, sets up native + // menus. Once created, native menus are controlled via the DOM, including + // destruction. + static void CreateNativeMenuBar(nsIWidget* aParent, + dom::Element* aMenuBarElement); +}; + +} // namespace widget +} // namespace mozilla + +#endif // mozilla_widget_NativeMenuSupport_h diff --git a/widget/cocoa/NativeMenuSupport.mm b/widget/cocoa/NativeMenuSupport.mm new file mode 100644 index 000000000000..54047feda344 --- /dev/null +++ b/widget/cocoa/NativeMenuSupport.mm @@ -0,0 +1,22 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/widget/NativeMenuSupport.h" + +#include "MainThreadUtils.h" +#include "nsMenuBarX.h" + +namespace mozilla::widget { + +void NativeMenuSupport::CreateNativeMenuBar(nsIWidget* aParent, dom::Element* aMenuBarElement) { + MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Attempting to create native menu bar on wrong thread!"); + + RefPtr mb = new nsMenuBarX(); + + nsresult rv = mb->Create(aParent, aMenuBarElement); + MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); +} + +} // namespace mozilla::widget diff --git a/widget/cocoa/moz.build b/widget/cocoa/moz.build index b7cd0e5c0c4a..f72e9539ba34 100644 --- a/widget/cocoa/moz.build +++ b/widget/cocoa/moz.build @@ -29,10 +29,15 @@ EXPORTS += [ "nsCocoaUtils.h", ] +EXPORTS.mozilla.widget += [ + "NativeMenuSupport.h", +] + UNIFIED_SOURCES += [ "GfxInfo.mm", "IconLoaderHelperCocoa.mm", "NativeKeyBindings.mm", + "NativeMenuSupport.mm", "nsAppShell.mm", "nsBidiKeyboard.mm", "nsCocoaFeatures.mm", diff --git a/widget/cocoa/nsMenuBarX.h b/widget/cocoa/nsMenuBarX.h index 5fd29aace4cc..fb7cba73886a 100644 --- a/widget/cocoa/nsMenuBarX.h +++ b/widget/cocoa/nsMenuBarX.h @@ -12,7 +12,6 @@ #include "nsMenuBaseX.h" #include "nsMenuGroupOwnerX.h" #include "nsChangeObserver.h" -#include "nsINativeMenuService.h" #include "nsString.h" class nsMenuBarX; @@ -34,19 +33,6 @@ class Element; - (id)initWithApplicationMenu:(nsMenuBarX*)aApplicationMenu; @end -// The native menu service for creating native menu bars. -class nsNativeMenuServiceX : public nsINativeMenuService { - public: - NS_DECL_ISUPPORTS - - nsNativeMenuServiceX() {} - - NS_IMETHOD CreateNativeMenuBar(nsIWidget* aParent, mozilla::dom::Element* aMenuBarNode) override; - - protected: - virtual ~nsNativeMenuServiceX() {} -}; - // Objective-C class used to allow us to intervene with keyboard event handling. // We allow mouse actions to work normally. @interface GeckoNSMenu : NSMenu { diff --git a/widget/cocoa/nsMenuBarX.mm b/widget/cocoa/nsMenuBarX.mm index 2f19995631b1..1284359b0c16 100644 --- a/widget/cocoa/nsMenuBarX.mm +++ b/widget/cocoa/nsMenuBarX.mm @@ -47,18 +47,6 @@ static nsIContent* sAboutItemContent = nullptr; static nsIContent* sPrefItemContent = nullptr; static nsIContent* sQuitItemContent = nullptr; -NS_IMPL_ISUPPORTS(nsNativeMenuServiceX, nsINativeMenuService) - -NS_IMETHODIMP nsNativeMenuServiceX::CreateNativeMenuBar(nsIWidget* aParent, - mozilla::dom::Element* aMenuBarElement) { - NS_ASSERTION(NS_IsMainThread(), "Attempting to create native menu bar on wrong thread!"); - - RefPtr mb = new nsMenuBarX(); - if (!mb) return NS_ERROR_OUT_OF_MEMORY; - - return mb->Create(aParent, aMenuBarElement); -} - // // ApplicationMenuDelegate Objective-C class // diff --git a/widget/cocoa/nsWidgetFactory.mm b/widget/cocoa/nsWidgetFactory.mm index 590227f93edc..3e90119ce5dd 100644 --- a/widget/cocoa/nsWidgetFactory.mm +++ b/widget/cocoa/nsWidgetFactory.mm @@ -71,9 +71,6 @@ NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsUserIdleServiceX, nsUserIdleServiceX: NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(ScreenManager, ScreenManager::GetAddRefedSingleton) NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(OSXNotificationCenter, Init) -#include "nsMenuBarX.h" -NS_GENERIC_FACTORY_CONSTRUCTOR(nsNativeMenuServiceX) - #include "nsMacDockSupport.h" NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacDockSupport) @@ -119,7 +116,6 @@ NS_DEFINE_NAMED_CID(NS_PRINTSETTINGSSERVICE_CID); NS_DEFINE_NAMED_CID(NS_PRINTDIALOGSERVICE_CID); NS_DEFINE_NAMED_CID(NS_IDLE_SERVICE_CID); NS_DEFINE_NAMED_CID(NS_SYSTEMALERTSSERVICE_CID); -NS_DEFINE_NAMED_CID(NS_NATIVEMENUSERVICE_CID); NS_DEFINE_NAMED_CID(NS_MACDOCKSUPPORT_CID); NS_DEFINE_NAMED_CID(NS_MACFINDERPROGRESS_CID); NS_DEFINE_NAMED_CID(NS_MACSHARINGSERVICE_CID); @@ -151,7 +147,6 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = { {&kNS_PRINTDIALOGSERVICE_CID, false, NULL, nsPrintDialogServiceXConstructor}, {&kNS_IDLE_SERVICE_CID, false, NULL, nsUserIdleServiceXConstructor}, {&kNS_SYSTEMALERTSSERVICE_CID, false, NULL, OSXNotificationCenterConstructor}, - {&kNS_NATIVEMENUSERVICE_CID, false, NULL, nsNativeMenuServiceXConstructor}, {&kNS_MACDOCKSUPPORT_CID, false, NULL, nsMacDockSupportConstructor}, {&kNS_MACFINDERPROGRESS_CID, false, NULL, nsMacFinderProgressConstructor}, {&kNS_MACSHARINGSERVICE_CID, false, NULL, nsMacSharingServiceConstructor}, @@ -181,7 +176,6 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = { {NS_PRINTDIALOGSERVICE_CONTRACTID, &kNS_PRINTDIALOGSERVICE_CID}, {"@mozilla.org/widget/useridleservice;1", &kNS_IDLE_SERVICE_CID}, {"@mozilla.org/system-alerts-service;1", &kNS_SYSTEMALERTSSERVICE_CID}, - {"@mozilla.org/widget/nativemenuservice;1", &kNS_NATIVEMENUSERVICE_CID}, {"@mozilla.org/widget/macdocksupport;1", &kNS_MACDOCKSUPPORT_CID}, {"@mozilla.org/widget/macfinderprogress;1", &kNS_MACFINDERPROGRESS_CID}, {"@mozilla.org/widget/macsharingservice;1", &kNS_MACSHARINGSERVICE_CID}, diff --git a/widget/moz.build b/widget/moz.build index 6f50842e1b90..df5d495f6494 100644 --- a/widget/moz.build +++ b/widget/moz.build @@ -68,9 +68,6 @@ elif toolkit == "cocoa": "nsITouchBarInput.idl", "nsITouchBarUpdater.idl", ] - EXPORTS += [ - "nsINativeMenuService.h", - ] TEST_DIRS += ["tests", "tests/gtest"] diff --git a/widget/nsINativeMenuService.h b/widget/nsINativeMenuService.h deleted file mode 100644 index e92d7a74a3bc..000000000000 --- a/widget/nsINativeMenuService.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef nsINativeMenuService_h_ -#define nsINativeMenuService_h_ - -#include "nsISupports.h" - -class nsIWidget; -class nsIContent; -namespace mozilla { -namespace dom { -class Element; -} -} // namespace mozilla - -// {90DF88F9-F084-4EF3-829A-49496E636DED} -#define NS_INATIVEMENUSERVICE_IID \ - { \ - 0x90DF88F9, 0xF084, 0x4EF3, { \ - 0x82, 0x9A, 0x49, 0x49, 0x6E, 0x63, 0x6D, 0xED \ - } \ - } - -class nsINativeMenuService : public nsISupports { - public: - NS_DECLARE_STATIC_IID_ACCESSOR(NS_INATIVEMENUSERVICE_IID) - // Given a top-level window widget and a menu bar DOM node, sets up native - // menus. Once created, native menus are controlled via the DOM, including - // destruction. - NS_IMETHOD CreateNativeMenuBar(nsIWidget* aParent, - mozilla::dom::Element* aMenuBarNode) = 0; -}; - -NS_DEFINE_STATIC_IID_ACCESSOR(nsINativeMenuService, NS_INATIVEMENUSERVICE_IID) - -#endif // nsINativeMenuService_h_ diff --git a/xpfe/appshell/AppWindow.cpp b/xpfe/appshell/AppWindow.cpp index 3d376e91788b..78c2d9d3bf8d 100644 --- a/xpfe/appshell/AppWindow.cpp +++ b/xpfe/appshell/AppWindow.cpp @@ -80,7 +80,7 @@ #include "mozilla/dom/DocumentL10n.h" #ifdef XP_MACOSX -# include "nsINativeMenuService.h" +# include "mozilla/widget/NativeMenuSupport.h" # define USE_NATIVE_MENUS #endif @@ -2987,11 +2987,6 @@ static void LoadNativeMenus(Document* aDoc, nsIWidget* aParentWindow) { if (gfxPlatform::IsHeadless()) { return; } - nsCOMPtr nms = - do_GetService("@mozilla.org/widget/nativemenuservice;1"); - if (!nms) { - return; - } // Find the menubar tag (if there is more than one, we ignore all but // the first). @@ -3005,11 +3000,12 @@ static void LoadNativeMenus(Document* aDoc, nsIWidget* aParentWindow) { menubarNode = menubarElements->Item(0); } + using widget::NativeMenuSupport; if (menubarNode) { nsCOMPtr menubarContent(do_QueryInterface(menubarNode)); - nms->CreateNativeMenuBar(aParentWindow, menubarContent); + NativeMenuSupport::CreateNativeMenuBar(aParentWindow, menubarContent); } else { - nms->CreateNativeMenuBar(aParentWindow, nullptr); + NativeMenuSupport::CreateNativeMenuBar(aParentWindow, nullptr); } }