diff --git a/src/main/index.js b/src/main/index.js index 1e0db01..a4c1734 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,4 +1,4 @@ -import {app, BrowserWindow} from 'electron'; +import {BrowserWindow, app, dialog} from 'electron'; import * as path from 'path'; import {format as formatUrl} from 'url'; @@ -67,4 +67,18 @@ app.on('activate', () => { // 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(); + } + }); }); diff --git a/src/renderer/index.js b/src/renderer/index.js index 427d3b9..15e5507 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -18,11 +18,6 @@ document.body.appendChild(appTarget); GUI.setAppElement(appTarget); const WrappedGui = AppStateHOC(GUI); -if (process.env.NODE_ENV === 'production' && typeof window === 'object') { - // Warn before navigating away - window.onbeforeunload = () => true; -} - const onStorageInit = storageInstance => { storageInstance.addHelper(new ElectronStorageHelper(storageInstance)); // storageInstance.addOfficialScratchWebStores(); // TODO: do we want this?