This repository has been archived by the owner on Feb 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
161 lines (130 loc) · 3.96 KB
/
index.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
/* 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 system = require("sdk/system");
const winUtils = require("sdk/deprecated/window-utils");
const {storage} = require("sdk/simple-storage");
const {ActionButton} = require("sdk/ui/button/action");
const {getActiveView, getNodeView} = require("sdk/view/core");
// Import shared modifiers code.
const gModifiers = require(self.data.url("shared-modifiers.js"));
const hotkeys = require("hotkeys");
const command = require("command");
// Storage v2.
const STORAGE_KEY = "overlays_v2";
let panel = panels.Panel({
width: 500,
height: 500,
contentURL: "chrome://customizable-shortcuts/content/panel.xul",
contentScriptWhen: "ready",
contentScriptFile: [
"panel-bug995889.js",
"panel-buttons.js",
"panel-treeview.js",
"panel-hotkeys.js",
"panel-tree.js",
"panel-keygroups.js",
"panel-overlays.js",
"panel-conflicts.js",
"shared-modifiers.js",
"panel.js"
].map(file => self.data.url(file)),
onShow() {
panel.port.emit("showing");
},
onHide() {
panel.port.emit("hiding");
}
});
let view = getActiveView(panel);
view.setAttribute("ignorekeys", "true");
view.setAttribute("noautohide", "true");
panel.port.on("ready", function () {
panel.port.emit("platform", gPlatform = system.platform);
panel.port.emit("overlays", gOverlays = storage[STORAGE_KEY] || {});
panel.port.emit("hotkeys", gHotKeys = hotkeys.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(window) {
if (utils.isBrowser(window)) {
window.addEventListener("keydown", handlePossibleShortcut);
window.addEventListener("click", maybeHidePanel);
}
},
onUntrack(window) {
if (utils.isBrowser(window)) {
window.removeEventListener("keydown", handlePossibleShortcut);
window.removeEventListener("click", maybeHidePanel);
}
}
});
let gPlatform;
let gHotKeys = {};
let gOverlays = {};
panel.port.on("overlays", function (overlays) {
storage[STORAGE_KEY] = gOverlays = overlays;
});
function findOverlay(event) {
let evmodifiers = gModifiers.fromEvent(event);
return Object.keys(gOverlays).find(id => {
let {disabled, key, modifiers} = gOverlays[id];
return !disabled &&
evmodifiers == modifiers &&
event.key.toLowerCase() == key.toLowerCase();
});
}
function findOverridden(event) {
let modifiers = gModifiers.fromEvent(event);
return Object.keys(gOverlays).map(id => gHotKeys[id]).some(hotkey => {
return modifiers == hotkey.modifiers &&
event.key.toLowerCase() == hotkey.key.toLowerCase();
});
}
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();
}
}
function maybeHidePanel(event) {
if (!panel.isShowing) {
return;
}
let isLeftMouse = event.button == 0;
let targetsToolbarButton = event.originalTarget == getNodeView(button);
// Block the click event if the user clicked our toolbar button with the
// left mouse button while the panel is open. In that case we don't want
// the click event to cause the panel to be shown again immediately.
if (isLeftMouse && targetsToolbarButton) {
// Block the click event's default action.
event.preventDefault();
}
// Hide the panel.
panel.hide();
}