Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 0q98ahdsg3987y1h987y/fb3ad45a40fe42816cb7 to your computer and use it in GitHub Desktop.
Save 0q98ahdsg3987y1h987y/fb3ad45a40fe42816cb7 to your computer and use it in GitHub Desktop.
Prevent Electron from closing the main window if there are unsaved changes
# In main.coffee
closeAfterSave = false
mainWindow.on 'close', (event) ->
if !closeAfterSave
event.preventDefault()
mainWindow.webContents.send 'closingWindow'
return
ipc.on 'doClose', ->
closeAfterSave = true
mainWindow.close()
return
# In app.coffee
ipc.on 'closingWindow', (event) ->
if !file.saved
result = dialog.showMessageBox(remote.getCurrentWindow(),
type: 'warning'
buttons: [
'Save'
'Cancel'
'Don\'t Save'
]
title: 'Save changes?'
message: 'You have unsaved changes. Would you like to save them first?'
detail: 'Your changes will be lost if you don\'t save them.')
if result == 0
handleSaveRequest()
else if result == 1
return false
ipc.send 'doClose'
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment