Bug 1793629 - Implement attention indicator for the unified extensions button, r...
[gecko.git] / layout / xul / nsMenuBarFrame.cpp
blobbae6d7050fee819f0e49000bbbbe28159688d730
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMenuBarFrame.h"
8 #include "nsIContent.h"
9 #include "nsAtom.h"
10 #include "nsPresContext.h"
11 #include "nsCSSRendering.h"
12 #include "nsNameSpaceManager.h"
13 #include "nsGkAtoms.h"
14 #include "nsMenuFrame.h"
15 #include "nsMenuPopupFrame.h"
16 #include "nsUnicharUtils.h"
17 #include "nsPIDOMWindow.h"
18 #include "nsIInterfaceRequestorUtils.h"
19 #include "nsCSSFrameConstructor.h"
20 #ifdef XP_WIN
21 # include "nsISound.h"
22 # include "nsWidgetsCID.h"
23 #endif
24 #include "nsUTF8Utils.h"
25 #include "mozilla/ComputedStyle.h"
26 #include "mozilla/PresShell.h"
27 #include "mozilla/TextEvents.h"
28 #include "mozilla/dom/Document.h"
29 #include "mozilla/dom/Event.h"
30 #include "mozilla/dom/KeyboardEvent.h"
32 using namespace mozilla;
33 using mozilla::dom::KeyboardEvent;
36 // NS_NewMenuBarFrame
38 // Wrapper for creating a new menu Bar container
40 nsIFrame* NS_NewMenuBarFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
41 return new (aPresShell) nsMenuBarFrame(aStyle, aPresShell->GetPresContext());
44 NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
46 NS_QUERYFRAME_HEAD(nsMenuBarFrame)
47 NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
48 NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
51 // nsMenuBarFrame cntr
53 nsMenuBarFrame::nsMenuBarFrame(ComputedStyle* aStyle,
54 nsPresContext* aPresContext)
55 : nsBoxFrame(aStyle, aPresContext, kClassID),
56 mStayActive(false),
57 mIsActive(false),
58 mActiveByKeyboard(false),
59 mCurrentMenu(nullptr) {} // cntr
61 void nsMenuBarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
62 nsIFrame* aPrevInFlow) {
63 nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
65 // Create the menu bar listener.
66 mMenuBarListener = new nsMenuBarListener(this, aContent);
69 NS_IMETHODIMP
70 nsMenuBarFrame::SetActive(bool aActiveFlag) {
71 // If the activity is not changed, there is nothing to do.
72 if (mIsActive == aActiveFlag) return NS_OK;
74 if (!aActiveFlag) {
75 // Don't deactivate when switching between menus on the menubar.
76 if (mStayActive) return NS_OK;
78 // if there is a request to deactivate the menu bar, check to see whether
79 // there is a menu popup open for the menu bar. In this case, don't
80 // deactivate the menu bar.
81 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
82 if (pm && pm->IsPopupOpenForMenuParent(this)) return NS_OK;
85 mIsActive = aActiveFlag;
86 if (mIsActive) {
87 InstallKeyboardNavigator();
88 } else {
89 mActiveByKeyboard = false;
90 RemoveKeyboardNavigator();
93 constexpr auto active = u"DOMMenuBarActive"_ns;
94 constexpr auto inactive = u"DOMMenuBarInactive"_ns;
96 FireDOMEvent(mIsActive ? active : inactive, mContent);
98 return NS_OK;
101 nsMenuFrame* nsMenuBarFrame::ToggleMenuActiveState() {
102 if (mIsActive) {
103 // Deactivate the menu bar
104 SetActive(false);
105 if (mCurrentMenu) {
106 nsMenuFrame* closeframe = mCurrentMenu;
107 closeframe->SelectMenu(false);
108 mCurrentMenu = nullptr;
109 return closeframe;
111 } else {
112 // if the menu bar is already selected (eg. mouseover), deselect it
113 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
115 // Set the active menu to be the top left item (e.g., the File menu).
116 // We use an attribute called "menuactive" to track the current
117 // active menu.
118 nsMenuFrame* firstFrame =
119 nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
120 if (firstFrame) {
121 // Activate the menu bar
122 SetActive(true);
123 firstFrame->SelectMenu(true);
125 // Track this item for keyboard navigation.
126 mCurrentMenu = firstFrame;
130 return nullptr;
133 nsMenuFrame* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent,
134 bool aPeek) {
135 uint32_t charCode = aKeyEvent->CharCode();
137 AutoTArray<uint32_t, 10> accessKeys;
138 WidgetKeyboardEvent* nativeKeyEvent =
139 aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
140 if (nativeKeyEvent) {
141 nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
143 if (accessKeys.IsEmpty() && charCode) accessKeys.AppendElement(charCode);
145 if (accessKeys.IsEmpty())
146 return nullptr; // no character was pressed so just return
148 // Enumerate over our list of frames.
149 nsContainerFrame* immediateParent =
150 nsXULPopupManager::ImmediateParentFrame(this);
152 // Find a most preferred accesskey which should be returned.
153 nsIFrame* foundMenu = nullptr;
154 size_t foundIndex = accessKeys.NoIndex;
155 nsIFrame* currFrame = immediateParent->PrincipalChildList().FirstChild();
157 while (currFrame) {
158 nsIContent* current = currFrame->GetContent();
160 // See if it's a menu item.
161 if (nsXULPopupManager::IsValidMenuItem(current, false)) {
162 // Get the shortcut attribute.
163 nsAutoString shortcutKey;
164 if (current->IsElement()) {
165 current->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey,
166 shortcutKey);
168 if (!shortcutKey.IsEmpty()) {
169 ToLowerCase(shortcutKey);
170 const char16_t* start = shortcutKey.BeginReading();
171 const char16_t* end = shortcutKey.EndReading();
172 uint32_t ch = UTF16CharEnumerator::NextChar(&start, end);
173 size_t index = accessKeys.IndexOf(ch);
174 if (index != accessKeys.NoIndex &&
175 (foundIndex == accessKeys.NoIndex || index < foundIndex)) {
176 foundMenu = currFrame;
177 foundIndex = index;
181 currFrame = currFrame->GetNextSibling();
183 if (foundMenu) {
184 return do_QueryFrame(foundMenu);
187 // didn't find a matching menu item
188 #ifdef XP_WIN
189 if (!aPeek) {
190 // behavior on Windows - this item is on the menu bar, beep and deactivate
191 // the menu bar
192 if (mIsActive) {
193 nsCOMPtr<nsISound> soundInterface = do_GetService("@mozilla.org/sound;1");
194 if (soundInterface) soundInterface->Beep();
197 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
198 if (pm) {
199 nsIFrame* popup = pm->GetTopPopup(ePopupTypeMenu);
200 if (popup) pm->HidePopup(popup->GetContent(), true, true, true, false);
203 SetCurrentMenuItem(nullptr);
204 SetActive(false);
207 #endif // #ifdef XP_WIN
209 return nullptr;
212 /* virtual */
213 nsMenuFrame* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu; }
215 NS_IMETHODIMP
216 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem) {
217 if (mCurrentMenu == aMenuItem) return NS_OK;
219 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
221 if (aMenuItem) aMenuItem->SelectMenu(true);
223 mCurrentMenu = aMenuItem;
225 return NS_OK;
228 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
229 mCurrentMenu->SelectMenu(false);
230 mCurrentMenu = nullptr;
233 class nsMenuBarSwitchMenu : public Runnable {
234 public:
235 nsMenuBarSwitchMenu(nsIContent* aMenuBar, nsIContent* aOldMenu,
236 nsIContent* aNewMenu, bool aSelectFirstItem)
237 : mozilla::Runnable("nsMenuBarSwitchMenu"),
238 mMenuBar(aMenuBar),
239 mOldMenu(aOldMenu),
240 mNewMenu(aNewMenu),
241 mSelectFirstItem(aSelectFirstItem) {}
243 NS_IMETHOD Run() override {
244 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
245 if (!pm) return NS_ERROR_UNEXPECTED;
247 // if switching from one menu to another, set a flag so that the call to
248 // HidePopup doesn't deactivate the menubar when the first menu closes.
249 nsMenuBarFrame* menubar = nullptr;
250 if (mOldMenu && mNewMenu) {
251 menubar = do_QueryFrame(mMenuBar->GetPrimaryFrame());
252 if (menubar) menubar->SetStayActive(true);
255 if (mOldMenu) {
256 AutoWeakFrame weakMenuBar(menubar);
257 pm->HidePopup(mOldMenu, false, false, false, false);
258 // clear the flag again
259 if (mNewMenu && weakMenuBar.IsAlive()) menubar->SetStayActive(false);
262 if (mNewMenu) {
263 pm->ShowMenu(mNewMenu, mSelectFirstItem);
266 return NS_OK;
269 private:
270 nsCOMPtr<nsIContent> mMenuBar;
271 nsCOMPtr<nsIContent> mOldMenu;
272 nsCOMPtr<nsIContent> mNewMenu;
273 bool mSelectFirstItem;
276 NS_IMETHODIMP
277 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem, bool aSelectFirstItem,
278 bool aFromKey) {
279 if (mCurrentMenu == aMenuItem) return NS_OK;
281 // check if there's an open context menu, we ignore this
282 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
283 if (pm && pm->HasContextMenu(nullptr)) return NS_OK;
285 nsIContent* aOldMenu = nullptr;
286 nsIContent* aNewMenu = nullptr;
288 // Unset the current child.
289 bool wasOpen = false;
290 if (mCurrentMenu) {
291 wasOpen = mCurrentMenu->IsOpen();
292 mCurrentMenu->SelectMenu(false);
293 if (wasOpen) {
294 nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
295 if (popupFrame) aOldMenu = popupFrame->GetContent();
299 // set to null first in case the IsAlive check below returns false
300 mCurrentMenu = nullptr;
302 // Set the new child.
303 if (aMenuItem) {
304 nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
305 aMenuItem->SelectMenu(true);
306 mCurrentMenu = aMenuItem;
307 if (wasOpen && !aMenuItem->IsDisabled()) aNewMenu = content;
310 // use an event so that hiding and showing can be done synchronously, which
311 // avoids flickering
312 nsCOMPtr<nsIRunnable> event = new nsMenuBarSwitchMenu(
313 GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
314 return mContent->OwnerDoc()->Dispatch(TaskCategory::Other, event.forget());
317 nsMenuFrame* nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent) {
318 if (!mCurrentMenu) return nullptr;
320 if (mCurrentMenu->IsOpen()) return mCurrentMenu->Enter(aEvent);
322 return mCurrentMenu;
325 bool nsMenuBarFrame::MenuClosed() {
326 SetActive(false);
327 if (!mIsActive && mCurrentMenu) {
328 mCurrentMenu->SelectMenu(false);
329 mCurrentMenu = nullptr;
330 return true;
332 return false;
335 void nsMenuBarFrame::InstallKeyboardNavigator() {
336 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
337 if (pm) pm->SetActiveMenuBar(this, true);
340 void nsMenuBarFrame::RemoveKeyboardNavigator() {
341 if (!mIsActive) {
342 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
343 if (pm) pm->SetActiveMenuBar(this, false);
347 void nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot,
348 PostDestroyData& aPostDestroyData) {
349 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
350 if (pm) pm->SetActiveMenuBar(this, false);
352 mMenuBarListener->OnDestroyMenuBarFrame();
353 mMenuBarListener = nullptr;
355 nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);