Refactor desktop notification override
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user