2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2019-09-06 20:10:40 +03:00
|
|
|
#ifndef mozilla_GlobalKeyListener_h_
|
|
|
|
#define mozilla_GlobalKeyListener_h_
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2016-04-22 20:12:54 +03:00
|
|
|
#include "mozilla/EventForwards.h"
|
2017-06-16 01:06:00 +03:00
|
|
|
#include "mozilla/layers/KeyboardMap.h"
|
2011-06-25 03:12:34 +04:00
|
|
|
#include "nsIDOMEventListener.h"
|
2018-10-31 23:39:03 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
class nsAtom;
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2013-04-14 22:27:33 +04:00
|
|
|
namespace mozilla {
|
2016-03-16 04:58:28 +03:00
|
|
|
class EventListenerManager;
|
2017-11-10 02:42:39 +03:00
|
|
|
class WidgetKeyboardEvent;
|
2017-06-06 02:24:35 +03:00
|
|
|
struct IgnoreModifierState;
|
2019-09-06 20:10:40 +03:00
|
|
|
|
|
|
|
namespace layers {
|
|
|
|
class KeyboardMap;
|
|
|
|
}
|
|
|
|
|
2013-04-14 22:27:33 +04:00
|
|
|
namespace dom {
|
|
|
|
class Element;
|
|
|
|
class EventTarget;
|
2018-02-09 19:17:09 +03:00
|
|
|
class KeyboardEvent;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
2013-04-14 22:27:33 +04:00
|
|
|
|
2019-09-06 20:10:40 +03:00
|
|
|
class KeyEventHandler;
|
2014-12-12 15:17:37 +03:00
|
|
|
|
2019-09-06 20:10:40 +03:00
|
|
|
/**
|
|
|
|
* A generic listener for key events.
|
|
|
|
*
|
|
|
|
* Maintains a list of shortcut handlers and is registered as a listener for DOM
|
|
|
|
* key events from a target. Responsible for executing the appropriate handler
|
|
|
|
* when a keyboard event is received.
|
|
|
|
*/
|
|
|
|
class GlobalKeyListener : public nsIDOMEventListener {
|
2000-09-22 09:02:20 +04:00
|
|
|
public:
|
2020-05-04 17:29:02 +03:00
|
|
|
explicit GlobalKeyListener(dom::EventTarget* aTarget);
|
2003-09-11 16:25:06 +04:00
|
|
|
|
2016-03-16 04:58:28 +03:00
|
|
|
void InstallKeyboardEventListenersTo(
|
|
|
|
EventListenerManager* aEventListenerManager);
|
|
|
|
void RemoveKeyboardEventListenersFrom(
|
|
|
|
EventListenerManager* aEventListenerManager);
|
|
|
|
|
2000-09-22 09:02:20 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2011-06-25 03:12:34 +04:00
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2000-09-22 09:02:20 +04:00
|
|
|
|
|
|
|
protected:
|
2019-09-06 20:10:40 +03:00
|
|
|
virtual ~GlobalKeyListener() = default;
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
void WalkHandlers(dom::KeyboardEvent* aKeyEvent);
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
// walk the handlers, looking for one to handle the event
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
bool WalkHandlersInternal(dom::KeyboardEvent* aKeyEvent, bool aExecute,
|
2015-03-12 18:03:08 +03:00
|
|
|
bool* aOutReservedForChrome = nullptr);
|
2000-09-22 09:02:20 +04:00
|
|
|
|
2014-12-12 15:17:37 +03:00
|
|
|
// walk the handlers for aEvent, aCharCode and aIgnoreModifierState. Execute
|
|
|
|
// it if aExecute = true.
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
bool WalkHandlersAndExecute(dom::KeyboardEvent* aKeyEvent, uint32_t aCharCode,
|
2014-12-12 15:17:37 +03:00
|
|
|
const IgnoreModifierState& aIgnoreModifierState,
|
2015-03-12 18:03:08 +03:00
|
|
|
bool aExecute,
|
|
|
|
bool* aOutReservedForChrome = nullptr);
|
2014-03-18 19:16:47 +04:00
|
|
|
|
2016-03-16 04:58:28 +03:00
|
|
|
// HandleEvent function for the capturing phase in the default event group.
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
void HandleEventOnCaptureInDefaultEventGroup(dom::KeyboardEvent* aEvent);
|
2016-03-16 04:58:28 +03:00
|
|
|
// HandleEvent function for the capturing phase in the system event group.
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
void HandleEventOnCaptureInSystemEventGroup(dom::KeyboardEvent* aEvent);
|
2014-03-18 19:16:47 +04:00
|
|
|
|
2015-03-12 18:03:08 +03:00
|
|
|
// Check if any handler would handle the given event. Optionally returns
|
|
|
|
// whether the command handler for the event is marked with the "reserved"
|
|
|
|
// attribute.
|
2019-03-12 04:57:42 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2020-05-04 17:29:02 +03:00
|
|
|
bool HasHandlerForEvent(dom::KeyboardEvent* aEvent,
|
2015-03-12 18:03:08 +03:00
|
|
|
bool* aOutReservedForChrome = nullptr);
|
2008-04-15 08:16:24 +04:00
|
|
|
|
2017-11-10 02:42:39 +03:00
|
|
|
// Returns true if the key would be reserved for the given handler. A reserved
|
|
|
|
// key is not sent to a content process or single-process equivalent.
|
2019-09-06 20:10:40 +03:00
|
|
|
bool IsReservedKey(WidgetKeyboardEvent* aKeyEvent, KeyEventHandler* aHandler);
|
2017-11-10 02:42:39 +03:00
|
|
|
|
2003-02-28 04:04:45 +03:00
|
|
|
// lazily load the handlers. Overridden to handle being attached
|
|
|
|
// to a particular element rather than the document
|
2019-09-06 20:10:40 +03:00
|
|
|
virtual void EnsureHandlers() = 0;
|
2019-09-05 19:51:27 +03:00
|
|
|
|
2019-09-06 20:10:40 +03:00
|
|
|
virtual bool CanHandle(KeyEventHandler* aHandler, bool aWillExecute) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsDisabled() const { return false; }
|
|
|
|
|
2020-05-04 17:29:02 +03:00
|
|
|
virtual already_AddRefed<dom::EventTarget> GetHandlerTarget(
|
2019-09-06 20:10:40 +03:00
|
|
|
KeyEventHandler* aHandler) {
|
|
|
|
return do_AddRef(mTarget);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:29:02 +03:00
|
|
|
dom::EventTarget* mTarget; // weak ref;
|
2019-09-06 20:10:40 +03:00
|
|
|
|
|
|
|
KeyEventHandler* mHandler; // Linked list of event handlers.
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A listener for shortcut keys defined in XUL keyset elements.
|
|
|
|
*
|
|
|
|
* Listens for keyboard events from the document object and triggers the
|
|
|
|
* appropriate XUL key elements.
|
|
|
|
*/
|
|
|
|
class XULKeySetGlobalKeyListener final : public GlobalKeyListener {
|
|
|
|
public:
|
2020-05-04 17:29:02 +03:00
|
|
|
explicit XULKeySetGlobalKeyListener(dom::Element* aElement,
|
|
|
|
dom::EventTarget* aTarget);
|
2019-09-06 20:10:40 +03:00
|
|
|
|
2020-05-04 17:29:02 +03:00
|
|
|
static void AttachKeyHandler(dom::Element* aElementTarget);
|
|
|
|
static void DetachKeyHandler(dom::Element* aElementTarget);
|
2019-09-06 20:10:40 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~XULKeySetGlobalKeyListener();
|
2007-01-09 10:33:18 +03:00
|
|
|
|
|
|
|
// Returns the element which was passed as a parameter to the constructor,
|
2014-03-18 19:16:47 +04:00
|
|
|
// unless the element has been removed from the document. Optionally returns
|
|
|
|
// whether the disabled attribute is set on the element (assuming the element
|
|
|
|
// is non-null).
|
2020-05-04 17:29:02 +03:00
|
|
|
dom::Element* GetElement(bool* aIsDisabled = nullptr) const;
|
2019-09-06 20:10:40 +03:00
|
|
|
|
|
|
|
virtual void EnsureHandlers() override;
|
|
|
|
|
|
|
|
virtual bool CanHandle(KeyEventHandler* aHandler,
|
|
|
|
bool aWillExecute) const override;
|
|
|
|
virtual bool IsDisabled() const override;
|
2020-05-04 17:29:02 +03:00
|
|
|
virtual already_AddRefed<dom::EventTarget> GetHandlerTarget(
|
2019-09-06 20:10:40 +03:00
|
|
|
KeyEventHandler* aHandler) override;
|
2016-03-18 05:25:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetElementForHandler() retrieves an element for the handler. The element
|
|
|
|
* may be a command element or a key element.
|
|
|
|
*
|
|
|
|
* @param aHandler The handler.
|
|
|
|
* @param aElementForHandler Must not be nullptr. The element is returned to
|
|
|
|
* this.
|
|
|
|
* @return true if the handler is valid. Otherwise, false.
|
|
|
|
*/
|
2019-09-06 20:10:40 +03:00
|
|
|
bool GetElementForHandler(KeyEventHandler* aHandler,
|
2020-05-04 17:29:02 +03:00
|
|
|
dom::Element** aElementForHandler) const;
|
2016-03-18 05:25:22 +03:00
|
|
|
|
2016-03-19 17:16:21 +03:00
|
|
|
/**
|
|
|
|
* IsExecutableElement() returns true if aElement is executable.
|
|
|
|
* Otherwise, false. aElement should be a command element or a key element.
|
|
|
|
*/
|
2020-05-04 17:29:02 +03:00
|
|
|
bool IsExecutableElement(dom::Element* aElement) const;
|
2016-03-19 17:16:21 +03:00
|
|
|
|
2007-01-09 10:33:18 +03:00
|
|
|
// Using weak pointer to the DOM Element.
|
|
|
|
nsWeakPtr mWeakPtrForElement;
|
2019-09-06 20:10:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens for built-in shortcut keys.
|
|
|
|
*
|
|
|
|
* Listens to DOM keyboard events from the window or text input and runs the
|
|
|
|
* built-in shortcuts (see dom/events/keyevents) as necessary.
|
|
|
|
*/
|
|
|
|
class RootWindowGlobalKeyListener final : public GlobalKeyListener {
|
|
|
|
public:
|
2020-05-04 17:29:02 +03:00
|
|
|
explicit RootWindowGlobalKeyListener(dom::EventTarget* aTarget);
|
2007-01-09 10:33:18 +03:00
|
|
|
|
2020-05-04 17:29:02 +03:00
|
|
|
static void AttachKeyHandler(dom::EventTarget* aTarget);
|
2019-09-06 20:10:40 +03:00
|
|
|
|
|
|
|
static layers::KeyboardMap CollectKeyboardShortcuts();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Is an HTML editable element focused
|
|
|
|
static bool IsHTMLEditorFocused();
|
|
|
|
|
|
|
|
virtual void EnsureHandlers() override;
|
2000-09-22 09:02:20 +04:00
|
|
|
};
|
|
|
|
|
2019-09-06 20:10:40 +03:00
|
|
|
} // namespace mozilla
|
2000-09-22 09:02:20 +04:00
|
|
|
|
|
|
|
#endif
|