KeyboardMap.h (3801B)
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/. */ 6 7 #ifndef mozilla_layers_KeyboardMap_h 8 #define mozilla_layers_KeyboardMap_h 9 10 #include <stdint.h> // for uint32_t 11 12 #include "InputData.h" // for KeyboardInput 13 #include "nsTArray.h" // for nsTArray 14 #include "mozilla/Maybe.h" // for mozilla::Maybe 15 #include "KeyboardScrollAction.h" // for KeyboardScrollAction 16 17 namespace mozilla { 18 19 struct IgnoreModifierState; 20 21 namespace layers { 22 23 class KeyboardMap; 24 25 /** 26 * This class is an off main-thread <xul:handler> for scrolling commands. 27 */ 28 class KeyboardShortcut final { 29 public: 30 KeyboardShortcut(); 31 32 /** 33 * Create a keyboard shortcut that when matched can be handled by executing 34 * the specified keyboard action. 35 */ 36 KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType, 37 uint32_t aKeyCode, uint32_t aCharCode, Modifiers aModifiers, 38 Modifiers aModifiersMask, 39 const KeyboardScrollAction& aAction); 40 41 /** 42 * Create a keyboard shortcut that when matched should be handled by ignoring 43 * the keyboard event and dispatching it to content. 44 */ 45 KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType, 46 uint32_t aKeyCode, uint32_t aCharCode, Modifiers aModifiers, 47 Modifiers aModifiersMask); 48 49 /** 50 * There are some default actions for keyboard inputs that are hardcoded in 51 * EventStateManager instead of being represented as XBL handlers. This adds 52 * keyboard shortcuts to match these inputs and dispatch them to content. 53 */ 54 static void AppendHardcodedShortcuts(nsTArray<KeyboardShortcut>& aShortcuts); 55 56 protected: 57 friend mozilla::layers::KeyboardMap; 58 59 bool Matches(const KeyboardInput& aInput, const IgnoreModifierState& aIgnore, 60 uint32_t aOverrideCharCode = 0) const; 61 62 private: 63 bool MatchesKey(const KeyboardInput& aInput, 64 uint32_t aOverrideCharCode) const; 65 bool MatchesModifiers(const KeyboardInput& aInput, 66 const IgnoreModifierState& aIgnore) const; 67 68 public: 69 // The action to perform when this shortcut is matched, 70 // and not flagged to be dispatched to content 71 KeyboardScrollAction mAction; 72 73 // Only one of mKeyCode or mCharCode may be non-zero 74 // whichever one is non-zero is the one to compare when matching 75 uint32_t mKeyCode; 76 uint32_t mCharCode; 77 78 // The modifiers that must be active for this shortcut 79 Modifiers mModifiers; 80 // The modifiers to compare when matching this shortcut 81 Modifiers mModifiersMask; 82 83 // The type of keyboard event to match against 84 KeyboardInput::KeyboardEventType mEventType; 85 86 // Whether events matched by this must be dispatched to content 87 bool mDispatchToContent; 88 }; 89 90 /** 91 * A keyboard map is an off main-thread <xul:binding> for scrolling commands. 92 */ 93 class KeyboardMap final { 94 public: 95 KeyboardMap(); 96 explicit KeyboardMap(nsTArray<KeyboardShortcut>&& aShortcuts); 97 98 const nsTArray<KeyboardShortcut>& Shortcuts() const { return mShortcuts; } 99 100 /** 101 * Search through the internal list of shortcuts for a match for the input 102 * event 103 */ 104 Maybe<KeyboardShortcut> FindMatch(const KeyboardInput& aEvent) const; 105 106 private: 107 Maybe<KeyboardShortcut> FindMatchInternal( 108 const KeyboardInput& aEvent, const IgnoreModifierState& aIgnore, 109 uint32_t aOverrideCharCode = 0) const; 110 111 CopyableTArray<KeyboardShortcut> mShortcuts; 112 }; 113 114 } // namespace layers 115 } // namespace mozilla 116 117 #endif // mozilla_layers_KeyboardMap_h