// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import * as Host from '../host/host.js';
import {KeyboardShortcut} from './KeyboardShortcut.js';
import {ForwardedShortcut} from './ShortcutRegistry.js';

/**
 * @unrestricted
 */
export class ForwardedInputEventHandler {
  constructor() {
    Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
        Host.InspectorFrontendHostAPI.Events.KeyEventUnhandled, this._onKeyEventUnhandled, this);
  }

  /**
   * @param {!Common.Event} event
   */
  _onKeyEventUnhandled(event) {
    const data = event.data;
    const type = /** @type {string} */ (data.type);
    const key = /** @type {string} */ (data.key);
    const keyCode = /** @type {number} */ (data.keyCode);
    const modifiers = /** @type {number} */ (data.modifiers);

    if (type !== 'keydown') {
      return;
    }

    self.UI.context.setFlavor(ForwardedShortcut, ForwardedShortcut.instance);
    self.UI.shortcutRegistry.handleKey(KeyboardShortcut.makeKey(keyCode, modifiers), key);
    self.UI.context.setFlavor(ForwardedShortcut, null);
  }
}

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import * as Host from '../host/host.js';
import {KeyboardShortcut} from './KeyboardShortcut.js';
import {ForwardedShortcut} from './ShortcutRegistry.js';

/**
 * @unrestricted
 */
export class ForwardedInputEventHandler {
  constructor() {
    Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
        Host.InspectorFrontendHostAPI.Events.KeyEventUnhandled, this._onKeyEventUnhandled, this);
  }

  /**
   * @param {!Common.Event} event
   */
  _onKeyEventUnhandled(event) {
    const data = event.data;
    const type = /** @type {string} */ (data.type);
    const key = /** @type {string} */ (data.key);
    const keyCode = /** @type {number} */ (data.keyCode);
    const modifiers = /** @type {number} */ (data.modifiers);

    if (type !== 'keydown') {
      return;
    }

    self.UI.context.setFlavor(ForwardedShortcut, ForwardedShortcut.instance);
    self.UI.shortcutRegistry.handleKey(KeyboardShortcut.makeKey(keyCode, modifiers), key);
    self.UI.context.setFlavor(ForwardedShortcut, null);
  }
}

new ForwardedInputEventHandler();

Fix forwarding keyboard shortcuts from target page
DevTools: Fix forwarding keyboard shortcuts from target page

This was regressed by crrev.com/c/devtools/devtools-frontend/+/1942290
which removed an unused variable in response to a linting rule. That
variable referenced a call to construct a ForwardedInputEventHandler
instance. That constructor has a side-effect of registering event
listeners with the host and still needs to be run.

Restoring the call to the constructor fixes the issue.

Bug: 1049910
Change-Id: I6338164fcbbce6cae688d90c6d11cd76ed719f1e
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2043060
Reviewed-by: Tim van der Lippe <dba8716ee7f8d16236046f74d2167cb94410f6ed@chromium.org>
Reviewed-by: Robert Paveza <7787129de3d0cb4911c68d958db3023863584560@microsoft.com>
Commit-Queue: Tony Ross <d5807c9a1621d46cc76cd6770054768aff0ae774@microsoft.com>
