Improve main window behavior and code readability

- Closing behavior on Mac is now consistent with other platforms
- The main window is not shown until ready
- Minor code reorg to separate concerns and improve readability
This commit is contained in:
Christopher Willis-Ford 2018-12-14 13:03:48 -08:00
parent 1145c44915
commit 6bf5d4a41a

View file

@ -2,29 +2,38 @@ import {BrowserWindow, app, dialog} from 'electron';
import * as path from 'path';
import {format as formatUrl} from 'url';
const isDevelopment = process.env.NODE_ENV !== 'production';
// const defaultSize = {width: 1096, height: 715}; // minimum
const defaultSize = {width: 1280, height: 800}; // good for MAS screenshots
// global reference to mainWindow (necessary to prevent window from being garbage collected)
let mainWindow;
const isDevelopment = process.env.NODE_ENV !== 'production';
const createMainWindow = () => {
const window = new BrowserWindow({
width: 1096,
height: 715,
useContentSize: true
width: defaultSize.width,
height: defaultSize.height,
useContentSize: true,
show: false
});
const webContents = window.webContents;
if (isDevelopment) {
window.webContents.openDevTools();
webContents.openDevTools();
import('electron-devtools-installer').then(importedModule => {
const {default: installExtension, REACT_DEVELOPER_TOOLS} = importedModule;
installExtension(REACT_DEVELOPER_TOOLS);
// TODO: add logging package and bring back the lines below
// .then(name => console.log(`Added Extension: ${name}`))
// .then(name => console.log(`Added browser extension: ${name}`))
// .catch(err => console.log('An error occurred: ', err));
});
}
webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
});
if (isDevelopment) {
window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
} else {
@ -35,15 +44,23 @@ const createMainWindow = () => {
}));
}
window.on('closed', () => {
mainWindow = null;
webContents.on('will-prevent-unload', ev => {
const choice = dialog.showMessageBox(window, {
type: 'question',
message: 'Leave Scratch?',
detail: 'Any unsaved changes will be lost.',
buttons: ['Stay', 'Leave'],
cancelId: 0, // closing the dialog means "stay"
defaultId: 0 // pressing enter or space without explicitly selecting something means "stay"
});
const shouldQuit = (choice === 1);
if (shouldQuit) {
ev.preventDefault();
}
});
window.webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
window.once('ready-to-show', () => {
window.show();
});
return window;
@ -51,34 +68,16 @@ const createMainWindow = () => {
// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
app.quit();
});
app.on('activate', () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
mainWindow = createMainWindow();
}
});
// global reference to mainWindow (necessary to prevent window from being garbage collected)
let _mainWindow;
// create main BrowserWindow when electron is ready
app.on('ready', () => {
mainWindow = createMainWindow();
mainWindow.webContents.on('will-prevent-unload', ev => {
const choice = dialog.showMessageBox(mainWindow, {
type: 'question',
buttons: ['Stay', 'Leave'],
message: 'Leave Scratch?',
cancelId: 0, // closing the dialog means "stay"
defaultId: 0, // pressing enter or space without explicitly selecting something means "stay"
detail: 'Any unsaved changes will be lost.'
});
const shouldQuit = (choice === 1);
if (shouldQuit) {
ev.preventDefault();
}
_mainWindow = createMainWindow();
_mainWindow.on('closed', () => {
_mainWindow = null;
});
});