summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/extensions/shortcut_input.js
blob: 43f7d273ed1bac1bfb8390f19ac5213e80abb651 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2016 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.

/** @enum {number} */
const ShortcutError = {
  NO_ERROR: 0,
  INCLUDE_START_MODIFIER: 1,
  TOO_MANY_MODIFIERS: 2,
  NEED_CHARACTER: 3,
};

cr.define('extensions', function() {
  'use strict';

  // The UI to display and manage keyboard shortcuts set for extension commands.
  const ShortcutInput = Polymer({
    is: 'extensions-shortcut-input',

    properties: {
      /** @type {!extensions.KeyboardShortcutDelegate} */
      delegate: Object,

      item: {
        type: String,
        value: '',
      },

      commandName: {
        type: String,
        value: '',
      },

      shortcut: {
        type: String,
        value: '',
      },

      /** @private */
      capturing_: {
        type: Boolean,
        value: false,
      },

      /** @private {!ShortcutError} */
      error_: {
        type: Number,
        value: 0,
      },

      /** @private */
      pendingShortcut_: {
        type: String,
        value: '',
      },
    },

    /** @override */
    ready: function() {
      const node = this.$.input;
      node.addEventListener('mouseup', this.startCapture_.bind(this));
      node.addEventListener('blur', this.endCapture_.bind(this));
      node.addEventListener('focus', this.startCapture_.bind(this));
      node.addEventListener('keydown', this.onKeyDown_.bind(this));
      node.addEventListener('keyup', this.onKeyUp_.bind(this));
    },

    /** @private */
    startCapture_: function() {
      if (this.capturing_) {
        return;
      }
      this.capturing_ = true;
      this.delegate.setShortcutHandlingSuspended(true);
    },

    /** @private */
    endCapture_: function() {
      if (!this.capturing_) {
        return;
      }
      this.pendingShortcut_ = '';
      this.capturing_ = false;
      const input = this.$.input;
      input.blur();
      input.invalid = false;
      this.delegate.setShortcutHandlingSuspended(false);
    },

    /**
     * @param {!KeyboardEvent} e
     * @private
     */
    onKeyDown_: function(e) {
      if (e.target == this.$.clear) {
        return;
      }

      if (e.keyCode == extensions.Key.Escape) {
        if (!this.capturing_) {
          // If we're not currently capturing, allow escape to propagate.
          return;
        }
        // Otherwise, escape cancels capturing.
        this.endCapture_();
        e.preventDefault();
        e.stopPropagation();
        return;
      }
      if (e.keyCode == extensions.Key.Tab) {
        // Allow tab propagation for keyboard navigation.
        return;
      }

      if (!this.capturing_) {
        this.startCapture_();
      }

      this.handleKey_(e);
    },

    /**
     * @param {!KeyboardEvent} e
     * @private
     */
    onKeyUp_: function(e) {
      // Ignores pressing 'Space' or 'Enter' on the clear button. In 'Enter's
      // case, the clear button disappears before key-up, so 'Enter's key-up
      // target becomes the input field, not the clear button, and needs to
      // be caught explicitly.
      if (e.target == this.$.clear || e.key == 'Enter') {
        return;
      }

      if (e.keyCode == extensions.Key.Escape ||
          e.keyCode == extensions.Key.Tab) {
        return;
      }

      this.handleKey_(e);
    },

    /**
     * @param {!ShortcutError} error
     * @param {string} includeStartModifier
     * @param {string} tooManyModifiers
     * @param {string} needCharacter
     * @return {string} UI string.
     * @private
     */
    getErrorString_: function(
        error, includeStartModifier, tooManyModifiers, needCharacter) {
      if (error == ShortcutError.TOO_MANY_MODIFIERS) {
        return tooManyModifiers;
      }
      if (error == ShortcutError.NEED_CHARACTER) {
        return needCharacter;
      }
      return includeStartModifier;
    },

    /**
     * @param {!KeyboardEvent} e
     * @private
     */
    handleKey_: function(e) {
      // While capturing, we prevent all events from bubbling, to prevent
      // shortcuts lacking the right modifier (F3 for example) from activating
      // and ending capture prematurely.
      e.preventDefault();
      e.stopPropagation();

      // We don't allow both Ctrl and Alt in the same keybinding.
      // TODO(devlin): This really should go in extensions.hasValidModifiers,
      // but that requires updating the existing page as well.
      if (e.ctrlKey && e.altKey) {
        this.error_ = ShortcutError.TOO_MANY_MODIFIERS;
        this.$.input.invalid = true;
        return;
      }
      if (!extensions.hasValidModifiers(e)) {
        this.pendingShortcut_ = '';
        this.error_ = ShortcutError.INCLUDE_START_MODIFIER;
        this.$.input.invalid = true;
        return;
      }
      this.pendingShortcut_ = extensions.keystrokeToString(e);
      if (!extensions.isValidKeyCode(e.keyCode)) {
        this.error_ = ShortcutError.NEED_CHARACTER;
        this.$.input.invalid = true;
        return;
      }
      this.$.input.invalid = false;

      this.commitPending_();
      this.endCapture_();
    },

    /** @private */
    commitPending_: function() {
      this.shortcut = this.pendingShortcut_;
      this.delegate.updateExtensionCommandKeybinding(
          this.item, this.commandName, this.shortcut);
    },

    /**
     * @return {string} The text to be displayed in the shortcut field.
     * @private
     */
    computeText_: function() {
      const shortcutString =
          this.capturing_ ? this.pendingShortcut_ : this.shortcut;
      return shortcutString.split('+').join(' + ');
    },

    /**
     * @return {boolean} Whether the clear button is hidden.
     * @private
     */
    computeClearHidden_: function() {
      // We don't want to show the clear button if the input is currently
      // capturing a new shortcut or if there is no shortcut to clear.
      return this.capturing_ || !this.shortcut;
    },

    /** @private */
    onClearTap_: function() {
      assert(this.shortcut);

      this.pendingShortcut_ = '';
      this.commitPending_();
      this.endCapture_();
    },
  });

  return {ShortcutInput: ShortcutInput};
});