From 03b19f2539fede4c1f3f5ef63de589bb2b2fde53 Mon Sep 17 00:00:00 2001 From: Marcelo van Kampen Date: Sat, 8 Jan 2022 19:55:52 -0300 Subject: [PATCH 1/2] set app window to be single instance --- src/main.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main.js b/src/main.js index 765c159..c19ab89 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 From 262ef78588d12e9be0517da3f4fe70a425506fc2 Mon Sep 17 00:00:00 2001 From: Marcelo van Kampen Date: Sat, 8 Jan 2022 20:10:58 -0300 Subject: [PATCH 2/2] saves the project on exit --- src/app/src/editor/ScratchJr.js | 10 ++++++++++ src/main.js | 31 +++++++++++++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) 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 c19ab89..9bf6685 100644 --- a/src/main.js +++ b/src/main.js @@ -141,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. @@ -472,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);