Skip to content

Alert before quit electron with unsaved changes #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const path = require('path')

async function openFolderDialog(win) {
// https://stackoverflow.com/questions/46027287/electron-open-folder-dialog
let dir = await dialog.showOpenDialog(win, { properties: [ 'openDirectory' ] })
const dir = await dialog.showOpenDialog(win, { properties: [ 'openDirectory' ] })
return dir.filePaths[0] || null
}

function listFolder(folder) {
files = fs.readdirSync(path.resolve(folder))
let files = fs.readdirSync(path.resolve(folder))
// Filter out directories
files = files.filter(f => {
let filePath = path.resolve(folder, f)
Expand Down Expand Up @@ -38,7 +38,7 @@ function ilistFolder(folder) {

function getAllFiles(dirPath, arrayOfFiles) {
// https://coderrocketfuel.com/article/recursively-list-all-the-files-in-a-directory-using-node-js
files = ilistFolder(dirPath)
let files = ilistFolder(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function(file) {
const p = path.join(dirPath, file.path)
Expand Down
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy