Skip to content

Commit ba3241d

Browse files
committed
Electron will wait for UI to confirm close
1 parent df37ed8 commit ba3241d

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Diff for: backend/ipc.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
getAllFiles
77
} = require('./helpers.js')
88

9-
module.exports = function registerIPCHandlers(win, ipcMain) {
9+
module.exports = function registerIPCHandlers(win, ipcMain, app) {
1010
ipcMain.handle('open-folder', async (event) => {
1111
console.log('ipcMain', 'open-folder')
1212
const folder = await openFolderDialog(win)
@@ -107,4 +107,15 @@ module.exports = function registerIPCHandlers(win, ipcMain) {
107107

108108
win.setMinimumSize(minWidth, minHeight)
109109
})
110+
111+
ipcMain.handle('confirm-close', () => {
112+
console.log('ipcMain', 'confirm-close')
113+
app.exit()
114+
})
115+
116+
win.on('close', (event) => {
117+
console.log('BrowserWindow', 'close')
118+
event.preventDefault()
119+
win.webContents.send('check-before-close')
120+
})
110121
}

Diff for: index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ function createWindow () {
2323
// and load the index.html of the app.
2424
win.loadFile('ui/arduino/index.html')
2525

26-
registerIPCHandlers(win, ipcMain)
26+
registerIPCHandlers(win, ipcMain, app)
2727
registerMenu(win)
28+
29+
app.on('activate', () => {
30+
if (BrowserWindow.getAllWindows().length === 0) createWindow()
31+
})
32+
// app.on('window-all-closed', () => {
33+
// if (process.platform !== 'darwin') app.quit()
34+
// })
2835
}
2936

3037

3138
// TODO: Loading splash screen
32-
3339
app.whenReady().then(createWindow)

Diff for: preload.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ const Disk = {
151151
const Window = {
152152
setWindowSize: (minWidth, minHeight) => {
153153
ipcRenderer.invoke('set-window-size', minWidth, minHeight)
154-
}
154+
},
155+
beforeClose: (callback) => ipcRenderer.on('check-before-close', callback),
156+
confirmClose: () => ipcRenderer.invoke('confirm-close')
155157
}
156158

159+
157160
contextBridge.exposeInMainWorld('BridgeSerial', Serial)
158161
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
159162
contextBridge.exposeInMainWorld('BridgeWindow', Window)

Diff for: ui/arduino/store.js

+9
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,15 @@ async function store(state, emitter) {
13031303
emitter.emit('render')
13041304
})
13051305

1306+
win.beforeClose(async () => {
1307+
const hasChanges = !!state.openFiles.find(f => f.parentFolder && f.hasChanges)
1308+
if (hasChanges) {
1309+
const response = await confirm('You may have unsaved changes. Are you sure you want to proceed?', 'Yes', 'Cancel')
1310+
if (!response) return false
1311+
}
1312+
await win.confirmClose()
1313+
})
1314+
13061315
function createFile(args) {
13071316
const {
13081317
source,

0 commit comments

Comments
 (0)