forked from hql287/Manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
272 lines (251 loc) · 6.82 KB
/
app.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
console.time('init')
// Node Libs
const fs = require('fs');
const os = require('os');
const url = require('url');
const path = require('path');
const glob = require('glob');
const isDev = require('electron-is-dev');
// Electron Libs
const {app, BrowserWindow, ipcMain} = require('electron');
// 3rd Party Libs
const appConfig = require('electron-settings');
require('dotenv').config();
let tourWindow = null;
let mainWindow = null;
let previewWindow = null;
function createTourWindow() {
// Creating a New Window
tourWindow = new BrowserWindow({
width: 700,
height: 600,
show: false,
frame: false,
resizable: false,
movable: false,
title: 'Tour Window',
backgroundColor: '#F9FAFA',
});
// Register WindowID with appConfig
appConfig.set('tourWindowID', parseInt(tourWindow.id));
// Load Content
tourWindow.loadURL(
url.format({
pathname: path.join(__dirname, './tour/index.html'),
protocol: 'file:',
slashes: true,
})
);
// Add Event Listeners
tourWindow.on('show', event => {
if (isDev) tourWindow.webContents.openDevTools({mode: 'detach'});
});
tourWindow.on('close', event => {
event.preventDefault();
if (isDev) tourWindow.webContents.closeDevTools();
tourWindow.hide();
});
}
function createMainWindow() {
// Get window state
const mainWindownStateKeeper = windowStateKeeper('main');
// Creating a new window
mainWindow = new BrowserWindow({
x: mainWindownStateKeeper.x,
y: mainWindownStateKeeper.y,
width: mainWindownStateKeeper.width,
height: mainWindownStateKeeper.height,
minWidth: 600,
minHeight: 400,
titleBarStyle: 'hiddenInset',
backgroundColor: '#2e2c29',
show: false,
title: 'Main Window',
});
// Register WindowID
appConfig.set('mainWindowID', parseInt(mainWindow.id));
// Track window state
mainWindownStateKeeper.track(mainWindow);
// Load Content
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, './app/index.html'),
protocol: 'file:',
slashes: true,
})
);
// Add Event Listeners
mainWindow.on('show', event => {
if (isDev) mainWindow.webContents.openDevTools({mode: 'detach'});
});
mainWindow.on('close', event => {
event.preventDefault();
if (isDev) mainWindow.webContents.closeDevTools();
mainWindow.hide();
});
}
function createPreviewWindow() {
// Get window state
const previewWindownStateKeeper = windowStateKeeper('preview');
// Create New Window
previewWindow = new BrowserWindow({
x: previewWindownStateKeeper.x,
y: previewWindownStateKeeper.y,
width: previewWindownStateKeeper.width,
height: previewWindownStateKeeper.height,
minWidth: 1024,
minHeight: 800,
titleBarStyle: 'hiddenInset',
backgroundColor: '#2e2c29',
show: false,
title: 'Preview Window',
});
// Register WindowID
appConfig.set('previewWindowID', parseInt(previewWindow.id));
// Track window state
previewWindownStateKeeper.track(previewWindow);
// Load Content
previewWindow.loadURL(
url.format({
pathname: path.join(__dirname, './preview/index.html'),
protocol: 'file:',
slashes: true,
})
);
// Add Event Listener
previewWindow.on('show', event => {
if (isDev) previewWindow.webContents.openDevTools({mode: 'detach'});
});
previewWindow.on('close', event => {
event.preventDefault();
if (isDev) previewWindow.webContents.closeDevTools();
previewWindow.hide();
});
}
function addDevToolsExtension() {
if (process.env.REACT_DEV_TOOLS_PATH) BrowserWindow.addDevToolsExtension(process.env.REACT_DEV_TOOLS_PATH);
if (process.env.REDUX_DEV_TOOLS_PATH) BrowserWindow.addDevToolsExtension(process.env.REDUX_DEV_TOOLS_PATH);
}
function setInitialValues() {
// Tour
if (!appConfig.has('tour')) {
appConfig.set('tour', {
isActive: false,
hasBeenTaken: false,
});
}
// Windows last visible state
if (!appConfig.has('winsLastVisibleState')) {
appConfig.set('winsLastVisibleState', {
isMainWinVisible: true,
isPreviewWinVisible: false,
});
}
// Default Info
if (!appConfig.has('info')) {
// Set Default Logo
const logoPath = path.resolve(__dirname, './static/imgs/default_logo.svg');
const logoData = fs.readFileSync(logoPath);
const logoBase64String = 'data:image/svg+xml;base64,' + logoData.toString('base64');
// Other defaults
appConfig.set('info', {
logo: logoBase64String,
fullname: 'Manta Ray',
company: 'Oceanic Preservation Society',
address: '336 Bon Air Center #384 Greenbrae, CA 94904',
email: '[email protected]',
phone: '+01 (0) 1-2345-6789',
website: 'http://www.opsociety.org/',
});
}
// Default App Settings
if (!appConfig.has('appSettings')) {
appConfig.set('appSettings', {
exportDir: os.homedir(),
template: 'default',
language: 'en',
currency: 'USD',
sound: 'default',
muted: false,
});
}
}
function addEventListeners() {
ipcMain.on('quit-app', () => {
app.quit();
});
// Use with autoUpdater
ipcMain.on('restart-app', () => {
app.relaunch();
});
}
function loadMainProcessFiles() {
const files = glob.sync(path.join(__dirname, 'main/*.js'));
files.forEach(file => require(file));
}
function windowStateKeeper(windowName) {
let window, windowState;
function setBounds() {
// Restore from appConfig
if (appConfig.has(`windowState.${windowName}`)) {
windowState = appConfig.get(`windowState.${windowName}`);
return;
}
// Default
windowState = {
x: undefined,
y: undefined,
width: 1000,
height: 800,
};
}
function saveState() {
if (!windowState.isMaximized) {
windowState = window.getBounds();
}
windowState.isMaximized = window.isMaximized();
appConfig.set(`windowState.${windowName}`, windowState);
}
function track(win) {
window = win;
['resize', 'move'].forEach(event => {
win.on(event, saveState);
});
}
setBounds();
return {
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
isMaximized: windowState.isMaximized,
track,
};
}
function initialize() {
app.on('ready', () => {
createTourWindow();
createMainWindow();
createPreviewWindow();
setInitialValues();
if (isDev) addDevToolsExtension();
addEventListeners();
loadMainProcessFiles();
// Show Window
const {showWindow} = require('./main/tour');
showWindow('startup');
});
// Reactive the app
app.on('activate', () => {
const {showWindow} = require('./main/tour');
showWindow('activate');
});
// Close all windows before quit the app
app.on('before-quit', () => {
tourWindow.destroy();
mainWindow.destroy();
previewWindow.destroy();
});
console.timeEnd('init');
}
initialize();