scratch-desktop/src/main/index.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-09-19 15:15:02 -07:00
import {app, BrowserWindow} from 'electron';
import * as path from 'path';
import {format as formatUrl} from 'url';
2017-08-27 14:15:38 -05:00
2018-09-19 15:15:02 -07:00
const isDevelopment = process.env.NODE_ENV !== 'production';
2017-08-27 14:15:38 -05:00
// global reference to mainWindow (necessary to prevent window from being garbage collected)
2018-09-19 15:15:02 -07:00
let mainWindow;
const createMainWindow = () => {
const window = new BrowserWindow();
if (isDevelopment) {
window.webContents.openDevTools();
}
if (isDevelopment) {
window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
} else {
window.loadURL(formatUrl({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file',
slashes: true
}));
}
window.on('closed', () => {
mainWindow = null;
});
window.webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
});
return window;
};
2017-08-27 14:15:38 -05:00
// quit application when all windows are closed
2017-08-27 14:15:38 -05:00
app.on('window-all-closed', () => {
2018-09-19 15:15:02 -07:00
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
});
2017-08-27 14:15:38 -05:00
app.on('activate', () => {
2018-09-19 15:15:02 -07:00
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
mainWindow = createMainWindow();
}
});
2017-08-27 14:15:38 -05:00
// create main BrowserWindow when electron is ready
2017-08-27 14:15:38 -05:00
app.on('ready', () => {
2018-09-19 15:15:02 -07:00
mainWindow = createMainWindow();
});