-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.js
338 lines (293 loc) · 13 KB
/
main.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
"use strict";
/* To see the below console.log printouts, you must launch DDE from
a cmd line. IN windows run a terminal window, CD to your
Program Files folder and find a file dde_dev_env.exe
then that's the right folder so
in hte terminal window, cd to that folder and
enter dde_dev_env to launch.
Then the below console.log prinouts appear in that terminal window.
*/
var electron = require('electron')
const SerialPort = require("serialport")
// Module to control application life.
const app = electron.app
const globalShortcut = electron.globalShortcut
const os = require('os')
const user_homedir = os.homedir()
const ipc = require('electron').ipcMain
const request = require("request");
global.app_path = app.getAppPath()
console.log("in main.js with __dirname: " + __dirname)
console.log("in main.js with app_path: " + global.app_path)
const fs = require('fs');
// Module to create native browser window.
//Because MS screwed up the relationhip between OneDrive and Documents in Windows 10,
//we do a lot of work to work around their bug.
let documents_dir = app.getPath("documents")
if (!documents_dir.endsWith("Documents")) { documents_dir += "/Documents" } //needed on Unbuntu OS for Dexter running DDE, all of whcih have a Documents dir
console.log("First try getting 'documents' yielded: " + documents_dir)
let the_dde_apps_folder = documents_dir + "/dde_apps"
let exists = fs.existsSync(the_dde_apps_folder)
console.log(the_dde_apps_folder + " exists=" + exists)
if(!exists) { //probably on windows
let last_backslash_index = documents_dir.lastIndexOf("\\")
let before_docs_path = documents_dir.substring(0, last_backslash_index)
the_dde_apps_folder = before_docs_path + "\\OneDrive\\Documents\\dde_apps"
exists = fs.existsSync(the_dde_apps_folder)
console.log(the_dde_apps_folder + " exists=" + exists)
if (!exists) {
console.log('DDE cannot find the folder "Documents/apps_dir/" . You must create it.')
the_dde_apps_folder = documents_dir + "/dde_apps" //user will need to create it
}
}
//let init_file_one_drive = app.getPath("OneDrive/documents") + "/dde_apps"
//exists = fs.existsSync(init_file_one_drive)
//console.log(init_file_one_drive + " exists 2: " + exists)
//console.log("documents_dir = " + documents_dir)
global.dde_apps_folder = the_dde_apps_folder //documents_dir + "/dde_apps"
console.log("in main.js with dde_apps_folder: " + global.dde_apps_folder)
const BrowserWindow = electron.BrowserWindow
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow
function is_kiosk_mode() {
let kiosk_mode = false
let init_path = the_dde_apps_folder + "/dde_persistent.json"
if(fs.existsSync(init_path)){
let content = fs.readFileSync(init_path, "utf8")
if(typeof(content) == "string"){
const start_of_content = content.indexOf("{")
if (start_of_content != -1) { content = content.substring(start_of_content) } //get rid of comment at top of file that isn't official JSON.
try {
let obj = JSON.parse(content)
console.log(JSON.stringify(obj))
if(obj.kiosk){
console.log("DDE set to kiosk mode. Quit by typing:\n" +
"WinOS: Alt+F4\n" +
"MacOS: Cmd+Q\n" +
"Linux: Ctrl+Alt+Esc")
kiosk_mode = true
}
else {
console.log("DDE is not in kiosk mode.")
kiosk_mode = false
}
}
catch(err) {
console.log(init_path + " does not have valid JSON.")
}
}
else {
console.log(init_path + " exists but could not be read.")
}
}
else {console.log("There is no file: " + init_path)}
return kiosk_mode
}
function createWindow() {
let kiosk_mode = is_kiosk_mode()
// Create the browser window.
console.log("kiosk mode? " + kiosk_mode)
mainWindow = new BrowserWindow({width: 1000, height: 600, show: false,
kiosk: kiosk_mode, //makes DDE window be FULL SCREEN, ie no os title bar, etc. locks down app.
title: "Dexter Development Environment" //not obvious that this actually shows up anywhere.
})
//mainWindow.focus() //doesn't do anything.
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)
// Open the DevTools.
//mainWindow.webContents.openDevTools() //shows chrome dev tools. nice.
// Emitted when the main window is closed. (ie click the "red circle" in Mac window bar
//as well as chose "Quit" from the Top window bar on Mac
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
console.log("top of on.closed")
//mainWindow.webContents.send('save_current_file') //doesn't work now using window.onbeforeunload in reneder process (editor.js)
mainWindow = null
})
mainWindow.once('ready-to-show', () => { mainWindow.show()})
var mainWindow_for_closure = mainWindow
mainWindow.on("resize", function(){
mainWindow_for_closure.webContents.send('record_dde_window_size')
})
mainWindow.on("move", function(){
mainWindow_for_closure.webContents.send('record_dde_window_size')
})
}
// win.setSize(width, height)
// win.getSize() //retruns an array of 2 ints, width and height.
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', function() {
createWindow();
//console.log("hi fry")
mainWindow.webContents.send("main_is_ready")
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
//console.log("top of on window-all-closed")
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
function register_shortcuts(){
globalShortcut.register('CommandOrControl+X', () => {
mainWindow.webContents.cut()
})
globalShortcut.register('CommandOrControl+C', () => {
mainWindow.webContents.copy()
})
globalShortcut.register('CommandOrControl+V', () => {
mainWindow.webContents.paste()
})
}
app.on("browser-window-focus", register_shortcuts)
app.on("browser-window-blur", function(){ globalShortcut.unregisterAll() })
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
const accel_key = ((os.platform() == "darwin")? "cmd" : "ctrl")
//const MenuItem = require('menu-item') //fails
require('electron-context-menu')({
//prepend: (params, browserWindow) => [{
// only show it when right-clicking images
// visible: params.mediaType === 'image'
//}],
showInspectElement: true,
labels: {cut: 'Cut ' + accel_key + ' x',
copy: 'Copy ' + accel_key + ' c',
paste: 'Paste ' + accel_key + ' v'
}
/* //fails because I cant find how to load/require MenuItem
append: function(){
return [new MenuItem({label: "Select Call",
click: function(){console.log("clicked select call")}
})
] } */
});
ipc.on('synchronous-message', function (event, arg) {
event.returnValue = 'pong'
})
ipc.on("open_dev_tools", function(event){
console.log("top of open_dev_tools2")
if (mainWindow.isDevToolsOpened()) {
mainWindow.devToolsWebContents.focus()
}
else { mainWindow.webContents.openDevTools({mode:"undocked"})} //if devtools window is closed this opens it
//and focuses on it. If Not it does nothing but i need to focus on the window.
//mainWindow.webContents.devToolsWebContents.focus() //errors
//mainWindow.blur() //causes step button to remain "down".
//mainWindow.webContents.toggleDevTools()
//mainWindow.webContents.toggleDevTools()
//console.log(mainWindow.webContents)
event.returnValue = true //needed just to get the open_dev_tools button to not be highlighted
})
//called from serial.js serial_devices to get sychronous return value
ipc.on('serial_devices', function(event){
//console.log("in main sd")
SerialPort.list(function(err, ports) {
//console.log("in main sd ports: " + ports)
event.returnValue = ports
})
})
//used by render process get_page for synchronous call
//beware, if the url is non-existent, this might not return an error.
ipc.on('get_page', function(event, url_or_options){
console.log("in get_page in main.js with: " + url_or_options)
try{
request(url_or_options,
function(err, response, body) {
//when the computer isn't connected to the web, response
//is undefined.
var stat = (response ? response.statusCode : null)
console.log("main get page with err: " + err +
"\n statusCode: " + stat +
"\nbody: " + body)
if (err) { event.returnValue = "Error: " + err }
else if(stat !== 200){
event.returnValue = "Error: got status code: " + stat
}
else { event.returnValue = body }
})
}
catch(err) { event.returnValue = "Error: " + err}
})
ipc.on('show_page', function(event, url, options={width: 800, height: 600}){
console.log("in show_page in main.js with url: " + url + " options: " + JSON.stringify(options))
try{
let win = new BrowserWindow(options)
win.loadURL(url)
//win.loadUrl("data:text/html;charset=utf-8," + encodeURI("<body><i>hillo</i></body>"));
win.show()
event.returnValue = null
}
catch(err) { event.return_value = "Error: " + err }
})
// see https://github.com/konsumer/electron-prompt/blob/master/main.js
// prompt normally not supported by Electron, but this clever implementation works
var promptResponse
ipc.on('prompt', function(eventRet, arg) {
console.log("top of main on prompt")
console.log(arg)
promptResponse = null
var promptWindow = new BrowserWindow({
x: arg.x,
y: arg.y,
width: arg.width,
height: arg.height,
show: false,
resizable: false,
movable: false,
alwaysOnTop: true,
frame: false
})
const title_html = '<div style="margin:3px;padding:0px;background-color:' + arg.window_frame_background_color +
';font-size:21px;">' + arg.title + '</div><div style="padding:10px;">' +
arg.description +
'</div>'
const body_html = ' <input style="background-color:white;margin:10px;width:360px;font-size:14px;" id="val_id" value="' + arg.default_value + '" autofocus /> ' +
`<button onclick="require(\'electron\').ipcRenderer.send('prompt-response', val_id.value); window.close();">Ok</button>` +
'<button onclick="window.close();">Cancel</button>'
const style_html = ' <style>body {font-family: sans-serif; background-color:' + arg.background_color + '; margin:0px; padding:0px; border:8px solid }' +
'\nbutton {float:right; margin-left: 10px; margin-right: 10px; background-color:' +
arg.button_background_color +
'} ' +
'\nlabel,input {margin-bottom: 10px; width: 100%; display:block;}' +
'</style>'
console.log("\ntitle_html")
console.log(title_html)
console.log("\nbody_html")
console.log(body_html)
console.log("\nstyle_html")
console.log(style_html)
promptWindow.on('closed', function(some_arg) {
console.log("promptWindow.on closed called.")
console.log(some_arg)
console.log("Got val_id: " + val_id)
promptWindow = null
})
const prompt_html = title_html + body_html + style_html
promptWindow.loadURL('data:text/html,' + prompt_html)
promptWindow.show()
//console.log("the typein val: " + document.getElementById('val').value) errors document not defined
})
ipc.on('prompt-response', function(event, arg) {
console.log("on prompt-reponse called with event: " + event + " arg: " + arg)
if (arg === ''){ arg = null }
promptResponse = arg
})
ipc.on('set_dde_window_size', function(event, x, y, width, height){
mainWindow.setPosition(x, y)
mainWindow.setSize(width, height)
})