Skip to content

Commit

Permalink
Electron will wait for UI to confirm close
Browse files Browse the repository at this point in the history
  • Loading branch information
murilopolese committed Apr 17, 2024
1 parent df37ed8 commit ba3241d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
13 changes: 12 additions & 1 deletion backend/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})
}
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 4 additions & 1 deletion preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions ui/arduino/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ba3241d

Please sign in to comment.