Merge branch 'refactoring-notifications'
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const OriginalNotification = Notification;
|
const OriginalNotification = Notification;
|
||||||
const {remote} = require('electron');
|
const {ipcRenderer, remote} = require('electron');
|
||||||
const {throttle} = require('underscore');
|
const {throttle} = require('underscore');
|
||||||
const osVersion = require('../../common/osVersion');
|
const osVersion = require('../../common/osVersion');
|
||||||
const dingDataURL = require('../../assets/ding.mp3'); // https://github.com/mattermost/platform/blob/v3.7.3/webapp/images/ding.mp3
|
const dingDataURL = require('../../assets/ding.mp3'); // https://github.com/mattermost/platform/blob/v3.7.3/webapp/images/ding.mp3
|
||||||
@@ -13,8 +13,8 @@ const playDing = throttle(() => {
|
|||||||
ding.play();
|
ding.play();
|
||||||
}, 3000, {trailing: false});
|
}, 3000, {trailing: false});
|
||||||
|
|
||||||
function override(eventHandlers) {
|
class EnhancedNotification extends OriginalNotification {
|
||||||
Notification = function constructor(title, options) { // eslint-disable-line no-global-assign, no-native-reassign
|
constructor(title, options) {
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
// Replace with application icon.
|
// Replace with application icon.
|
||||||
options.icon = appIconURL;
|
options.icon = appIconURL;
|
||||||
@@ -22,84 +22,46 @@ function override(eventHandlers) {
|
|||||||
// Notification Center shows app's icon, so there were two icons on the notification.
|
// Notification Center shows app's icon, so there were two icons on the notification.
|
||||||
Reflect.deleteProperty(options, 'icon');
|
Reflect.deleteProperty(options, 'icon');
|
||||||
}
|
}
|
||||||
this.notification = new OriginalNotification(title, options);
|
|
||||||
if (eventHandlers.notification) {
|
super(title, options);
|
||||||
eventHandlers.notification(title, options);
|
|
||||||
}
|
ipcRenderer.send('notified', {
|
||||||
|
title,
|
||||||
|
options
|
||||||
|
});
|
||||||
|
|
||||||
if (process.platform === 'win32' && osVersion.isLowerThanOrEqualWindows8_1()) {
|
if (process.platform === 'win32' && osVersion.isLowerThanOrEqualWindows8_1()) {
|
||||||
if (!options.silent) {
|
if (!options.silent) {
|
||||||
playDing();
|
playDing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// static properties
|
|
||||||
Notification.__defineGetter__('permission', () => {
|
|
||||||
return OriginalNotification.permission;
|
|
||||||
});
|
|
||||||
|
|
||||||
// instance properties
|
|
||||||
function defineReadProperty(property) {
|
|
||||||
Notification.prototype.__defineGetter__(property, function getter() {
|
|
||||||
return this.notification[property];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
defineReadProperty('title');
|
|
||||||
defineReadProperty('dir');
|
|
||||||
defineReadProperty('lang');
|
|
||||||
defineReadProperty('body');
|
|
||||||
defineReadProperty('tag');
|
|
||||||
defineReadProperty('icon');
|
|
||||||
defineReadProperty('data');
|
|
||||||
defineReadProperty('silent');
|
|
||||||
|
|
||||||
// unsupported properties
|
set onclick(handler) {
|
||||||
defineReadProperty('noscreen');
|
super.onclick = () => {
|
||||||
defineReadProperty('renotify');
|
const currentWindow = remote.getCurrentWindow();
|
||||||
defineReadProperty('sound');
|
if (process.platform === 'win32') {
|
||||||
defineReadProperty('sticky');
|
// show() breaks Aero Snap state.
|
||||||
defineReadProperty('vibrate');
|
if (currentWindow.isVisible()) {
|
||||||
|
currentWindow.focus();
|
||||||
// event handlers
|
} else if (currentWindow.isMinimized()) {
|
||||||
function defineEventHandler(event, callback) {
|
currentWindow.restore();
|
||||||
defineReadProperty(event);
|
|
||||||
Notification.prototype.__defineSetter__(event, function setter(originalCallback) {
|
|
||||||
this.notification[event] = () => {
|
|
||||||
const callbackevent = {
|
|
||||||
preventDefault() {
|
|
||||||
this.isPrevented = true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (callback) {
|
|
||||||
callback(callbackevent);
|
|
||||||
if (!callbackevent.isPrevented) {
|
|
||||||
originalCallback();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
originalCallback();
|
currentWindow.show();
|
||||||
}
|
}
|
||||||
};
|
} else if (currentWindow.isMinimized()) {
|
||||||
});
|
currentWindow.restore();
|
||||||
|
} else {
|
||||||
|
currentWindow.show();
|
||||||
|
}
|
||||||
|
ipcRenderer.sendToHost('onNotificationClick');
|
||||||
|
handler();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
defineEventHandler('onclick', eventHandlers.onclick);
|
|
||||||
defineEventHandler('onerror', eventHandlers.onerror);
|
|
||||||
|
|
||||||
// obsolete handlers
|
get onclick() {
|
||||||
defineEventHandler('onclose', eventHandlers.onclose);
|
return super.onclick;
|
||||||
defineEventHandler('onshow', eventHandlers.onshow);
|
}
|
||||||
|
|
||||||
// static methods
|
|
||||||
Notification.requestPermission = (callback) => {
|
|
||||||
OriginalNotification.requestPermission(callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
// instance methods
|
|
||||||
Notification.prototype.close = function close() {
|
|
||||||
this.notification.close();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = EnhancedNotification;
|
||||||
override
|
|
||||||
};
|
|
||||||
|
@@ -3,7 +3,9 @@
|
|||||||
const electron = require('electron');
|
const electron = require('electron');
|
||||||
const ipc = electron.ipcRenderer;
|
const ipc = electron.ipcRenderer;
|
||||||
const webFrame = electron.webFrame;
|
const webFrame = electron.webFrame;
|
||||||
const notification = require('../js/notification');
|
const EnhancedNotification = require('../js/notification');
|
||||||
|
|
||||||
|
Notification = EnhancedNotification; // eslint-disable-line no-global-assign, no-native-reassign
|
||||||
|
|
||||||
Reflect.deleteProperty(global.Buffer); // http://electron.atom.io/docs/tutorial/security/#buffer-global
|
Reflect.deleteProperty(global.Buffer); // http://electron.atom.io/docs/tutorial/security/#buffer-global
|
||||||
|
|
||||||
@@ -117,37 +119,6 @@ function isElementVisible(elem) {
|
|||||||
return elem.offsetHeight !== 0;
|
return elem.offsetHeight !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.override({
|
|
||||||
|
|
||||||
// Send a notification event to the main process.
|
|
||||||
notification(title, options) {
|
|
||||||
ipc.send('notified', {
|
|
||||||
title,
|
|
||||||
options
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// Show window even if it is hidden/minimized when notification is clicked.
|
|
||||||
onclick() {
|
|
||||||
const currentWindow = electron.remote.getCurrentWindow();
|
|
||||||
if (process.platform === 'win32') {
|
|
||||||
// show() breaks Aero Snap state.
|
|
||||||
if (currentWindow.isVisible()) {
|
|
||||||
currentWindow.focus();
|
|
||||||
} else if (currentWindow.isMinimized()) {
|
|
||||||
currentWindow.restore();
|
|
||||||
} else {
|
|
||||||
currentWindow.show();
|
|
||||||
}
|
|
||||||
} else if (currentWindow.isMinimized()) {
|
|
||||||
currentWindow.restore();
|
|
||||||
} else {
|
|
||||||
currentWindow.show();
|
|
||||||
}
|
|
||||||
ipc.sendToHost('onNotificationClick');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function resetMisspelledState() {
|
function resetMisspelledState() {
|
||||||
ipc.once('spellchecker-is-ready', () => {
|
ipc.once('spellchecker-is-ready', () => {
|
||||||
const element = document.activeElement;
|
const element = document.activeElement;
|
||||||
|
Reference in New Issue
Block a user