2018-01-26 19:53:51 +00:00
|
|
|
import { BrowserWindow, Menu, shell } from 'electron';
|
|
|
|
import { URL } from 'url';
|
2018-06-15 23:25:29 +00:00
|
|
|
import path from 'path';
|
2018-01-26 19:53:51 +00:00
|
|
|
import MENU from './menu';
|
2018-06-14 00:19:04 +00:00
|
|
|
import popupContextMenu from './contextMenu';
|
2018-02-07 04:28:28 +00:00
|
|
|
import { APP_TITLE } from '../constants';
|
2018-01-26 19:53:51 +00:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production';
|
|
|
|
|
|
|
|
// Cached reference, preventing recreations
|
2018-03-07 23:36:05 +00:00
|
|
|
let window: BrowserWindow | null;
|
2018-01-26 19:53:51 +00:00
|
|
|
|
|
|
|
// Construct new BrowserWindow
|
|
|
|
export default function getWindow() {
|
|
|
|
if (window) {
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
window = new BrowserWindow({
|
2018-02-07 04:28:28 +00:00
|
|
|
title: APP_TITLE,
|
2018-01-26 19:53:51 +00:00
|
|
|
backgroundColor: '#fbfbfb',
|
|
|
|
width: 1220,
|
2018-04-17 19:49:29 +00:00
|
|
|
height: process.platform === 'darwin' ? 680 : 720,
|
2018-04-16 23:30:58 +00:00
|
|
|
minWidth: 480,
|
2018-01-26 19:53:51 +00:00
|
|
|
minHeight: 400,
|
2018-04-16 23:30:58 +00:00
|
|
|
titleBarStyle: 'hidden',
|
2018-01-26 19:53:51 +00:00
|
|
|
webPreferences: {
|
|
|
|
devTools: true,
|
|
|
|
nodeIntegration: false,
|
2018-06-15 23:25:29 +00:00
|
|
|
contextIsolation: true,
|
|
|
|
preload: path.join(__dirname, 'preload.js')
|
2018-01-26 19:53:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-16 23:30:58 +00:00
|
|
|
const appUrl = isDevelopment ? `http://localhost:3000` : `file://${__dirname}/index.html`;
|
2018-01-26 19:53:51 +00:00
|
|
|
window.loadURL(appUrl);
|
|
|
|
|
|
|
|
window.on('closed', () => {
|
|
|
|
window = null;
|
|
|
|
});
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
window.webContents.on('new-window', (ev: any, urlStr: string) => {
|
2018-01-26 19:53:51 +00:00
|
|
|
// Kill all new window requests by default
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
// Only allow HTTPS urls to actually be opened
|
|
|
|
const url = new URL(urlStr);
|
|
|
|
if (url.protocol === 'https:') {
|
|
|
|
shell.openExternal(urlStr);
|
|
|
|
} else {
|
|
|
|
console.warn(`Blocked request to open new window '${urlStr}', only HTTPS links are allowed`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-14 00:19:04 +00:00
|
|
|
window.webContents.on('context-menu', (_, props) => {
|
|
|
|
popupContextMenu(window!, isDevelopment, props);
|
|
|
|
});
|
|
|
|
|
2018-04-25 03:29:34 +00:00
|
|
|
// TODO: Figure out updater release process
|
|
|
|
// window.webContents.on('did-finish-load', () => {
|
|
|
|
// updater(window!);
|
|
|
|
// });
|
2018-01-26 19:53:51 +00:00
|
|
|
|
|
|
|
window.webContents.on('devtools-opened', () => {
|
2018-03-07 23:36:05 +00:00
|
|
|
window!.focus();
|
2018-01-26 19:53:51 +00:00
|
|
|
setImmediate(() => {
|
2018-03-07 23:36:05 +00:00
|
|
|
window!.focus();
|
2018-01-26 19:53:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isDevelopment) {
|
|
|
|
window.webContents.on('did-fail-load', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (window && window.webContents) {
|
|
|
|
window.webContents.reload();
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(MENU));
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|