-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
155 lines (126 loc) · 5.07 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
const {app, BrowserWindow, Menu, dialog, ipcMain } = require('electron')
const path = require('path')
const packagejson = require ( './package.json' )
const { productName, version } = require ( './package.json' )
const electronLocalshortcut = require ( 'electron-localshortcut' )
require('@electron/remote/main').initialize()
const Configstore = require ( 'configstore' )
let config = new Configstore ( packagejson.name, {} )
global.recentProjects = config.get ( 'recentProjects' )
if ( !global.recentProjects )
global.recentProjects = []
global.PROD = false
let wasChanged = false
if ( 'ELECTRON_IS_PROD' in process.env ) { // If env variable was set manually ...
// Set global.PROD according the value of ELECTRON_IS_DEV
global.PROD = Number.parseInt(process.env.ELECTRON_IS_PROD) === 1
} else {
// Electron Builder cannot set environment variables. Thus in the finally packaged app,
// we cannot detect the environment based on the env variable (unless someone would set
// it manually). But we can detect if the app is running in producion mode by checking the
// isPackaged property of app
global.PROD = app.isPackaged
}
require ( './logger.js' ) // Stay below the definition of global.PROD. global.PROD is needed to switch the logging path
log ( 'Running in ' + (global.PROD?'production':'development') + ' mode!' )
let mainWindow = undefined
function createWindow () {
mainWindow = new BrowserWindow({
width: global.PROD?1200:1000,
height: global.PROD?800:1000,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false
}
})
require("@electron/remote/main").enable(mainWindow.webContents)
mainWindow.loadFile('index.html')
mainWindow.setTitle ( productName + " V" + version )
let wc = mainWindow.webContents
if ( !global.PROD )
wc.openDevTools()
var menuJSON = []
if ( process.platform === 'darwin' )
menuJSON.push ({ label: 'App Menu', submenu: [{ role: 'quit'}] })
var projectMenuJSON = { label: 'Project', submenu: [
{ label: "New CTRL-N", click () { wc.send ( 'PROJECT_NEW' ) } },
{ label: "Open CTRL-O", click () { openProject() } },
{ label: "Save CTRL-S", click () { wc.send ( 'PROJECT_SAVE') } },
{ label: "Save As", click () { saveAs() } },
{ type : "separator" },
{ label: "Export EXCEL", click () { exportExcel() } },
{ type : "separator" }
]}
for ( let recentProject of global.recentProjects )
projectMenuJSON.submenu.push ( {label: recentProject.path, click () { wc.send ( 'PROJECT_OPEN', recentProject.path ) } })
projectMenuJSON.submenu.push ({ type : "separator" })
projectMenuJSON.submenu.push ({ label: "Exit CTRL-Q", click () { app.exit() } })
function saveAs () {
dialog.showSaveDialog ({
title: "Save Project As",
filters: [ {name: "OGP", extensions: ["ogp"]} ]
}).then ( (res) => {
if ( !res.canceled )
wc.send ( 'PROJECT_SAVE_AS', res.filePath )
})
}
function exportExcel () {
dialog.showSaveDialog ({
title: "EXCEL Export",
filters: [ {name: "XLSX", extensions: ["xlsx"]} ]
}).then ( (res) => {
if ( !res.canceled )
wc.send ( 'EXPORT_EXCEL', res.filePath )
})
}
function openProject () {
dialog.showOpenDialog ({
title: "Open Project",
filters: [ {name: "OGP", extensions: ["ogp"]} ]
}).then ( (res) => {
if ( !res.canceled )
wc.send ( 'PROJECT_OPEN', res.filePaths[0] )
})
}
menuJSON.push ( projectMenuJSON )
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) )
ipcMain.on ( 'TRIGGER_PROJECT_SAVE_AS', (ev, data) => saveAs ())
ipcMain.on ( "setWasChanged", ( event, val ) => wasChanged = val )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+R', () => checkIfSaved ('RELOAD') )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+N', () => wc.send ( 'PROJECT_NEW') )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+O', () => openProject () )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+S', () => wc.send ( 'PROJECT_SAVE') )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+Q', () => checkIfSaved ('EXIT') )
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+Alt+Shift+I', () => wc.openDevTools() )
}
function checkIfSaved ( action ) {
let btnIndex = undefined
if ( wasChanged ) {
btnIndex = dialog.showMessageBoxSync ( mainWindow, {
type: 'question',
buttons: ['Yes', 'No'],
defaultId: 1,
title: ' ',
message: 'Unsaved data!',
detail: `You have unsaved changes! Do you really want to ${action==='EXIT'?'quit':'reload'}?`
})
}
if ( !wasChanged || btnIndex === 0 ) {
wasChanged = false
switch ( action ) {
case 'EXIT': app.exit(); break
case 'RELOAD': mainWindow.reload(); break
default: log ("ERROR: Unsupported action for checkIfSaved() function!", ERROR )
}
}
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})