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 "nsIMutationObserver.h"
14 #include "nsHashKeys.h"
15 #include "nsIObserver.h"
16 #include "nsTHashMap.h"
21 class nsChangeObserver
;
25 @
class MOZMenuItemRepresentedObject
;
27 // Fixed command IDs that work even without a JS listener, for our fallback menu bar.
28 // Dynamic command IDs start counting from eCommand_ID_Last.
30 eCommand_ID_About
= 1,
31 eCommand_ID_Prefs
= 2,
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,
42 // The menu group owner observes DOM mutations, notifies registered nsChangeObservers, and manages
43 // command registration.
44 // There is one owner per menubar, and one per standalone native menu.
45 class nsMenuGroupOwnerX
: public nsIMutationObserver
, public nsIObserver
{
47 // Both parameters can be null.
48 nsMenuGroupOwnerX(mozilla::dom::Element
* aElement
, nsMenuBarX
* aMenuBarIfMenuBar
);
50 void RegisterForContentChanges(nsIContent
* aContent
, nsChangeObserver
* aMenuObject
);
51 void UnregisterForContentChanges(nsIContent
* aContent
);
52 uint32_t RegisterForCommand(nsMenuItemX
* aMenuItem
);
53 void UnregisterCommand(uint32_t aCommandID
);
54 nsMenuItemX
* GetMenuItemForCommandID(uint32_t aCommandID
);
56 void RegisterForLocaleChanges();
57 void UnregisterForLocaleChanges();
59 // The representedObject that's used for all menu items under this menu group owner.
60 MOZMenuItemRepresentedObject
* GetRepresentedObject() { return mRepresentedObject
; }
62 // If this is the group owner for a menubar, return the menubar, otherwise nullptr.
63 nsMenuBarX
* GetMenuBar() { return mMenuBar
.get(); }
67 NS_DECL_NSIMUTATIONOBSERVER
70 virtual ~nsMenuGroupOwnerX();
72 nsChangeObserver
* LookupContentChangeObserver(nsIContent
* aContent
);
74 RefPtr
<nsIContent
> mContent
;
76 // Unique command id (per menu-bar) to give to next item that asks.
77 uint32_t mCurrentCommandID
= eCommand_ID_Last
;
79 // stores observers for content change notification
80 nsTHashMap
<nsPtrHashKey
<nsIContent
>, nsChangeObserver
*> mContentToObserverTable
;
82 // stores mapping of command IDs to menu objects
83 nsTHashMap
<nsUint32HashKey
, nsMenuItemX
*> mCommandToMenuObjectTable
;
85 MOZMenuItemRepresentedObject
* mRepresentedObject
= nil
; // [strong]
86 mozilla::WeakPtr
<nsMenuBarX
> mMenuBar
;
89 @interface MOZMenuItemRepresentedObject
: NSObject
90 - (id
)initWithMenuGroupOwner
:(nsMenuGroupOwnerX
*)aMenuGroupOwner
;
91 - (void)setMenuGroupOwner
:(nsMenuGroupOwnerX
*)aMenuGroupOwner
;
92 - (nsMenuGroupOwnerX
*)menuGroupOwner
;
95 #endif // nsMenuGroupOwner_h_