Skip to content

Commit 85ce509

Browse files
authored
Merge pull request scratchfoundation#98 from cwillisf/dark-dev-dont-hang
work around startup hang in dev on Win10 dark mode
2 parents 9981328 + d1c47e8 commit 85ce509

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

src/main/index.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {BrowserWindow, Menu, app, dialog, ipcMain} from 'electron';
2-
import * as path from 'path';
2+
import fs from 'fs';
3+
import path from 'path';
34
import {format as formatUrl} from 'url';
5+
46
import {getFilterForExtension} from './FileFilters';
57
import telemetry from './ScratchDesktopTelemetry';
68
import MacOSMenu from './MacOSMenu';
@@ -28,23 +30,9 @@ const createWindow = ({search = null, url = 'index.html', ...browserWindowOption
2830
const webContents = window.webContents;
2931

3032
if (isDevelopment) {
31-
webContents.openDevTools();
32-
import('electron-devtools-installer').then(importedModule => {
33-
const {default: installExtension, REACT_DEVELOPER_TOOLS} = importedModule;
34-
installExtension(REACT_DEVELOPER_TOOLS);
35-
// TODO: add logging package and bring back the lines below
36-
// .then(name => console.log(`Added browser extension: ${name}`))
37-
// .catch(err => console.log('An error occurred: ', err));
38-
});
33+
webContents.openDevTools({mode: 'detach', activate: true});
3934
}
4035

41-
webContents.on('devtools-opened', () => {
42-
window.focus();
43-
setImmediate(() => {
44-
window.focus();
45-
});
46-
});
47-
4836
const fullUrl = formatUrl(isDevelopment ?
4937
{ // Webpack Dev Server
5038
hostname: 'localhost',
@@ -166,8 +154,41 @@ app.on('will-quit', () => {
166154
telemetry.appWillClose();
167155
});
168156

157+
// work around https://github.com/MarshallOfSound/electron-devtools-installer/issues/122
158+
// which seems to be a result of https://github.com/electron/electron/issues/19468
159+
if (process.platform === 'win32') {
160+
const appUserDataPath = app.getPath('userData');
161+
const devToolsExtensionsPath = path.join(appUserDataPath, 'DevTools Extensions');
162+
try {
163+
fs.unlinkSync(devToolsExtensionsPath);
164+
} catch (_) {
165+
// don't complain if the file doesn't exist
166+
}
167+
}
168+
169169
// create main BrowserWindow when electron is ready
170170
app.on('ready', () => {
171+
if (isDevelopment) {
172+
import('electron-devtools-installer').then(importedModule => {
173+
const {default: installExtension, ...devToolsExtensions} = importedModule;
174+
const extensionsToInstall = [
175+
devToolsExtensions.REACT_DEVELOPER_TOOLS,
176+
devToolsExtensions.REACT_PERF,
177+
devToolsExtensions.REDUX_DEVTOOLS
178+
];
179+
for (const extension of extensionsToInstall) {
180+
// WARNING: depending on a lot of things including the version of Electron `installExtension` might
181+
// return a promise that never resolves, especially if the extension is already installed.
182+
installExtension(extension).then(
183+
// eslint-disable-next-line no-console
184+
extensionName => console.log(`Installed dev extension: ${extensionName}`),
185+
// eslint-disable-next-line no-console
186+
errorMessage => console.error(`Error installing dev extension: ${errorMessage}`)
187+
);
188+
}
189+
});
190+
}
191+
171192
_windows.main = createMainWindow();
172193
_windows.main.on('closed', () => {
173194
delete _windows.main;

0 commit comments

Comments
 (0)