forked from hql287/Manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
260 lines (242 loc) · 6.03 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
// Node Libs
const path = require('path');
const url = require('url');
const glob = require('glob');
const os = require('os');
// 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;
// Create Tour Window
function createTourWindow() {
// Creating a New Window
tourWindow = new BrowserWindow({
width: 600,
height: 600,
show: false,
frame: false,
resizable: false,
movable: false,
title: 'Welcome 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('close', event => {
event.preventDefault();
tourWindow.hide();
});
}
// Create Main Window
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: 800,
minHeight: 600,
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('close', event => {
event.preventDefault();
mainWindow.hide();
});
}
// Create Preview Window
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: 1030,
minHeight: 1000,
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('close', event => {
event.preventDefault();
previewWindow.hide();
});
}
// Add Devtool Extensions
function addDevToolsExtension() {
BrowserWindow.addDevToolsExtension(process.env.REACT_DEV_TOOLS_PATH);
BrowserWindow.addDevToolsExtension(process.env.REDUX_DEV_TOOLS_PATH);
}
// Set Initial Values
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')) {
appConfig.set('info', {
logo: '',
fullname: '',
company: '',
address: '',
email: '',
phone: '',
website: '',
});
}
// Default App Settings
if (!appConfig.has('appSettings')) {
appConfig.set('appSettings', {
exportDir: os.homedir(),
template: 'default',
language: 'en',
currency: 'USD',
sound: 'default',
muted: false,
});
}
}
// Add Event Listener
function addEventListeners() {
ipcMain.on('quit-app', () => {
tourWindow.destroy();
mainWindow.destroy();
previewWindow.destroy();
app.quit();
});
}
// Initialize
function initialize() {
// Start the app
app.on('ready', () => {
// Create The Welcome Window
createTourWindow();
// Create The Main Window
createMainWindow();
// Create Preview Window
createPreviewWindow();
// Set Initial Values
setInitialValues();
// Add Devtools Extenstion
addDevToolsExtension();
// Add Event Listener
addEventListeners();
// Load all main process files
loadMainProcessFiles();
// Show Window
const { showWindow } = require('./main/tour');
showWindow('startup');
});
// Reactive the app
app.on('activate', () => {
const { showWindow } = require('./main/tour');
showWindow('activate');
});
}
initialize();
// HELPERS
// ===========================================
// Imports Main Process Files
function loadMainProcessFiles() {
const files = glob.sync(path.join(__dirname, 'main/*.js'));
files.forEach(file => require(file));
}
// Windows State Keeper
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,
});
}