Fix notifications and audio are only muted in active workspace (#68)

This commit is contained in:
Quang Lam 2019-12-07 21:33:07 -06:00 committed by GitHub
parent 385b438992
commit bd6860a220
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 30 deletions

View file

@ -1,6 +1,9 @@
const mainWindow = require('../windows/main');
const { getPreference } = require('./preferences');
const sendToAllWindows = require('./send-to-all-windows');
const {
setViewsAudioPref,
setViewsNotificationsPref,
} = require('./views');
let pauseNotificationsInfo;
@ -121,13 +124,8 @@ const updatePauseNotificationsInfo = () => {
// Send update to webview
const shouldPauseNotifications = pauseNotificationsInfo !== null;
const shouldMuteAudio = shouldPauseNotifications && getPreference('pauseNotificationsMuteAudio');
const views = mainWindow.get().getBrowserViews();
if (views) {
views.forEach((view) => {
view.webContents.send('should-pause-notifications-changed', shouldPauseNotifications);
view.webContents.setAudioMuted(shouldMuteAudio);
});
}
setViewsAudioPref(shouldMuteAudio);
setViewsNotificationsPref(shouldPauseNotifications);
sendToAllWindows('should-pause-notifications-changed', pauseNotificationsInfo);
// set schedule for reupdating

View file

@ -19,6 +19,8 @@ const views = {};
const badgeCounts = {};
const didFailLoad = {};
let activeId;
let shouldMuteAudio;
let shouldPauseNotifications;
const extractDomain = (fullUrl) => {
const matches = fullUrl.match(/^https?:\/\/([^/?#]+)(?:[/?#]|$)/i);
@ -213,6 +215,16 @@ const addView = (browserWindow, workspace) => {
view.webContents.send('update-target-url', url);
});
// Handle audio & notification preferences
if (shouldMuteAudio !== undefined) {
view.webContents.setAudioMuted(shouldMuteAudio);
}
if (shouldPauseNotifications !== undefined) {
view.webContents.once('did-stop-loading', () => {
view.webContents.send('should-pause-notifications-changed', shouldPauseNotifications);
});
}
views[workspace.id] = view;
if (workspace.active) {
@ -306,9 +318,29 @@ const removeView = (id) => {
}
};
const setViewsAudioPref = (_shouldMuteAudio) => {
shouldMuteAudio = _shouldMuteAudio;
Object.values(views).forEach((view) => {
if (view != null) {
view.webContents.setAudioMuted(_shouldMuteAudio);
}
});
};
const setViewsNotificationsPref = (_shouldPauseNotifications) => {
shouldPauseNotifications = _shouldPauseNotifications;
Object.values(views).forEach((view) => {
if (view != null) {
view.webContents.send('should-pause-notifications-changed', _shouldPauseNotifications);
}
});
};
module.exports = {
addView,
getView,
setActiveView,
removeView,
setViewsAudioPref,
setViewsNotificationsPref,
};

View file

@ -172,32 +172,31 @@ ipcRenderer.on('should-pause-notifications-changed', (e, val) => {
// https://github.com/quanglam2807/singlebox/issues/21
const initialShouldPauseNotifications = ipcRenderer.sendSync('get-pause-notifications-info') != null;
webFrame.executeJavaScript(`
window.chrome = {
runtime: {
sendMessage: () => {},
connect: () => {
return {
onMessage: {
addListener: () => {},
removeListener: () => {},
},
postMessage: () => {},
disconnect: () => {},
(function() {
window.chrome = {
runtime: {
sendMessage: () => {},
connect: () => {
return {
onMessage: {
addListener: () => {},
removeListener: () => {},
},
postMessage: () => {},
disconnect: () => {},
}
}
}
}
}
window.electronSafeIpc = {
send: () => null,
on: () => null,
};
window.desktop = undefined;
window.electronSafeIpc = {
send: () => null,
on: () => null,
};
window.desktop = undefined;
// Customize Notification behavior
// https://stackoverflow.com/questions/53390156/how-to-override-javascript-web-api-notification-object
(function() {
// Customize Notification behavior
// https://stackoverflow.com/questions/53390156/how-to-override-javascript-web-api-notification-object
const oldNotification = window.Notification;
let shouldPauseNotifications = ${initialShouldPauseNotifications};
@ -216,7 +215,7 @@ window.desktop = undefined;
window.Notification.requestPermission = oldNotification.requestPermission;
Object.defineProperty(Notification, 'permission', {
get() {
return 'granted';
return oldNotification.permission;
}
});
})();