Bug 1835529 [wpt PR 40276] - Update wpt metadata, a=testonly
[gecko.git] / dom / xul / XULMenuElement.cpp
blob928e850012877bb98806a236c1ba486f48163bf9
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 "mozilla/dom/XULMenuElement.h"
8 #include "mozilla/StaticAnalysisFunctions.h"
9 #include "mozilla/dom/XULButtonElement.h"
10 #include "mozilla/dom/XULMenuElementBinding.h"
11 #include "mozilla/dom/XULPopupElement.h"
12 #include "mozilla/dom/KeyboardEvent.h"
13 #include "mozilla/dom/KeyboardEventBinding.h"
14 #include "nsXULPopupManager.h"
15 #include "nsMenuPopupFrame.h"
17 namespace mozilla::dom {
19 JSObject* XULMenuElement::WrapNode(JSContext* aCx,
20 JS::Handle<JSObject*> aGivenProto) {
21 return XULMenuElement_Binding::Wrap(aCx, this, aGivenProto);
24 Element* XULMenuElement::GetActiveMenuChild() {
25 RefPtr popup = GetMenuPopupContent();
26 return popup ? popup->GetActiveMenuChild() : nullptr;
29 void XULMenuElement::SetActiveMenuChild(Element* aChild) {
30 RefPtr popup = GetMenuPopupContent();
31 if (NS_WARN_IF(!popup)) {
32 return;
35 if (!aChild) {
36 popup->SetActiveMenuChild(nullptr);
37 return;
39 auto* button = XULButtonElement::FromNode(aChild);
40 if (NS_WARN_IF(!button) || NS_WARN_IF(!button->IsMenu())) {
41 return;
43 // KnownLive because it's aChild.
44 popup->SetActiveMenuChild(MOZ_KnownLive(button));
47 bool XULButtonElement::HandleKeyPress(KeyboardEvent& keyEvent) {
48 RefPtr<nsXULPopupManager> pm = nsXULPopupManager::GetInstance();
49 if (!pm) {
50 return false;
53 // if event has already been handled, bail
54 if (keyEvent.DefaultPrevented()) {
55 return false;
58 if (keyEvent.IsMenuAccessKeyPressed()) {
59 return false;
62 nsMenuPopupFrame* popupFrame = GetMenuPopup(FlushType::Frames);
63 if (NS_WARN_IF(!popupFrame)) {
64 return false;
67 uint32_t keyCode = keyEvent.KeyCode();
68 switch (keyCode) {
69 case KeyboardEvent_Binding::DOM_VK_UP:
70 case KeyboardEvent_Binding::DOM_VK_DOWN:
71 case KeyboardEvent_Binding::DOM_VK_HOME:
72 case KeyboardEvent_Binding::DOM_VK_END: {
73 nsNavigationDirection theDirection;
74 theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
75 return pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
77 default:
78 return pm->HandleShortcutNavigation(keyEvent, popupFrame);
82 } // namespace mozilla::dom