This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
453 lines (396 loc) · 9.53 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
'use strict';
const path = require('path');
const os = require('os');
const fs = require('fs');
const electron = require('electron');
const settings = require('electron-settings');
const screenshot = require('screenshot-node');
const isDev = require('electron-is-dev');
// New Upload object to handle upload and upload events
const Upload = require('./upload');
const autostart = require('./autostart');
const upload = new Upload();
const globalShortcut = electron.globalShortcut;
const ipc = electron.ipcMain;
const Menu = electron.Menu;
const Tray = electron.Tray;
const app = electron.app;
let appIcon = null;
// Needed to get transparent window on linux
if (process.platform === 'linux') {
app.disableHardwareAcceleration();
}
// Adds debug features like hotkeys for triggering dev tools and reload
if (isDev) {
require('electron-debug')();
}
// Prevent window being garbage collected
let mainWindow;
let mainRender = null;
let settingsWindow = null;
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance
// Should maybe open the main window or flash the tray to
// indicate that there allready is an app running.
console.log(workingDirectory);
});
if (shouldQuit) {
app.quit();
}
// Dereference main window/process
function onClosedMainWindow() {
// Dereference the window
mainWindow = null;
}
function onClosedMainRender() {
// Dereference the main render
mainRender = null;
}
function onClosedSettingsWindow() {
settingsWindow = null;
}
function createMainWindow() {
const win = new electron.BrowserWindow({
width: 0,
height: 0,
show: false,
icon: path.join(__dirname, '/assets/64x64.png'),
skipTaskbar: true
});
win.on('closed', onClosedMainWindow);
return win;
}
function createAboutWindow() {
let win = new electron.BrowserWindow({
width: electron.screen.getPrimaryDisplay().bounds.width * 0.2,
height: electron.screen.getPrimaryDisplay().bounds.width * 0.15,
show: false,
icon: path.join(__dirname, '/assets/64x64.png')
});
const windowPath = path.join('file://', __dirname, 'windows/about.html');
win.loadURL(windowPath);
win.on('closed', () => {
win = null;
});
win.on('ready-to-show', () => {
win.show();
});
}
function createMainRenderWindow() {
if (mainRender !== null) {
mainRender.focus();
return;
}
const win = new electron.BrowserWindow({
width: electron.screen.getPrimaryDisplay().bounds.width * 0.2,
height: electron.screen.getPrimaryDisplay().bounds.width * 0.3,
show: false,
icon: path.join(__dirname, '/assets/64x64.png')
});
const windowPath = path.join('file://', __dirname, 'windows/main.html');
win.loadURL(windowPath);
win.upload = upload;
win.on('closed', onClosedMainRender);
win.on('ready-to-show', () => {
win.show();
});
return win;
}
function createSettingsWindow() {
if (settingsWindow !== null) {
settingsWindow.focus();
return;
}
const win = new electron.BrowserWindow({
width: electron.screen.getPrimaryDisplay().bounds.width * 0.2,
height: electron.screen.getPrimaryDisplay().bounds.width * 0.3,
show: false,
icon: path.join(__dirname, '/assets/64x64.png')
});
const windowPath = path.join('file://', __dirname, 'windows/settings.html');
win.loadURL(windowPath);
win.upload = upload;
win.on('closed', onClosedSettingsWindow);
win.on('ready-to-show', () => {
win.show();
});
return win;
}
// Take screenshot
function takeScreenshot(size, bounds = {x: 0, y: 0, width: 0, height: 0}) {
setTimeout(() => {
const tempDir = os.tmpdir();
const tmpath = path.join(tempDir, String(Date.now()));
screenshot.saveScreenshot(bounds.x, bounds.y, bounds.width, bounds.height, tmpath, err => {
if (err) {
console.log(err);
}
// Saves the screenshot to a specified location
function saveFile() {
electron.dialog.showSaveDialog({title: 'Save File', defaultPath: os.homedir() + '/.png'}, filename => {
// Check to see if it is undefine (User closed dialog window)
if (filename !== undefined) {
fs.createReadStream(tmpath).pipe(fs.createWriteStream(filename));
}
});
}
// Application Menu
const appMenu = Menu.buildFromTemplate([
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CommandOrControl+S',
click: saveFile
},
{
label: 'Save As...',
click: saveFile
},
{
label: 'Close Window',
role: 'close'
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Copy',
accelerator: 'CommandOrControl+C',
click: () => {
electron.clipboard.writeImage(tmpath);
}
}
]
},
{
label: 'Help',
submenu: [
{
label: 'Version ' + app.getVersion(),
enabled: false
},
{
label: 'Report an Issue...',
click() {
electron.shell.openExternal('https://github.com/Kuzat/hyperdesktopjs/issues/new');
}
},
{
label: 'About Hyperdesktopjs',
click: createAboutWindow
}
]
}
]);
// Creating the window
let win = new electron.BrowserWindow({
title: 'Preview window',
show: false,
width: size.width,
height: size.height,
icon: path.join(__dirname, '/assets/64x64.png')
});
win.tempName = tmpath;
win.setMenu(appMenu);
const windowPath = path.join('file://', __dirname, 'windows/screenshot-preview.html');
win.loadURL(windowPath);
ipc.once('ready-for-show', () => {
win.show();
});
const uploadFunc = () => {
upload.upload(tmpath);
};
ipc.once('ready-for-upload-' + tmpath, uploadFunc);
win.on('closed', () => {
ipc.removeListener('ready-for-upload', uploadFunc);
win = null;
fs.unlinkSync(tmpath);
});
return win;
});
}, 200);
}
// Get screenshot bounds
function getBounds(callback) {
let win = new electron.BrowserWindow({
title: 'snipper',
fullscreen: true,
width: electron.screen.getPrimaryDisplay().bounds.width,
height: electron.screen.getPrimaryDisplay().bounds.height,
transparent: true,
frame: false,
alwaysOnTop: true,
skipTaskbar: true,
resizeable: false,
movable: false,
show: false
});
const windowPath = path.join('file://', __dirname, 'windows/snippet.html');
win.loadURL(windowPath);
win.on('closed', () => {
win = null;
});
win.on('ready-to-show', () => {
win.show();
});
ipc.once('ready-for-bounds', (event, arg) => {
callback(arg);
});
}
// ######### HANDLE APP EVENTS ###########
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
// Seting default settings
settings.set('default', {
hotkeys: {
screenshot: 'CommandOrControl+Shift+3',
selectiveScreenshot: 'CommandOrControl+Shift+4'
},
upload: {
type: 0,
options: ['Imgur', 'FTP', 'none']
},
general: {
saveToFolder: {
active: false,
folder: ''
},
copyToClipboard: false,
openLink: true,
autoLaunch: true
},
version: app.getVersion()
});
if (settings.get('user') === undefined) {
settings.set('user', settings.get('default'));
}
if (settings.get('user.version') !== app.getVersion()) {
settings.set('user', settings.get('default'));
}
mainWindow = createMainWindow();
// Update autostart settings
autostart.update();
// A suitable size for the preview window
let size = null;
if (!shouldQuit) {
size = {
width: electron.screen.getPrimaryDisplay().bounds.width * 0.65,
height: electron.screen.getPrimaryDisplay().bounds.height * 0.80
};
}
// Tray icon Menu. Click functions needs to be implemented
appIcon = new Tray(path.join(__dirname, '/assets/64x64.png'));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Open Window',
click() {
mainRender = createMainRenderWindow();
}
},
{
label: 'Take Selective Screenshot',
click() {
getBounds(bounds => {
takeScreenshot(size, bounds);
});
}
},
{
label: 'Take Screenshot',
click() {
takeScreenshot(size);
}
},
{type: 'separator'},
{
label: 'Settings',
click() {
settingsWindow = createSettingsWindow();
}
},
{
label: 'About',
click: createAboutWindow
},
{label: 'Quit', role: 'quit'}
]);
// Application Menu
const appMenu = Menu.buildFromTemplate([
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CommandOrControl+S'
},
{
label: 'Save As...'
},
{
label: 'Close Window',
role: 'close'
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Copy',
accelerator: 'CommandOrControl+C'
}
]
},
{
label: 'Help',
submenu: [
{
label: 'Version ' + app.getVersion(),
enabled: false
},
{
label: 'Report an Issue...',
click() {
electron.shell.openExternal('https://github.com/Kuzat/hyperdesktopjs/issues/new');
}
},
{
label: 'About Hyperdesktopjs',
click: createAboutWindow
}
]
}
]);
// Set the application menu
Menu.setApplicationMenu(appMenu);
// Set the context menu for the tray icon
appIcon.setContextMenu(contextMenu);
// Set the global hotkeys to the different screenshot methods
const val = settings.get('user.hotkeys');
// Full screenshot
globalShortcut.register(val.screenshot, () => {
takeScreenshot(size);
});
// Screenshot of selected area
globalShortcut.register(val.selectiveScreenshot, () => {
getBounds(bounds => {
takeScreenshot(size, bounds);
});
});
});