From 290fe195b6bd78dd6810a91222f363170f04fdce Mon Sep 17 00:00:00 2001 From: Antelle Date: Wed, 4 Nov 2015 09:16:39 +0300 Subject: [PATCH] fix #3: desktop app close handler --- app/scripts/comp/launcher.js | 12 +++++++++--- app/scripts/views/app-view.js | 14 +++++++++++++- electron/main.js | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/scripts/comp/launcher.js b/app/scripts/comp/launcher.js index bc9f3df3..37db38de 100644 --- a/app/scripts/comp/launcher.js +++ b/app/scripts/comp/launcher.js @@ -8,6 +8,9 @@ if (window.process && window.process.versions && window.process.versions.electro name: 'electron', version: window.process.versions.electron, req: window.require, + remReq: function(mod) { + return this.req('remote').require(mod); + }, openLink: function(href) { this.req('shell').openExternal(href); }, @@ -16,12 +19,11 @@ if (window.process && window.process.versions && window.process.versions.electro this.req('remote').getCurrentWindow().openDevTools(); }, getSaveFileName: function(defaultPath, cb) { - var remote = this.req('remote'); if (defaultPath) { - var homePath = remote.require('app').getPath('userDesktop'); + var homePath = this.remReq('app').getPath('userDesktop'); defaultPath = this.req('path').join(homePath, defaultPath); } - remote.require('dialog').showSaveDialog({ + this.remReq('dialog').showSaveDialog({ title: 'Save Passwords Database', defaultPath: defaultPath, filters: [{ name: 'KeePass files', extensions: ['kdbx'] }] @@ -35,6 +37,10 @@ if (window.process && window.process.versions && window.process.versions.electro }, fileExists: function(path) { return this.req('fs').existsSync(path); + }, + exit: function() { + Launcher.exitRequested = true; + this.remReq('app').quit(); } }; window.launcherOpen = function(path) { diff --git a/app/scripts/views/app-view.js b/app/scripts/views/app-view.js index 3056c9c4..8dd52889 100644 --- a/app/scripts/views/app-view.js +++ b/app/scripts/views/app-view.js @@ -186,8 +186,20 @@ var AppView = Backbone.View.extend({ } }, - beforeUnload: function() { + beforeUnload: function(e) { if (this.model.files.hasUnsavedFiles()) { + if (Launcher && !Launcher.exitRequested) { + Alerts.yesno({ + header: 'Unsaved changes!', + body: 'You have unsaved files, all changes will be lost.', + buttons: [{result: 'yes', title: 'Exit and discard unsaved changes'}, {result: '', title: 'Don\'t exit'}], + success: function() { + Launcher.exit(); + } + }); + e.returnValue = false; + return false; + } return 'You have unsaved files, all changes will be lost.'; } }, diff --git a/electron/main.js b/electron/main.js index c5151a1c..84f61096 100644 --- a/electron/main.js +++ b/electron/main.js @@ -16,6 +16,7 @@ var mainWindow = null, app.on('window-all-closed', function() { app.quit(); }); app.on('ready', function() { var htmlPath = path.join(app.getPath('userData'), 'index.html'); + htmlPath = path.join(__dirname, '../tmp/index.html'); mainWindow = new BrowserWindow({ show: false,