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) : mMenu(aMenu) {
40 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
42 MOZ_COUNT_CTOR(nsSystemStatusBarCocoa::StatusItem);
45 [[NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength] retain];
46 mStatusItem.menu = mMenu->NativeNSMenu();
47 mStatusItem.highlightMode = YES;
49 // We want the status item to get its image from menu item that mMenu was
50 // initialized with. Icon loads are asynchronous, so we need to let the menu
51 // know about the item so that it can update its icon as soon as it has
53 mMenu->SetContainerStatusBarItem(mStatusItem);
55 NS_OBJC_END_TRY_ABORT_BLOCK;
58 nsSystemStatusBarCocoa::StatusItem::~StatusItem() {
59 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
61 mMenu->SetContainerStatusBarItem(nil);
62 [NSStatusBar.systemStatusBar removeStatusItem:mStatusItem];
63 [mStatusItem release];
66 MOZ_COUNT_DTOR(nsSystemStatusBarCocoa::StatusItem);
68 NS_OBJC_END_TRY_ABORT_BLOCK;