forked from GetScatter/ScatterDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron.js
263 lines (208 loc) · 6.61 KB
/
electron.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const electron = require('electron');
const {app, BrowserWindow, Tray, Menu, MenuItem, ipcMain} = electron;
const path = require("path");
const url = require("url");
const isDev = process.mainModule.filename.indexOf('app.asar') === -1;
let icon = isDev
? 'static/icons/icon.png'
: __dirname + '/static/icons/icon.png';
let trayIcon = isDev
? 'static/icons/icon-tray.png'
: __dirname + '/static/icons/icon-tray.png';
let mainUrl = isPopup => isDev ? `http://localhost:8080/${isPopup ? '/#/popout' : ''}` : url.format({
pathname: path.join(__dirname, "dist", "index.html"),
protocol: "file:",
slashes: true,
hash:isPopup ? '/popout' : null
});
const quit = () => {
if(global && global.appShared && global.appShared.savingData){
setTimeout(() => {
quit();
}, 100);
} else {
if(global && global.appShared && global.appShared.QuitWatcher !== null)
global.appShared.QuitWatcher();
app.quit();
}
}
let tray, mainWindow;
const setupMenu = () => {
const menu = new Menu();
mainWindow.setMenu(menu);
const template = [{
label: "Application",
submenu: [
{ label: "About Application", selector: "orderFrontStandardAboutPanel:" },
{ type: "separator" },
{ label: "Quit", accelerator: "Command+Q", click: () => { quit(); }}
]}, {
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
};
const restoreInstance = () => {
mainWindow.show();
if(mainWindow.isMinimized()) mainWindow.restore();
};
const activateInstance = e => {
if(e) e.preventDefault();
if(!mainWindow) return;
restoreInstance();
};
const setupTray = () => {
tray = new Tray(trayIcon);
const contextMenu = Menu.buildFromTemplate([
{label: 'Open', type: 'normal', click:() => restoreInstance()},
{label: 'Exit', type: 'normal', click:() => quit()}
]);
tray.setToolTip('Scatter Desktop Companion');
tray.setContextMenu(contextMenu);
tray.on('click', () => restoreInstance())
};
const createScatterInstance = () => {
app.setAsDefaultProtocolClient('scatter');
const createMainWindow = (show, backgroundColor) => new BrowserWindow({
width: 900,
height: 800,
frame: false,
radii: [5,5,5,5],
icon,
resizable: true,
minWidth: 800,
minHeight:720,
titleBarStyle:'hiddenInset',
backgroundColor,
show,
});
mainWindow = createMainWindow(false, '#111111');
mainWindow.loadURL(mainUrl(false));
// if main window is ready to show, then destroy the splash window and show up the main window
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
// mainWindow.openDevTools();
mainWindow.loadURL(mainUrl(false));
mainWindow.on('closed', () => mainWindow = null);
mainWindow.on('close', () => quit());
setupTray();
setupMenu();
LowLevelWindowService.onMainWindowReady();
};
app.on('ready', createScatterInstance);
app.on('activate', activateInstance);
app.on('window-all-closed', () => quit())
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
const isLocal = url.startsWith('https://127.0.0.1');
if (isLocal) {
event.preventDefault()
callback(true)
} else {
callback(false)
}
})
const callDeepLink = url => {
if(global.appShared.ApiWatcher !== null)
global.appShared.ApiWatcher(url);
}
const shouldQuit = app.makeSingleInstance(argv => {
if (process.platform === 'win32') callDeepLink(argv.slice(1));
if (mainWindow) activateInstance();
})
if (shouldQuit) quit();
app.on('will-finish-launching', () => {
app.on('open-url', (e, url) => {
e.preventDefault();
callDeepLink(url)
})
});
const isMac = () => process.platform === 'darwin';
let waitingPopup;
class LowLevelWindowService {
static getWindow(width = 800, height = 600){
return new Promise(resolve => {
const win = new BrowserWindow({ width, height, frame: false, radii: [5,5,5,5], icon:'assets/icon.png', show:false, });
win.loadURL(mainUrl(true));
win.once('ready-to-show', () => resolve(win));
})
}
static async onMainWindowReady(){
waitingPopup = await this.getWindow(1,1);
}
static async openPopOut(onReady = () => {}, onClosed = () => {}, width = 800, height = 600, dontHide = false){
let win = waitingPopup;
if(!win) win = await this.getWindow();
else waitingPopup = null;
win.setSize(width, height);
// Getting the screen to display the popup based on
// where the user is at the time ( for dual monitors )
const mousePoint = electron.screen.getCursorScreenPoint();
const activeDisplay = electron.screen.getDisplayNearestPoint(mousePoint);
let {width:screenWidth, height:screenHeight} = activeDisplay.workAreaSize;
const leftBound = activeDisplay.bounds.x;
win.setPosition(screenWidth + leftBound - width - 2, screenHeight - height - 2);
win.once('closed', async () => {
// This is a fix for MacOS systems which causes the
// main window to always pop up after popups closing.
if (!dontHide && isMac()) {
// mainWindow.hide();
Menu.sendActionToFirstResponder('hide:');
// mainWindow.show();
}
onClosed(win);
win = null;
});
onReady(win);
win.show();
win.setAlwaysOnTop(true, "floating");
win.focus();
if(isMac()){
app.dock.hide();
win.setAlwaysOnTop(false);
win.setVisibleOnAllWorkspaces(true);
win.setFullScreenable(false);
app.dock.show();
}
waitingPopup = await this.getWindow(1, 1);
return win;
}
}
const notifier = require("node-notifier");
class NotificationService {
static pushNotification(title, body){
notifier.notify({
message:body,
title,
appID:'com.get-scatter.server',
sound: false,
icon,
wait:false
});
}
}
const Transport = require('@ledgerhq/hw-transport-node-hid');
const NodeMachineId = require('node-machine-id');
global.appShared = { Transport, QuitWatcher:null, ApiWatcher:null, LowLevelWindowService, NotificationService, NodeMachineId, savingData:false };
const ecc = require("eosjs-ecc");
let seed, key;
ipcMain.on('key', (event, arg) => {
if(event.sender.history[0].indexOf('popout') > -1) return;
if(key) return;
key = arg;
});
ipcMain.on('seeding', (event, arg) => seed = arg);
ipcMain.on('seed', (event, arg) => {
const {data, sig} = arg;
if(!isDev && ecc.recover(sig, 'seed') !== key) return event.sender.send('seed', null);
event.sender.send('seed', seed);
});