Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / cocoa / nsMenuGroupOwnerX.h
blobda859d14f3a871ec498d8dc52c49b1425fc6592d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 #ifndef nsMenuGroupOwnerX_h_
7 #define nsMenuGroupOwnerX_h_
9 #import <Cocoa/Cocoa.h>
11 #include "mozilla/WeakPtr.h"
13 #include "nsStubMutationObserver.h"
14 #include "nsHashKeys.h"
15 #include "nsIObserver.h"
16 #include "nsMenuBarX.h"
17 #include "nsTHashMap.h"
18 #include "nsString.h"
20 class nsMenuItemX;
21 class nsChangeObserver;
22 class nsIWidget;
23 class nsIContent;
25 @class MOZMenuItemRepresentedObject;
27 // Fixed command IDs that work even without a JS listener, for our fallback menu
28 // bar. Dynamic command IDs start counting from eCommand_ID_Last.
29 enum {
30 eCommand_ID_About = 1,
31 eCommand_ID_Prefs = 2,
32 eCommand_ID_Quit = 3,
33 eCommand_ID_HideApp = 4,
34 eCommand_ID_HideOthers = 5,
35 eCommand_ID_ShowAll = 6,
36 eCommand_ID_Update = 7,
37 eCommand_ID_TouchBar = 8,
38 eCommand_ID_Account = 9,
39 eCommand_ID_Last = 10
42 // The menu group owner observes DOM mutations, notifies registered
43 // nsChangeObservers, and manages command registration. There is one owner per
44 // menubar, and one per standalone native menu.
45 class nsMenuGroupOwnerX : public nsMultiMutationObserver, public nsIObserver {
46 public:
47 // Both parameters can be null.
48 nsMenuGroupOwnerX(mozilla::dom::Element* aElement,
49 nsMenuBarX* aMenuBarIfMenuBar);
51 void RegisterForContentChanges(nsIContent* aContent,
52 nsChangeObserver* aMenuObject);
53 void UnregisterForContentChanges(nsIContent* aContent);
54 uint32_t RegisterForCommand(nsMenuItemX* aMenuItem);
55 void UnregisterCommand(uint32_t aCommandID);
56 nsMenuItemX* GetMenuItemForCommandID(uint32_t aCommandID);
58 void RegisterForLocaleChanges();
59 void UnregisterForLocaleChanges();
61 // The representedObject that's used for all menu items under this menu group
62 // owner.
63 MOZMenuItemRepresentedObject* GetRepresentedObject() {
64 return mRepresentedObject;
67 // If this is the group owner for a menubar, return the menubar, otherwise
68 // nullptr.
69 nsMenuBarX* GetMenuBar() { return mMenuBar.get(); }
71 NS_DECL_ISUPPORTS
72 NS_DECL_NSIOBSERVER
73 NS_DECL_NSIMUTATIONOBSERVER
75 protected:
76 virtual ~nsMenuGroupOwnerX();
78 nsChangeObserver* LookupContentChangeObserver(nsIContent* aContent);
80 RefPtr<nsIContent> mContent;
82 // Unique command id (per menu-bar) to give to next item that asks.
83 uint32_t mCurrentCommandID = eCommand_ID_Last;
85 // stores observers for content change notification
86 nsTHashMap<nsPtrHashKey<nsIContent>, nsChangeObserver*>
87 mContentToObserverTable;
89 // stores mapping of command IDs to menu objects
90 nsTHashMap<nsUint32HashKey, nsMenuItemX*> mCommandToMenuObjectTable;
92 MOZMenuItemRepresentedObject* mRepresentedObject = nil; // [strong]
93 mozilla::WeakPtr<nsMenuBarX> mMenuBar;
96 @interface MOZMenuItemRepresentedObject : NSObject
97 - (id)initWithMenuGroupOwner:(nsMenuGroupOwnerX*)aMenuGroupOwner;
98 - (void)setMenuGroupOwner:(nsMenuGroupOwnerX*)aMenuGroupOwner;
99 - (nsMenuGroupOwnerX*)menuGroupOwner;
100 @end
102 #endif // nsMenuGroupOwner_h_