forked from boltgolt/boltobserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeybinds.js
More file actions
148 lines (121 loc) · 3.38 KB
/
keybinds.js
File metadata and controls
148 lines (121 loc) · 3.38 KB
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
const fs = require("fs")
const JSON5 = require("json5")
const path = require("path")
const electron = require("electron")
let win = false
let socket = false
let effects = {}
function executeAction(subject, command) {
switch (command) {
case "toggle":
if (typeof effects[subject] == "undefined") effects[subject] = false
effects[subject] = !effects[subject]
break
case "on":
case "true":
effects[subject] = true
break
case "off":
case "false":
effects[subject] = false
break
default:
console.warn(`WARNING: Unkown keybind command in keybind "${subject}:${command}"`)
return
}
socket.send({
type: "effect",
data: {
key: subject,
value: effects[subject]
}
})
switch (subject) {
case "window.fullscreen":
win.setFullScreen(effects[subject])
break
case "window.mousePassthrough":
win.setIgnoreMouseEvents(effects[subject])
break
}
}
function parseBind(binds) {
function nextBind(binds) {
if (binds.length > 0) parseBind(binds)
}
let bind = binds
if (typeof binds == "object") {
bind = binds[0]
binds.shift()
}
let actionRegex = /([\w\.]*?)\s?->\s?(\w*)/
let functionRegex = /([\w\.]*?)\((.*?)\)/
if (bind.match(actionRegex)) {
let parsed = actionRegex.exec(bind)
executeAction(parsed[1], parsed[2])
if (binds.length > 0) parseBind(binds)
}
else if (bind.match(functionRegex)) {
let parsed = functionRegex.exec(bind)
let argument = parsed[2]
switch (parsed[1]) {
case "functions.sleep":
setTimeout(() => {
if (binds.length > 0) parseBind(binds)
}, argument * 1000)
break
case "functions.reload":
// Reload both electron window and browsers
win.reload()
socket.send({
type: "pageUpdate"
})
// Wait a second for everything to load before executing the next action
setTimeout(() => {
if (binds.length > 0) parseBind(binds)
}, 100)
break
case "window.width":
win.setSize(Math.min(0, argument), win.getSize()[1])
return nextBind(binds)
case "window.height":
win.setSize(win.getSize()[0], Math.min(0, argument))
return nextBind(binds)
case "window.left":
win.setBounds({x: parseInt(argument)})
return nextBind(binds)
case "window.top":
win.setBounds({y: parseInt(argument)})
return nextBind(binds)
default:
console.warn(`WARNING: Unkown keybind function in keybind "${bind}"`)
return
}
}
}
module.exports = (_socket, _win) => {
win = _win
socket = _socket
let rawArr = JSON5.parse(fs.readFileSync(path.join(__dirname, "config", "keybinds.json5"), "utf8"))
if (fs.existsSync(path.join(__dirname, "config", "keybinds.override.json5"))) {
let override = JSON5.parse(fs.readFileSync(path.join(__dirname, "config", "keybinds.override.json5"), "utf8"))
Object.assign(rawArr, override)
}
for (let rawBind in rawArr) {
if (electron.globalShortcut.isRegistered(rawBind)) {
console.warn(`WARNING: Keybind "${rawBind}" is already used, registering anyway`)
}
try {
let registered = electron.globalShortcut.register(rawBind, () => {
parseBind(rawArr[rawBind])
})
if (!registered) {
console.warn(`WARNING: Keybind "${rawBind}" could not be registered`)
}
} catch (e) {
console.warn(`WARNING: Error while registering Keybind "${rawBind}"`)
}
}
let count = Object.keys(rawArr).length
if (count > 0) console.info(`Registered ${count} ${count == 1 ? "keybind" : "keybinds"} with the OS`)
}