-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (111 loc) · 3.16 KB
/
index.js
File metadata and controls
134 lines (111 loc) · 3.16 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
// jshint esversion: 6
'use strict';
const app = require('app');
const BrowserWindow = require('browser-window');
const Menu = require('menu');
const template = require('./scripts/menu');
const jsonfile = require('jsonfile');
// shouldn't have to manually include this but we do, thanks babel
const Buffer = require("buffer").Buffer;
const fs = require("fs");
const ipcMain = require('ipc-main');
// R core
const R = require("r-script");
// report crashes to the Electron project
require('crash-reporter').start();
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// prevent window being garbage collected
let mainWindow;
var menu = Menu.buildFromTemplate(template);
function onClosed() {
// dereference the window
// for multiple windows store them in an array
mainWindow = null;
}
function createMainWindow() {
const is2nd = process.argv.indexOf('--2nd') >= 0;
var opts = {
width: 1200,
height: 600,
'accept-first-mouse': true,
'title-bar-style': 'hidden'
};
if (is2nd) {
setOptsForDualScreen(opts);
}
const win = new BrowserWindow(opts);
if (process.env.DEV) {
win.loadURL('http://localhost:8000/dev.html');
win.openDevTools();
} else {
win.loadURL(`file://${__dirname}/index.html`);
}
win.on('closed', onClosed);
if (menu) {
Menu.setApplicationMenu(menu);
menu = null;
}
return win;
}
function setOptsForDualScreen(opts) {
var atomScreen = require('screen');
var displays = atomScreen.getAllDisplays();
var d2 = displays.length > 1 ? displays[1] : null;
if (d2) {
opts.x = d2.bounds.x + (d2.size.width - opts.width) / 2;
opts.y = d2.bounds.y + (d2.size.height - opts.height) / 2;
}
}
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
mainWindow = createMainWindow();
// dev environment live reloader shiet
if (process.env.DEV) {
const watcher = require('./scripts/watcher.js');
watcher.watch(app, ['./index.js', './scripts']);
}
});
// TODO: come up with a seperate file for ipcMain event handling
// Or an object wrapper or something
// Respond to request for r version from frontend
ipcMain.on('request-r-version', (event, arg) => {
R("r/version.r")
.data({})
.call(function(err, d) {
if (err){
event.sender.send('r-version', new Buffer(err).toString());
} else event.sender.send('r-version', d);
});
});
// reads a file for the first time
ipcMain.on('request-updateFile', (event, arg) => {
let ret;
jsonfile.readFile(arg, function(err, obj) {
if (err) {
ipcMain.sender.send('updateFile',
"Error: problem reading your file.");
}
else ipcMain.sender.send('updateFile', obj);
});
});
ipcMain.on('request-updateRDef', (event, arg) => {
jsonfile.readFile('./cache/r_doc_model.json', function(err, obj) {
if (err) {
ipcMain.sender.send('updateRDef',
"Error: problem reading R defs your file.");
}
else ipcMain.sender.send('updateRDef', obj);
});
});
ipcMain.on('synchronous-message', (event, arg) => {
// console.log(arg); // prints "ping"
event.returnValue = 'sync: print this string in react frontend --thanks, the backend';
});