Skip to content

Commit

Permalink
Add dnd polling - macos only
Browse files Browse the repository at this point in the history
  • Loading branch information
whitecrownclown committed Feb 27, 2019
1 parent 5fb7530 commit a11aa5f
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 24 deletions.
120 changes: 101 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"publish-snap": "del dist && tsc && electron-builder --linux && snapcraft push --release=stable dist/*.snap"
},
"dependencies": {
"@sindresorhus/do-not-disturb": "^0.2.0",
"@types/event-emitter": "^0.3.3",
"@types/is-online": "^8.0.0",
"electron-context-menu": "^0.11.0",
"electron-debug": "^2.1.0",
Expand All @@ -36,9 +38,10 @@
"electron-updater": "^4.0.6",
"electron-util": "^0.11.0",
"element-ready": "^3.0.0",
"event-emitter": "^0.3.5",
"facebook-locales": "^1.0.591",
"lodash": "^4.17.11",
"is-online": "^8.0.0",
"lodash": "^4.17.11",
"p-wait-for": "^2.0.1"
},
"devDependencies": {
Expand Down
25 changes: 25 additions & 0 deletions source/do-not-disturb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {is} from 'electron-util';
import macosDND from '@sindresorhus/do-not-disturb';
import emitter from './event-emitter';

const pollingInterval = 3000;
const getDoNotDisturb = async (): Promise<boolean> => (is.macos ? macosDND.isEnabled() : false);

const startPolling = async (): Promise<void> => {
let currentValue = await getDoNotDisturb();

setInterval(async () => {
const newValue = await getDoNotDisturb();

if (newValue !== currentValue) {
currentValue = newValue;
emitter.emit('dndprefs_changed', newValue);
}
}, pollingInterval);
};

if (is.macos) {
startPolling();
}

export {getDoNotDisturb};
3 changes: 3 additions & 0 deletions source/event-emitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ee from 'event-emitter';

export default ee();
17 changes: 13 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import electronContextMenu from 'electron-context-menu';
import isDev from 'electron-is-dev';
import electronDebug from 'electron-debug';
import {darkMode, is} from 'electron-util';
import {getDoNotDisturb} from 'electron-notification-state';
import {bestFacebookLocaleFor} from 'facebook-locales';
import updateAppMenu from './menu';
import config from './config';
import tray from './tray';
import {sendAction, sendBackgroundAction} from './util';
import {process as processEmojiUrl} from './emoji';
import ensureOnline from './ensure-online';
import {getDoNotDisturb} from './do-not-disturb';
import emitter from './event-emitter';
import './touch-bar'; // eslint-disable-line import/no-unassigned-import

ipcMain.setMaxListeners(100);
Expand Down Expand Up @@ -101,9 +102,7 @@ async function updateBadge(conversations: Conversation[]): Promise<void> {
}

if (is.macos) {
const isDNDEnabled = await getDoNotDisturb();

if (!isDNDEnabled && config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) {
if (config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) {
app.dock.bounce('informational');
prevMessageCount = messageCount;
}
Expand Down Expand Up @@ -367,6 +366,16 @@ function createMainWindow(): BrowserWindow {
mainWindow.show();
}

if (is.macos) {
emitter.on('dndprefs_changed', (doNotDisturb: boolean) => {
webContents.send('toggle-sounds', !doNotDisturb);
webContents.send(
'toggle-mute-notifications',
config.get('notificationsMuted') || doNotDisturb
);
});
}

const isDNDEnabled = await getDoNotDisturb();

webContents.send('toggle-sounds', !isDNDEnabled);
Expand Down
1 change: 1 addition & 0 deletions source/sindresorhus-do-not-disturb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@sindresorhus/do-not-disturb';

0 comments on commit a11aa5f

Please sign in to comment.