Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / cocoa / nsSystemStatusBarCocoa.mm
blobf46ee0ce3e9c2468eb4761b26ae4fd07827b73b9
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)
19 NS_IMETHODIMP
20 nsSystemStatusBarCocoa::AddItem(Element* aElement) {
21   if (!aElement->IsAnyOfXULElements(nsGkAtoms::menu, nsGkAtoms::menupopup)) {
22     return NS_ERROR_FAILURE;
23   }
25   RefPtr<NativeMenuMac> menu = new NativeMenuMac(aElement);
27   nsCOMPtr<nsISupports> keyPtr = aElement;
28   mItems.InsertOrUpdate(keyPtr, mozilla::MakeUnique<StatusItem>(menu));
30   return NS_OK;
33 NS_IMETHODIMP
34 nsSystemStatusBarCocoa::RemoveItem(Element* aElement) {
35   mItems.Remove(aElement);
36   return NS_OK;
39 nsSystemStatusBarCocoa::StatusItem::StatusItem(NativeMenuMac* aMenu)
40     : mMenu(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
53   // loaded.
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];
65   mStatusItem = nil;
67   MOZ_COUNT_DTOR(nsSystemStatusBarCocoa::StatusItem);
69   NS_OBJC_END_TRY_ABORT_BLOCK;