2017-05-27 11:38:15 +08:00
|
|
|
import path from 'path';
|
2018-09-29 18:32:42 +08:00
|
|
|
import { BrowserWindow, Menu, globalShortcut, dialog } from 'electron';
|
2017-06-05 18:17:42 +08:00
|
|
|
import Store from 'electron-store';
|
2017-05-27 11:38:15 +08:00
|
|
|
import autoUpdate from './update';
|
2017-08-18 03:17:31 -05:00
|
|
|
import { catchConsoleLogLink, removeUnecessaryTabs } from './devtools';
|
2019-10-24 17:23:51 +08:00
|
|
|
import { selectRNDebuggerWorkerContext } from '../app/utils/devtools';
|
2018-09-29 18:32:42 +08:00
|
|
|
import { readConfig, filePath as configFile } from './config';
|
2017-05-27 11:38:15 +08:00
|
|
|
|
2017-06-05 18:17:42 +08:00
|
|
|
const store = new Store();
|
2017-05-27 11:38:15 +08:00
|
|
|
|
2019-10-24 17:21:25 +08:00
|
|
|
const executeJavaScript = (win, script) => win.webContents.executeJavaScript(script);
|
2017-08-17 22:58:42 -05:00
|
|
|
|
|
|
|
|
export const checkWindowInfo = win => executeJavaScript(win, 'window.checkWindowInfo()');
|
|
|
|
|
|
|
|
|
|
const checkIsOpenInEditorEnabled = win => executeJavaScript(win, 'window.isOpenInEditorEnabled()');
|
2017-05-27 11:38:15 +08:00
|
|
|
|
2017-06-28 17:16:04 +08:00
|
|
|
const changeMenuItems = menus => {
|
|
|
|
|
const rootMenuItems = Menu.getApplicationMenu().items;
|
|
|
|
|
Object.entries(menus).forEach(([key, subMenu]) => {
|
2017-08-17 22:58:42 -05:00
|
|
|
const rootMenuItem = rootMenuItems.find(({ label }) => label === key);
|
2017-06-28 17:16:04 +08:00
|
|
|
if (!rootMenuItem || !rootMenuItem.submenu) return;
|
|
|
|
|
|
|
|
|
|
Object.entries(subMenu).forEach(([subKey, menuSet]) => {
|
2017-08-17 22:58:42 -05:00
|
|
|
const menuItem = rootMenuItem.submenu.items.find(({ label }) => label === subKey);
|
2017-06-28 17:16:04 +08:00
|
|
|
if (!menuItem) return;
|
|
|
|
|
|
|
|
|
|
Object.assign(menuItem, menuSet);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-10-06 20:43:22 +08:00
|
|
|
const invokeDevMethod = (win, name) =>
|
|
|
|
|
executeJavaScript(win, `window.invokeDevMethod && window.invokeDevMethod('${name}')`);
|
|
|
|
|
|
|
|
|
|
const registerKeyboradShortcut = win => {
|
|
|
|
|
const prefix = process.platform === 'darwin' ? 'Command' : 'Ctrl';
|
|
|
|
|
// If another window focused, register a new shortcut
|
|
|
|
|
if (globalShortcut.isRegistered(`${prefix}+R`) || globalShortcut.isRegistered(`${prefix}+I`)) {
|
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
|
}
|
|
|
|
|
globalShortcut.register(`${prefix}+R`, () => invokeDevMethod(win, 'reload'));
|
|
|
|
|
globalShortcut.register(`${prefix}+I`, () => invokeDevMethod(win, 'toggleElementInspector'));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const unregisterKeyboradShortcut = () => globalShortcut.unregisterAll();
|
|
|
|
|
|
|
|
|
|
const registerShortcuts = async win => {
|
|
|
|
|
registerKeyboradShortcut(win);
|
2017-08-17 22:58:42 -05:00
|
|
|
changeMenuItems({
|
|
|
|
|
Debugger: {
|
|
|
|
|
'Stay in Front': {
|
|
|
|
|
checked: win.isAlwaysOnTop(),
|
|
|
|
|
},
|
2017-11-16 23:15:15 +08:00
|
|
|
'Enable Open in Editor for Console Log': {
|
2017-08-17 22:58:42 -05:00
|
|
|
checked: await checkIsOpenInEditorEnabled(win),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2017-10-06 20:43:22 +08:00
|
|
|
};
|
2017-08-17 22:58:42 -05:00
|
|
|
|
2017-10-19 13:34:51 +08:00
|
|
|
const minSize = 100;
|
2018-09-29 18:32:42 +08:00
|
|
|
export const createWindow = ({ iconPath, isPortSettingRequired, port }) => {
|
|
|
|
|
const { config, isConfigBroken, error } = readConfig();
|
|
|
|
|
|
|
|
|
|
if (isConfigBroken) {
|
|
|
|
|
dialog.showErrorBox(
|
|
|
|
|
'Root config error',
|
|
|
|
|
`Parse root config failed, please checkout \`${configFile}\`, the error trace:\n\n` +
|
|
|
|
|
`${error}\n\n` +
|
|
|
|
|
'RNDebugger will load default config instead. ' +
|
|
|
|
|
'You can click `Debugger` -> `Open Config File` in application menu.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-05 18:17:42 +08:00
|
|
|
const winBounds = store.get('winBounds');
|
2017-10-18 13:16:12 +08:00
|
|
|
const increasePosition = BrowserWindow.getAllWindows().length * 10;
|
2017-10-19 13:34:51 +08:00
|
|
|
const { width, height } = winBounds || {};
|
2017-05-27 11:38:15 +08:00
|
|
|
const win = new BrowserWindow({
|
|
|
|
|
...winBounds,
|
2017-10-19 13:34:51 +08:00
|
|
|
width: width && width >= minSize ? width : 1024,
|
|
|
|
|
height: height && height >= minSize ? height : 750,
|
|
|
|
|
minWidth: minSize,
|
|
|
|
|
minHeight: minSize,
|
2017-10-18 13:16:12 +08:00
|
|
|
...(increasePosition && winBounds.x && winBounds.y
|
2017-05-27 11:38:15 +08:00
|
|
|
? {
|
2017-10-18 13:16:12 +08:00
|
|
|
x: winBounds.x + increasePosition,
|
|
|
|
|
y: winBounds.y + increasePosition,
|
2017-05-27 11:38:15 +08:00
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
backgroundColor: '#272c37',
|
|
|
|
|
tabbingIdentifier: 'rndebugger',
|
2019-09-11 15:35:29 +08:00
|
|
|
webPreferences: {
|
|
|
|
|
nodeIntegration: true,
|
|
|
|
|
},
|
2018-09-29 18:32:42 +08:00
|
|
|
...config.windowBounds,
|
2017-05-27 11:38:15 +08:00
|
|
|
});
|
2018-09-29 18:32:42 +08:00
|
|
|
const isFirstWindow = BrowserWindow.getAllWindows().length === 1;
|
2017-05-27 11:38:15 +08:00
|
|
|
|
2019-10-24 17:23:51 +08:00
|
|
|
const { timesJSLoadToRefreshDevTools = -1 } = config;
|
2019-09-08 12:22:30 +08:00
|
|
|
win.debuggerConfig = {
|
2018-09-29 18:32:42 +08:00
|
|
|
port,
|
|
|
|
|
editor: config.editor,
|
|
|
|
|
fontFamily: config.fontFamily,
|
|
|
|
|
defaultReactDevToolsTheme: config.defaultReactDevToolsTheme,
|
2019-09-08 11:05:44 +08:00
|
|
|
defaultReactDevToolsPort: config.defaultReactDevToolsPort,
|
2018-09-29 18:32:42 +08:00
|
|
|
networkInspect: config.defaultNetworkInspect && 1,
|
|
|
|
|
isPortSettingRequired: isPortSettingRequired && 1,
|
2019-10-24 17:23:51 +08:00
|
|
|
timesJSLoadToRefreshDevTools,
|
2018-09-29 18:32:42 +08:00
|
|
|
};
|
2019-09-08 12:22:30 +08:00
|
|
|
win.loadURL(`file://${path.resolve(__dirname)}/app.html`);
|
2017-05-27 11:38:15 +08:00
|
|
|
win.webContents.on('did-finish-load', () => {
|
2019-10-24 17:21:25 +08:00
|
|
|
win.webContents.zoomLevel = config.zoomLevel || store.get('zoomLevel', 0);
|
2017-05-27 11:38:15 +08:00
|
|
|
win.focus();
|
2017-10-06 20:43:22 +08:00
|
|
|
registerShortcuts(win);
|
2018-03-14 13:04:46 +08:00
|
|
|
if (process.env.E2E_TEST !== '1' && !isPortSettingRequired) {
|
2017-05-27 11:38:15 +08:00
|
|
|
win.openDevTools();
|
|
|
|
|
}
|
2018-09-29 18:32:42 +08:00
|
|
|
const checkUpdate = config.autoUpdate !== false;
|
|
|
|
|
if (checkUpdate && isFirstWindow) {
|
2017-05-27 11:38:15 +08:00
|
|
|
autoUpdate(iconPath);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-17 22:58:42 -05:00
|
|
|
win.webContents.on('devtools-opened', async () => {
|
|
|
|
|
const { location } = await checkWindowInfo(win);
|
|
|
|
|
catchConsoleLogLink(win, location.host, location.port);
|
2018-09-29 18:32:42 +08:00
|
|
|
if (config.showAllDevToolsTab !== true) {
|
|
|
|
|
removeUnecessaryTabs(win);
|
|
|
|
|
}
|
2019-10-24 17:23:51 +08:00
|
|
|
selectRNDebuggerWorkerContext(win);
|
2017-08-17 22:58:42 -05:00
|
|
|
});
|
2017-10-06 20:43:22 +08:00
|
|
|
win.on('show', () => {
|
|
|
|
|
if (!win.isFocused()) return;
|
|
|
|
|
registerShortcuts(win);
|
|
|
|
|
});
|
|
|
|
|
win.on('focus', () => registerShortcuts(win));
|
|
|
|
|
win.on('restore', () => registerShortcuts(win));
|
|
|
|
|
win.on('hide', () => unregisterKeyboradShortcut());
|
|
|
|
|
win.on('blur', () => unregisterKeyboradShortcut());
|
|
|
|
|
win.on('minimize', () => unregisterKeyboradShortcut());
|
2017-09-26 22:17:17 +08:00
|
|
|
win.close = async () => {
|
2017-10-06 20:43:22 +08:00
|
|
|
unregisterKeyboradShortcut();
|
2017-10-19 13:34:51 +08:00
|
|
|
store.set('winBounds', win.getBounds());
|
2019-10-24 17:21:25 +08:00
|
|
|
store.set('zoomLevel', win.webContents.zoomLevel);
|
2017-09-26 22:17:17 +08:00
|
|
|
await executeJavaScript(win, 'window.beforeWindowClose && window.beforeWindowClose()');
|
|
|
|
|
win.destroy();
|
|
|
|
|
};
|
|
|
|
|
win.on('close', event => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
win.close();
|
2017-07-18 15:59:41 +08:00
|
|
|
});
|
2017-10-19 13:38:29 +08:00
|
|
|
|
|
|
|
|
// https://github.com/electron/electron/issues/10442
|
2017-09-10 16:07:09 +08:00
|
|
|
win._setEscapeTouchBarItem = () => {}; // eslint-disable-line
|
2017-10-19 13:38:29 +08:00
|
|
|
|
2017-05-27 11:38:15 +08:00
|
|
|
return win;
|
|
|
|
|
};
|