|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = ["ShortcutUtils"]; |
|
8 |
|
9 const Cu = Components.utils; |
|
10 Cu.import("resource://gre/modules/Services.jsm"); |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 |
|
13 XPCOMUtils.defineLazyGetter(this, "PlatformKeys", function() { |
|
14 return Services.strings.createBundle( |
|
15 "chrome://global-platform/locale/platformKeys.properties"); |
|
16 }); |
|
17 |
|
18 XPCOMUtils.defineLazyGetter(this, "Keys", function() { |
|
19 return Services.strings.createBundle( |
|
20 "chrome://global/locale/keys.properties"); |
|
21 }); |
|
22 |
|
23 let ShortcutUtils = { |
|
24 /** |
|
25 * Prettifies the modifier keys for an element. |
|
26 * |
|
27 * @param Node aElemKey |
|
28 * The key element to get the modifiers from. |
|
29 * @param boolean aNoCloverLeaf |
|
30 * Pass true to use a descriptive string instead of the cloverleaf symbol. (OS X only) |
|
31 * @return string |
|
32 * A prettified and properly separated modifier keys string. |
|
33 */ |
|
34 prettifyShortcut: function(aElemKey, aNoCloverLeaf) { |
|
35 let elemString = ""; |
|
36 let elemMod = aElemKey.getAttribute("modifiers"); |
|
37 let haveCloverLeaf = false; |
|
38 |
|
39 if (elemMod.match("accel")) { |
|
40 if (Services.appinfo.OS == "Darwin") { |
|
41 // XXX bug 779642 Use "Cmd-" literal vs. cloverleaf meta-key until |
|
42 // Orion adds variable height lines. |
|
43 if (aNoCloverLeaf) { |
|
44 elemString += "Cmd-"; |
|
45 } else { |
|
46 haveCloverLeaf = true; |
|
47 } |
|
48 } else { |
|
49 elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + |
|
50 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
51 } |
|
52 } |
|
53 if (elemMod.match("access")) { |
|
54 if (Services.appinfo.OS == "Darwin") { |
|
55 elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + |
|
56 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
57 } else { |
|
58 elemString += PlatformKeys.GetStringFromName("VK_ALT") + |
|
59 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
60 } |
|
61 } |
|
62 if (elemMod.match("os")) { |
|
63 elemString += PlatformKeys.GetStringFromName("VK_WIN") + |
|
64 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
65 } |
|
66 if (elemMod.match("shift")) { |
|
67 elemString += PlatformKeys.GetStringFromName("VK_SHIFT") + |
|
68 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
69 } |
|
70 if (elemMod.match("alt")) { |
|
71 elemString += PlatformKeys.GetStringFromName("VK_ALT") + |
|
72 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
73 } |
|
74 if (elemMod.match("ctrl") || elemMod.match("control")) { |
|
75 elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + |
|
76 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
77 } |
|
78 if (elemMod.match("meta")) { |
|
79 elemString += PlatformKeys.GetStringFromName("VK_META") + |
|
80 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
81 } |
|
82 |
|
83 if (haveCloverLeaf) { |
|
84 elemString += PlatformKeys.GetStringFromName("VK_META") + |
|
85 PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); |
|
86 } |
|
87 |
|
88 let key; |
|
89 let keyCode = aElemKey.getAttribute("keycode"); |
|
90 if (keyCode) { |
|
91 try { |
|
92 // Some keys might not exist in the locale file, which will throw: |
|
93 key = Keys.GetStringFromName(keyCode.toUpperCase()); |
|
94 } catch (ex) { |
|
95 Cu.reportError("Error finding " + keyCode + ": " + ex); |
|
96 key = keyCode.replace(/^VK_/, ''); |
|
97 } |
|
98 } else { |
|
99 key = aElemKey.getAttribute("key"); |
|
100 key = key.toUpperCase(); |
|
101 } |
|
102 return elemString + key; |
|
103 } |
|
104 }; |
|
105 |
|
106 Object.freeze(ShortcutUtils); |