Bug 1037433 - Add nsSystemStatusBarCocoa implementation of nsISystemStatusBar. . r=smichaud
authorMarkus Stange <mstange@themasta.com>
Fri, 18 Jul 2014 21:06:13 +0200
changeset 215767 bfa9065c5da1eb93c0f8ec10e5197af4cefe68e8
parent 215766 c7723b9fc8d5b53ca18624c14410d3a33292cd4a
child 215768 81a1e00bc476004078a35fa6d7c504433c309989
push id3857
push userraliiev@mozilla.com
push dateTue, 02 Sep 2014 16:39:23 +0000
treeherdermozilla-beta@5638b907b505 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmichaud
bugs1037433
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1037433 - Add nsSystemStatusBarCocoa implementation of nsISystemStatusBar. . r=smichaud This can be accessed from script using Cc["@mozilla.org/widget/macsystemstatusbar;1"].getService(Ci.nsISystemStatusBar).
widget/cocoa/moz.build
widget/cocoa/nsSystemStatusBarCocoa.h
widget/cocoa/nsSystemStatusBarCocoa.mm
widget/cocoa/nsWidgetFactory.mm
widget/nsWidgetsCID.h
--- a/widget/cocoa/moz.build
+++ b/widget/cocoa/moz.build
@@ -45,16 +45,17 @@ UNIFIED_SOURCES += [
     'nsNativeThemeCocoa.mm',
     'nsPrintDialogX.mm',
     'nsPrintOptionsX.mm',
     'nsPrintSettingsX.mm',
     'nsScreenCocoa.mm',
     'nsScreenManagerCocoa.mm',
     'nsSound.mm',
     'nsStandaloneNativeMenu.mm',
+    'nsSystemStatusBarCocoa.mm',
     'nsToolkit.mm',
     'nsWidgetFactory.mm',
     'nsWindowMap.mm',
     'OSXNotificationCenter.mm',
     'WidgetTraceEvent.mm',
 ]
 
 # These files cannot be built in unified mode because they force NSPR logging.
new file mode 100644
--- /dev/null
+++ b/widget/cocoa/nsSystemStatusBarCocoa.h
@@ -0,0 +1,40 @@
+/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
+/* 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 nsSystemStatusBarCocoa_h_
+#define nsSystemStatusBarCocoa_h_
+
+#include "nsISystemStatusBar.h"
+#include "nsClassHashtable.h"
+#include "nsAutoPtr.h"
+
+class nsStandaloneNativeMenu;
+@class NSStatusItem;
+
+class nsSystemStatusBarCocoa : public nsISystemStatusBar
+{
+public:
+  nsSystemStatusBarCocoa() {}
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSISYSTEMSTATUSBAR
+
+protected:
+  virtual ~nsSystemStatusBarCocoa() {}
+
+  struct StatusItem
+  {
+    StatusItem(nsStandaloneNativeMenu* aMenu);
+    ~StatusItem();
+
+  private:
+    nsRefPtr<nsStandaloneNativeMenu> mMenu;
+    NSStatusItem* mStatusItem;
+  };
+
+  nsClassHashtable<nsISupportsHashKey, StatusItem> mItems;
+};
+
+#endif // nsSystemStatusBarCocoa_h_
new file mode 100644
--- /dev/null
+++ b/widget/cocoa/nsSystemStatusBarCocoa.mm
@@ -0,0 +1,63 @@
+/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
+/* 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/. */
+
+#import <Cocoa/Cocoa.h>
+
+#include "nsComponentManagerUtils.h"
+#include "nsSystemStatusBarCocoa.h"
+#include "nsStandaloneNativeMenu.h"
+#include "nsObjCExceptions.h"
+#include "nsIDOMElement.h"
+
+NS_IMPL_ISUPPORTS(nsSystemStatusBarCocoa, nsISystemStatusBar)
+
+NS_IMETHODIMP
+nsSystemStatusBarCocoa::AddItem(nsIDOMElement* aDOMElement)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  nsRefPtr<nsStandaloneNativeMenu> menu = new nsStandaloneNativeMenu();
+  nsresult rv = menu->Init(aDOMElement);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  nsCOMPtr<nsISupports> keyPtr = aDOMElement;
+  mItems.Put(keyPtr, new StatusItem(menu));
+
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP
+nsSystemStatusBarCocoa::RemoveItem(nsIDOMElement* aDOMElement)
+{
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  mItems.Remove(aDOMElement);
+
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+nsSystemStatusBarCocoa::StatusItem::StatusItem(nsStandaloneNativeMenu* aMenu)
+  : mMenu(aMenu)
+{
+  NSMenu* nativeMenu = nil;
+  mMenu->GetNativeMenu(reinterpret_cast<void**>(&nativeMenu));
+
+  mStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
+  [mStatusItem setMenu:nativeMenu];
+  [mStatusItem setHighlightMode:YES];
+}
+
+nsSystemStatusBarCocoa::StatusItem::~StatusItem()
+{
+  [[NSStatusBar systemStatusBar] removeStatusItem:mStatusItem];
+  [mStatusItem release];
+  mStatusItem = nil;
+}
--- a/widget/cocoa/nsWidgetFactory.mm
+++ b/widget/cocoa/nsWidgetFactory.mm
@@ -74,16 +74,19 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsNativeT
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacDockSupport)
 
 #include "nsMacWebAppUtils.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacWebAppUtils)
 
 #include "nsStandaloneNativeMenu.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsStandaloneNativeMenu)
 
+#include "nsSystemStatusBarCocoa.h"
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsSystemStatusBarCocoa)
+
 #include "GfxInfo.h"
 namespace mozilla {
 namespace widget {
 // This constructor should really be shared with all platforms.
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(GfxInfo, Init)
 }
 }
 
@@ -107,16 +110,17 @@ NS_DEFINE_NAMED_CID(NS_PRINTSESSION_CID)
 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_MACWEBAPPUTILS_CID);
 NS_DEFINE_NAMED_CID(NS_STANDALONENATIVEMENU_CID);
+NS_DEFINE_NAMED_CID(NS_MACSYSTEMSTATUSBAR_CID);
 NS_DEFINE_NAMED_CID(NS_GFXINFO_CID);
 
 static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
   { &kNS_WINDOW_CID, false, NULL, nsCocoaWindowConstructor },
   { &kNS_POPUP_CID, false, NULL, nsCocoaWindowConstructor },
   { &kNS_CHILD_CID, false, NULL, nsChildViewConstructor },
   { &kNS_FILEPICKER_CID, false, NULL, nsFilePickerConstructor,
     mozilla::Module::MAIN_PROCESS_ONLY },
@@ -144,16 +148,17 @@ static const mozilla::Module::CIDEntry k
   { &kNS_PRINTDIALOGSERVICE_CID, false, NULL, nsPrintDialogServiceXConstructor,
     mozilla::Module::MAIN_PROCESS_ONLY },
   { &kNS_IDLE_SERVICE_CID, false, NULL, nsIdleServiceXConstructor },
   { &kNS_SYSTEMALERTSSERVICE_CID, false, NULL, OSXNotificationCenterConstructor },
   { &kNS_NATIVEMENUSERVICE_CID, false, NULL, nsNativeMenuServiceXConstructor },
   { &kNS_MACDOCKSUPPORT_CID, false, NULL, nsMacDockSupportConstructor },
   { &kNS_MACWEBAPPUTILS_CID, false, NULL, nsMacWebAppUtilsConstructor },
   { &kNS_STANDALONENATIVEMENU_CID, false, NULL, nsStandaloneNativeMenuConstructor },
+  { &kNS_MACSYSTEMSTATUSBAR_CID, false, NULL, nsSystemStatusBarCocoaConstructor },
   { &kNS_GFXINFO_CID, false, NULL, mozilla::widget::GfxInfoConstructor },
   { NULL }
 };
 
 static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
   { "@mozilla.org/widgets/window/mac;1", &kNS_WINDOW_CID },
   { "@mozilla.org/widgets/popup/mac;1", &kNS_POPUP_CID },
   { "@mozilla.org/widgets/childwindow/mac;1", &kNS_CHILD_CID },
@@ -183,16 +188,17 @@ static const mozilla::Module::ContractID
   { NS_PRINTDIALOGSERVICE_CONTRACTID, &kNS_PRINTDIALOGSERVICE_CID,
     mozilla::Module::MAIN_PROCESS_ONLY },
   { "@mozilla.org/widget/idleservice;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/mac-web-app-utils;1", &kNS_MACWEBAPPUTILS_CID },
   { "@mozilla.org/widget/standalonenativemenu;1", &kNS_STANDALONENATIVEMENU_CID },
+  { "@mozilla.org/widget/macsystemstatusbar;1", &kNS_MACSYSTEMSTATUSBAR_CID },
   { "@mozilla.org/gfx/info;1", &kNS_GFXINFO_CID },
   { NULL }
 };
 
 static void
 nsWidgetCocoaModuleDtor()
 {
   NativeKeyBindings::Shutdown();
--- a/widget/nsWidgetsCID.h
+++ b/widget/nsWidgetsCID.h
@@ -61,16 +61,21 @@
 { 0x1F39AE50, 0xB6A0, 0x4B37,                         \
   { 0x90, 0xF4, 0x60, 0xAF, 0x61, 0x41, 0x93, 0xD8 }}
 
 // {2451BAED-8DC3-46D9-9E30-96E1BAA03666}
 #define NS_MACDOCKSUPPORT_CID \
 { 0x2451BAED, 0x8DC3, 0x46D9, \
   { 0x9E, 0x30, 0x96, 0xE1, 0xBA, 0xA0, 0x36, 0x66 } }
 
+// {b6e1a890-b2b8-4883-a65f-9476f6185313}
+#define NS_MACSYSTEMSTATUSBAR_CID \
+{ 0xb6e1a890, 0xb2b8, 0x4883, \
+  { 0xa6, 0x5f, 0x94, 0x76, 0xf6, 0x18, 0x53, 0x13 } }
+
 //-----------------------------------------------------------
 //Drag & Drop & Clipboard
 //-----------------------------------------------------------
 // {8B5314BB-DB01-11d2-96CE-0060B0FB9956}
 #define NS_DRAGSERVICE_CID      \
 { 0x8b5314bb, 0xdb01, 0x11d2, { 0x96, 0xce, 0x0, 0x60, 0xb0, 0xfb, 0x99, 0x56 } }
 
 // {8B5314BC-DB01-11d2-96CE-0060B0FB9956}