diff --git a/src/app/src/editor/ScratchJr.js b/src/app/src/editor/ScratchJr.js index a126625..af821cd 100755 --- a/src/app/src/editor/ScratchJr.js +++ b/src/app/src/editor/ScratchJr.js @@ -20,6 +20,8 @@ import Localization from '../utils/Localization'; import {libInit, gn, scaleMultiplier, newHTML, isAndroid, isTablet, getUrlVars, CSSTransition3D, frame} from '../utils/lib'; +import { ipcRenderer } from 'electron'; + let workingCanvas = document.createElement('canvas'); let workingCanvas2 = document.createElement('canvas'); let activeFocus; @@ -992,3 +994,11 @@ export default class ScratchJr { } } } + + +ipcRenderer.on('app-close', function(event) { + // save the project + ScratchJr.saveProject(null, function () {}); + // tell electron that it can exit now + event.sender.send('app-closed-acked'); +}); \ No newline at end of file diff --git a/src/main.js b/src/main.js index 765c159..9bf6685 100644 --- a/src/main.js +++ b/src/main.js @@ -37,6 +37,19 @@ isDev = (isDev) || process.env.DEBUG_SCRATCHJR; const { app, dialog, BrowserWindow, BrowserView, ipcMain, Menu } = require('electron'); +var myWindow = null; + +var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { + // Someone tried to run a second instance, we should focus our window. + if (myWindow) { + if (myWindow.isMinimized()) myWindow.restore(); + myWindow.focus(); + } +}); + +if (shouldQuit) { + app.quit(); +} /* eslint-enable import/extensions */ // --> ON @@ -128,23 +141,30 @@ function createWindow() { win.webContents.openDevTools(); } - // Emitted when the window is closed. - win.on('closed', () => { - // save the database if it has been opened. - if (dataStore.databaseManager) { - dataStore.databaseManager.save(); - } - - // Dereference the window object, usually you would store windows - // in an array if your app supports multi windows, this is the time - // when you should delete the corresponding element. - win = null; + win.on('close', (e) => { + e.preventDefault(); + //tell editor to flush the data + win.webContents.send('app-close'); }); win.webContents.on('did-finish-load', () => { }); + } +ipcMain.on('app-closed-acked',(event) => { + // save the database if it has been opened. + if (dataStore.databaseManager) { + dataStore.databaseManager.save(); + } + + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + win = null; + app.exit(); +}) + // This method will be called when Electron has finished // initialization and is ready to create browser windows. @@ -459,7 +479,7 @@ ipcMain.on('io_getAudioData', (event, audioName) => { ipcMain.on('database_stmt', (event, json) => { // {"stmt":"select name,thumbnail,id,isgift from projects where deleted = ? AND version = ? AND gallery IS NULL order by ctime desc","values":["NO","iOSv01"]} - if (DEBUG_DATABASE) debugLog('database_stmt', json); + if (DEBUG_DATABASE) debugLog('database_stmt', json); const db = dataStore.getDatabaseManager(); event.returnValue = db.stmt(json);