OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 Tilde: { code: 192, name: "Tilde" }, | 97 Tilde: { code: 192, name: "Tilde" }, |
98 Backslash: { code: 220, name: "\\" }, | 98 Backslash: { code: 220, name: "\\" }, |
99 SingleQuote: { code: 222, name: "\'" }, | 99 SingleQuote: { code: 222, name: "\'" }, |
100 get CtrlOrMeta() | 100 get CtrlOrMeta() |
101 { | 101 { |
102 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe
r platforms | 102 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe
r platforms |
103 return WebInspector.isMac() ? this.Meta : this.Ctrl; | 103 return WebInspector.isMac() ? this.Meta : this.Ctrl; |
104 }, | 104 }, |
105 }; | 105 }; |
106 | 106 |
| 107 WebInspector.KeyboardShortcut.KeyBindings = {}; |
| 108 |
| 109 (function() { |
| 110 for (var key in WebInspector.KeyboardShortcut.Keys) { |
| 111 var descriptor = WebInspector.KeyboardShortcut.Keys[key]; |
| 112 if (typeof descriptor === "object" && descriptor["code"]) { |
| 113 var name = typeof descriptor["name"] === "string" ? descriptor["name
"] : key; |
| 114 WebInspector.KeyboardShortcut.KeyBindings[name] = { code: descriptor
["code"] }; |
| 115 } |
| 116 } |
| 117 })(); |
| 118 |
107 /** | 119 /** |
108 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in t
he higher 8 bits. | 120 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in t
he higher 8 bits. |
109 * It is useful for matching pressed keys. | 121 * It is useful for matching pressed keys. |
110 * | 122 * |
111 * @param {number|string} keyCode The code of the key, or a character "a-z" whic
h is converted to a keyCode value. | 123 * @param {number|string} keyCode The code of the key, or a character "a-z" whic
h is converted to a keyCode value. |
112 * @param {number=} modifiers Optional list of modifiers passed as additional pa
ramerters. | 124 * @param {number=} modifiers Optional list of modifiers passed as additional pa
rameters. |
113 * @return {number} | 125 * @return {number} |
114 */ | 126 */ |
115 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) | 127 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) |
116 { | 128 { |
117 if (typeof keyCode === "string") | 129 if (typeof keyCode === "string") |
118 keyCode = keyCode.charCodeAt(0) - 32; | 130 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0); |
119 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; | 131 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; |
120 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); | 132 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); |
121 } | 133 } |
122 | 134 |
123 /** | 135 /** |
124 * @param {?KeyboardEvent} keyboardEvent | 136 * @param {?KeyboardEvent} keyboardEvent |
125 * @return {number} | 137 * @return {number} |
126 */ | 138 */ |
127 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) | 139 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) |
128 { | 140 { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 */ | 184 */ |
173 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) | 185 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) |
174 { | 186 { |
175 return { | 187 return { |
176 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key
: key.code, modifiers), | 188 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key
: key.code, modifiers), |
177 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) | 189 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) |
178 }; | 190 }; |
179 } | 191 } |
180 | 192 |
181 /** | 193 /** |
| 194 * @param {string} shortcut |
| 195 * @return {number} |
| 196 */ |
| 197 WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut = function(shortcut) |
| 198 { |
| 199 var parts = shortcut.split(/\+(?!$)/); |
| 200 var modifiers = 0; |
| 201 for (var i = 0; i < parts.length; ++i) { |
| 202 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefi
ned") { |
| 203 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]]; |
| 204 continue; |
| 205 } |
| 206 console.assert(i === parts.length - 1, "Modifiers-only shortcuts are not
allowed (encountered <" + shortcut + ">)"); |
| 207 var key = WebInspector.KeyboardShortcut.Keys[parts[i]] || WebInspector.K
eyboardShortcut.KeyBindings[parts[i]]; |
| 208 if (key && key.shiftKey) |
| 209 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; |
| 210 return WebInspector.KeyboardShortcut.makeKey(key ? key.code : parts[i].t
oLowerCase(), modifiers) |
| 211 } |
| 212 console.assert(false); |
| 213 return 0; |
| 214 } |
| 215 |
| 216 /** |
182 * @param {string|!WebInspector.KeyboardShortcut.Key} key | 217 * @param {string|!WebInspector.KeyboardShortcut.Key} key |
183 * @param {number=} modifiers | 218 * @param {number=} modifiers |
184 * @return {string} | 219 * @return {string} |
185 */ | 220 */ |
186 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) | 221 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) |
187 { | 222 { |
188 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp
ector.KeyboardShortcut._keyName(key); | 223 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp
ector.KeyboardShortcut._keyName(key); |
189 } | 224 } |
190 | 225 |
191 /** | 226 /** |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt) | 264 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt) |
230 res += isMac ? optKey : "Alt + "; | 265 res += isMac ? optKey : "Alt + "; |
231 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) | 266 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) |
232 res += isMac ? shiftKey : "Shift + "; | 267 res += isMac ? shiftKey : "Shift + "; |
233 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) | 268 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) |
234 res += isMac ? cmdKey : "Win + "; | 269 res += isMac ? cmdKey : "Win + "; |
235 | 270 |
236 return res; | 271 return res; |
237 }; | 272 }; |
238 | 273 |
| 274 /** |
| 275 * @param {!KeyboardEvent} event |
| 276 */ |
| 277 WebInspector.KeyboardShortcut.handleShortcut = function(event) |
| 278 { |
| 279 var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event); |
| 280 var extensions = WebInspector.KeyboardShortcut._keysToActionExtensions[key]; |
| 281 if (!extensions) |
| 282 return; |
| 283 |
| 284 function handler(extension) |
| 285 { |
| 286 var result = extension.instance().handleAction(event); |
| 287 if (result) |
| 288 event.consume(true); |
| 289 delete WebInspector.KeyboardShortcut._pendingActionTimer; |
| 290 return result; |
| 291 } |
| 292 |
| 293 for (var i = 0; i < extensions.length; ++i) { |
| 294 var ident = event.keyIdentifier; |
| 295 if (/^F\d+|Control|Shift|Alt|Meta|Win|U\+001B$/.test(ident) || event.ctr
lKey || event.altKey || event.metaKey) { |
| 296 if (handler(extensions[i])) |
| 297 return; |
| 298 } else { |
| 299 WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handl
er.bind(null, extensions[i]), 0); |
| 300 break; |
| 301 } |
| 302 } |
| 303 } |
| 304 |
239 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); | 305 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); |
| 306 |
| 307 WebInspector.KeyboardShortcut._onKeyPress = function(event) |
| 308 { |
| 309 if (!WebInspector.KeyboardShortcut._pendingActionTimer) |
| 310 return; |
| 311 |
| 312 var target = event.target; |
| 313 if (WebInspector.isBeingEdited(event.target)) { |
| 314 clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer); |
| 315 delete WebInspector.KeyboardShortcut._pendingActionTimer; |
| 316 } |
| 317 } |
| 318 |
| 319 WebInspector.KeyboardShortcut.registerActions = function() |
| 320 { |
| 321 document.addEventListener("keypress", WebInspector.KeyboardShortcut._onKeyPr
ess, true); |
| 322 WebInspector.KeyboardShortcut._keysToActionExtensions = {}; |
| 323 var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDe
legate); |
| 324 extensions.forEach(registerBindings); |
| 325 |
| 326 /** |
| 327 * @param {!WebInspector.ModuleManager.Extension} extension |
| 328 */ |
| 329 function registerBindings(extension) |
| 330 { |
| 331 var bindings = extension.descriptor().bindings; |
| 332 for (var i = 0; bindings && i < bindings.length; ++i) { |
| 333 if (!platformMatches(bindings[i].platform)) |
| 334 continue; |
| 335 var shortcuts = bindings[i].shortcut.split(/\s+/); |
| 336 shortcuts.forEach(registerShortcut.bind(null, extension)); |
| 337 } |
| 338 } |
| 339 |
| 340 /** |
| 341 * @param {!WebInspector.ModuleManager.Extension} extension |
| 342 * @param {string} shortcut |
| 343 */ |
| 344 function registerShortcut(extension, shortcut) |
| 345 { |
| 346 var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(short
cut); |
| 347 if (!key) |
| 348 return; |
| 349 if (WebInspector.KeyboardShortcut._keysToActionExtensions[key]) |
| 350 WebInspector.KeyboardShortcut._keysToActionExtensions[key].push(exte
nsion); |
| 351 else |
| 352 WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [extens
ion]; |
| 353 } |
| 354 |
| 355 /** |
| 356 * @param {string=} platformsString |
| 357 * @return {boolean} |
| 358 */ |
| 359 function platformMatches(platformsString) |
| 360 { |
| 361 if (!platformsString) |
| 362 return true; |
| 363 var platforms = platformsString.split(","); |
| 364 var isMatch = false; |
| 365 var currentPlatform = WebInspector.platform(); |
| 366 for (var i = 0; !isMatch && i < platforms.length; ++i) |
| 367 isMatch = platforms[i] === currentPlatform; |
| 368 return isMatch; |
| 369 } |
| 370 } |
OLD | NEW |