From b614d3620dcb170b2c6571f79e56533d1704291b Mon Sep 17 00:00:00 2001 From: rephole Date: Wed, 28 Oct 2015 01:06:26 +0300 Subject: [PATCH] Code refactoring is recompleted. --- app/main.js | 59 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/app/main.js b/app/main.js index 7fac1aa..0cb8d05 100644 --- a/app/main.js +++ b/app/main.js @@ -24,29 +24,27 @@ global.whatsApp = { init: function() { - this.createMenu(); - this.createTray(); - this.clearCache(); - this.openWindow(); + whatsApp.createMenu(); + whatsApp.createTray(); + + whatsApp.clearCache(); + whatsApp.openWindow(); - app.on('window-all-closed', onlyWin(function() { - app.quit() - })); }, createMenu: function() { - this.menu = + whatsApp.menu = appMenu.buildFromTemplate(require('./menu')); - appMenu.setApplicationMenu(this.menu); + appMenu.setApplicationMenu(whatsApp.menu); }, createTray: function() { - this.tray = new AppTray(__dirname + '/assets/img/trayTemplate.png'); + whatsApp.tray = new AppTray(__dirname + '/assets/img/trayTemplate.png'); - this.tray.on('clicked', function() { - this.window.show(); - }.bind(this)); + whatsApp.tray.on('clicked', function() { + whatsApp.window.show(); + }); - this.tray.setToolTip('WhatsApp Desktop'); + whatsApp.tray.setToolTip('WhatsApp Desktop'); }, clearCache: function() { try{ @@ -54,7 +52,7 @@ }catch(e){} }, openWindow: function() { - this.window = new BrowserWindow( + whatsApp.window = new BrowserWindow( { 'width': 1000, 'height': 720, @@ -62,12 +60,12 @@ 'node-integration': false } ); - this.window.loadUrl('https://web.whatsapp.com', { + whatsApp.window.loadUrl('https://web.whatsapp.com', { userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36' }); - this.window.show(); + whatsApp.window.show(); - this.window.on('page-title-updated', onlyOSX(function(event, title) { + whatsApp.window.on('page-title-updated', onlyOSX(function(event, title) { var count = title.match(/\((\d+)\)/); count = count ? count[1] : ''; @@ -77,21 +75,30 @@ } })); - this.window.webContents.on("new-window", function(e, url){ + whatsApp.window.webContents.on("new-window", function(e, url){ require('shell').openExternal(url); e.preventDefault(); }); - this.window.on('close', function(e){ - if (process.platform === 'darwin' && this.window.forceClose === false) { + + whatsApp.window.on('close', onlyOSX(function(e) { + if (whatsApp.window.forceClose !== true) { e.preventDefault(); - this.window.hide(); + whatsApp.window.hide(); } - }.bind(this)); + })); - app.on('before-quit', function() { - this.window && (this.window.forceClose = true); - }.bind(this)); + app.on('before-quit', onlyOSX(function() { + whatsApp.window.forceClose = true + })); + + app.on('activate-with-no-open-windows', onlyOSX(function() { + whatsApp.window.show(); + })); + + app.on('window-all-closed', onlyWin(function() { + app.quit(); + })); } };