Refactor desktop notification override

This commit is contained in:
Yuya Ochiai 2016-02-20 21:36:09 +09:00
parent a3bf36b31b
commit 695c091bac
2 changed files with 97 additions and 39 deletions

View file

@ -0,0 +1,80 @@
const OriginalNotification = Notification;
function override(eventHandlers) {
Notification = function(title, options) {
this.notification = new OriginalNotification(title, options);
if (eventHandlers.notification) {
eventHandlers.notification();
}
};
// static properties
Notification.__defineGetter__('permission', function() {
return OriginalNotification.permission;
});
// instance properties
var defineReadProperty = function(property) {
Notification.prototype.__defineGetter__(property, function() {
return this.notification[property];
});
};
defineReadProperty('title');
defineReadProperty('dir');
defineReadProperty('lang');
defineReadProperty('body');
defineReadProperty('tag');
defineReadProperty('icon');
defineReadProperty('data');
defineReadProperty('silent');
// unsupported properties
defineReadProperty('noscreen');
defineReadProperty('renotify');
defineReadProperty('sound');
defineReadProperty('sticky');
defineReadProperty('vibrate');
// event handlers
var defineEventHandler = function(event, callback) {
defineReadProperty(event);
Notification.prototype.__defineSetter__(event, function(originalCallback) {
this.notification[event] = function() {
callbackevent = {
preventDefault: function() {
this.isPrevented = true;
}
};
if (callback) {
callback(callbackevent);
if (!callbackevent.isPrevented) {
originalCallback();
}
}
else {
originalCallback();
}
}
});
}
defineEventHandler('onclick', eventHandlers.onclick);
defineEventHandler('onerror', eventHandlers.onerror);
// obsolete handlers
defineEventHandler('onclose', eventHandlers.onclose);
defineEventHandler('onshow', eventHandlers.onshow);
// static methods
Notification.requestPermission = function(callback) {
OriginalNotification.requestPermission(callback);
};
// instance methods
Notification.prototype.close = function() {
this.notification.close();
};
}
module.exports = {
override: override
};

View file

@ -2,7 +2,7 @@
const electron = require('electron');
const ipc = electron.ipcRenderer;
const NativeNotification = Notification;
const notification = require('../js/notification');
var hasClass = function(element, className) {
var rclass = /[\t\r\n\f]/g;
@ -107,35 +107,21 @@ function isLowerThanOrEqualWindows8_1() {
return (osVersion.major <= 6 && osVersion.minor <= 3);
};
// Show balloon when notified.
function overrideNotificationWithBalloon() {
Notification = function(title, options) {
ipc.send('notified', {
title: title,
options: options
});
};
Notification.permission = NativeNotification.permission;
Notification.requestPermission = function(callback) {
callback('granted');
};
Notification.prototype.close = function() {};
};
// Show window even if it is hidden/minimized when notification is clicked.
function overrideNotification() {
Notification = function(title, options) {
this.notification = new NativeNotification(title, options);
};
Notification.permission = NativeNotification.permission;
Notification.requestPermission = function(callback) {
callback('granted');
};
Notification.prototype.close = function() {
this.notification.close();
};
Notification.prototype.__defineSetter__('onclick', function(callback) {
this.notification.onclick = function() {
if (process.platform === 'win32' && isLowerThanOrEqualWindows8_1()) {
// Show balloon when notified.
notification.override({
notification: function(title, options) {
ipc.send('notified', {
title: title,
options: options
});
}
});
}
else {
// Show window even if it is hidden/minimized when notification is clicked.
notification.override({
onclick: function() {
if (process.platform === 'win32') {
// show() breaks Aero Snap state.
electron.remote.getCurrentWindow().focus();
@ -144,14 +130,6 @@ function overrideNotification() {
electron.remote.getCurrentWindow().show();
}
ipc.sendToHost('onNotificationClick');
callback();
};
}
});
}
if (process.platform === 'win32' && isLowerThanOrEqualWindows8_1()) {
overrideNotificationWithBalloon();
}
else {
overrideNotification();
}