Created
December 2, 2015 13:20
-
-
Save 0q98ahdsg3987y1h987y/fb3ad45a40fe42816cb7 to your computer and use it in GitHub Desktop.
Prevent Electron from closing the main window if there are unsaved changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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