Electron-414 (Add a logic not to close the notifications on mouse hover) (#338)

- Add logic to not close the notification on mouse hover
- Clear timer when the notification content is reset
- Move logic from preload to main process
- Remove unwanted code
- Remove event listener on reset
This commit is contained in:
Kiran Niranjan 2018-05-01 12:04:24 +05:30 committed by Vishwas Shashidhar
parent 3a9f156317
commit 4d15f03492
2 changed files with 65 additions and 2 deletions

View File

@ -10,6 +10,9 @@ const electron = require('electron');
const ipc = electron.ipcRenderer;
const whiteColorRegExp = new RegExp(/^(?:white|#fff(?:fff)?|rgba?\(\s*255\s*,\s*255\s*,\s*255\s*(?:,\s*1\s*)?\))$/i);
// event functions ref
let onMouseLeaveFunc;
let onMouseOverFunc;
/**
* Sets style for a notification
@ -134,6 +137,28 @@ function setContents(event, notificationObj) {
const winId = notificationObj.windowId;
if (!notificationObj.sticky) {
onMouseLeaveFunc = onMouseLeave.bind(this);
onMouseOverFunc = onMouseOver.bind(this);
container.addEventListener('mouseleave', onMouseLeaveFunc);
container.addEventListener('mouseover', onMouseOverFunc);
}
/**
* Start a new timer to close the notification
*/
function onMouseLeave() {
ipc.send('electron-notify-mouseleave', winId, notificationObj);
}
/**
* Clear all timeouts to prevent notification
* from closing
*/
function onMouseOver() {
ipc.send('electron-notify-mouseover', winId);
}
// note: use onclick because we only want one handler, for case
// when content gets overwritten by notf with same tag
closeButton.onclick = function(clickEvent) {
@ -197,7 +222,10 @@ function reset() {
let newContainer = container.cloneNode(true);
container.parentNode.replaceChild(newContainer, container);
let newCloseButton = closeButton.cloneNode(true);
closeButton.parentNode.replaceChild(newCloseButton, closeButton)
closeButton.parentNode.replaceChild(newCloseButton, closeButton);
container.removeEventListener('mouseleave', onMouseLeaveFunc);
container.removeEventListener('mouseover', onMouseOverFunc);
}
ipc.on('electron-notify-set-contents', setContents);

View File

@ -435,7 +435,6 @@ function setNotificationContents(notfWindow, notfObj) {
});
let closeNotificationSafely = buildCloseNotificationSafely(closeFunc);
// don't start timer to close if we aren't sticky
if (!notfObj.sticky) {
timeoutId = setTimeout(function() {
closeNotificationSafely('timeout');
@ -744,6 +743,42 @@ function cleanUpInactiveWindow() {
inactiveWindows = [];
}
/**
* Start a new timer to close the notification
* @param event
* @param winId
* @param notificationObj
*/
function onMouseLeave(event, winId, notificationObj) {
if (winId) {
const notificationWindow = BrowserWindow.fromId(winId);
if (notificationWindow && !notificationWindow.isDestroyed()) {
notificationWindow.displayTimer = setTimeout(function () {
let closeFunc = buildCloseNotification(BrowserWindow.fromId(winId), notificationObj);
buildCloseNotificationSafely(closeFunc)('close');
}, 3000);
}
}
}
/**
* Clears the timer for a specific notification window
* @param event
* @param winId
*/
function onMouseOver(event, winId) {
if (winId) {
const notificationWindow = BrowserWindow.fromId(winId);
if (notificationWindow) {
clearTimeout(notificationWindow.displayTimer);
}
}
}
// capturing mouse events
ipc.on('electron-notify-mouseleave', onMouseLeave);
ipc.on('electron-notify-mouseover', onMouseOver);
module.exports.notify = notify;
module.exports.updateConfig = updateConfig;