gecko-dev/dom/events/KeyEventHandler.h
Dave Townsend df895249c6 Bug 1550058: Move most keyboard shortcut handling out of XBL. r=masayuki
Most of our keyboard shortcut handling is handled by nsXBLWindowKeyHandler along
with nsXBLPrototypeHandler. With the impending removal of XBL this needs to
change.

This patch moves nsXBLWindowKeyHandler to dom/events/GlobalKeyListener and copies
nsXBLPrototypeHandler to dom/events/KeyEventHandler. Windows, text elements and
XUL <keyset> are changed to use the new copies and anything unnecessary for
those is stripped out.

XBL handler elements still remain using the existing nsXBLPrototypeHandler path.
Some of the code is ripped out there to make it compile. There is probably a
lot more that can be removed but since the whole of XBL is likely gone soon I'm
not sure it is worth cleaning that up much.

Differential Revision: https://phabricator.services.mozilla.com/D42336

--HG--
rename : dom/xbl/nsXBLWindowKeyHandler.cpp => dom/events/GlobalKeyListener.cpp
rename : dom/xbl/nsXBLWindowKeyHandler.h => dom/events/GlobalKeyListener.h
rename : dom/xbl/nsXBLPrototypeHandler.cpp => dom/events/KeyEventHandler.cpp
rename : dom/xbl/nsXBLPrototypeHandler.h => dom/events/KeyEventHandler.h
rename : dom/xbl/builtin/ShortcutKeyDefinitionsForBrowserCommon.h => dom/events/ShortcutKeyDefinitionsForBrowserCommon.h
rename : dom/xbl/builtin/ShortcutKeyDefinitionsForEditorCommon.h => dom/events/ShortcutKeyDefinitionsForEditorCommon.h
rename : dom/xbl/builtin/ShortcutKeyDefinitionsForInputCommon.h => dom/events/ShortcutKeyDefinitionsForInputCommon.h
rename : dom/xbl/builtin/ShortcutKeyDefinitionsForInputCommon.h => dom/events/ShortcutKeyDefinitionsForTextAreaCommon.h
rename : dom/xbl/builtin/ShortcutKeys.cpp => dom/events/ShortcutKeys.cpp
rename : dom/xbl/builtin/ShortcutKeys.h => dom/events/ShortcutKeys.h
rename : dom/xbl/builtin/android/ShortcutKeyDefinitions.cpp => dom/events/android/ShortcutKeyDefinitions.cpp
rename : dom/xbl/builtin/android/moz.build => dom/events/android/moz.build
rename : dom/xbl/builtin/emacs/ShortcutKeyDefinitions.cpp => dom/events/emacs/ShortcutKeyDefinitions.cpp
rename : dom/xbl/builtin/android/moz.build => dom/events/emacs/moz.build
rename : dom/xbl/builtin/mac/ShortcutKeyDefinitions.cpp => dom/events/mac/ShortcutKeyDefinitions.cpp
rename : dom/xbl/builtin/android/moz.build => dom/events/mac/moz.build
rename : dom/xbl/builtin/unix/ShortcutKeyDefinitions.cpp => dom/events/unix/ShortcutKeyDefinitions.cpp
rename : dom/xbl/builtin/android/moz.build => dom/events/unix/moz.build
rename : dom/xbl/builtin/win/ShortcutKeyDefinitions.cpp => dom/events/win/ShortcutKeyDefinitions.cpp
rename : dom/xbl/builtin/android/moz.build => dom/events/win/moz.build
extra : moz-landing-system : lando
2019-09-06 17:10:40 +00:00

178 lines
5.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_KeyEventHandler_h_
#define mozilla_KeyEventHandler_h_
#include "mozilla/EventForwards.h"
#include "mozilla/MemoryReporting.h"
#include "nsAtom.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsIController.h"
#include "nsAutoPtr.h"
#include "nsIWeakReference.h"
#include "nsCycleCollectionParticipant.h"
#include "js/TypeDecls.h"
#include "mozilla/ShortcutKeys.h"
namespace mozilla {
namespace layers {
class KeyboardShortcut;
} // namespace layers
struct IgnoreModifierState;
namespace dom {
class Event;
class UIEvent;
class Element;
class EventTarget;
class KeyboardEvent;
class Element;
} // namespace dom
using namespace dom;
// Values of the reserved attribute. When unset, the default value depends on
// the permissions.default.shortcuts preference.
enum ReservedKey : uint8_t {
ReservedKey_False = 0,
ReservedKey_True = 1,
ReservedKey_Unset = 2,
};
class KeyEventHandler final {
public:
// This constructor is used only by XUL key handlers (e.g., <key>)
explicit KeyEventHandler(Element* aHandlerElement, ReservedKey aReserved);
// This constructor is used for keyboard handlers for browser, editor, input
// and textarea elements.
explicit KeyEventHandler(ShortcutKeyData* aKeyData);
~KeyEventHandler();
/**
* Try and convert this XBL handler into an APZ KeyboardShortcut for handling
* key events on the compositor thread. This only works for XBL handlers that
* represent scroll commands.
*
* @param aOut the converted KeyboardShortcut, must be non null
* @return whether the handler was converted into a KeyboardShortcut
*/
bool TryConvertToKeyboardShortcut(layers::KeyboardShortcut* aOut) const;
bool EventTypeEquals(nsAtom* aEventType) const {
return mEventName == aEventType;
}
// if aCharCode is not zero, it is used instead of the charCode of
// aKeyEventHandler.
bool KeyEventMatched(KeyboardEvent* aDomKeyboardEvent, uint32_t aCharCode,
const IgnoreModifierState& aIgnoreModifierState);
already_AddRefed<Element> GetHandlerElement();
ReservedKey GetIsReserved() { return mReserved; }
KeyEventHandler* GetNextHandler() { return mNextHandler; }
void SetNextHandler(KeyEventHandler* aHandler) { mNextHandler = aHandler; }
MOZ_CAN_RUN_SCRIPT
nsresult ExecuteHandler(EventTarget* aTarget, Event* aEvent);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
public:
static uint32_t gRefCnt;
protected:
void Init() {
++gRefCnt;
if (gRefCnt == 1) {
// Get the primary accelerator key.
InitAccessKeys();
}
}
already_AddRefed<nsIController> GetController(EventTarget* aTarget);
inline int32_t GetMatchingKeyCode(const nsAString& aKeyName);
void ConstructPrototype(Element* aKeyElement,
const char16_t* aEvent = nullptr,
const char16_t* aCommand = nullptr,
const char16_t* aKeyCode = nullptr,
const char16_t* aCharCode = nullptr,
const char16_t* aModifiers = nullptr);
void BuildModifiers(nsAString& aModifiers);
void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers,
Element* aKeyElement, const char* aMessageName);
void GetEventType(nsAString& aEvent);
bool ModifiersMatchMask(UIEvent* aEvent,
const IgnoreModifierState& aIgnoreModifierState);
MOZ_CAN_RUN_SCRIPT
nsresult DispatchXBLCommand(EventTarget* aTarget, Event* aEvent);
MOZ_CAN_RUN_SCRIPT
nsresult DispatchXULKeyCommand(Event* aEvent);
Modifiers GetModifiers() const;
Modifiers GetModifiersMask() const;
static int32_t KeyToMask(int32_t key);
static int32_t AccelKeyMask();
static int32_t kMenuAccessKey;
static void InitAccessKeys();
static const int32_t cShift;
static const int32_t cAlt;
static const int32_t cControl;
static const int32_t cMeta;
static const int32_t cOS;
static const int32_t cShiftMask;
static const int32_t cAltMask;
static const int32_t cControlMask;
static const int32_t cMetaMask;
static const int32_t cOSMask;
static const int32_t cAllModifiers;
protected:
union {
nsIWeakReference*
mHandlerElement; // For XUL <key> element handlers. [STRONG]
char16_t* mCommand; // For built-in shortcuts the command to execute.
};
// The following four values make up 32 bits.
bool mIsXULKey; // This handler is either for a XUL <key> element or it is
// a command dispatcher.
uint8_t mMisc; // Miscellaneous extra information. For key events,
// stores whether or not we're a key code or char code.
// For mouse events, stores the clickCount.
ReservedKey mReserved; // <key> is reserved for chrome. Not used by handlers.
int32_t mKeyMask; // Which modifier keys this event handler expects to have
// down in order to be matched.
// The primary filter information for mouse/key events.
int32_t mDetail; // For key events, contains a charcode or keycode. For
// mouse events, stores the button info.
// Prototype handlers are chained. We own the next handler in the chain.
KeyEventHandler* mNextHandler;
RefPtr<nsAtom> mEventName; // The type of the event, e.g., "keypress"
};
} // namespace mozilla
#endif