forked from OttoDIY/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electronApp.js
208 lines (204 loc) · 6.14 KB
/
electronApp.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
var {electron, ipcMain, app, BrowserWindow, globalShortcut, dialog} = require('electron')
var { autoUpdater } = require("electron-updater")
var path = require('path')
var mainWindow
var termWindow
var factoryWindow
var promptWindow
var promptOptions
var promptAnswer
autoUpdater.autoDownload = false
autoUpdater.logger = null
function createWindow () {
mainWindow = new BrowserWindow({width: 1240, height: 700, icon: 'www/media/icon.png', frame: false, movable: true,webPreferences:{
nodeIntegration:true
}})
if (process.platform == 'win32' && process.argv.length >= 2) {
mainWindow.loadURL(path.join(__dirname, '../../www/index.html?url='+process.argv[1]))
} else {
//mainWindow.loadURL(path.join(__dirname, '../../www/index.html'))
mainWindow.loadURL('file://' + __dirname + '/www/index_electron.html');
}
mainWindow.setMenu(null)
mainWindow.on('closed', function () {
mainWindow = null
})
}
function createTerm() {
termWindow = new BrowserWindow({width: 640, height: 560, 'parent': mainWindow, resizable: false, movable: true, frame: false, modal: true,webPreferences:{
nodeIntegration:true
}})
// termWindow.loadURL(path.join(__dirname, "../../../www/term.html"))
termWindow.loadURL('file://' + __dirname + '/www/term.html');
termWindow.setMenu(null)
termWindow.on('closed', function () {
termWindow = null
})
}
function createRepl() {
termWindow = new BrowserWindow({width: 640, height: 515, 'parent': mainWindow, resizable: false, movable: true, frame: false, modal: true,webPreferences:{
nodeIntegration:true
}})
// termWindow.loadURL(path.join(__dirname, "../../../www/repl.html"))
termWindow.loadURL('file://' + __dirname + '/www/repl.html');
termWindow.setMenu(null)
termWindow.on('closed', function () {
termWindow = null
})
}
function createfactory() {
factoryWindow = new BrowserWindow({width: 1066, height: 640, 'parent': mainWindow, resizable: true, movable: true, frame: false,webPreferences:{
nodeIntegration:true
}})
//factoryWindow.loadURL(path.join(__dirname, "../../../www/factory.html"))
termWindow.loadURL('file://' + __dirname + '/www/factory.html');
factoryWindow.setMenu(null)
factoryWindow.on('closed', function () {
factoryWindow = null
})
}
function promptModal(options, callback) {
promptOptions = options
promptWindow = new BrowserWindow({width:360, height: 135, 'parent': mainWindow, resizable: false, movable: true, frame: false, modal: true,webPreferences:{
nodeIntegration:true
}})
//promptWindow.loadURL(path.join(__dirname, "../../../www/modalVar.html"))
promptWindow.loadURL('file://' + __dirname + '/www/modalVar.html');
promptWindow.on('closed', function () {
promptWindow = null
callback(promptAnswer)
})
}
function open_console(mainWindow = BrowserWindow.getFocusedWindow()) {
if (mainWindow) mainWindow.webContents.toggleDevTools()
}
function refresh(mainWindow = BrowserWindow.getFocusedWindow()) {
if (mainWindow) mainWindow.webContents.reloadIgnoringCache()
}
app.on('ready', function () {
createWindow()
globalShortcut.register('F8', open_console)
globalShortcut.register('F5', refresh)
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
app.on('window-all-closed', function () {
globalShortcut.unregisterAll()
if (process.platform !== 'darwin') app.quit()
})
ipcMain.on("version", function () {
autoUpdater.checkForUpdates()
})
ipcMain.on("prompt", function () {
createTerm()
})
ipcMain.on("repl", function () {
createRepl()
})
ipcMain.on("factory", function () {
createfactory()
})
ipcMain.on("openDialog", function (event, data) {
event.returnValue = JSON.stringify(promptOptions, null, '')
})
ipcMain.on("closeDialog", function (event, data) {
promptAnswer = data
})
ipcMain.on("modalVar", function (event, arg) {
promptModal(
{"label": arg, "value": "", "ok": "OK"},
function(data) {
event.returnValue = data
}
)
})
ipcMain.on('save-bin', function (event) {
dialog.showSaveDialog(mainWindow,{
title: 'Exporter les binaires',
defaultPath: 'Otto_hex',
filters: [{ name: 'Binary', extensions: ['hex']}]
},
function(filename){
event.sender.send('saved-bin', filename)
})
})
ipcMain.on('save-ino', function (event) {
dialog.showSaveDialog(mainWindow,{
title: 'Save format .INO',
defaultPath: 'Otto_Arduino',
filters: [{ name: 'Arduino', extensions: ['ino'] }]
},
function(filename){
event.sender.send('saved-ino', filename)
})
})
ipcMain.on('save-py', function (event) {
dialog.showSaveDialog(mainWindow,{
title: 'Save format .PY',
defaultPath: 'Otto_python',
filters: [{ name: 'python', extensions: ['py'] }]
},
function(filename){
event.sender.send('saved-py', filename)
})
})
ipcMain.on('save-bloc', function (event) {
dialog.showSaveDialog(mainWindow,{
title: 'Save format .BLOC',
defaultPath: 'Otto_block',
filters: [{ name: 'Ottoblockly', extensions: ['bloc'] }]
},
function(filename){
event.sender.send('saved-bloc', filename)
})
})
ipcMain.on('save-csv', function (event) {
dialog.showSaveDialog(mainWindow,{
title: 'Save format CSV',
defaultPath: 'Otto_csv',
filters: [{ name: 'data', extensions: ['csv'] }]
},
function(filename){
event.sender.send('saved-csv', filename)
})
})
autoUpdater.on('error', function(error) {
dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString())
})
autoUpdater.on('update-available', function() {
dialog.showMessageBox(mainWindow,{
type: 'none',
title: 'Update',
message: "A new version is available, do you want to download and install it now?",
buttons: ['Yes', 'No'],
cancelId: 1,
noLink: true
},
function(buttonIndex) {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate()
}
else {
return
}
})
})
autoUpdater.on('update-not-available', function() {
dialog.showMessageBox(mainWindow,{
title: 'Updated',
message: 'Your version is up to date.'
})
})
autoUpdater.on('update-downloaded', function() {
dialog.showMessageBox(mainWindow,{
title: 'Updated',
message: "Download finished, the application will install then restart.."
}, function() {
setImmediate(function(){
autoUpdater.quitAndInstall()
})
})
})
module.exports.open_console = open_console
module.exports.refresh = refresh