1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
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/. */
6 #import <Cocoa/Cocoa.h>
8 #include "nsComponentManagerUtils.h"
9 #include "nsSystemStatusBarCocoa.h"
10 #include "NativeMenuMac.h"
11 #include "nsObjCExceptions.h"
12 #include "mozilla/dom/Element.h"
14 using mozilla::dom::Element;
15 using mozilla::widget::NativeMenuMac;
17 NS_IMPL_ISUPPORTS(nsSystemStatusBarCocoa, nsISystemStatusBar)
20 nsSystemStatusBarCocoa::AddItem(Element* aElement) {
21 if (!aElement->IsAnyOfXULElements(nsGkAtoms::menu, nsGkAtoms::menupopup)) {
22 return NS_ERROR_FAILURE;
25 RefPtr<NativeMenuMac> menu = new NativeMenuMac(aElement);
27 nsCOMPtr<nsISupports> keyPtr = aElement;
28 mItems.InsertOrUpdate(keyPtr, mozilla::MakeUnique<StatusItem>(menu));
34 nsSystemStatusBarCocoa::RemoveItem(Element* aElement) {
35 mItems.Remove(aElement);
39 nsSystemStatusBarCocoa::StatusItem::StatusItem(NativeMenuMac* aMenu)
41 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
43 MOZ_COUNT_CTOR(nsSystemStatusBarCocoa::StatusItem);
45 mStatusItem = [[NSStatusBar.systemStatusBar
46 statusItemWithLength:NSSquareStatusItemLength] retain];
47 mStatusItem.menu = mMenu->NativeNSMenu();
48 mStatusItem.highlightMode = YES;
50 // We want the status item to get its image from menu item that mMenu was
51 // initialized with. Icon loads are asynchronous, so we need to let the menu
52 // know about the item so that it can update its icon as soon as it has
54 mMenu->SetContainerStatusBarItem(mStatusItem);
56 NS_OBJC_END_TRY_ABORT_BLOCK;
59 nsSystemStatusBarCocoa::StatusItem::~StatusItem() {
60 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
62 mMenu->SetContainerStatusBarItem(nil);
63 [NSStatusBar.systemStatusBar removeStatusItem:mStatusItem];
64 [mStatusItem release];
67 MOZ_COUNT_DTOR(nsSystemStatusBarCocoa::StatusItem);
69 NS_OBJC_END_TRY_ABORT_BLOCK;