summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/src/KeyboardMap.h
blob: 32ec8ea61dfef0564b3ae7f5a0c19576128269e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- 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_layers_KeyboardMap_h
#define mozilla_layers_KeyboardMap_h

#include <stdint.h>  // for uint32_t

#include "InputData.h"             // for KeyboardInput
#include "nsIScrollableFrame.h"    // for nsIScrollableFrame::ScrollUnit
#include "nsTArray.h"              // for nsTArray
#include "mozilla/Maybe.h"         // for mozilla::Maybe
#include "KeyboardScrollAction.h"  // for KeyboardScrollAction

namespace mozilla {

struct IgnoreModifierState;

namespace layers {

class KeyboardMap;

/**
 * This class is an off main-thread <xul:handler> for scrolling commands.
 */
class KeyboardShortcut final {
 public:
  KeyboardShortcut();

  /**
   * Create a keyboard shortcut that when matched can be handled by executing
   * the specified keyboard action.
   */
  KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType,
                   uint32_t aKeyCode, uint32_t aCharCode, Modifiers aModifiers,
                   Modifiers aModifiersMask,
                   const KeyboardScrollAction& aAction);

  /**
   * Create a keyboard shortcut that when matched should be handled by ignoring
   * the keyboard event and dispatching it to content.
   */
  KeyboardShortcut(KeyboardInput::KeyboardEventType aEventType,
                   uint32_t aKeyCode, uint32_t aCharCode, Modifiers aModifiers,
                   Modifiers aModifiersMask);

  /**
   * There are some default actions for keyboard inputs that are hardcoded in
   * EventStateManager instead of being represented as XBL handlers. This adds
   * keyboard shortcuts to match these inputs and dispatch them to content.
   */
  static void AppendHardcodedShortcuts(nsTArray<KeyboardShortcut>& aShortcuts);

 protected:
  friend mozilla::layers::KeyboardMap;

  bool Matches(const KeyboardInput& aInput, const IgnoreModifierState& aIgnore,
               uint32_t aOverrideCharCode = 0) const;

 private:
  bool MatchesKey(const KeyboardInput& aInput,
                  uint32_t aOverrideCharCode) const;
  bool MatchesModifiers(const KeyboardInput& aInput,
                        const IgnoreModifierState& aIgnore) const;

 public:
  // The action to perform when this shortcut is matched,
  // and not flagged to be dispatched to content
  KeyboardScrollAction mAction;

  // Only one of mKeyCode or mCharCode may be non-zero
  // whichever one is non-zero is the one to compare when matching
  uint32_t mKeyCode;
  uint32_t mCharCode;

  // The modifiers that must be active for this shortcut
  Modifiers mModifiers;
  // The modifiers to compare when matching this shortcut
  Modifiers mModifiersMask;

  // The type of keyboard event to match against
  KeyboardInput::KeyboardEventType mEventType;

  // Whether events matched by this must be dispatched to content
  bool mDispatchToContent;
};

/**
 * A keyboard map is an off main-thread <xul:binding> for scrolling commands.
 */
class KeyboardMap final {
 public:
  KeyboardMap();
  explicit KeyboardMap(nsTArray<KeyboardShortcut>&& aShortcuts);

  const nsTArray<KeyboardShortcut>& Shortcuts() const { return mShortcuts; }

  /**
   * Search through the internal list of shortcuts for a match for the input
   * event
   */
  Maybe<KeyboardShortcut> FindMatch(const KeyboardInput& aEvent) const;

 private:
  Maybe<KeyboardShortcut> FindMatchInternal(
      const KeyboardInput& aEvent, const IgnoreModifierState& aIgnore,
      uint32_t aOverrideCharCode = 0) const;

  CopyableTArray<KeyboardShortcut> mShortcuts;
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_layers_KeyboardMap_h