-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (162 loc) · 6.25 KB
/
main.js
File metadata and controls
195 lines (162 loc) · 6.25 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
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
'use strict';
const { app, BrowserWindow, Tray, Menu, nativeImage, ipcMain } = require('electron');
const path = require('path');
try {
require('electron-reloader')(module);
} catch(_) {}
function handleSquirrelEvent() {
if (process.argv.length === 1) return false;
const ChildProcess = require('child_process');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function(command, args) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, { 'detached': true });
} catch(error) {}
return spawnedProcess;
};
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
app.quit();
return true;
}
}
if (handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
// Formats a raw URI Request to extract the raw args
function formatCMD(arrCMD) {
const strCMD = arrCMD.find(a => a.startsWith('scp-wallet://'));
if (strCMD && strCMD.length > 13) {
// Successfully found args!
return strCMD.endsWith('/') ? strCMD.substring(13).slice(0, -1) :
strCMD.substring(13);
} else {
// No args could be extracted
return '';
}
}
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('scp-wallet', process.execPath, [path.resolve(process.argv[1])]);
}
} else {
app.setAsDefaultProtocolClient('scp-wallet');
}
const fLockSuccess = app.requestSingleInstanceLock();
if (fLockSuccess) {
const DB = require('./src/database/index.js');
const { platform } = require('os');
app.allowRendererProcessReuse = true;
let mainWindow;
let tray;
let fMinToTray = true;
function createWindow() {
mainWindow = new BrowserWindow({
'width': 1024,
'height': 679,
'minWidth': 480,
'minHeight': 540,
'webPreferences': {
'nodeIntegration': true,
},
// Windows: ico, others: png
'icon': path.join(__dirname, 'public', 'imgs', 'scp.' +
(platform() === 'win32' ? 'ico' : 'png')),
'backgroundColor': '#202225'
});
// If the user has a wallet, load the index app, otherwise load the 'setup/begin' app
DB.init().then(() => {
DB.getWallet().then(hasWallet => {
if (hasWallet === null || !hasWallet) {
mainWindow.loadFile('public/begin.html');
} else {
mainWindow.loadFile('public/index.html');
}
});
});
// Setup native menus (tray, context menu, etc)
Menu.setApplicationMenu(null);
const icon = nativeImage.createFromPath(path.join(__dirname, 'public', 'imgs', 'scp-tray.png'));
tray = new Tray(icon);
const trayMenu = Menu.buildFromTemplate([
{ label: 'SCP Wallet', icon, enabled: false },
{ type: 'separator' },
{ label: 'Open', click: () => mainWindow.show() },
{ label: 'Minimise', click: () => mainWindow.minimize() },
{ type: 'separator' },
{ role: 'toggleDevTools', click: () => mainWindow.webContents.toggleDevTools() },
{ type: 'separator' },
{ label: 'Quit', role: 'quit' }
]);
tray.setContextMenu(trayMenu);
tray.on('click', tray.popUpContextMenu);
mainWindow.on('closed', () => mainWindow = null);
mainWindow.on('minimize', () => {
if (tray && fMinToTray) mainWindow.hide();
});
// Accept settings changes from the Render process for 'Minimise to Tray'
ipcMain.on('changeMinToTray', (evt, msg) => fMinToTray = msg);
// Process any launch args
if (process.argv.length > 1) {
// Check if any params were sent, and execute if so.
const strArgs = formatCMD(process.argv);
if (strArgs) {
// Once the main window has loaded, fire the command!
mainWindow.webContents.once('did-finish-load', () => {
mainWindow.webContents.send('cmd', strArgs);
});
}
}
}
app.on('ready', createWindow);
app.on('second-instance', (evt, commandLine) => {
// A second intence was ran: focus our primary instance.
if (mainWindow) {
if (mainWindow.isMinimized())
mainWindow.restore();
mainWindow.focus();
// Check if any params were sent, and execute if so.
const strArgs = formatCMD(commandLine);
if (strArgs)
mainWindow.webContents.send('cmd', strArgs);
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', () => {
if (mainWindow === null) createWindow();
});
} else {
// Second Instance (we'll just nuke it, for now)
if (process.platform !== 'darwin') app.quit();
}