This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
354 lines (302 loc) · 9.68 KB
/
index.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const bugsnag = require('bugsnag');
const electron = require('electron');
const app = require('electron').app;
const path = require('path');
const pjson = require('./package.json');
bugsnag.register('b3d3a88f13b0f8e22b4485b8b04939bd', {
releaseStage: process.env.NODE_ENV,
appVersion: pjson.version,
sendCode: true,
projectRoot: path.resolve(app.getPath('exe'), '../..'), // Locates `Content` directory within the .app
metaData: {
process: {
arch: process.arch,
argv: process.argv,
pid: process.pid,
platform: process.platform,
version: process.version,
cwd: process.cwd(),
features: process.features,
uptime: process.uptime(),
versions: process.versions,
}
}
});
bugsnag.onBeforeNotify(function () {
// Notifies us of initial startup runtime errors & normal errors when user has specified allowing bugreports
return !global.options || global.options.bugreport === true;
});
process.on('unhandledRejection', function (err) {
console.error('Unhandled error: ' + (err && err.stack || err));
bugsnag.notify(err);
});
var ipc = require('electron').ipcMain;
var globalShortcut = require('electron').globalShortcut;
var autoUpdater = require('electron').autoUpdater;
var menubar = require('menubar');
var fs = require('fs');
var AutoLaunch = require('auto-launch');
var os = require('os')
const isDev = require('electron-is-dev');
try {
global.options = require(os.homedir() + '/.nimble-options.json');
} catch (e) {
global.options = {
"mathjs": true,
"startup": true,
"center": false,
"bugreport": true,
"autoupdate": true,
theme: {
"red": false,
"orange": true,
"yellow": false,
"green": false,
"blue": false,
"purple": false,
"pink": false,
"contrast": false
},
enableDefaultSuggestions: true,
customSuggestions: []
}
}
require('shelljs/global');
var mb = menubar({
height: 42,
width: 380,
icon: __dirname + '/assets/img/menubar_iconTemplate.png',
index: 'file://' + __dirname + '/src/index.html',
preloadWindow: true
});
ipc.on('resize', function(event, arg) {
// this is the animation code that crashes
/*
var h = mb.window.getSize()[1];
while(h !== arg.height) {
setTimeout(function () {
if(h > arg.height) {
h--;
} else if (h < arg.height) {
h++;
}
console.log(h)
mb.window.setSize(mb.window.getSize()[0], h)
}, 1)
}
*/
var finalDim = {
height: null,
width: null
};
if (arg.height > 533) {
// if height is way too big, then just set it short and scroll
mb.window.setBounds({
x: mb.window.getPosition()[0],
y: optfunc.getYValue(),
width: arg.width,
height: 533
}, true);
finalDim.height = 533;
finalDim.width = arg.width;
} else {
mb.window.setBounds({
x: mb.window.getPosition()[0],
y: optfunc.getYValue(),
width: arg.width,
height: arg.height
}, true);
finalDim.height = arg.height;
finalDim.width = arg.width;
}
optfunc.position();
console.log("Resizing window to " + finalDim.width + " x " + finalDim.height + "\n");
});
ipc.on('toggleview', function(event) {
var position = [mb.window.getPosition()[0], mb.window.getPosition()[1]];
if(mb.window.isVisible() === true) {
mb.hideWindow();
} else if (mb.window.isVisible() === false) {
mb.showWindow();
}
mb.window.setPosition(position[0], position[1]);
});
ipc.on('reset-window', function(event) {
mb.window.setBounds({
x: mb.window.getPosition()[0],
y: optfunc.getYValue(),
width: 380,
height: 42,
}, true);
optfunc.position();
});
// console.log handler
ipc.on('node_console', function(event, arg) {
console.log(arg.m + "\n")
});
ipc.on('save_options', function(event, arg) {
fs.writeFile(os.homedir() + '/.nimble-options.json', arg, function(err) {
if(err) {
console.log(err);
}
mb.window.webContents.send('did-save-options', !err);
console.log("Options were saved.\n");
});
// make options available here too
global.options = JSON.parse(arg);
// things to do
optfunc.startup();
optfunc.position();
});
// various functions that make options work
var optfunc = {
startup: function() {
var nimbleAutoLauncher = new AutoLaunch({
name: 'Nimble',
path: path.resolve(app.getAppPath(), '../../../')
});
// startup?
if (global.options.startup === true) {
nimbleAutoLauncher.enable();
} else {
nimbleAutoLauncher.disable();
}
return null
},
position: function() {
if(global.options.center === true) {
mb.setOption("window-position", "center");
mb.positioner.move("center");
} else if (global.options.center === false) {
mb.setOption("window-position", "trayCenter");
if (global.bounds) {
mb.positioner.move("trayCenter", global.bounds)
}
}
return null
},
getYValue: function() {
try {
if (global.autohide === true && global.options.center === false) {
return 22
} else {
return mb.window.getPosition()[1]
}
} catch (e) {
return mb.window.getPosition()[1]
}
}
}
ipc.on('quit', function(){
app.quit();
})
mb.on('after-create-window', function() {
// error/log forwarding
process.on("uncaughtException", function(err) {
mb.window.webContents.send("error", err);
})
var _consolelog = console.log.bind(console);
console.log = function log(message) {
mb.window.webContents.send('log', message);
_consolelog(message);
};
mb.window.setResizable(false);
mb.tray.setPressedImage(__dirname + '/assets/img/menubar_icon_pressed.png');
mb.tray
.on('click', click)
.on('right-click', rightClick)
// check if menubar is set to autohide
var script = "defaults read NSGlobalDomain _HIHideMenuBar -bool"
var output = exec(script, {async:true})
output.stdout.on("data", function(data) {
if (data == 1) {
global.autohide = true
console.log("menubar is set to autohide")
} else if (data == 0) {
global.autohide = false
} else {
global.autohide = false
}
mb.window.setBounds({
x: mb.window.getPosition()[0],
y: optfunc.getYValue(),
width: 380,
height: 42
});
})
output.stderr.on("data", function(data) {
console.log("no autohide setting was found, setting to default")
global.autohide = false
mb.window.setBounds({
x: mb.window.getPosition()[0],
y: optfunc.getYValue(),
width: 380,
height: 42
});
})
function click(e, bounds) {
if (e.shiftKey) {
mb.window.openDevTools({
detach: true
})
}
global.bounds = bounds;
}
function rightClick(e, bounds) {
mb.window.webContents.send("tray-rightclick");
}
// global hotkey to toggle nimble
globalShortcut.register('CmdOrCtrl+Shift+=', function() {
var position = [mb.window.getPosition()[0], mb.window.getPosition()[1]];
if(mb.window.isVisible() === true) {
mb.hideWindow();
} else if (mb.window.isVisible() === false) {
mb.showWindow();
}
mb.window.setPosition(position[0], position[1]);
});
});
mb.on('after-show', function() {
if (mb.window) {
mb.window.webContents.send("window-open");
mb.window.setPosition(mb.window.getPosition()[0], optfunc.getYValue());
}
})
mb.on('ready', function() {
// screen size
var screen = require('electron').screen;
global.screenSize = screen.getPrimaryDisplay().size;
// auto update
if (global.options.autoupdate === true && isDev === false) {
var updateFeed = 'https://nimble-autoupdate.herokuapp.com/update/osx/';
autoUpdater.setFeedURL(updateFeed + pjson.version);
autoUpdater.checkForUpdates();
autoUpdater.on('update-available', function() {
console.log('update available and downloading');
require('electron').dialog.showMessageBox({
"message": "Update Downloading",
"detail": "A new update is currently available and downloading. Nimble will let you know before it quits to install the update.",
"buttons": []
})
});
autoUpdater.on('update-downloaded', function(event) {
console.log('update downloaded: ' + event);
require('electron').dialog.showMessageBox({
"message": "Update Ready To Install",
"detail": "Nimble has downloaded a new update. Would you like to quit Nimble and install it?",
"buttons": ["Yes", "No"],
}, function(response) {
switch(response) {
case 1:
break;
case 0:
autoUpdater.quitAndInstall();
break;
}
});
});
}
});
mb.on('will-quit', function() {
globalShortcut.unregisterAll();
});