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 "ShortcutKeys.h"
8 #include "mozilla/KeyEventHandler.h"
9 #include "nsContentUtils.h"
11 #include "mozilla/TextEvents.h"
15 NS_IMPL_ISUPPORTS(ShortcutKeys
, nsIObserver
);
17 StaticRefPtr
<ShortcutKeys
> ShortcutKeys::sInstance
;
19 ShortcutKeys::ShortcutKeys()
20 : mBrowserHandlers(nullptr),
21 mEditorHandlers(nullptr),
22 mInputHandlers(nullptr),
23 mTextAreaHandlers(nullptr) {
24 MOZ_ASSERT(!sInstance
, "Attempt to instantiate a second ShortcutKeys.");
25 nsContentUtils::RegisterShutdownObserver(this);
28 ShortcutKeys::~ShortcutKeys() {
29 delete mBrowserHandlers
;
30 delete mEditorHandlers
;
31 delete mInputHandlers
;
32 delete mTextAreaHandlers
;
35 nsresult
ShortcutKeys::Observe(nsISupports
* aSubject
, const char* aTopic
,
36 const char16_t
* aData
) {
37 // Clear our strong reference so we can clean up.
38 ShortcutKeys::Shutdown();
42 void ShortcutKeys::Shutdown() { sInstance
= nullptr; }
45 KeyEventHandler
* ShortcutKeys::GetHandlers(HandlerType aType
) {
47 sInstance
= new ShortcutKeys();
50 return sInstance
->EnsureHandlers(aType
);
54 nsAtom
* ShortcutKeys::ConvertEventToDOMEventType(
55 const WidgetKeyboardEvent
* aWidgetKeyboardEvent
) {
56 switch (aWidgetKeyboardEvent
->mMessage
) {
58 return nsGkAtoms::keydown
;
60 return nsGkAtoms::keyup
;
61 // eAccessKeyNotFound event is always created from eKeyPress event and
62 // the original eKeyPress event has stopped its propagation before
63 // dispatched into the DOM tree in this process and not matched with remote
64 // content's access keys. So, we should treat it as an eKeyPress event and
65 // execute a command if it's registered as a shortcut key.
67 case eAccessKeyNotFound
:
68 return nsGkAtoms::keypress
;
70 MOZ_ASSERT_UNREACHABLE(
71 "All event messages relating to shortcut keys should be handled");
76 KeyEventHandler
* ShortcutKeys::EnsureHandlers(HandlerType aType
) {
77 ShortcutKeyData
* keyData
;
78 KeyEventHandler
** cache
;
81 case HandlerType::eBrowser
:
82 keyData
= &sBrowserHandlers
[0];
83 cache
= &mBrowserHandlers
;
85 case HandlerType::eEditor
:
86 keyData
= &sEditorHandlers
[0];
87 cache
= &mEditorHandlers
;
89 case HandlerType::eInput
:
90 keyData
= &sInputHandlers
[0];
91 cache
= &mInputHandlers
;
93 case HandlerType::eTextArea
:
94 keyData
= &sTextAreaHandlers
[0];
95 cache
= &mTextAreaHandlers
;
98 MOZ_ASSERT(false, "Unknown handler type requested.");
105 KeyEventHandler
* lastHandler
= nullptr;
106 while (keyData
->event
) {
107 KeyEventHandler
* handler
= new KeyEventHandler(keyData
);
109 lastHandler
->SetNextHandler(handler
);
113 lastHandler
= handler
;
120 } // namespace mozilla