-
Notifications
You must be signed in to change notification settings - Fork 25
/
preload.js
216 lines (198 loc) · 4.84 KB
/
preload.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const { remote, app, ipcRenderer } = require("electron");
const find = require("find-process");
const ps = require("ps-node");
const isDev = require("electron-is-dev");
const { Tray, Menu, BrowserWindow } = remote;
const url = require("url");
const path = require("path");
const { execFile } = require("child_process");
const Store = require("electron-store");
const config = new Store({
projectName: "SwitchDock"
});
let tray = new Tray(
path.join(
__dirname,
`/assets/app-icons/${
process.platform == "darwin" ? "tray/icon.png" : "icon.png"
}`
)
);
let settingsWindowOpened = false;
canShowIntro = () => {
const status = config.get("showIntro");
return status == null ? true : status;
};
// creates an intro window
createIntroWindow = () => {
let win = new remote.BrowserWindow({
width: 600,
height: 400,
show: false,
frame: false,
hasShadow: false,
transparent: true,
maximizable: false,
minimizable: false,
skipTaskbar: true,
resizable: false,
webPreferences: {
devTools: false,
nodeIntegration: true
}
});
win.loadURL(
url.format({
pathname: path.join(__dirname, "intro.html"),
protocol: "file:",
slashes: true
})
);
win.on("closed", () => {});
win.once("ready-to-show", () => {
win.show();
});
};
//show intro on startup.
if (canShowIntro()) createIntroWindow();
createSettingsWindow = () => {
let win = new BrowserWindow({
width: 600,
height: 400,
show: false,
maximizable: false,
minimizable: false,
resizable: false,
webPreferences: {
// devTools: true,
nodeIntegration: true
}
});
win.loadURL(
url.format({
pathname: path.join(__dirname, "settings.html"),
protocol: "file:",
slashes: true
})
);
win.on("closed", () => {
settingsWindowOpened = false;
trayMenu.items[1].enabled = true;
// send the update to the switch service..
try {
window.SWITCH_SERVICE_CHANNEL.emit(
"switch-service-incoming",
JSON.stringify({
type: "config-update",
data: {
...config.get("config"),
disableAltGr: config.get("disableAltGr")
}
})
);
} catch (e) {}
});
win.once("ready-to-show", () => {
settingsWindowOpened = true;
win.setMenu(null);
win.show();
tray.setContextMenu(trayMenu);
trayMenu.items[1].enabled = false;
});
};
const trayMenuTemplate = [
{
label: "Show dock...",
click: () => {
try {
window.SWITCH_SERVICE_CHANNEL.emit(
"switch-service-incoming",
JSON.stringify({
type: "show-dock"
})
);
} catch (e) {}
}
},
{
label: "Preferences...",
click: () => createSettingsWindow(),
enabled: !settingsWindowOpened
},
{
type: "separator"
},
{
label: "Quit",
click: () => {
// stop swicth service...
if (process.platform == "darwin") StartOrStopSwitchMacService(false);
ipcRenderer.send("quit-switch");
}
}
];
if (process.platform == "darwin") {
// If platform is mac add extra menu item to cater for starting
// and stoping services ...
trayMenuTemplate.unshift({
type: "separator"
});
trayMenuTemplate.unshift({
label: "Turn Off",
click: () => {
StartOrStopSwitchMacService(false);
}
});
// try and stop any existing switch service
// and then build up tray menu
StartOrStopSwitchMacService(true);
} else {
// build and show tray menu ...
buildTrayMenu();
}
function buildTrayMenu() {
let trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(trayMenu);
}
/**
* Swicth service spawn stategy for mac OS
*/
const SPWAN_SWITCH_SERVICE_MAC = function() {
return execFile(
isDev
? path.join(__dirname, "/service-binaries/switch")
: path.join(path.dirname(__dirname), "/service-binaries/switch"),
[],
(error, stdout, stderr) => {}
);
};
/**
* Tries to start a new switch service but then kills the existing ones
* @param {boolean} start If true, its a start operation otherwise its a stop
*/
function StartOrStopSwitchMacService(start = true) {
find("name", "/service-binaries/switch", false).then(function(list) {
list.forEach(p => {
ps.kill(p.pid, err => {});
});
if (start) {
let child = SPWAN_SWITCH_SERVICE_MAC();
// on error kill service and respawn
child.stderr.on("data", data => {
child.kill();
// auto spwan..
child = SPWAN_SWITCH_SERVICE_MAC();
});
trayMenuTemplate[0].label = "Turn Off";
trayMenuTemplate[0].click = () => {
StartOrStopSwitchMacService(false);
};
} else {
trayMenuTemplate[0].label = "Turn On";
trayMenuTemplate[0].click = () => {
StartOrStopSwitchMacService(true);
};
}
buildTrayMenu();
});
}