Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect "Do Not Disturb" on macOS #760

Merged
merged 11 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 96 additions & 13 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"publish-snap": "del dist && tsc && electron-builder --linux && snapcraft push --release=stable dist/*.snap"
},
"dependencies": {
"@sindresorhus/do-not-disturb": "^1.1.0",
"electron-context-menu": "^0.13.0",
"electron-debug": "^3.0.1",
"electron-dl": "^1.14.0",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Caprine is an unofficial and privacy-focused Facebook Messenger app with many us
- Silent auto-updates
- Custom text size
- Emoji style setting
- Respects Do Not Disturb\*

\*macOS only

Expand Down
37 changes: 31 additions & 6 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './browser/conversation-list'; // eslint-disable-line import/no-unassigne

const selectedConversationSelector = '._5l-3._1ht1._1ht2';
const preferencesSelector = '._10._4ebx.uiLayer._4-hy';
const messengerSoundsSelector = `${preferencesSelector} ._374d ._6bkz`;

async function withMenu(
menuButtonElement: HTMLElement,
Expand Down Expand Up @@ -55,7 +56,9 @@ function selectMenuItem(itemNumber: number): void {
`.uiLayer:not(.hidden_elem) ._54nq._2i-c._558b._2n_z li:nth-child(${itemNumber}) a`
)!;

selector.click();
if (selector) {
selector.click();
}
}

async function selectOtherListViews(itemNumber: number): Promise<void> {
Expand Down Expand Up @@ -161,10 +164,8 @@ function setSidebarVisibility(): void {
ipc.send('set-sidebar-visibility');
}

ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => {
const preferencesAreOpen = isPreferencesOpen();

if (!preferencesAreOpen) {
async function openHiddenPreferences(): Promise<boolean> {
if (!isPreferencesOpen()) {
const style = document.createElement('style');
// Hide both the backdrop and the preferences dialog
style.textContent = `${preferencesSelector} ._3ixn, ${preferencesSelector} ._59s7 { opacity: 0 !important }`;
Expand All @@ -174,7 +175,31 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus:

// Will clean up itself after the preferences are closed
document.querySelector<HTMLElement>(preferencesSelector)!.append(style);

return true;
}

return false;
}

ipc.on(
'toggle-sounds',
async (_event: ElectronEvent, checked: boolean): Promise<void> => {
const shouldClosePreferences = await openHiddenPreferences();

const soundsCheckbox = document.querySelector<HTMLInputElement>(messengerSoundsSelector)!;
if (typeof checked === 'undefined' || checked !== soundsCheckbox.checked) {
soundsCheckbox.click();
}

if (shouldClosePreferences) {
closePreferences();
}
}
);

ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus: boolean) => {
const shouldClosePreferences = await openHiddenPreferences();

const notificationCheckbox = document.querySelector<HTMLInputElement>(
selectors.notificationCheckbox
Expand All @@ -191,7 +216,7 @@ ipc.on('toggle-mute-notifications', async (_event: ElectronEvent, defaultStatus:

ipc.send('mute-notifications-toggled', !notificationCheckbox.checked);

if (!preferencesAreOpen) {
if (shouldClosePreferences) {
closePreferences();
}
});
Expand Down
1 change: 1 addition & 0 deletions source/do-not-disturb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@sindresorhus/do-not-disturb';
26 changes: 24 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import electronLocalshortcut = require('electron-localshortcut');
import electronDebug = require('electron-debug');
import {is, darkMode} from 'electron-util';
import {bestFacebookLocaleFor} from 'facebook-locales';
import doNotDisturb = require('@sindresorhus/do-not-disturb');
import updateAppMenu from './menu';
import config from './config';
import tray from './tray';
Expand Down Expand Up @@ -69,6 +70,7 @@ let mainWindow: BrowserWindow;
let isQuitting = false;
let prevMessageCount = 0;
let dockMenu: Menu;
let isDNDEnabled = false;

if (!app.requestSingleInstanceLock()) {
app.quit();
Expand Down Expand Up @@ -101,7 +103,12 @@ function updateBadge(conversations: Conversation[]): void {
app.setBadgeCount(messageCount);
}

if (is.macos && config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) {
if (
is.macos &&
!isDNDEnabled &&
config.get('bounceDockOnMessage') &&
prevMessageCount !== messageCount
) {
app.dock.bounce('informational');
prevMessageCount = messageCount;
}
Expand Down Expand Up @@ -391,7 +398,22 @@ function createMainWindow(): BrowserWindow {
mainWindow.show();
}

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

isDNDEnabled = await doNotDisturb.isEnabled();
}

webContents.send('toggle-sounds', !isDNDEnabled);
webContents.send('toggle-mute-notifications', config.get('notificationsMuted') || isDNDEnabled);

webContents.send('toggle-message-buttons', config.get('showMessageButtons'));

await webContents.executeJavaScript(
Expand Down