Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef mozilla_layers_KeyboardMap_h
7 : #define mozilla_layers_KeyboardMap_h
8 :
9 : #include <stdint.h> // for uint32_t
10 :
11 : #include "InputData.h" // for KeyboardInput
12 : #include "nsIScrollableFrame.h" // for nsIScrollableFrame::ScrollUnit
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 : {
30 : public:
31 : KeyboardShortcut();
32 :
33 : /**
34 : * Create a keyboard shortcut that when matched can be handled by executing
35 : * the specified keyboard action.
36 : */
37 : KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType,
38 : uint32_t aKeyCode,
39 : uint32_t aCharCode,
40 : Modifiers aModifiers,
41 : Modifiers aModifiersMask,
42 : const KeyboardScrollAction& aAction);
43 :
44 : /**
45 : * Create a keyboard shortcut that when matched should be handled by ignoring
46 : * the keyboard event and dispatching it to content.
47 : */
48 : KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType,
49 : uint32_t aKeyCode,
50 : uint32_t aCharCode,
51 : Modifiers aModifiers,
52 : Modifiers aModifiersMask);
53 :
54 : /**
55 : * There are some default actions for keyboard inputs that are hardcoded in
56 : * EventStateManager instead of being represented as XBL handlers. This adds
57 : * keyboard shortcuts to match these inputs and dispatch them to content.
58 : */
59 : static void AppendHardcodedShortcuts(nsTArray<KeyboardShortcut>& aShortcuts);
60 :
61 : protected:
62 : friend mozilla::layers::KeyboardMap;
63 :
64 : bool Matches(const KeyboardInput& aInput,
65 : const IgnoreModifierState& aIgnore,
66 : uint32_t aOverrideCharCode = 0) const;
67 :
68 : private:
69 : bool MatchesKey(const KeyboardInput& aInput,
70 : uint32_t aOverrideCharCode) const;
71 : bool MatchesModifiers(const KeyboardInput& aInput,
72 : const IgnoreModifierState& aIgnore) const;
73 :
74 : public:
75 : // The action to perform when this shortcut is matched,
76 : // and not flagged to be dispatched to content
77 : KeyboardScrollAction mAction;
78 :
79 : // Only one of mKeyCode or mCharCode may be non-zero
80 : // whichever one is non-zero is the one to compare when matching
81 : uint32_t mKeyCode;
82 : uint32_t mCharCode;
83 :
84 : // The modifiers that must be active for this shortcut
85 : Modifiers mModifiers;
86 : // The modifiers to compare when matching this shortcut
87 : Modifiers mModifiersMask;
88 :
89 : // The type of keyboard event to match against
90 : KeyboardInput::KeyboardEventType mEventType;
91 :
92 : // Whether events matched by this must be dispatched to content
93 : bool mDispatchToContent;
94 : };
95 :
96 : /**
97 : * A keyboard map is an off main-thread <xul:binding> for scrolling commands.
98 : */
99 0 : class KeyboardMap final
100 : {
101 : public:
102 : KeyboardMap();
103 : explicit KeyboardMap(nsTArray<KeyboardShortcut>&& aShortcuts);
104 :
105 0 : const nsTArray<KeyboardShortcut>& Shortcuts() const { return mShortcuts; }
106 :
107 : /**
108 : * Search through the internal list of shortcuts for a match for the input event
109 : */
110 : Maybe<KeyboardShortcut> FindMatch(const KeyboardInput& aEvent) const;
111 :
112 : private:
113 : Maybe<KeyboardShortcut> FindMatchInternal(const KeyboardInput& aEvent,
114 : const IgnoreModifierState& aIgnore,
115 : uint32_t aOverrideCharCode = 0) const;
116 :
117 : nsTArray<KeyboardShortcut> mShortcuts;
118 : };
119 :
120 : } // namespace layers
121 : } // namespace mozilla
122 :
123 : #endif // mozilla_layers_KeyboardMap_h
|