Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
Show application window on notification click (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker authored and ronjouch committed Aug 22, 2018
1 parent e228f0c commit 27ea3fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/src/components/mainWindow/mainWindow.js
Expand Up @@ -322,6 +322,10 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
});
}

ipcMain.on('notification-click', () => {
mainWindow.show();
});

mainWindow.webContents.on('new-window', onNewWindow);
mainWindow.webContents.on('will-navigate', onWillNavigate);

Expand Down
24 changes: 17 additions & 7 deletions app/src/static/preload.js
Expand Up @@ -8,14 +8,19 @@ import fs from 'fs';
const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js');
const log = require('loglevel');
/**
* Patches window.Notification to set a callback on a new Notification
* @param callback
* Patches window.Notification to:
* - set a callback on a new Notification
* - set a callback for clicks on notifications
* @param createCallback
* @param clickCallback
*/
function setNotificationCallback(callback) {
function setNotificationCallback(createCallback, clickCallback) {
const OldNotify = window.Notification;
const newNotify = (title, opt) => {
callback(title, opt);
return new OldNotify(title, opt);
createCallback(title, opt);
const instance = new OldNotify(title, opt);
instance.addEventListener('click', clickCallback);
return instance;
};
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', {
Expand All @@ -35,9 +40,14 @@ function injectScripts() {
require(INJECT_JS_PATH);
}

setNotificationCallback((title, opt) => {
function notifyNotificationCreate(title, opt) {
ipcRenderer.send('notification', title, opt);
});
}
function notifyNotificationClick() {
ipcRenderer.send('notification-click');
}

setNotificationCallback(notifyNotificationCreate, notifyNotificationClick);

document.addEventListener('DOMContentLoaded', () => {
injectScripts();
Expand Down

0 comments on commit 27ea3fc

Please sign in to comment.