From ba3241d3ca0c86f34758985ee629307c0c6663d1 Mon Sep 17 00:00:00 2001 From: Murilo Polese Date: Wed, 17 Apr 2024 12:26:37 +0200 Subject: [PATCH] Electron will wait for UI to confirm close --- backend/ipc.js | 13 ++++++++++++- index.js | 10 ++++++++-- preload.js | 5 ++++- ui/arduino/store.js | 9 +++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/ipc.js b/backend/ipc.js index c97daba..b1133b4 100644 --- a/backend/ipc.js +++ b/backend/ipc.js @@ -6,7 +6,7 @@ const { getAllFiles } = require('./helpers.js') -module.exports = function registerIPCHandlers(win, ipcMain) { +module.exports = function registerIPCHandlers(win, ipcMain, app) { ipcMain.handle('open-folder', async (event) => { console.log('ipcMain', 'open-folder') const folder = await openFolderDialog(win) @@ -107,4 +107,15 @@ module.exports = function registerIPCHandlers(win, ipcMain) { win.setMinimumSize(minWidth, minHeight) }) + + ipcMain.handle('confirm-close', () => { + console.log('ipcMain', 'confirm-close') + app.exit() + }) + + win.on('close', (event) => { + console.log('BrowserWindow', 'close') + event.preventDefault() + win.webContents.send('check-before-close') + }) } diff --git a/index.js b/index.js index 2992967..3f01633 100644 --- a/index.js +++ b/index.js @@ -23,11 +23,17 @@ function createWindow () { // and load the index.html of the app. win.loadFile('ui/arduino/index.html') - registerIPCHandlers(win, ipcMain) + registerIPCHandlers(win, ipcMain, app) registerMenu(win) + + app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) createWindow() + }) + // app.on('window-all-closed', () => { + // if (process.platform !== 'darwin') app.quit() + // }) } // TODO: Loading splash screen - app.whenReady().then(createWindow) diff --git a/preload.js b/preload.js index d7b24e8..cce145f 100644 --- a/preload.js +++ b/preload.js @@ -151,9 +151,12 @@ const Disk = { const Window = { setWindowSize: (minWidth, minHeight) => { ipcRenderer.invoke('set-window-size', minWidth, minHeight) - } + }, + beforeClose: (callback) => ipcRenderer.on('check-before-close', callback), + confirmClose: () => ipcRenderer.invoke('confirm-close') } + contextBridge.exposeInMainWorld('BridgeSerial', Serial) contextBridge.exposeInMainWorld('BridgeDisk', Disk) contextBridge.exposeInMainWorld('BridgeWindow', Window) diff --git a/ui/arduino/store.js b/ui/arduino/store.js index 80656a5..6d965e7 100644 --- a/ui/arduino/store.js +++ b/ui/arduino/store.js @@ -1303,6 +1303,15 @@ async function store(state, emitter) { emitter.emit('render') }) + win.beforeClose(async () => { + const hasChanges = !!state.openFiles.find(f => f.parentFolder && f.hasChanges) + if (hasChanges) { + const response = await confirm('You may have unsaved changes. Are you sure you want to proceed?', 'Yes', 'Cancel') + if (!response) return false + } + await win.confirmClose() + }) + function createFile(args) { const { source,