This repository has been archived by the owner on Feb 15, 2018. It is now read-only.
/
main.js
173 lines (135 loc) · 3.66 KB
/
main.js
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
/* 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/. */
"use strict";
const self = require("sdk/self");
const panels = require("sdk/panel");
const utils = require("sdk/window/utils");
const winUtils = require("sdk/deprecated/window-utils");
const {storage} = require("sdk/simple-storage");
const {ActionButton} = require("sdk/ui/button/action");
const keys = require("keys");
const command = require("command");
let panel = panels.Panel({
width: 500,
height: 500,
contentURL: "chrome://customizable-shortcuts/content/panel.xul",
contentScriptWhen: "ready",
contentScriptFile: [
self.data.url("panel-bug995889.js"),
self.data.url("panel-buttons.js"),
self.data.url("panel-treeview.js"),
self.data.url("panel-keys.js"),
self.data.url("panel-tree.js"),
self.data.url("panel-keygroups.js"),
self.data.url("panel-overlays.js"),
self.data.url("panel-conflicts.js"),
self.data.url("panel.js")
],
onShow: function () {
panel.port.emit("focus");
}
});
let {getActiveView} = require("sdk/view/core");
let view = getActiveView(panel);
view.setAttribute("ignorekeys", "true");
panel.port.on("ready", function () {
panel.port.emit("overlays", gOverlays = storage.overlays || {});
panel.port.emit("keys", gKeys = keys.get());
});
panel.port.on("hide", () => panel.hide());
let button = ActionButton({
id: "shortcuts-button",
label: "Customize Shortcuts",
icon: {
"16": "./icon-16.png",
"32": "./icon-32.png"
},
onClick: function () {
panel.show({position: button});
}
});
new winUtils.WindowTracker({
onTrack: function (window) {
if (utils.isBrowser(window)) {
window.addEventListener("keydown", handlePossibleShortcut);
}
},
onUntrack: function (window) {
if (utils.isBrowser(window)) {
window.removeEventListener("keydown", handlePossibleShortcut);
}
}
});
let gKeys = {};
let gOverlays = {};
panel.port.on("overlays", function (overlays) {
storage.overlays = gOverlays = overlays;
});
const MODIFIER_KEYS = {16: "shift", 17: "control", 18: "alt", 91: "meta", 92: "meta", 224: "meta"};
function modifiersFromEvent(event) {
let modifiers = [];
if (event.ctrlKey) {
modifiers.push("control");
}
if (event.shiftKey) {
modifiers.push("shift");
}
if (event.metaKey) {
modifiers.push("meta");
}
if (event.altKey) {
modifiers.push("alt");
}
return modifiers;
}
function isModifier(key) {
return key in MODIFIER_KEYS;
}
function sameModifiers(m1, m2) {
if (m1.length != m2.length) {
return false;
}
m2 = new Set(m2);
for (let modifier of m1) {
if (!m2.has(modifier)) {
return false;
}
}
return true;
}
function findOverlay(event) {
let modifiers = modifiersFromEvent(event);
for (let id of Object.keys(gOverlays)) {
let overlay = gOverlays[id];
if (!overlay.disabled && event.keyCode == overlay.code && sameModifiers(modifiers, overlay.modifiers)) {
return id;
}
}
return null;
}
function findOverridden(event) {
let modifiers = modifiersFromEvent(event);
for (let id of Object.keys(gOverlays)) {
if (event.keyCode == gKeys[id].code && sameModifiers(modifiers, gKeys[id].modifiers)) {
return true;
}
}
return false;
}
function handlePossibleShortcut(event) {
// Ignore synthetic events.
if (!event.isTrusted) {
return;
}
let overlay = findOverlay(event);
if (overlay) {
command.execute(overlay);
} else {
overlay = findOverridden(event);
}
if (overlay) {
event.preventDefault();
event.stopPropagation();
}
}