Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / dom / events / GlobalKeyListener.h
blob78a00efe5375b7572c07c0e559e7d4bcaa0216c6
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 #ifndef mozilla_GlobalKeyListener_h_
8 #define mozilla_GlobalKeyListener_h_
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/layers/KeyboardMap.h"
12 #include "nsIDOMEventListener.h"
13 #include "nsIWeakReferenceUtils.h"
15 class nsAtom;
17 namespace mozilla {
18 class EventListenerManager;
19 class WidgetKeyboardEvent;
20 struct IgnoreModifierState;
22 namespace layers {
23 class KeyboardMap;
26 namespace dom {
27 class Element;
28 class EventTarget;
29 class KeyboardEvent;
30 } // namespace dom
32 class KeyEventHandler;
34 /**
35 * A generic listener for key events.
37 * Maintains a list of shortcut handlers and is registered as a listener for DOM
38 * key events from a target. Responsible for executing the appropriate handler
39 * when a keyboard event is received.
41 class GlobalKeyListener : public nsIDOMEventListener {
42 public:
43 explicit GlobalKeyListener(dom::EventTarget* aTarget);
45 void InstallKeyboardEventListenersTo(
46 EventListenerManager* aEventListenerManager);
47 void RemoveKeyboardEventListenersFrom(
48 EventListenerManager* aEventListenerManager);
50 NS_DECL_ISUPPORTS
51 NS_DECL_NSIDOMEVENTLISTENER
53 protected:
54 virtual ~GlobalKeyListener() = default;
56 MOZ_CAN_RUN_SCRIPT
57 void WalkHandlers(dom::KeyboardEvent* aKeyEvent);
59 // walk the handlers, looking for one to handle the event
60 MOZ_CAN_RUN_SCRIPT
61 bool WalkHandlersInternal(dom::KeyboardEvent* aKeyEvent, bool aExecute,
62 bool* aOutReservedForChrome = nullptr);
64 // walk the handlers for aEvent, aCharCode and aIgnoreModifierState. Execute
65 // it if aExecute = true.
66 MOZ_CAN_RUN_SCRIPT
67 bool WalkHandlersAndExecute(dom::KeyboardEvent* aKeyEvent, uint32_t aCharCode,
68 const IgnoreModifierState& aIgnoreModifierState,
69 bool aExecute,
70 bool* aOutReservedForChrome = nullptr);
72 // HandleEvent function for the capturing phase in the default event group.
73 MOZ_CAN_RUN_SCRIPT
74 void HandleEventOnCaptureInDefaultEventGroup(dom::KeyboardEvent* aEvent);
75 // HandleEvent function for the capturing phase in the system event group.
76 MOZ_CAN_RUN_SCRIPT
77 void HandleEventOnCaptureInSystemEventGroup(dom::KeyboardEvent* aEvent);
79 // Check if any handler would handle the given event. Optionally returns
80 // whether the command handler for the event is marked with the "reserved"
81 // attribute.
82 MOZ_CAN_RUN_SCRIPT
83 bool HasHandlerForEvent(dom::KeyboardEvent* aEvent,
84 bool* aOutReservedForChrome = nullptr);
86 // Returns true if the key would be reserved for the given handler. A reserved
87 // key is not sent to a content process or single-process equivalent.
88 bool IsReservedKey(WidgetKeyboardEvent* aKeyEvent, KeyEventHandler* aHandler);
90 // lazily load the handlers. Overridden to handle being attached
91 // to a particular element rather than the document
92 virtual void EnsureHandlers() = 0;
94 virtual bool CanHandle(KeyEventHandler* aHandler, bool aWillExecute) const {
95 return true;
98 virtual bool IsDisabled() const { return false; }
100 virtual already_AddRefed<dom::EventTarget> GetHandlerTarget(
101 KeyEventHandler* aHandler) {
102 return do_AddRef(mTarget);
105 dom::EventTarget* mTarget; // weak ref;
107 KeyEventHandler* mHandler; // Linked list of event handlers.
111 * A listener for shortcut keys defined in XUL keyset elements.
113 * Listens for keyboard events from the document object and triggers the
114 * appropriate XUL key elements.
116 class XULKeySetGlobalKeyListener final : public GlobalKeyListener {
117 public:
118 explicit XULKeySetGlobalKeyListener(dom::Element* aElement,
119 dom::EventTarget* aTarget);
121 static void AttachKeyHandler(dom::Element* aElementTarget);
122 static void DetachKeyHandler(dom::Element* aElementTarget);
124 protected:
125 virtual ~XULKeySetGlobalKeyListener();
127 // Returns the element which was passed as a parameter to the constructor,
128 // unless the element has been removed from the document. Optionally returns
129 // whether the disabled attribute is set on the element (assuming the element
130 // is non-null).
131 dom::Element* GetElement(bool* aIsDisabled = nullptr) const;
133 virtual void EnsureHandlers() override;
135 virtual bool CanHandle(KeyEventHandler* aHandler,
136 bool aWillExecute) const override;
137 virtual bool IsDisabled() const override;
138 virtual already_AddRefed<dom::EventTarget> GetHandlerTarget(
139 KeyEventHandler* aHandler) override;
142 * GetElementForHandler() retrieves an element for the handler. The element
143 * may be a command element or a key element.
145 * @param aHandler The handler.
146 * @param aElementForHandler Must not be nullptr. The element is returned to
147 * this.
148 * @return true if the handler is valid. Otherwise, false.
150 bool GetElementForHandler(KeyEventHandler* aHandler,
151 dom::Element** aElementForHandler) const;
154 * IsExecutableElement() returns true if aElement is executable.
155 * Otherwise, false. aElement should be a command element or a key element.
157 bool IsExecutableElement(dom::Element* aElement) const;
159 // Using weak pointer to the DOM Element.
160 nsWeakPtr mWeakPtrForElement;
164 * Listens for built-in shortcut keys.
166 * Listens to DOM keyboard events from the window or text input and runs the
167 * built-in shortcuts (see dom/events/keyevents) as necessary.
169 class RootWindowGlobalKeyListener final : public GlobalKeyListener {
170 public:
171 explicit RootWindowGlobalKeyListener(dom::EventTarget* aTarget);
173 static void AttachKeyHandler(dom::EventTarget* aTarget);
175 static layers::KeyboardMap CollectKeyboardShortcuts();
177 protected:
178 // Is an HTML editable element focused
179 static bool IsHTMLEditorFocused();
181 virtual void EnsureHandlers() override;
184 } // namespace mozilla
186 #endif