-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.js
106 lines (96 loc) · 2.57 KB
/
events.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
const sdlToWebKeys = {
ctrl: 'Control',
shift: 'Shift',
alt: 'Alt',
meta: 'Meta',
space: ' ',
enter: 'Enter',
escape: 'Escape',
backspace: 'Backspace',
tab: 'Tab',
up: 'ArrowUp',
down: 'ArrowDown',
left: 'ArrowLeft',
right: 'ArrowRight',
}
function getWinEvent(type, e) {
const evt = {
type,
key: sdlToWebKeys[e.key] || e.key,
code: e.key,
repeat: e.repeat,
altKey: e.alt,
ctrlKey: e.ctrl,
shiftKey: e.shift,
};
return evt;
}
export default function initialize(appWindow) {
globalThis.document = globalThis.document || {};
let keyDownListeners = [];
let keyUpListeners = [];
let loadingEvents = [];
let resizeEvents = [];
globalThis.close = () => {
console.log('window.close');
process.exit(0);
}
appWindow.on('keyDown', (e) => {
keyDownListeners.forEach((listener) => {
const evt = getWinEvent('keydown', e);
listener(evt);
});
});
appWindow.on('keyUp', (e) => {
keyUpListeners.forEach((listener) => {
const evt = getWinEvent('keyup', e);
listener(evt);
});
});
globalThis.addEventListener = function (type, listener) {
if (type === 'keydown') {
keyDownListeners.push(listener);
} else if (type === 'keyup') {
keyUpListeners.push(listener);
} else if (type === 'load') {
loadingEvents.push(listener);
} else if (type === 'resize') {
resizeEvents.push(listener);
}
};
globalThis.document.addEventListener = globalThis.addEventListener;
globalThis.document.body.addEventListener = globalThis.addEventListener;
globalThis.removeEventListener = function (type, listener) {
if (type === 'keydown') {
keyDownListeners = keyDownListeners.filter((l) => l !== listener);
} else if (type === 'keyup') {
keyUpListeners = keyUpListeners.filter((l) => l !== listener);
} else if (type === 'load') {
loadingEvents = loadingEvents.filter((l) => l !== listener);
} else if (type === 'resize') {
resizeEvents = resizeEvents.filter((l) => l !== listener);
}
}
globalThis.document.removeEventListener = globalThis.removeEventListener;
globalThis.document.body.removeEventListener = globalThis.removeEventListener;
function callLoadingEvents() {
for (const event of loadingEvents) {
event({});
}
if (globalThis.onload) {
globalThis.onload();
}
}
function callResizeEvents() {
for (const event of resizeEvents) {
event({});
}
if (globalThis.onresize) {
globalThis.onresize();
}
}
return {
callLoadingEvents,
callResizeEvents,
}
}