-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_process.js
162 lines (132 loc) · 4.47 KB
/
main_process.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
// Basic init
const electron = require('electron')
const path = require('path');
const { Socket, Transport } = require('electron-ipc-socket');
const {app, BrowserWindow, crashReporter, ipcMain, Menu} = electron
const socketRoutes = require('./services');
const bindSocketToRoutes = require('./services/bindSocketToRoutes');
const isDevelopment = (process.env.NODE_ENV === 'development');
if (isDevelopment) {
// Let electron reload by itself when webpack watches changes in ./app/
require('electron-reload')(__dirname, {
electron: require('${__dirname}/../../node_modules/electron')
})
}
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
const contentPath = path.join(userDataPath, '/stories');
global.contentPath = contentPath;
// To avoid being garbage collected
let mainWindow
let forceQuit = false
/**
* Install react and redux devtools
*/
const installExtensions = async () => {
const installer = require('electron-devtools-installer');
const extensions = [
'REACT_DEVELOPER_TOOLS',
'REDUX_DEVTOOLS'
];
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
for (const name of extensions) {
try {
await installer.default(installer[name], forceDownload);
} catch (e) {
console.log(`Error installing ${name} extension: ${e.message}`);
}
}
};
app.on('ready', async () => {
/**
* install devtools extensions in dev mode
*/
if (isDevelopment) {
await installExtensions();
}
const mainScreen = electron.screen.getPrimaryDisplay();
mainWindow = new BrowserWindow({
width: mainScreen.bounds.width,
height: mainScreen.bounds.height,
icon: path.join(__dirname, 'assets/android-chrome-96x96.png')
})
const socket = Socket('main-win', Transport(ipcMain, mainWindow));
socket.open();
bindSocketToRoutes(socket, socketRoutes);
socket.on('event:ready', () => {
console.log('main-win ready');
});
mainWindow.loadURL(`file://${__dirname}/app/electronIndex.html`);
// show window once on first load
mainWindow.webContents.once('did-finish-load', () => {
mainWindow.show();
});
mainWindow.webContents.on('did-finish-load', () => {
// Handle window logic properly on macOS:
// 1. App should not terminate if window has been closed
// 2. Click on icon in dock should re-open the window
// 3. ⌘+Q should close the window and quit the app
if (process.platform === 'darwin') {
mainWindow.on('close', function (e) {
if (!forceQuit) {
e.preventDefault();
mainWindow.hide();
}
});
app.on('activate', () => {
mainWindow.show();
});
app.on('before-quit', () => {
forceQuit = true;
});
} else {
mainWindow.on('closed', () => {
mainWindow = null;
});
}
});
if (isDevelopment) {
// auto-open dev tools
mainWindow.webContents.openDevTools();
// add inspect element on right click menu
mainWindow.webContents.on('context-menu', (e, props) => {
Menu.buildFromTemplate([{
label: 'Inspect element',
click() {
mainWindow.inspectElement(props.x, props.y);
}
}]).popup(mainWindow);
});
} else {
Menu.setApplicationMenu(
Menu.buildFromTemplate(
[
{
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]
}
]
)
);
}
})
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
crashReporter.start({
productName: 'dicto',
companyName: 'dictoapp',
submitURL: 'https://github.com/dictoapp/dicto/issues/new?template=bug_report.md',
uploadToServer: false
});