From 199b1423e5d7ebcbc4fb72b35b846bdd5a3f6a22 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Mon, 6 Jul 2015 09:38:04 +0800 Subject: [PATCH] Implemented mac app badge through listening for webview title changes --- app/assets/js/index.js | 9 ++++++--- app/main.js | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/assets/js/index.js b/app/assets/js/index.js index b3e7501..d4e797f 100644 --- a/app/assets/js/index.js +++ b/app/assets/js/index.js @@ -22,6 +22,12 @@ ipc.on('params', function(message) { require('shell').openExternal(e.url); }); + // We check for desktop notifications by listening to a title change in the webview + // Not elegant, but it will have to do + webView.addEventListener('page-title-set', function(event) { + ipc.send('notification-message', 'TITLE_CHANGED'); + }); + var webViewDiv = document.getElementById('webViewDiv'); webViewDiv.appendChild(webView); @@ -49,8 +55,5 @@ ipc.on('params', function(message) { var webView = document.getElementById('webView'); webView.undo(); }); - - - }); diff --git a/app/main.js b/app/main.js index 3322363..215aae5 100644 --- a/app/main.js +++ b/app/main.js @@ -2,10 +2,10 @@ * Created by JiaHao on 4/7/15. */ - var app = require('app'); var fs = require('fs'); var BrowserWindow = require('browser-window'); +var ipc = require('ipc'); const APP_ARGS_FILE_PATH = __dirname + '/targetUrl.txt'; require('crash-reporter').start(); @@ -31,7 +31,7 @@ app.on('ready', function() { ); mainWindow.loadUrl('file://' + __dirname + '/index.html'); - //mainWindow.openDevTools(); + mainWindow.openDevTools(); mainWindow.webContents.on('did-finish-load', function() { fs.readFile(APP_ARGS_FILE_PATH, 'utf8', function (error, data) { if (error) { @@ -45,9 +45,22 @@ app.on('ready', function() { }) }); + // if the window is focused, clear the badge + mainWindow.on('focus', function () { + app.dock.setBadge(''); + }); mainWindow.on('closed', function() { mainWindow = null; - }) + }); +}); +// listen for a notification message +ipc.on('notification-message', function(event, arg) { + console.log(arg); // prints "ping" + if (arg === 'TITLE_CHANGED') { + if (!mainWindow.isFocused()) { + app.dock.setBadge('●'); + } + } }); \ No newline at end of file